Skip to content

Commit 5e28dbe

Browse files
Merge pull request #497 from yaxum62:bool_matcher
PiperOrigin-RevId: 698296798
2 parents 684afc1 + a591055 commit 5e28dbe

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
15+
use crate::{
16+
description::Description,
17+
matcher::{Matcher, MatcherBase, MatcherResult},
18+
};
19+
20+
/// Matches boolean value `true`.
21+
pub fn is_true() -> BoolMatcher {
22+
BoolMatcher { expected: true }
23+
}
24+
25+
/// Matches boolean value `false`.
26+
pub fn is_false() -> BoolMatcher {
27+
BoolMatcher { expected: false }
28+
}
29+
30+
/// Matches a bool value or bool reference.
31+
#[derive(MatcherBase)]
32+
pub struct BoolMatcher {
33+
expected: bool,
34+
}
35+
36+
impl 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+
49+
impl 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+
59+
impl<'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)]
69+
mod tests {
70+
use super::*;
71+
use crate::prelude::*;
72+
73+
#[test]
74+
fn match_value() -> Result<()> {
75+
verify_that!(true, is_true())?;
76+
verify_that!(true, not(is_false()))?;
77+
verify_that!(false, is_false())?;
78+
verify_that!(false, not(is_true()))
79+
}
80+
81+
#[test]
82+
fn match_ref() -> Result<()> {
83+
let t = true;
84+
let f = false;
85+
86+
verify_that!(&t, is_true())?;
87+
verify_that!(&t, not(is_false()))?;
88+
verify_that!(&f, is_false())?;
89+
verify_that!(&f, not(is_true()))
90+
}
91+
92+
#[test]
93+
fn describe() {
94+
assert_eq!(is_true().describe(MatcherResult::Match).to_string(), "is true");
95+
assert_eq!(is_true().describe(MatcherResult::NoMatch).to_string(), "is false");
96+
assert_eq!(is_false().describe(MatcherResult::Match).to_string(), "is false");
97+
assert_eq!(is_false().describe(MatcherResult::NoMatch).to_string(), "is true");
98+
}
99+
}

googletest/src/matchers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
mod all_matcher;
1818
mod any_matcher;
1919
mod anything_matcher;
20+
mod bool_matcher;
2021
mod char_count_matcher;
2122
mod conjunction_matcher;
2223
mod container_eq_matcher;
@@ -59,6 +60,7 @@ mod tuple_matcher;
5960
mod unordered_elements_are_matcher;
6061

6162
pub use anything_matcher::anything;
63+
pub use bool_matcher::{is_false, is_true};
6264
pub use char_count_matcher::char_count;
6365
pub use container_eq_matcher::container_eq;
6466
pub use contains_matcher::{contains, ContainsMatcher};

0 commit comments

Comments
 (0)