]> git.lizzy.rs Git - rust.git/blob - library/test/src/test_result.rs
Rollup merge of #89869 - kpreid:from-doc, r=yaahc
[rust.git] / library / test / src / test_result.rs
1 use std::any::Any;
2
3 use super::bench::BenchSamples;
4 use super::options::ShouldPanic;
5 use super::time;
6 use super::types::TestDesc;
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     TrBench(BenchSamples),
23     TrTimedFail,
24 }
25
26 /// Creates a `TestResult` depending on the raw result of test execution
27 /// and associated data.
28 pub fn calc_result<'a>(
29     desc: &TestDesc,
30     task_result: Result<(), &'a (dyn Any + 'static + Send)>,
31     time_opts: &Option<time::TestTimeOptions>,
32     exec_time: &Option<time::TestExecTime>,
33 ) -> TestResult {
34     let result = match (&desc.should_panic, task_result) {
35         (&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
36         (&ShouldPanic::YesWithMessage(msg), Err(ref err)) => {
37             let maybe_panic_str = err
38                 .downcast_ref::<String>()
39                 .map(|e| &**e)
40                 .or_else(|| err.downcast_ref::<&'static str>().copied());
41
42             if maybe_panic_str.map(|e| e.contains(msg)).unwrap_or(false) {
43                 TestResult::TrOk
44             } else if let Some(panic_str) = maybe_panic_str {
45                 TestResult::TrFailedMsg(format!(
46                     r#"panic did not contain expected string
47       panic message: `{:?}`,
48  expected substring: `{:?}`"#,
49                     panic_str, msg
50                 ))
51             } else {
52                 TestResult::TrFailedMsg(format!(
53                     r#"expected panic with string value,
54  found non-string value: `{:?}`
55      expected substring: `{:?}`"#,
56                     (**err).type_id(),
57                     msg
58                 ))
59             }
60         }
61         (&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
62             TestResult::TrFailedMsg("test did not panic as expected".to_string())
63         }
64         _ => TestResult::TrFailed,
65     };
66
67     // If test is already failed (or allowed to fail), do not change the result.
68     if result != TestResult::TrOk {
69         return result;
70     }
71
72     // Check if test is failed due to timeout.
73     if let (Some(opts), Some(time)) = (time_opts, exec_time) {
74         if opts.error_on_excess && opts.is_critical(desc, time) {
75             return TestResult::TrTimedFail;
76         }
77     }
78
79     result
80 }
81
82 /// Creates a `TestResult` depending on the exit code of test subprocess.
83 pub fn get_result_from_exit_code(
84     desc: &TestDesc,
85     code: i32,
86     time_opts: &Option<time::TestTimeOptions>,
87     exec_time: &Option<time::TestExecTime>,
88 ) -> TestResult {
89     let result = match code {
90         TR_OK => TestResult::TrOk,
91         TR_FAILED => TestResult::TrFailed,
92         _ => TestResult::TrFailedMsg(format!("got unexpected return code {}", code)),
93     };
94
95     // If test is already failed (or allowed to fail), do not change the result.
96     if result != TestResult::TrOk {
97         return result;
98     }
99
100     // Check if test is failed due to timeout.
101     if let (Some(opts), Some(time)) = (time_opts, exec_time) {
102         if opts.error_on_excess && opts.is_critical(desc, time) {
103             return TestResult::TrTimedFail;
104         }
105     }
106
107     result
108 }