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)]
22 TrBench(BenchSamples),
26 /// Creates a `TestResult` depending on the raw result of test execution
27 /// and associated data.
28 pub fn calc_result<'a>(
30 task_result: Result<(), &'a (dyn Any + 'static + Send)>,
31 time_opts: &Option<time::TestTimeOptions>,
32 exec_time: &Option<time::TestExecTime>,
34 let result = match (&desc.should_panic, task_result) {
35 (&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
36 (&ShouldPanic::YesWithMessage(msg), Err(ref err)) => {
37 let maybe_panic_str = err
38 .downcast_ref::<String>()
40 .or_else(|| err.downcast_ref::<&'static str>().copied());
42 if maybe_panic_str.map(|e| e.contains(msg)).unwrap_or(false) {
44 } else if let Some(panic_str) = maybe_panic_str {
45 TestResult::TrFailedMsg(format!(
46 r#"panic did not contain expected string
47 panic message: `{:?}`,
48 expected substring: `{:?}`"#,
52 TestResult::TrFailedMsg(format!(
53 r#"expected panic with string value,
54 found non-string value: `{:?}`
55 expected substring: `{:?}`"#,
61 (&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
62 TestResult::TrFailedMsg("test did not panic as expected".to_string())
64 _ => TestResult::TrFailed,
67 // If test is already failed (or allowed to fail), do not change the result.
68 if result != TestResult::TrOk {
72 // Check if test is failed due to timeout.
73 if let (Some(opts), Some(time)) = (time_opts, exec_time) {
74 if opts.error_on_excess && opts.is_critical(desc, time) {
75 return TestResult::TrTimedFail;
82 /// Creates a `TestResult` depending on the exit code of test subprocess.
83 pub fn get_result_from_exit_code(
86 time_opts: &Option<time::TestTimeOptions>,
87 exec_time: &Option<time::TestExecTime>,
89 let result = match code {
90 TR_OK => TestResult::TrOk,
91 TR_FAILED => TestResult::TrFailed,
92 _ => TestResult::TrFailedMsg(format!("got unexpected return code {code}")),
95 // If test is already failed (or allowed to fail), do not change the result.
96 if result != TestResult::TrOk {
100 // Check if test is failed due to timeout.
101 if let (Some(opts), Some(time)) = (time_opts, exec_time) {
102 if opts.error_on_excess && opts.is_critical(desc, time) {
103 return TestResult::TrTimedFail;