Skip to main content

mumutest/
suite.rs

1use std::sync::{Arc, Mutex};
2use serde::{Serialize, Deserialize};
3use lazy_static::lazy_static;
4
5#[derive(Clone, Serialize, Deserialize)]
6pub struct TestEntry {
7    pub name: String,
8    pub passed: bool,
9    pub time_us: i64,
10    pub output: String,
11}
12
13#[derive(Clone, Serialize, Deserialize)]
14pub struct FileReport {
15    pub suite: String,
16    pub tests: Vec<TestEntry>,
17}
18
19pub struct GlobalTestSuite {
20    pub suite_name: Option<String>,
21    pub tests: Vec<Arc<Mutex<TestEntry>>>,
22}
23
24lazy_static! {
25    pub static ref GLOBAL_SUITE: Mutex<GlobalTestSuite> = Mutex::new(GlobalTestSuite::new());
26}
27
28use std::cell::RefCell;
29thread_local! {
30    pub static CURRENT_TEST_ARC: RefCell<Option<Arc<Mutex<TestEntry>>>> = RefCell::new(None);
31}
32
33impl GlobalTestSuite {
34    pub fn new() -> Self {
35        Self {
36            suite_name: None,
37            tests: vec![],
38        }
39    }
40}
41
42/// Mark current test as failed (thread-local context)
43pub fn mark_fail(msg: &str) {
44    eprintln!("[test] => FAIL: {}", msg);
45
46    CURRENT_TEST_ARC.with(|cell| {
47        if let Some(ref arc) = *cell.borrow() {
48            let mut t = arc.lock().unwrap();
49            t.passed = false;
50            if !t.output.is_empty() {
51                t.output.push('\n');
52            }
53            t.output.push_str(msg);
54        }
55    });
56}
57