attribute_fixtures/
attribute_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// Module to contain the fixture functions with attribute-style macros
10mod test_module {
11    use super::*;
12
13    // Setup function with the attribute style
14    #[setup]
15    fn setup() {
16        println!("Setting up test environment with attribute macro...");
17        TEST_COUNTER.with(|counter| {
18            *counter.borrow_mut() = 0;
19        });
20    }
21
22    // Teardown function with the attribute style
23    #[tear_down]
24    fn tear_down() {
25        println!("Cleaning up test environment with attribute macro...");
26        TEST_COUNTER.with(|counter| {
27            println!("Final counter value: {}", *counter.borrow());
28        });
29    }
30
31    // Test function with the attribute style
32    #[with_fixtures]
33    pub fn run_test() {
34        // Initial value should be 0 (set by the setup function)
35        expect!(get_counter()).to_equal(0);
36
37        // Increment and check
38        increment_counter();
39        expect!(get_counter()).to_equal(1);
40    }
41}
42
43// Helper to increment the counter
44fn increment_counter() {
45    TEST_COUNTER.with(|counter| {
46        *counter.borrow_mut() += 1;
47    });
48}
49
50// Helper to get the counter value
51fn get_counter() -> u32 {
52    TEST_COUNTER.with(|counter| *counter.borrow())
53}
54
55fn main() {
56    // Enable enhanced output for better test reporting
57    config().enhanced_output(true).apply();
58
59    println!("Running test with attribute-style fixtures:");
60    test_module::run_test();
61
62    println!("\nTest passed!");
63}