module_fixtures/
module_fixtures.rs

1use rest::prelude::*;
2use std::cell::RefCell;
3
4// A shared counter for demonstration
5thread_local! {
6    static TEST_COUNTER: RefCell<u32> = RefCell::new(0);
7}
8
9// Helper to increment the counter
10fn increment_counter() {
11    TEST_COUNTER.with(|counter| {
12        *counter.borrow_mut() += 1;
13    });
14}
15
16// Helper to get the counter value
17fn get_counter() -> u32 {
18    return TEST_COUNTER.with(|counter| *counter.borrow());
19}
20
21// Define functions to demonstrate module fixtures
22fn setup_module_fixtures() {
23    println!("\nExample of using module-level fixtures:");
24    println!("In a real test, you would use:");
25    println!("#[with_fixtures_module]");
26    println!("mod my_test_module {{");
27    println!("    #[setup]");
28    println!("    fn setup() {{ /* setup code */ }}");
29    println!("");
30    println!("    #[tear_down]");
31    println!("    fn tear_down() {{ /* cleanup code */ }}");
32    println!("");
33    println!("    #[test]");
34    println!("    fn test_something() {{ /* fixtures automatically applied */ }}");
35    println!("}}\n");
36}
37
38// Module fixture setup example
39fn run_fixture_setup() {
40    println!("Setting up test environment...");
41    TEST_COUNTER.with(|counter| {
42        *counter.borrow_mut() = 0;
43    });
44}
45
46// Module fixture teardown example
47fn run_fixture_teardown() {
48    println!("Cleaning up test environment...");
49    TEST_COUNTER.with(|counter| {
50        println!("Final counter value: {}", *counter.borrow());
51    });
52}
53
54// Simulate a test
55fn run_simulated_test() {
56    // Run a simulated setup
57    run_fixture_setup();
58
59    println!("Running test...");
60    // Test code
61    expect!(get_counter()).to_equal(0);
62
63    // Do something in the test
64    increment_counter();
65    expect!(get_counter()).to_equal(1);
66
67    // Run a simulated teardown
68    run_fixture_teardown();
69}
70
71fn main() {
72    // Enable enhanced output for better test reporting
73    config().enhanced_output(true).apply();
74
75    // Explain the module fixture concept
76    setup_module_fixtures();
77
78    // Run a simulated test
79    run_simulated_test();
80
81    println!("\nAll tests passed!");
82}