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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::fmt;
use std::marker::PhantomData;

use crate::core::{style, Format, Formatter, MatchFailure, Matcher};
use crate::matchers::{EqualMatcher, Mismatch};

/// A formatter for [`Mismatch`] types.
///
/// # Examples
///
/// ```
/// # use xpct::format::MismatchFormat;
/// let format: MismatchFormat<String, String> = MismatchFormat::new(
///     "to equal",
///     "to not equal",
/// );
/// ```
///
/// ```
/// # use xpct::format::MismatchFormat;
/// let format: MismatchFormat<u32, u32> = MismatchFormat::new(
///     "to be greater than or equal to",
///     "to not be greater than or equal to",
/// );
/// ```
#[derive(Debug)]
pub struct MismatchFormat<Actual, Expected> {
    marker: PhantomData<(Actual, Expected)>,
    pos_msg: String,
    neg_msg: String,
}

impl<Actual, Expected> MismatchFormat<Actual, Expected> {
    /// Create a new [`MismatchFormat`].
    ///
    /// This accepts two error messages: the one to use in the *positive* case (when we were
    /// expecting the matcher to succeed) and the one to use in the *negative* case (when we were
    /// expecting the matcher to fail).
    pub fn new(pos_msg: impl Into<String>, neg_msg: impl Into<String>) -> Self {
        Self {
            marker: PhantomData,
            pos_msg: pos_msg.into(),
            neg_msg: neg_msg.into(),
        }
    }
}

impl<Actual, Expected> Format for MismatchFormat<Actual, Expected>
where
    Actual: fmt::Debug,
    Expected: fmt::Debug,
{
    type Value = MatchFailure<Mismatch<Actual, Expected>>;

    fn fmt(self, f: &mut Formatter, value: Self::Value) -> crate::Result<()> {
        match value {
            MatchFailure::Pos(mismatch) => {
                f.set_style(style::important());
                f.write_str("Expected:\n");

                f.set_style(style::bad());
                f.write_str(format!("{}{:?}\n", style::indent(1), mismatch.actual));

                f.set_style(style::important());
                f.write_str(self.pos_msg);
                f.write_str(":\n");

                f.set_style(style::bad());
                f.write_str(format!("{}{:?}\n", style::indent(1), mismatch.expected));
            }
            MatchFailure::Neg(mismatch) => {
                f.set_style(style::important());
                f.write_str("Expected:\n");

                f.set_style(style::bad());
                f.write_str(format!("{}{:?}\n", style::indent(1), mismatch.actual));

                f.set_style(style::important());
                f.write_str(self.neg_msg);
                f.write_str(":\n");

                f.set_style(style::bad());
                f.write_str(format!("{}{:?}\n", style::indent(1), mismatch.expected));
            }
        };

        Ok(())
    }
}

/// Succeeds when the actual value equals the expected value.
///
/// # Examples
///
/// ```
/// use xpct::{expect, equal};
///
/// expect!("foobar").to(equal("foobar"));
/// ```
pub fn equal<'a, Actual, Expected>(expected: Expected) -> Matcher<'a, Actual, Actual>
where
    Actual: fmt::Debug + PartialEq<Expected> + Eq + 'a,
    Expected: fmt::Debug + 'a,
{
    Matcher::simple(
        EqualMatcher::new(expected),
        MismatchFormat::new("to equal", "to not equal"),
    )
}

#[cfg(test)]
mod tests {
    use super::equal;
    use crate::expect;

    #[test]
    fn succeeds_when_equal() {
        expect!("some string").to(equal("some string"));
    }

    #[test]
    fn succeeds_when_not_equal() {
        expect!("some string").to_not(equal("a different string"));
    }

    #[test]
    #[should_panic]
    fn fails_when_equal() {
        expect!("some string").to_not(equal("some string"));
    }

    #[test]
    #[should_panic]
    fn fails_when_not_equal() {
        expect!("some string").to(equal("a different string"));
    }
}