3 use super::bench::BenchSamples;
4 use super::options::ShouldPanic;
6 use super::types::TestDesc;
8 pub use self::TestResult::*;
10 // Return codes for secondary process.
11 // Start somewhere other than 0 so we know the return code means what we think
13 pub const TR_OK: i32 = 50;
14 pub const TR_FAILED: i32 = 51;
16 #[derive(Debug, Clone, PartialEq)]
23 TrBench(BenchSamples),
27 /// Creates a `TestResult` depending on the raw result of test execution
28 /// and associated data.
29 pub fn calc_result<'a>(
31 task_result: Result<(), &'a (dyn Any + 'static + Send)>,
32 time_opts: &Option<time::TestTimeOptions>,
33 exec_time: &Option<time::TestExecTime>,
35 let result = match (&desc.should_panic, task_result) {
36 (&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
37 (&ShouldPanic::YesWithMessage(msg), Err(ref err)) => {
38 let maybe_panic_str = err
39 .downcast_ref::<String>()
41 .or_else(|| err.downcast_ref::<&'static str>().copied());
43 if maybe_panic_str.map(|e| e.contains(msg)).unwrap_or(false) {
45 } else if desc.allow_fail {
46 TestResult::TrAllowedFail
47 } else if let Some(panic_str) = maybe_panic_str {
48 TestResult::TrFailedMsg(format!(
49 r#"panic did not contain expected string
50 panic message: `{:?}`,
51 expected substring: `{:?}`"#,
55 TestResult::TrFailedMsg(format!(
56 r#"expected panic with string value,
57 found non-string value: `{:?}`
58 expected substring: `{:?}`"#,
64 (&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
65 TestResult::TrFailedMsg("test did not panic as expected".to_string())
67 _ if desc.allow_fail => TestResult::TrAllowedFail,
68 _ => TestResult::TrFailed,
71 // If test is already failed (or allowed to fail), do not change the result.
72 if result != TestResult::TrOk {
76 // Check if test is failed due to timeout.
77 if let (Some(opts), Some(time)) = (time_opts, exec_time) {
78 if opts.error_on_excess && opts.is_critical(desc, time) {
79 return TestResult::TrTimedFail;
86 /// Creates a `TestResult` depending on the exit code of test subprocess.
87 pub fn get_result_from_exit_code(
90 time_opts: &Option<time::TestTimeOptions>,
91 exec_time: &Option<time::TestExecTime>,
93 let result = match (desc.allow_fail, code) {
94 (_, TR_OK) => TestResult::TrOk,
95 (true, TR_FAILED) => TestResult::TrAllowedFail,
96 (false, TR_FAILED) => TestResult::TrFailed,
97 (_, _) => TestResult::TrFailedMsg(format!("got unexpected return code {}", code)),
100 // If test is already failed (or allowed to fail), do not change the result.
101 if result != TestResult::TrOk {
105 // Check if test is failed due to timeout.
106 if let (Some(opts), Some(time)) = (time_opts, exec_time) {
107 if opts.error_on_excess && opts.is_critical(desc, time) {
108 return TestResult::TrTimedFail;