]> git.lizzy.rs Git - rust.git/blob - src/libtest/test_result.rs
Rollup merge of #65775 - matthewjasper:reempty, r=pnkfelix
[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             if err
41                 .downcast_ref::<String>()
42                 .map(|e| &**e)
43                 .or_else(|| err.downcast_ref::<&'static str>().map(|e| *e))
44                 .map(|e| e.contains(msg))
45                 .unwrap_or(false)
46             {
47                 TestResult::TrOk
48             } else {
49                 if desc.allow_fail {
50                     TestResult::TrAllowedFail
51                 } else {
52                     TestResult::TrFailedMsg(
53                         format!("panic did not include expected string '{}'", msg)
54                     )
55                 }
56             }
57         }
58         (&ShouldPanic::Yes, Ok(())) => {
59             TestResult::TrFailedMsg("test did not panic as expected".to_string())
60         }
61         _ if desc.allow_fail => TestResult::TrAllowedFail,
62         _ => TestResult::TrFailed,
63     };
64
65     // If test is already failed (or allowed to fail), do not change the result.
66     if result != TestResult::TrOk {
67         return result;
68     }
69
70     // Check if test is failed due to timeout.
71     if let (Some(opts), Some(time)) = (time_opts, exec_time) {
72         if opts.error_on_excess && opts.is_critical(desc, time) {
73             return TestResult::TrTimedFail;
74         }
75     }
76
77     result
78 }
79
80 /// Creates a `TestResult` depending on the exit code of test subprocess.
81 pub fn get_result_from_exit_code(
82     desc: &TestDesc,
83     code: i32,
84     time_opts: &Option<time::TestTimeOptions>,
85     exec_time: &Option<time::TestExecTime>,
86 ) -> TestResult {
87     let result = match (desc.allow_fail, code) {
88         (_, TR_OK) => TestResult::TrOk,
89         (true, TR_FAILED) => TestResult::TrAllowedFail,
90         (false, TR_FAILED) => TestResult::TrFailed,
91         (_, _) => TestResult::TrFailedMsg(format!("got unexpected return code {}", code)),
92     };
93
94     // If test is already failed (or allowed to fail), do not change the result.
95     if result != TestResult::TrOk {
96         return result;
97     }
98
99     // Check if test is failed due to timeout.
100     if let (Some(opts), Some(time)) = (time_opts, exec_time) {
101         if opts.error_on_excess && opts.is_critical(desc, time) {
102             return TestResult::TrTimedFail;
103         }
104     }
105
106     result
107 }