fluent_test/backend/matchers/
boolean.rs

1use crate::backend::Assertion;
2use crate::backend::assertions::sentence::AssertionSentence;
3use std::fmt::Debug;
4
5pub trait BooleanMatchers {
6    fn to_be_true(self) -> Self;
7    fn to_be_false(self) -> Self;
8}
9
10/// Helper trait for boolean-like types
11trait AsBoolean {
12    fn is_true(&self) -> bool;
13    fn is_false(&self) -> bool;
14}
15
16// Implementation for bool
17impl AsBoolean for bool {
18    fn is_true(&self) -> bool {
19        *self
20    }
21
22    fn is_false(&self) -> bool {
23        !*self
24    }
25}
26
27// Implementation for &bool
28impl AsBoolean for &bool {
29    fn is_true(&self) -> bool {
30        **self
31    }
32
33    fn is_false(&self) -> bool {
34        !**self
35    }
36}
37
38// Single implementation for any type that implements AsBoolean
39impl<V> BooleanMatchers for Assertion<V>
40where
41    V: AsBoolean + Debug + Clone,
42{
43    fn to_be_true(self) -> Self {
44        let result = self.value.is_true();
45        let sentence = AssertionSentence::new("be", "true");
46
47        return self.add_step(sentence, result);
48    }
49
50    fn to_be_false(self) -> Self {
51        let result = self.value.is_false();
52        let sentence = AssertionSentence::new("be", "false");
53
54        return self.add_step(sentence, result);
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use crate::prelude::*;
61
62    #[test]
63    fn test_boolean_true() {
64        // Disable deduplication for tests
65        crate::Reporter::disable_deduplication();
66
67        // This should pass
68        expect!(true).to_be_true();
69        expect!(false).not().to_be_true();
70    }
71
72    #[test]
73    #[should_panic(expected = "not be true")]
74    fn test_not_true_fails() {
75        // This will evaluate and panic when the Assertion is dropped
76        let _assertion = expect!(true).not().to_be_true();
77        // Force the value to be dropped at the end of the function
78        std::hint::black_box(_assertion);
79    }
80
81    #[test]
82    #[should_panic(expected = "be true")]
83    fn test_false_to_be_true_fails() {
84        // This will evaluate and panic when the Assertion is dropped
85        let _assertion = expect!(false).to_be_true();
86        // Force the value to be dropped at the end of the function
87        std::hint::black_box(_assertion);
88    }
89
90    #[test]
91    fn test_boolean_false() {
92        // Disable deduplication for tests
93        crate::Reporter::disable_deduplication();
94
95        // This should pass
96        expect!(false).to_be_false();
97        expect!(true).not().to_be_false();
98    }
99
100    #[test]
101    #[should_panic(expected = "not be false")]
102    fn test_not_false_fails() {
103        // This will evaluate and panic when the Assertion is dropped
104        let _assertion = expect!(false).not().to_be_false();
105        // Force the value to be dropped at the end of the function
106        std::hint::black_box(_assertion);
107    }
108
109    #[test]
110    #[should_panic(expected = "be false")]
111    fn test_true_to_be_false_fails() {
112        // This will evaluate and panic when the Assertion is dropped
113        let _assertion = expect!(true).to_be_false();
114        // Force the value to be dropped at the end of the function
115        std::hint::black_box(_assertion);
116    }
117}