Browse Source

Reintroduce tests in src/schema.rs

master
Adrian Heine 4 years ago
parent
commit
25ab1ebf97
  1. 90
      src/schema.rs

90
src/schema.rs

@ -149,7 +149,6 @@ where
} }
} }
/*
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::cell::RefCell; use std::cell::RefCell;
@ -166,11 +165,7 @@ mod test {
Error(()), Error(()),
} }
impl Error for DummySymbolError {
fn description(&self) -> &str {
return "Description";
}
}
impl Error for DummySymbolError {}
impl fmt::Display for DummySymbolError { impl fmt::Display for DummySymbolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -179,28 +174,39 @@ mod test {
} }
#[derive(Debug)] #[derive(Debug)]
struct DummySymbol<'a> {
_execute: &'a dyn Fn() -> Result<(), Box<dyn Error>>,
_target_reached: &'a dyn Fn() -> Result<bool, Box<dyn Error>>,
struct DummySymbol<T, E> {
_target_reached: RefCell<T>,
_execute: RefCell<E>,
} }
impl<'b> Symbol for DummySymbol<'b> {
impl<
E: Iterator<Item = Result<(), Box<dyn Error>>>,
T: Iterator<Item = Result<bool, Box<dyn Error>>>,
> Symbol for DummySymbol<T, E>
{
fn target_reached(&self) -> Result<bool, Box<dyn Error>> { fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
(self._target_reached)()
self._target_reached.borrow_mut().next().unwrap()
} }
fn execute(&self) -> Result<(), Box<dyn Error>> { fn execute(&self) -> Result<(), Box<dyn Error>> {
(self._execute)()
self._execute.borrow_mut().next().unwrap()
} }
} }
impl<'a> DummySymbol<'a> {
fn new(
target_reached: &'a dyn Fn() -> Result<bool, Box<dyn Error>>,
execute: &'a dyn Fn() -> Result<(), Box<dyn Error>>,
) -> DummySymbol<'a> {
DummySymbol {
_target_reached: target_reached,
_execute: execute,
impl<
E: Iterator<Item = Result<(), Box<dyn Error>>>,
T: Iterator<Item = Result<bool, Box<dyn Error>>>,
> DummySymbol<T, E>
{
fn new<
IE: IntoIterator<IntoIter = E, Item = Result<(), Box<dyn Error>>>,
IT: IntoIterator<IntoIter = T, Item = Result<bool, Box<dyn Error>>>,
>(
target_reached: IT,
execute: IE,
) -> Self {
Self {
_target_reached: RefCell::new(target_reached.into_iter()),
_execute: RefCell::new(execute.into_iter()),
} }
} }
} }
@ -217,44 +223,37 @@ mod test {
impl Logger for DummyLogger { impl Logger for DummyLogger {
fn write(&mut self, line: &str) { fn write(&mut self, line: &str) {
self.log.push(From::from(line));
self.log.push(line.into());
} }
fn debug(&mut self, line: &str) { fn debug(&mut self, line: &str) {
self.log.push(From::from(line));
self.log.push(line.into());
} }
} }
#[test] #[test]
fn nothing_needed_to_be_done() { fn nothing_needed_to_be_done() {
let result = InitializingSymbolRunner::new(DummyLogger::new()) let result = InitializingSymbolRunner::new(DummyLogger::new())
.run_symbol(&DummySymbol::new(&|| Ok(true), &|| Ok(())));
.run_symbol(&DummySymbol::new(vec![Ok(true)], vec![Ok(())]), false);
assert!(result.is_ok()); assert!(result.is_ok());
} }
#[test] #[test]
fn everything_is_ok() { fn everything_is_ok() {
let first = RefCell::new(true);
let result = InitializingSymbolRunner::new(DummyLogger::new()).run_symbol(&DummySymbol::new(
&|| {
let mut _first = first.borrow_mut();
Ok(if *_first {
*_first = false;
true
} else {
false
})
},
&|| Ok(()),
));
let result = InitializingSymbolRunner::new(DummyLogger::new()).run_symbol(
&DummySymbol::new(vec![Ok(true), Ok(false)], vec![Ok(())]),
false,
);
assert!(result.is_ok()); assert!(result.is_ok());
} }
#[test] #[test]
fn executing_did_not_change_state() { fn executing_did_not_change_state() {
let result = InitializingSymbolRunner::new(DummyLogger::new())
.run_symbol(&DummySymbol::new(&|| Ok(false), &|| Ok(())));
let result = InitializingSymbolRunner::new(DummyLogger::new()).run_symbol(
&DummySymbol::new(vec![Ok(false), Ok(false)], vec![Ok(())]),
false,
);
assert_eq!( assert_eq!(
result.unwrap_err().description(),
result.unwrap_err().to_string(),
"Target not reached after executing symbol" "Target not reached after executing symbol"
); );
} }
@ -262,11 +261,14 @@ mod test {
#[test] #[test]
fn executing_did_not_work() { fn executing_did_not_work() {
let err = InitializingSymbolRunner::new(DummyLogger::new()) let err = InitializingSymbolRunner::new(DummyLogger::new())
.run_symbol(&DummySymbol::new(&|| Ok(false), &|| {
Err(Box::new(DummySymbolError::Error(())))
}))
.run_symbol(
&DummySymbol::new(
vec![Ok(false)],
vec![Err(Box::new(DummySymbolError::Error(())) as Box<dyn Error>)],
),
false,
)
.unwrap_err(); .unwrap_err();
assert_eq!(err.description(), "Description");
assert_eq!(err.to_string(), "Dummy symbol error");
} }
} }
*/
Loading…
Cancel
Save