1use std::any::Any;
2#[cfg(unix)]
3use std::os::unix::process::ExitStatusExt;
4use std::process::ExitStatus;
5
6pub use self::TestResult::*;
7use super::bench::BenchSamples;
8use super::options::ShouldPanic;
9use super::time;
10use super::types::TestDesc;
11
12pub(crate) const TR_OK: i32 = 50;
16
17#[cfg(windows)]
20const STATUS_FAIL_FAST_EXCEPTION: i32 = 0xC0000409u32 as i32;
21
22#[cfg(target_os = "fuchsia")]
28const ZX_TASK_RETCODE_EXCEPTION_KILL: i32 = -1028;
29
30#[derive(Debug, Clone, PartialEq)]
31pub enum TestResult {
32    TrOk,
33    TrFailed,
34    TrFailedMsg(String),
35    TrIgnored,
36    TrBench(BenchSamples),
37    TrTimedFail,
38}
39
40pub(crate) fn calc_result(
43    desc: &TestDesc,
44    panic_payload: Option<&(dyn Any + Send)>,
45    time_opts: Option<&time::TestTimeOptions>,
46    exec_time: Option<&time::TestExecTime>,
47) -> TestResult {
48    let result = match (desc.should_panic, panic_payload) {
49        (ShouldPanic::No, None) | (ShouldPanic::Yes, Some(_)) => TestResult::TrOk,
51
52        (ShouldPanic::YesWithMessage(msg), Some(err)) => {
54            let maybe_panic_str = err
55                .downcast_ref::<String>()
56                .map(|e| &**e)
57                .or_else(|| err.downcast_ref::<&'static str>().copied());
58
59            if maybe_panic_str.map(|e| e.contains(msg)).unwrap_or(false) {
60                TestResult::TrOk
61            } else if let Some(panic_str) = maybe_panic_str {
62                TestResult::TrFailedMsg(format!(
63                    r#"panic did not contain expected string
64      panic message: {panic_str:?}
65 expected substring: {msg:?}"#
66                ))
67            } else {
68                TestResult::TrFailedMsg(format!(
69                    r#"expected panic with string value,
70 found non-string value: `{:?}`
71     expected substring: {msg:?}"#,
72                    (*err).type_id()
73                ))
74            }
75        }
76
77        (ShouldPanic::Yes, None) | (ShouldPanic::YesWithMessage(_), None) => {
79            let fn_location = if !desc.source_file.is_empty() {
80                &format!(" at {}:{}:{}", desc.source_file, desc.start_line, desc.start_col)
81            } else {
82                ""
83            };
84            TestResult::TrFailedMsg(format!("test did not panic as expected{}", fn_location))
85        }
86
87        (ShouldPanic::No, Some(_)) => TestResult::TrFailed,
89    };
90
91    if result != TestResult::TrOk {
93        return result;
94    }
95
96    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}
105
106pub(crate) fn get_result_from_exit_code(
108    desc: &TestDesc,
109    status: ExitStatus,
110    time_opts: Option<&time::TestTimeOptions>,
111    exec_time: Option<&time::TestExecTime>,
112) -> TestResult {
113    let result = match status.code() {
114        Some(TR_OK) => TestResult::TrOk,
115        #[cfg(windows)]
116        Some(STATUS_FAIL_FAST_EXCEPTION) => TestResult::TrFailed,
117        #[cfg(unix)]
118        None => match status.signal() {
119            Some(libc::SIGABRT) => TestResult::TrFailed,
120            Some(signal) => {
121                TestResult::TrFailedMsg(format!("child process exited with signal {signal}"))
122            }
123            None => unreachable!("status.code() returned None but status.signal() was None"),
124        },
125        #[cfg(target_os = "fuchsia")]
127        Some(ZX_TASK_RETCODE_EXCEPTION_KILL) => TestResult::TrFailed,
128        #[cfg(not(unix))]
129        None => TestResult::TrFailedMsg(format!("unknown return code")),
130        #[cfg(any(windows, unix))]
131        Some(code) => TestResult::TrFailedMsg(format!("got unexpected return code {code}")),
132        #[cfg(not(any(windows, unix)))]
133        Some(_) => TestResult::TrFailed,
134    };
135
136    if result != TestResult::TrOk {
138        return result;
139    }
140
141    if let (Some(opts), Some(time)) = (time_opts, exec_time) {
143        if opts.error_on_excess && opts.is_critical(desc, time) {
144            return TestResult::TrTimedFail;
145        }
146    }
147
148    result
149}