use fluent_test::prelude::*;
use once_cell::sync::Lazy;
use std::sync::{
Mutex,
atomic::{AtomicUsize, Ordering},
};
static BEFORE_ALL_COUNTER: AtomicUsize = AtomicUsize::new(0);
static SETUP_COUNTER: AtomicUsize = AtomicUsize::new(0);
static TEARDOWN_COUNTER: AtomicUsize = AtomicUsize::new(0);
static AFTER_ALL_COUNTER: AtomicUsize = AtomicUsize::new(0);
static BEFORE_ALL_TEST_MUTEX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
#[with_fixtures_module]
mod lifecycle_fixtures {
use super::*;
#[before_all]
fn setup_module() {
BEFORE_ALL_COUNTER.fetch_add(1, Ordering::SeqCst);
}
#[setup]
fn setup_test() {
SETUP_COUNTER.fetch_add(1, Ordering::SeqCst);
}
#[tear_down]
fn teardown_test() {
TEARDOWN_COUNTER.fetch_add(1, Ordering::SeqCst);
}
#[after_all]
fn teardown_module() {
AFTER_ALL_COUNTER.fetch_add(1, Ordering::SeqCst);
}
#[test]
fn test_before_all_runs_once() {
let _guard = BEFORE_ALL_TEST_MUTEX.lock().unwrap();
let before_all_count = BEFORE_ALL_COUNTER.load(Ordering::SeqCst);
expect!(before_all_count).to_equal(1);
let after_all_count = AFTER_ALL_COUNTER.load(Ordering::SeqCst);
expect!(after_all_count).to_equal(0);
}
#[test]
fn test_setup_teardown_execution() {
let setup_count = SETUP_COUNTER.load(Ordering::SeqCst);
expect!(setup_count).to_be_greater_than(0);
std::thread::sleep(std::time::Duration::from_millis(10));
}
#[test]
fn test_setup_teardown_is_per_test() {
let setup_count = SETUP_COUNTER.load(Ordering::SeqCst);
expect!(setup_count).to_be_greater_than(0);
expect!(setup_count > 0 && BEFORE_ALL_COUNTER.load(Ordering::SeqCst) > 0).to_be_true();
}
}
static AFTER_ALL_EXECUTED: AtomicUsize = AtomicUsize::new(0);
static AFTER_ALL_MUTEX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
#[with_fixtures_module]
mod after_all_test {
use super::*;
#[after_all]
fn verify_after_all_runs() {
AFTER_ALL_EXECUTED.fetch_add(1, Ordering::SeqCst);
let setup_count = SETUP_COUNTER.load(Ordering::SeqCst);
let teardown_count = TEARDOWN_COUNTER.load(Ordering::SeqCst);
if setup_count == 0 || teardown_count == 0 {
eprintln!("ERROR in after_all verification: setup_count={}, teardown_count={}", setup_count, teardown_count);
}
}
#[test]
fn test_to_register_fixtures() {
let _guard = AFTER_ALL_MUTEX.lock().unwrap();
SETUP_COUNTER.fetch_add(1, Ordering::SeqCst);
expect!(true).to_be_true();
let after_all_exec_count = AFTER_ALL_EXECUTED.load(Ordering::SeqCst);
expect!(after_all_exec_count).to_equal(0);
}
#[tear_down]
fn mark_test_complete() {
TEARDOWN_COUNTER.fetch_add(1, Ordering::SeqCst);
}
}
#[test]
fn test_after_all_setup() {
let _guard = AFTER_ALL_MUTEX.lock().unwrap();
let after_all_count = AFTER_ALL_EXECUTED.load(Ordering::SeqCst);
expect!(after_all_count).to_equal(0);
}