use crate::backend::Assertion;
use crate::backend::assertions::sentence::AssertionSentence;
use std::fmt::Debug;
pub trait EqualityMatchers<T> {
fn to_equal(self, expected: T) -> Self;
fn to_equal_value(self, expected: T) -> Self;
}
trait AsEqualityComparable<T> {
fn equals<U: PartialEq<T>>(&self, expected: &U) -> bool;
}
impl<T: PartialEq> AsEqualityComparable<T> for T {
fn equals<U: PartialEq<T>>(&self, expected: &U) -> bool {
expected == self
}
}
impl<T: PartialEq> AsEqualityComparable<T> for &T {
fn equals<U: PartialEq<T>>(&self, expected: &U) -> bool {
expected == *self
}
}
impl<V, T> EqualityMatchers<T> for Assertion<V>
where
T: Debug + PartialEq + Clone,
V: AsEqualityComparable<T> + Debug + Clone,
{
fn to_equal(self, expected: T) -> Self {
return self.to_equal_value(expected);
}
fn to_equal_value(self, expected: T) -> Self {
let result = self.value.equals(&expected);
let sentence = AssertionSentence::new("be", format!("equal to {:?}", expected));
return self.add_step(sentence, result);
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[test]
fn test_equality() {
crate::Reporter::disable_deduplication();
expect!(42).to_equal(42);
expect!(42).not().to_equal(100);
expect!("hello").to_equal("hello");
expect!("hello").not().to_equal("world");
expect!(3.14).to_equal(3.14);
expect!(3.14).not().to_equal(2.71);
expect!(true).to_equal(true);
expect!(true).not().to_equal(false);
}
#[test]
#[should_panic(expected = "be equal to")]
fn test_equality_fails() {
let _assertion = expect!(42).to_equal(43);
std::hint::black_box(_assertion);
}
#[test]
#[should_panic(expected = "not be equal to")]
fn test_equality_not_fails() {
let _assertion = expect!(42).not().to_equal(42);
std::hint::black_box(_assertion);
}
}