sweet/matchers/
matcher_eq.rs

1use super::*;
2use std::fmt::Debug;
3
4#[extend::ext(name=SweetEq)]
5pub impl<T> T
6where
7	T: Debug,
8{
9	/// Performs an assertion ensuring this value is equal to `expected`.
10	///
11	/// ## Example
12	///
13	/// ```
14	/// # use sweet::prelude::*;
15	/// 1.xpect_eq(1);
16	/// ```
17	///
18	/// ## Panics
19	///
20	/// Panics if the value is not equal to `expected`.
21	fn xpect_eq<U>(&self, expected: U) -> &Self
22	where
23		T: PartialEq<U>,
24		U: Debug,
25	{
26		if self != &expected {
27			assert_ext::panic_expected_received_debug(expected, self);
28		}
29		self
30	}
31	/// Performs an assertion ensuring this value is not equal to `expected`.
32	///
33	/// ## Example
34	///
35	/// ```
36	/// # use sweet::prelude::*;
37	/// 1.xpect_not_eq(2);
38	/// ```
39	///
40	/// ## Panics
41	///
42	/// Panics if the value is equal to `expected`.
43	fn xpect_not_eq<U>(&self, expected: U) -> &Self
44	where
45		T: PartialEq<U>,
46		U: Debug,
47	{
48		if self == &expected {
49			assert_ext::panic_expected_received_display_debug(
50				format!("NOT {:?}", expected),
51				self,
52			);
53		}
54		self
55	}
56}
57
58#[cfg(test)]
59mod test {
60	use crate::prelude::*;
61
62	#[test]
63	fn equality() {
64		true.xpect_eq(true);
65		(&true).xpect_eq(true);
66
67		"foo".xpect_eq("foo");
68		"foo".to_string().xpect_eq("foo");
69
70		"foo".xpect_not_eq("bar".to_string());
71		"foo".to_string().xpect_not_eq("bar");
72	}
73}