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
use crate::core::{Join, Matcher};

/// A matcher for `have_count` assertions.
pub struct HaveCount {
    count: usize,
}

/// Returns a new `HaveCount` matcher.
pub fn have_count(c: usize) -> HaveCount {
    HaveCount { count: c }
}

impl<A, T> Matcher<A, ()> for HaveCount
where
    A: Iterator<Item = T> + Clone,
{
    fn failure_message(&self, join: Join, actual: &A) -> String {
        if join.is_assertion() {
            format!(
                "expected {} have count <{}>, got <{}>",
                join,
                self.count,
                actual.clone().count()
            )
        } else {
            format!("expected {} have count <{}>", join, self.count)
        }
    }

    fn matches(&self, actual: &A) -> bool {
        actual.clone().count() == self.count
    }
}

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

    #[test]
    fn test_to_have_count_2_message() {
        expect("abc".chars())
            .to(have_count(2))
            .assert_eq_message("expected to have count <2>, got <3>");
    }

    #[test]
    fn test_not_to_have_count_3_message() {
        expect("abc".chars())
            .not_to(have_count(3))
            .assert_eq_message("expected not to have count <3>")
    }
}