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