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
//! Defines the type `Contains`.

use crate::Assert;
use std::fmt::Debug;

/// Asserts that a value is in a vector.
///
/// # Examples
///
/// ```rust
/// use test4a::{Assert, Contains};
///
/// let assert = Contains::new(vec![10, 1, 42], 1);
/// assert!(assert.success());
/// ```
pub struct Contains<T: PartialEq + Debug> {
    vector: Vec<T>,
    value: T,
}

impl<T: PartialEq + Debug> Contains<T> {
    /// Constructor.
    pub fn new(vector: Vec<T>, value: T) -> Self {
        Self { vector, value }
    }
}

impl<T: PartialEq + Debug> Assert for Contains<T> {
    fn success(&self) -> bool {
        self.vector.contains(&self.value)
    }

    fn error_message(&self) -> String {
        "Assert `vector.contains(value)` has failed with\n".to_string()
            + &format!("    vector: `{:?}`\n", self.vector)
            + &format!("    value: `{:?}`", self.value)
    }
}

#[cfg(test)]
mod tests {
    use crate::asserts::assert::Assert;
    use crate::Contains;

    #[test]
    fn test_empty_vector() {
        let assert = Contains::new(Vec::new(), 0);
        assert!(!assert.success())
    }

    #[test]
    fn test_not_in_vector() {
        let assert = Contains::new(vec![0, 1, 2, 3], 4);
        assert!(!assert.success())
    }

    #[test]
    fn test_in_vector() {
        let assert = Contains::new(vec![0, 1, 2, 3], 2);
        assert!(assert.success())
    }
}