Skip to main content

googletest/matchers/
ne_matcher.rs

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
15use crate::{
16    description::Description,
17    matcher::{Matcher, MatcherBase, MatcherResult},
18};
19use 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/// ```
36pub fn ne<T>(expected: T) -> NEMatcher<T> {
37    NEMatcher { expected }
38}
39
40#[derive(MatcherBase)]
41pub struct NEMatcher<T> {
42    expected: T,
43}
44
45impl<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)]
59mod 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}