Skip to main content

googletest/matchers/
bool_matcher.rs

1// Copyright 2024 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16    description::Description,
17    matcher::{Matcher, MatcherBase, MatcherResult},
18};
19
20/// Matches boolean value `true`.
21pub fn is_true() -> BoolMatcher {
22    BoolMatcher { expected: true }
23}
24
25/// Matches boolean value `false`.
26pub fn is_false() -> BoolMatcher {
27    BoolMatcher { expected: false }
28}
29
30/// Matches a bool value or bool reference.
31#[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}