googletest/matchers/
bool_matcher.rs1use crate::{
16 description::Description,
17 matcher::{Matcher, MatcherBase, MatcherResult},
18};
19
20pub fn is_true() -> BoolMatcher {
22 BoolMatcher { expected: true }
23}
24
25pub fn is_false() -> BoolMatcher {
27 BoolMatcher { expected: false }
28}
29
30#[derive(MatcherBase)]
32pub struct BoolMatcher {
33 expected: bool,
34}
35
36impl BoolMatcher {
37 fn matches(&self, actual: bool) -> MatcherResult {
38 (actual == self.expected).into()
39 }
40
41 fn describe(&self, matcher_result: MatcherResult) -> Description {
42 match (matcher_result, self.expected) {
43 (MatcherResult::Match, true) | (MatcherResult::NoMatch, false) => "is true".into(),
44 (MatcherResult::Match, false) | (MatcherResult::NoMatch, true) => "is false".into(),
45 }
46 }
47}
48
49impl Matcher<bool> for BoolMatcher {
50 fn matches(&self, actual: bool) -> MatcherResult {
51 self.matches(actual)
52 }
53
54 fn describe(&self, matcher_result: MatcherResult) -> Description {
55 self.describe(matcher_result)
56 }
57}
58
59impl<'a> Matcher<&'a bool> for BoolMatcher {
60 fn matches(&self, actual: &'a bool) -> MatcherResult {
61 self.matches(*actual)
62 }
63 fn describe(&self, matcher_result: MatcherResult) -> Description {
64 self.describe(matcher_result)
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71 use crate::prelude::*;
72 use crate::Result;
73
74 #[test]
75 fn match_value() -> Result<()> {
76 verify_that!(true, is_true())?;
77 verify_that!(true, not(is_false()))?;
78 verify_that!(false, is_false())?;
79 verify_that!(false, not(is_true()))
80 }
81
82 #[test]
83 fn match_ref() -> Result<()> {
84 let t = true;
85 let f = false;
86
87 verify_that!(&t, is_true())?;
88 verify_that!(&t, not(is_false()))?;
89 verify_that!(&f, is_false())?;
90 verify_that!(&f, not(is_true()))
91 }
92
93 #[test]
94 fn describe() {
95 assert_eq!(is_true().describe(MatcherResult::Match).to_string(), "is true");
96 assert_eq!(is_true().describe(MatcherResult::NoMatch).to_string(), "is false");
97 assert_eq!(is_false().describe(MatcherResult::Match).to_string(), "is false");
98 assert_eq!(is_false().describe(MatcherResult::NoMatch).to_string(), "is true");
99 }
100}