3 use super::bench::BenchSamples;
5 use super::types::TestDesc;
6 use super::options::ShouldPanic;
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 assotiated 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>().map(|e| *e));
46 .map(|e| e.contains(msg))
52 TestResult::TrAllowedFail
54 if let Some(panic_str) = maybe_panic_str{
55 TestResult::TrFailedMsg(
56 format!(r#"panic did not contain expected string
57 panic message: `{:?}`,
58 expected substring: `{:?}`"#, panic_str, &*msg)
61 TestResult::TrFailedMsg(
62 format!(r#"expected panic with string value,
63 found non-string value: `{:?}`
64 expected substring: `{:?}`"#, (**err).type_id(), &*msg)
70 (&ShouldPanic::Yes, Ok(())) => {
71 TestResult::TrFailedMsg("test did not panic as expected".to_string())
73 _ if desc.allow_fail => TestResult::TrAllowedFail,
74 _ => TestResult::TrFailed,
77 // If test is already failed (or allowed to fail), do not change the result.
78 if result != TestResult::TrOk {
82 // Check if test is failed due to timeout.
83 if let (Some(opts), Some(time)) = (time_opts, exec_time) {
84 if opts.error_on_excess && opts.is_critical(desc, time) {
85 return TestResult::TrTimedFail;
92 /// Creates a `TestResult` depending on the exit code of test subprocess.
93 pub fn get_result_from_exit_code(
96 time_opts: &Option<time::TestTimeOptions>,
97 exec_time: &Option<time::TestExecTime>,
99 let result = match (desc.allow_fail, code) {
100 (_, TR_OK) => TestResult::TrOk,
101 (true, TR_FAILED) => TestResult::TrAllowedFail,
102 (false, TR_FAILED) => TestResult::TrFailed,
103 (_, _) => TestResult::TrFailedMsg(format!("got unexpected return code {}", code)),
106 // If test is already failed (or allowed to fail), do not change the result.
107 if result != TestResult::TrOk {
111 // Check if test is failed due to timeout.
112 if let (Some(opts), Some(time)) = (time_opts, exec_time) {
113 if opts.error_on_excess && opts.is_critical(desc, time) {
114 return TestResult::TrTimedFail;