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`.
is_true() -> BoolMatcher21 pub fn is_true() -> BoolMatcher {
22 BoolMatcher { expected: true }
23 }
24
25 /// Matches boolean value `false`.
is_false() -> BoolMatcher26 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 {
matches(&self, actual: bool) -> MatcherResult37 fn matches(&self, actual: bool) -> MatcherResult {
38 (actual == self.expected).into()
39 }
40
describe(&self, matcher_result: MatcherResult) -> Description41 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 {
matches(&self, actual: bool) -> MatcherResult50 fn matches(&self, actual: bool) -> MatcherResult {
51 self.matches(actual)
52 }
53
describe(&self, matcher_result: MatcherResult) -> Description54 fn describe(&self, matcher_result: MatcherResult) -> Description {
55 self.describe(matcher_result)
56 }
57 }
58
59 impl<'a> Matcher<&'a bool> for BoolMatcher {
matches(&self, actual: &'a bool) -> MatcherResult60 fn matches(&self, actual: &'a bool) -> MatcherResult {
61 self.matches(*actual)
62 }
describe(&self, matcher_result: MatcherResult) -> Description63 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]
match_value() -> Result<()>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]
match_ref() -> Result<()>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]
describe()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 }
100