]> git.lizzy.rs Git - rust.git/blob - src/libtest/test_result.rs
print a more useful error message on should_panic mismatch
[rust.git] / src / libtest / test_result.rs
1 use std::any::Any;
2
3 use super::bench::BenchSamples;
4 use super::time;
5 use super::types::TestDesc;
6 use super::options::ShouldPanic;
7
8 pub use self::TestResult::*;
9
10 // Return codes for secondary process.
11 // Start somewhere other than 0 so we know the return code means what we think
12 // it means.
13 pub const TR_OK: i32 = 50;
14 pub const TR_FAILED: i32 = 51;
15
16 #[derive(Debug, Clone, PartialEq)]
17 pub enum TestResult {
18     TrOk,
19     TrFailed,
20     TrFailedMsg(String),
21     TrIgnored,
22     TrAllowedFail,
23     TrBench(BenchSamples),
24     TrTimedFail,
25 }
26
27 unsafe impl Send for TestResult {}
28
29 /// Creates a `TestResult` depending on the raw result of test execution
30 /// and assotiated data.
31 pub fn calc_result<'a>(
32     desc: &TestDesc,
33     task_result: Result<(), &'a (dyn Any + 'static + Send)>,
34     time_opts: &Option<time::TestTimeOptions>,
35     exec_time: &Option<time::TestExecTime>
36 ) -> TestResult {
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>()
42                 .map(|e| &**e)
43                 .or_else(|| err.downcast_ref::<&'static str>().map(|e| *e));
44
45             if maybe_panic_str
46                 .map(|e| e.contains(msg))
47                 .unwrap_or(false)
48             {
49                 TestResult::TrOk
50             } else {
51                 if desc.allow_fail {
52                     TestResult::TrAllowedFail
53                 } else {
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)
59                         )
60                     } else {
61                         TestResult::TrFailedMsg(
62                             format!(r#"expected panic with string value,
63  found non-string value: `{:?}`
64      expected substring: `{:?}`"#, (**err).type_id(), &*msg)
65                         )
66                     }
67                 }
68             }
69         }
70         (&ShouldPanic::Yes, Ok(())) => {
71             TestResult::TrFailedMsg("test did not panic as expected".to_string())
72         }
73         _ if desc.allow_fail => TestResult::TrAllowedFail,
74         _ => TestResult::TrFailed,
75     };
76
77     // If test is already failed (or allowed to fail), do not change the result.
78     if result != TestResult::TrOk {
79         return result;
80     }
81
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;
86         }
87     }
88
89     result
90 }
91
92 /// Creates a `TestResult` depending on the exit code of test subprocess.
93 pub fn get_result_from_exit_code(
94     desc: &TestDesc,
95     code: i32,
96     time_opts: &Option<time::TestTimeOptions>,
97     exec_time: &Option<time::TestExecTime>,
98 ) -> TestResult {
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)),
104     };
105
106     // If test is already failed (or allowed to fail), do not change the result.
107     if result != TestResult::TrOk {
108         return result;
109     }
110
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;
115         }
116     }
117
118     result
119 }