1pub enum Matcher<I> {
2 Val(I),
3 Any,
4}
5
6impl<I: std::fmt::Debug> std::fmt::Debug for Matcher<I> {
7 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8 match self {
9 Self::Val(val) => write!(f, "{:?}", val),
10 Self::Any => write!(f, "Any"),
11 }
12 }
13}
14
15impl<I: PartialEq> PartialEq for Matcher<I> {
16 fn eq(&self, other: &Matcher<I>) -> bool {
17 use crate::matcher::Matcher::*;
18
19 match (self, other) {
20 (&Val(ref a), &Val(ref b)) => a == b,
21 _ => true,
22 }
23 }
24}
25
26pub fn eq<I>(input: I) -> Matcher<I> {
27 Matcher::Val(input)
28}
29
30pub fn any<I>() -> Matcher<I> {
31 Matcher::Any
32}
33
34#[cfg(test)]
35mod test {
36 use super::Matcher::*;
37 use table_test::table_test;
38
39 #[test]
40 fn test_eq() {
41 let table = vec![
42 ((Val(5), Val(6)), false),
43 ((Val(5), Val(5)), true),
44 ((Any, Val(5)), true),
45 ((Val(5), Any), true),
46 ((Any, Any), true),
47 ];
48
49 for (test_case, (matcher_1, matcher_2), expected) in table_test!(table) {
50 let actual = matcher_1.eq(&matcher_2);
51
52 test_case
53 .given(&format!("{:?}, {:?}", matcher_1, matcher_2))
54 .when("equal")
55 .then(&format!("is {}", expected))
56 .assert_eq(expected, actual);
57 }
58 }
59}