fluent_test/backend/matchers/
boolean.rs1use 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
10trait AsBoolean {
12 fn is_true(&self) -> bool;
13 fn is_false(&self) -> bool;
14}
15
16impl AsBoolean for bool {
18 fn is_true(&self) -> bool {
19 *self
20 }
21
22 fn is_false(&self) -> bool {
23 !*self
24 }
25}
26
27impl AsBoolean for &bool {
29 fn is_true(&self) -> bool {
30 **self
31 }
32
33 fn is_false(&self) -> bool {
34 !**self
35 }
36}
37
38impl<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 crate::Reporter::disable_deduplication();
66
67 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 let _assertion = expect!(true).not().to_be_true();
77 std::hint::black_box(_assertion);
79 }
80
81 #[test]
82 #[should_panic(expected = "be true")]
83 fn test_false_to_be_true_fails() {
84 let _assertion = expect!(false).to_be_true();
86 std::hint::black_box(_assertion);
88 }
89
90 #[test]
91 fn test_boolean_false() {
92 crate::Reporter::disable_deduplication();
94
95 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 let _assertion = expect!(false).not().to_be_false();
105 std::hint::black_box(_assertion);
107 }
108
109 #[test]
110 #[should_panic(expected = "be false")]
111 fn test_true_to_be_false_fails() {
112 let _assertion = expect!(true).to_be_false();
114 std::hint::black_box(_assertion);
116 }
117}