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
#![cfg(feature = "casefold")]

use std::{borrow::Cow, fmt};

use crate::core::Matcher;
use crate::matchers::strings::EqCasefoldMatcher;

use super::MismatchFormat;

/// Succeeds when the actual string equals the expected string regardless of case.
///
/// This uses Unicode case folding as opposed to just [`str::to_lowercase`].
///
/// # Examples
///
/// ```
/// use xpct::{expect, eq_casefold};
///
/// expect!("Fun").to(eq_casefold("fun"));
/// expect!("Spaß").to(eq_casefold("Spass"));
/// ```
pub fn eq_casefold<'a, Actual>(expected: impl Into<Cow<'a, str>>) -> Matcher<'a, Actual, Actual>
where
    Actual: fmt::Debug + AsRef<str> + 'a,
{
    Matcher::new(
        EqCasefoldMatcher::new(expected),
        MismatchFormat::new(
            "to equal case-insensitively",
            "to not equal case-insensitively",
        ),
    )
}

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

    #[test]
    fn succeeds_when_equal() {
        expect!("Spaß").to(eq_casefold("spass"));
    }

    #[test]
    fn succeeds_when_not_equal() {
        expect!("Spaß").to_not(eq_casefold("spas"));
    }

    #[test]
    #[should_panic]
    fn fails_when_equal() {
        expect!("Spaß").to_not(eq_casefold("spass"));
    }

    #[test]
    #[should_panic]
    fn fails_when_not_equal() {
        expect!("Spaß").to(eq_casefold("spas"));
    }
}