Skip to content

Commit df81601

Browse files
Googlercopybara-github
authored andcommitted
Add ne matcher
PiperOrigin-RevId: 838323026
1 parent 7404aa4 commit df81601

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

googletest/src/matchers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mod len_matcher;
4545
mod lt_matcher;
4646
mod matches_pattern;
4747
mod matches_regex_matcher;
48+
mod ne_matcher;
4849
mod near_matcher;
4950
mod none_matcher;
5051
mod not_matcher;
@@ -84,6 +85,7 @@ pub use le_matcher::le;
8485
pub use len_matcher::len;
8586
pub use lt_matcher::lt;
8687
pub use matches_regex_matcher::matches_regex;
88+
pub use ne_matcher::ne;
8789
pub use near_matcher::{approx_eq, near, NearMatcher};
8890
pub use none_matcher::none;
8991
pub use not_matcher::not;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2022 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+
use std::fmt::Debug;
20+
21+
/// Matches a value is not equal (in the sense of `!=`) to `expected`.
22+
///
23+
/// ```
24+
/// # use googletest::prelude::*;
25+
/// # fn should_pass() -> Result<()> {
26+
/// verify_that!(0, ne(1))?; // Passes
27+
/// # Ok(())
28+
/// # }
29+
/// # fn should_fail() -> Result<()> {
30+
/// verify_that!(0, ne(0))?; // Fails
31+
/// # Ok(())
32+
/// # }
33+
/// # should_pass().unwrap();
34+
/// # should_fail().unwrap_err();
35+
/// ```
36+
pub fn ne<T>(expected: T) -> NEMatcher<T> {
37+
NEMatcher { expected }
38+
}
39+
40+
#[derive(MatcherBase)]
41+
pub struct NEMatcher<T> {
42+
expected: T,
43+
}
44+
45+
impl<T: Debug, A: Debug + Copy + PartialEq<T>> Matcher<A> for NEMatcher<T> {
46+
fn matches(&self, actual: A) -> MatcherResult {
47+
(actual != self.expected).into()
48+
}
49+
50+
fn describe(&self, matcher_result: MatcherResult) -> Description {
51+
match matcher_result {
52+
MatcherResult::Match => format!("is not equal to {:?}", self.expected).into(),
53+
MatcherResult::NoMatch => format!("is equal to {:?}", self.expected).into(),
54+
}
55+
}
56+
}
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use crate::prelude::*;
61+
use crate::Result;
62+
use indoc::indoc;
63+
64+
#[test]
65+
fn ne_matches_i32_with_i32() -> Result<()> {
66+
verify_that!(123, ne(234))
67+
}
68+
69+
#[test]
70+
fn ne_matches_string_reference_with_string_reference() -> Result<()> {
71+
verify_that!("A string", ne("B string"))
72+
}
73+
74+
#[test]
75+
fn ne_matches_owned_string_with_string_reference() -> Result<()> {
76+
let value = "A string".to_string();
77+
verify_that!(value, ne("B string"))
78+
}
79+
80+
#[test]
81+
fn ne_matches_owned_string_reference_with_string_reference() -> Result<()> {
82+
let value = "A string".to_string();
83+
verify_that!(&value, ne("B string"))
84+
}
85+
86+
#[test]
87+
fn ne_struct_debug_diff() -> Result<()> {
88+
#[derive(Debug, PartialEq)]
89+
struct Strukt {
90+
int: i32,
91+
string: String,
92+
}
93+
94+
let result = verify_that!(
95+
Strukt { int: 123, string: "something".into() },
96+
ne(&Strukt { int: 123, string: "something".into() })
97+
);
98+
verify_that!(
99+
result,
100+
err(displays_as(contains_substring(indoc! {
101+
"
102+
Expected: is not equal to Strukt { int: 123, string: \"something\" }
103+
Actual: Strukt { int: 123, string: \"something\" },
104+
which is equal to Strukt { int: 123, string: \"something\" }
105+
"})))
106+
)
107+
}
108+
}

0 commit comments

Comments
 (0)