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
//! Helper utilities for working with [`predicates`](https://docs.rs/predicates),
//! inspired in part by
//! [Google Mock matchers](https://github.com/google/googletest/blob/master/googlemock/docs/cheat_sheet.md#matchers-matcherlist).

pub use predicates;
pub use predicates_tree;

/// Make predicate-based assertions with a better error message.
///
/// # Examples
///
/// ```
/// use assert_that::assert_that;
///
/// assert_that!("Hello World", str::similar("Hello World"));
///
/// assert_that!("Hello World", str::diff("Goodbye World"));
///
/// // Can be used with more complex predicates
/// assert_that!(
///     &1234,
///     ge(-5).and(le(i16::MAX)),
/// );
/// ```
///
/// Note that `predicate::*` functions from `predicates::prelude` are brought into
/// scope automatically when used within the macro invocation. See
/// `predicates` [documentation](https://docs.rs/predicates) for details about
/// available predicates.
#[macro_export]
macro_rules! assert_that {
    ($value:expr, $pred:expr $(,)?) => {{
        use $crate::predicates::prelude::*;
        use $crate::predicates_tree::CaseTreeExt;

        use predicate::*;

        if let Some(case) = $pred.find_case(false, $value) {
            panic!("{}", case.tree());
        };
    }};
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_success() {
        assert_that!(&(), always());
        assert_that!(&(), never().not());
        assert_that!(&(2 + 2), eq(4));
    }

    #[test]
    fn test_accept_trailing_comma() {
        assert_that!(
            "Always have a needle in your haystack",
            str::contains("needle"),
        );
    }

    #[test]
    #[should_panic(expected = "(var == 5 && var > 3)\n└── var == 5\n")]
    fn test_failure_shows_tree() {
        assert_that!(&(2 + 2), eq(5).and(gt(3)))
    }
}