1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
//! ## Usage
//!
//! ``` rust
//! use easy_assert::{actual_with, expected_with};
//! use easy_assert::custom_assertions::CustomAssert;
//!
//! struct TestStruct {
//! a: i32,
//! b: String,
//! c: bool,
//! }
//!
//! fn custom_match(val1: &TestStruct, val2: &TestStruct) -> bool {
//! val1.a == val2.a && val1.b.eq(&val2.b) && val1.c == val2.c
//! }
//!
//! fn custom_description(value: &TestStruct) -> String {
//! format!(
//! "TestStruct:\n a = {}\n, b = {}\n, c = {}",
//! value.a, value.b, value.c
//! )
//! }
//!
//! #[test]
//! fn my_test() {
//! let val1 = TestStruct {
//! a: 1,
//! b: String::from("a"),
//! c: false,
//! };
//! let val2 = TestStruct {
//! a: 1,
//! b: String::from("a"),
//! c: false,
//! };
//!
//! CustomAssert::assert_that(actual_with(val1, custom_description))
//! .matches_by(custom_match)
//! .to(expected_with(val2, custom_description));
//! }
//! ```
use crate::assertions::Matches;
use crate::{Actual, Expected};
pub struct CustomAssert<T> {
actual: Actual<T>,
}
impl<T> CustomAssert<T>
where
T: 'static,
{
pub fn assert_that(actual: Actual<T>) -> CustomAssert<T> {
CustomAssert { actual }
}
pub fn matches_by(self, matcher: fn(&T, &T) -> bool) -> Box<dyn Matches<T>> {
Box::new(CustomMatcher {
actual: self.actual,
matcher,
})
}
}
struct CustomMatcher<T> {
actual: Actual<T>,
matcher: fn(&T, &T) -> bool,
}
impl<T> Matches<T> for CustomMatcher<T> {
fn to(&self, expected: Expected<T>) {
let success = (self.matcher)(&self.actual.value, &expected.value);
if !success {
panic!(
"\n Actual: {} \n not matches with \n {} \n",
self.actual, expected
);
}
}
}