Skip to content

Commit 8dfc566

Browse files
committed
Add BoolMatcher that matches bool value and reference.
1 parent 684afc1 commit 8dfc566

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
/// Match a bool value or bool reference.
21+
#[derive(MatcherBase)]
22+
pub struct BoolMatcher {
23+
expected: bool,
24+
}
25+
26+
impl BoolMatcher {
27+
fn matches(&self, actual: bool) -> MatcherResult {
28+
(actual == self.expected).into()
29+
}
30+
31+
fn describe(&self, matcher_result: MatcherResult) -> Description {
32+
match (matcher_result, self.expected) {
33+
(MatcherResult::Match, true) | (MatcherResult::NoMatch, false) => "is true".into(),
34+
(MatcherResult::Match, false) | (MatcherResult::NoMatch, true) => "is false".into(),
35+
}
36+
}
37+
}
38+
39+
impl Matcher<bool> for BoolMatcher {
40+
fn matches(&self, actual: bool) -> MatcherResult {
41+
self.matches(actual)
42+
}
43+
44+
fn describe(&self, matcher_result: MatcherResult) -> Description {
45+
self.describe(matcher_result)
46+
}
47+
}
48+
49+
impl<'a> Matcher<&'a bool> for BoolMatcher {
50+
fn matches(&self, actual: &'a bool) -> MatcherResult {
51+
self.matches(*actual)
52+
}
53+
fn describe(&self, matcher_result: MatcherResult) -> Description {
54+
self.describe(matcher_result)
55+
}
56+
}
57+
58+
pub fn is_true() -> BoolMatcher {
59+
BoolMatcher { expected: true }
60+
}
61+
62+
pub fn is_false() -> BoolMatcher {
63+
BoolMatcher { expected: false }
64+
}
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use crate::prelude::*;
69+
70+
#[test]
71+
fn match_value() -> Result<()> {
72+
verify_that!(true, is_true())?;
73+
verify_that!(true, not(is_false()))?;
74+
verify_that!(false, is_false())?;
75+
verify_that!(false, not(is_true()))
76+
}
77+
78+
#[test]
79+
fn match_ref() -> Result<()> {
80+
let t = true;
81+
let f = false;
82+
83+
verify_that!(&t, is_true())?;
84+
verify_that!(&t, not(is_false()))?;
85+
verify_that!(&f, is_false())?;
86+
verify_that!(&f, not(is_true()))
87+
}
88+
}

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)