Skip to main content

asserting/
equality.rs

1//! Implementation of the equality assertions.
2
3use crate::assertions::{
4    AssertEquality, AssertHasDebugString, AssertHasDisplayString, AssertSameAs,
5};
6use crate::colored::{mark_diff, mark_diff_str};
7use crate::expectations::{
8    has_debug_string, has_display_string, is_equal_to, is_same_as, not, HasDebugString,
9    HasDisplayString, IsEqualTo, IsSameAs,
10};
11use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Invertible, Spec};
12use crate::std::fmt::{Debug, Display};
13use crate::std::format;
14use crate::std::string::{String, ToString};
15
16impl<S, E, R> AssertEquality<E> for Spec<'_, S, R>
17where
18    S: PartialEq<E> + Debug,
19    E: Debug,
20    R: FailingStrategy,
21{
22    fn is_equal_to(self, expected: E) -> Self {
23        self.expecting(is_equal_to(expected))
24    }
25
26    fn is_not_equal_to(self, expected: E) -> Self {
27        self.expecting(not(is_equal_to(expected)))
28    }
29}
30
31impl<S, E> Expectation<S> for IsEqualTo<E>
32where
33    S: PartialEq<E> + Debug,
34    E: Debug,
35{
36    fn test(&mut self, subject: &S) -> bool {
37        subject == &self.expected
38    }
39
40    fn message(
41        &self,
42        expression: &Expression<'_>,
43        actual: &S,
44        inverted: bool,
45        format: &DiffFormat,
46    ) -> String {
47        let not = if inverted { "not " } else { "" };
48        let expected = &self.expected;
49        let (marked_actual, marked_expected) = mark_diff(actual, expected, format);
50        format!(
51            "expected {expression} to be {not}equal to {expected:?}\n   but was: {marked_actual}\n  expected: {not}{marked_expected}",
52        )
53    }
54}
55
56impl<E> Invertible for IsEqualTo<E> {}
57
58impl<S, R> AssertSameAs<S> for Spec<'_, S, R>
59where
60    S: PartialEq + Debug,
61    R: FailingStrategy,
62{
63    fn is_same_as(self, expected: S) -> Self {
64        self.expecting(is_same_as(expected))
65    }
66
67    fn is_not_same_as(self, expected: S) -> Self {
68        self.expecting(not(is_same_as(expected)))
69    }
70}
71
72impl<S> Expectation<S> for IsSameAs<S>
73where
74    S: PartialEq + Debug,
75{
76    fn test(&mut self, subject: &S) -> bool {
77        subject == &self.expected
78    }
79
80    fn message(
81        &self,
82        expression: &Expression<'_>,
83        actual: &S,
84        inverted: bool,
85        format: &DiffFormat,
86    ) -> String {
87        let not = if inverted { "not " } else { "" };
88        let expected = &self.expected;
89        let (marked_actual, marked_expected) = mark_diff(actual, expected, format);
90        format!(
91            "expected {expression} to be {not}the same as {expected:?}\n   but was: {marked_actual}\n  expected: {not}{marked_expected}",
92        )
93    }
94}
95
96impl<E> Invertible for IsSameAs<E> {}
97
98impl<S, E, R> AssertHasDebugString<E> for Spec<'_, S, R>
99where
100    S: Debug,
101    E: AsRef<str>,
102    R: FailingStrategy,
103{
104    fn has_debug_string(self, expected: E) -> Self {
105        self.expecting(has_debug_string(expected))
106    }
107
108    fn does_not_have_debug_string(self, expected: E) -> Self {
109        self.expecting(not(has_debug_string(expected)))
110    }
111}
112
113impl<S, E> Expectation<S> for HasDebugString<E>
114where
115    S: Debug,
116    E: AsRef<str>,
117{
118    fn test(&mut self, subject: &S) -> bool {
119        format!("{subject:?}") == self.expected.as_ref()
120    }
121
122    fn message(
123        &self,
124        expression: &Expression<'_>,
125        actual: &S,
126        inverted: bool,
127        format: &DiffFormat,
128    ) -> String {
129        let not = if inverted { "not " } else { "" };
130        let expected = self.expected.as_ref();
131        let (marked_actual, marked_expected) =
132            mark_diff_str(&format!("{actual:?}"), expected, format);
133        format!(
134            "expected {expression} to {not}have a debug string equal to {expected:?}\n   but was: {marked_actual}\n  expected: {not}{marked_expected}",
135        )
136    }
137}
138
139impl<E> Invertible for HasDebugString<E> {}
140
141impl<S, E, R> AssertHasDisplayString<E> for Spec<'_, S, R>
142where
143    S: Display,
144    E: AsRef<str>,
145    R: FailingStrategy,
146{
147    fn has_display_string(self, expected: E) -> Self {
148        self.expecting(has_display_string(expected))
149    }
150
151    fn does_not_have_display_string(self, expected: E) -> Self {
152        self.expecting(not(has_display_string(expected)))
153    }
154}
155
156impl<S, E> Expectation<S> for HasDisplayString<E>
157where
158    S: Display,
159    E: AsRef<str>,
160{
161    fn test(&mut self, subject: &S) -> bool {
162        subject.to_string() == self.expected.as_ref()
163    }
164
165    fn message(
166        &self,
167        expression: &Expression<'_>,
168        actual: &S,
169        inverted: bool,
170        format: &DiffFormat,
171    ) -> String {
172        let not = if inverted { "not " } else { "" };
173        let expected = self.expected.as_ref();
174        let (marked_actual, marked_expected) = mark_diff_str(&actual.to_string(), expected, format);
175        format!(
176            "expected {expression} to {not}have a display string equal to {expected:?}\n   but was: \"{marked_actual}\"\n  expected: {not}\"{marked_expected}\"",
177        )
178    }
179}
180
181impl<E> Invertible for HasDisplayString<E> {}