lighthouse/tests/ef_tests/src/case_result.rs

53 lines
1.6 KiB
Rust
Raw Normal View History

2019-05-14 01:13:28 +00:00
use super::*;
2019-05-15 01:41:25 +00:00
use std::fmt::Debug;
2019-05-14 01:13:28 +00:00
#[derive(Debug, PartialEq, Clone)]
2019-05-15 01:27:22 +00:00
pub struct CaseResult {
2019-05-14 01:13:28 +00:00
pub case_index: usize,
pub desc: String,
pub result: Result<(), Error>,
}
2019-05-15 01:27:22 +00:00
impl CaseResult {
2019-05-14 01:13:28 +00:00
pub fn new<T: Debug>(case_index: usize, case: &T, result: Result<(), Error>) -> Self {
2019-05-15 01:27:22 +00:00
CaseResult {
2019-05-14 01:13:28 +00:00
case_index,
desc: format!("{:?}", case),
result,
}
}
}
/// Compares `result` with `expected`.
///
/// If `expected.is_none()` then `result` is expected to be `Err`. Otherwise, `T` in `result` and
/// `expected` must be equal.
2019-05-14 23:50:05 +00:00
pub fn compare_result<T, E>(result: &Result<T, E>, expected: &Option<T>) -> Result<(), Error>
2019-05-14 01:13:28 +00:00
where
T: PartialEq<T> + Debug,
E: Debug,
{
match (result, expected) {
// Pass: The should have failed and did fail.
(Err(_), None) => Ok(()),
// Fail: The test failed when it should have produced a result (fail).
(Err(e), Some(expected)) => Err(Error::NotEqual(format!(
2019-05-14 23:50:05 +00:00
"Got {:?} Expected {:?}",
2019-05-14 01:13:28 +00:00
e, expected
))),
// Fail: The test produced a result when it should have failed (fail).
(Ok(result), None) => Err(Error::DidntFail(format!("Got {:?}", result))),
// Potential Pass: The test should have produced a result, and it did.
(Ok(result), Some(expected)) => {
if result == expected {
Ok(())
} else {
Err(Error::NotEqual(format!(
"Got {:?} expected {:?}",
result, expected
)))
}
}
}
}