mumu-test 0.1.2

Test suite plugin for the Lava language
Documentation
use std::sync::{Arc, Mutex};
use serde::{Serialize, Deserialize};
use lazy_static::lazy_static;

#[derive(Clone, Serialize, Deserialize)]
pub struct TestEntry {
    pub name: String,
    pub passed: bool,
    pub time_us: i64,
    pub output: String,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct FileReport {
    pub suite: String,
    pub tests: Vec<TestEntry>,
}

pub struct GlobalTestSuite {
    pub suite_name: Option<String>,
    pub tests: Vec<Arc<Mutex<TestEntry>>>,
}

lazy_static! {
    pub static ref GLOBAL_SUITE: Mutex<GlobalTestSuite> = Mutex::new(GlobalTestSuite::new());
}

use std::cell::RefCell;
thread_local! {
    pub static CURRENT_TEST_ARC: RefCell<Option<Arc<Mutex<TestEntry>>>> = RefCell::new(None);
}

impl GlobalTestSuite {
    pub fn new() -> Self {
        Self {
            suite_name: None,
            tests: vec![],
        }
    }
}

/// Mark current test as failed (thread-local context)
pub fn mark_fail(msg: &str) {
    eprintln!("[test] => FAIL: {}", msg);

    CURRENT_TEST_ARC.with(|cell| {
        if let Some(ref arc) = *cell.borrow() {
            let mut t = arc.lock().unwrap();
            t.passed = false;
            if !t.output.is_empty() {
                t.output.push('\n');
            }
            t.output.push_str(msg);
        }
    });
}