]> git.lizzy.rs Git - rust.git/blob - src/libtest/test_result.rs
Split libtest into several smaller modules
[rust.git] / src / libtest / test_result.rs
1
2 use std::any::Any;
3
4 use super::bench::BenchSamples;
5 use super::time;
6 use super::types::TestDesc;
7 use super::options::ShouldPanic;
8
9 pub use self::TestResult::*;
10
11 // Return codes for secondary process.
12 // Start somewhere other than 0 so we know the return code means what we think
13 // it means.
14 pub const TR_OK: i32 = 50;
15 pub const TR_FAILED: i32 = 51;
16
17 #[derive(Debug, Clone, PartialEq)]
18 pub enum TestResult {
19     TrOk,
20     TrFailed,
21     TrFailedMsg(String),
22     TrIgnored,
23     TrAllowedFail,
24     TrBench(BenchSamples),
25     TrTimedFail,
26 }
27
28 unsafe impl Send for TestResult {}
29
30
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(format!("panic did not include expected string '{}'", msg))
53                 }
54             }
55         }
56         (&ShouldPanic::Yes, Ok(())) => TestResult::TrFailedMsg("test did not panic as expected".to_string()),
57         _ if desc.allow_fail => TestResult::TrAllowedFail,
58         _ => TestResult::TrFailed,
59     };
60
61     // If test is already failed (or allowed to fail), do not change the result.
62     if result != TestResult::TrOk {
63         return result;
64     }
65
66     // Check if test is failed due to timeout.
67     if let (Some(opts), Some(time)) = (time_opts, exec_time) {
68         if opts.error_on_excess && opts.is_critical(desc, time) {
69             return TestResult::TrTimedFail;
70         }
71     }
72
73     result
74 }
75
76 pub fn get_result_from_exit_code(
77     desc: &TestDesc,
78     code: i32,
79     time_opts: &Option<time::TestTimeOptions>,
80     exec_time: &Option<time::TestExecTime>,
81 ) -> TestResult {
82     let result = match (desc.allow_fail, code) {
83         (_, TR_OK) => TestResult::TrOk,
84         (true, TR_FAILED) => TestResult::TrAllowedFail,
85         (false, TR_FAILED) => TestResult::TrFailed,
86         (_, _) => TestResult::TrFailedMsg(format!("got unexpected return code {}", code)),
87     };
88
89     // If test is already failed (or allowed to fail), do not change the result.
90     if result != TestResult::TrOk {
91         return result;
92     }
93
94     // Check if test is failed due to timeout.
95     if let (Some(opts), Some(time)) = (time_opts, exec_time) {
96         if opts.error_on_excess && opts.is_critical(desc, time) {
97             return TestResult::TrTimedFail;
98         }
99     }
100
101     result
102 }