googletest/matchers/
ne_matcher.rs1use crate::{
16 description::Description,
17 matcher::{Matcher, MatcherBase, MatcherResult},
18};
19use std::fmt::Debug;
20
21pub 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}