asserting/
equality.rs

1//! Implementation of the equality assertions.
2
3use crate::assertions::AssertEquality;
4use crate::colored::mark_diff;
5use crate::expectations::{IsEqualTo, IsNotEqualTo};
6use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Spec};
7use crate::std::fmt::Debug;
8use crate::std::{format, string::String};
9
10impl<S, E, R> AssertEquality<E> for Spec<'_, S, R>
11where
12    S: PartialEq<E> + Debug,
13    E: Debug,
14    R: FailingStrategy,
15{
16    fn is_equal_to(self, expected: E) -> Self {
17        self.expecting(IsEqualTo { expected })
18    }
19
20    fn is_not_equal_to(self, expected: E) -> Self {
21        self.expecting(IsNotEqualTo { expected })
22    }
23}
24
25impl<S, E> Expectation<S> for IsEqualTo<E>
26where
27    S: PartialEq<E> + Debug,
28    E: Debug,
29{
30    fn test(&mut self, subject: &S) -> bool {
31        subject == &self.expected
32    }
33
34    fn message(&self, expression: &Expression<'_>, actual: &S, format: &DiffFormat) -> String {
35        let expected = &self.expected;
36        let (marked_actual, marked_expected) = mark_diff(actual, expected, format);
37        format!(
38            "expected {expression} is equal to {expected:?}\n   but was: {marked_actual}\n  expected: {marked_expected}",
39        )
40    }
41}
42
43impl<S, E> Expectation<S> for IsNotEqualTo<E>
44where
45    S: PartialEq<E> + Debug,
46    E: Debug,
47{
48    fn test(&mut self, subject: &S) -> bool {
49        subject != &self.expected
50    }
51
52    fn message(&self, expression: &Expression<'_>, actual: &S, _format: &DiffFormat) -> String {
53        format!(
54            "expected {expression} is not equal to {:?}\n   but was: {actual:?}\n  expected: not {:?}",
55            &self.expected, &self.expected
56        )
57    }
58}