logical_chain/
logical_chain.rs

1mod common;
2use common::guard_test;
3use rest::prelude::*;
4
5fn main() {
6    // Enable enhanced output for this example
7    config().enhanced_output(true).apply();
8
9    // Example of using the AND logical operator
10    let value = 42;
11
12    println!("\n----- Testing AND chains -----");
13    println!("For value = {}", value);
14
15    // A passing chain - all conditions are true
16    println!("\nExpecting value to be greater than 30 AND less than 50 AND even:");
17    expect!(value).to_be_greater_than(30).and().to_be_less_than(50).and().to_be_even();
18
19    // A failing chain - one condition is false
20    println!("\nExpecting value to be greater than 30 AND less than 40 AND odd (will fail):");
21    let success = guard_test(|| {
22        expect!(value).to_be_greater_than(30).and().to_be_less_than(40).and().to_be_odd();
23    });
24
25    if !success {
26        println!("Test failed as expected (AND assertion)");
27    }
28
29    // Example of using the OR logical operator
30    println!("\n----- Testing OR chains -----");
31
32    // A passing chain - at least one condition is true
33    println!("\nExpecting value to be less than 30 OR greater than 40 (will pass):");
34    let success = guard_test(|| {
35        expect!(value).to_be_less_than(30).or().to_be_greater_than(40);
36    });
37
38    if success {
39        println!("Test passed as expected (OR assertion with one true condition)");
40    }
41
42    // A failing chain - all conditions are false
43    println!("\nExpecting value to be less than 30 OR negative (will fail):");
44    let success = guard_test(|| {
45        expect!(value).to_be_less_than(30).or().to_be_negative();
46    });
47
48    if !success {
49        println!("Test failed as expected (OR assertion with all false conditions)");
50    }
51
52    // Mixed AND and OR chains
53    println!("\n----- Testing mixed AND/OR chains -----");
54
55    // A complex passing chain
56    println!("\nComplex passing chain with mixed AND/OR:");
57    let success = guard_test(|| {
58        expect!(value).to_be_greater_than(30).and().to_be_less_than(50).or().to_be_even();
59    });
60
61    if success {
62        println!("Mixed chain test passed as expected");
63    }
64
65    // Using NOT with chaining
66    println!("\n----- Testing with NOT modifier -----");
67
68    println!("\nExpecting value to NOT be greater than 50 AND to NOT be less than 30:");
69    let success = guard_test(|| {
70        expect!(value).not().to_be_greater_than(50).and().not().to_be_less_than(30);
71    });
72
73    if success {
74        println!("Test with NOT modifiers passed as expected");
75    }
76
77    // Using the fluent API with different types
78    println!("\n----- Testing with different types -----");
79
80    let text = "Hello, world!";
81    println!("\nString tests: text = \"{}\"", text);
82    let success = guard_test(|| {
83        expect!(text).to_contain("Hello").and().to_end_with("!").and().not().to_be_empty();
84    });
85
86    if success {
87        println!("String test passed as expected");
88    }
89
90    let numbers = vec![1, 2, 3, 4, 5];
91    println!("\nVec tests: numbers = {:?}", numbers);
92    let success = guard_test(|| {
93        expect!(&numbers).to_contain(3).and().to_have_length(5).and().not().to_be_empty();
94    });
95
96    if success {
97        println!("Vector test passed as expected");
98    }
99
100    println!("\nAll examples completed!");
101}