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 unsafe impl Send for TestResult {}
29 /// Creates a `TestResult` depending on the raw result of test execution
30 /// and associated data.
31 pub fn calc_result<'a>(
33 task_result: Result<(), &'a (dyn Any + 'static + Send)>,
34 time_opts: &Option<time::TestTimeOptions>,
35 exec_time: &Option<time::TestExecTime>,
37 let result = match (&desc.should_panic, task_result) {
38 (&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
39 (&ShouldPanic::YesWithMessage(msg), Err(ref err)) => {
40 let maybe_panic_str = err
41 .downcast_ref::<String>()
43 .or_else(|| err.downcast_ref::<&'static str>().copied());
45 if maybe_panic_str.map(|e| e.contains(msg)).unwrap_or(false) {
47 } else if desc.allow_fail {
48 TestResult::TrAllowedFail
49 } else if let Some(panic_str) = maybe_panic_str {
50 TestResult::TrFailedMsg(format!(
51 r#"panic did not contain expected string
52 panic message: `{:?}`,
53 expected substring: `{:?}`"#,
57 TestResult::TrFailedMsg(format!(
58 r#"expected panic with string value,
59 found non-string value: `{:?}`
60 expected substring: `{:?}`"#,
66 (&ShouldPanic::Yes, Ok(())) => {
67 TestResult::TrFailedMsg("test did not panic as expected".to_string())
69 _ if desc.allow_fail => TestResult::TrAllowedFail,
70 _ => TestResult::TrFailed,
73 // If test is already failed (or allowed to fail), do not change the result.
74 if result != TestResult::TrOk {
78 // Check if test is failed due to timeout.
79 if let (Some(opts), Some(time)) = (time_opts, exec_time) {
80 if opts.error_on_excess && opts.is_critical(desc, time) {
81 return TestResult::TrTimedFail;
88 /// Creates a `TestResult` depending on the exit code of test subprocess.
89 pub fn get_result_from_exit_code(
92 time_opts: &Option<time::TestTimeOptions>,
93 exec_time: &Option<time::TestExecTime>,
95 let result = match (desc.allow_fail, code) {
96 (_, TR_OK) => TestResult::TrOk,
97 (true, TR_FAILED) => TestResult::TrAllowedFail,
98 (false, TR_FAILED) => TestResult::TrFailed,
99 (_, _) => TestResult::TrFailedMsg(format!("got unexpected return code {}", code)),
102 // If test is already failed (or allowed to fail), do not change the result.
103 if result != TestResult::TrOk {
107 // Check if test is failed due to timeout.
108 if let (Some(opts), Some(time)) = (time_opts, exec_time) {
109 if opts.error_on_excess && opts.is_critical(desc, time) {
110 return TestResult::TrTimedFail;