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
//=============================================================================================
// Macros
//=============================================================================================

#[macro_export]
macro_rules! assert_contains {
    ($collection:expr, $item:expr) => {
        if let None = $collection.into_iter().find(|&x| x == $item) {
            panic!("assertion failed: (collection contains item)\n       item: {:?}\n collection: {:?}\n",
                    $item,
                    $collection,
            );
        }
    };
}

#[macro_export]
macro_rules! assert_all {
    ($collection:expr, $predicate:expr) => {
        if false == $collection.into_iter().all($predicate) {
            panic!("assertion failed: (all elements of collection match predicate) collection: {:?}",
                $collection,
            )
        }
    };
    ($collection:expr, $predicate:expr, $($arg:tt)+) => {
        if false == $collection.into_iter().all($predicate) {
            panic!("assertion failed: (all elements of collection match predicate)\n  predicate: {}\n collection: {:?}",
                format_args!($($arg)+),
                $collection,
            )
        }
    }
}

#[macro_export]
macro_rules! assert_any {
    ($collection:expr, $predicate:expr) => {
        if false == $collection.into_iter().any($predicate) {
            panic!("assertion failed: (any element of collection matches predicate) collection: {:?}",
                $collection,
            )
        }
    };
    ($collection:expr, $predicate:expr, $($arg:tt)+) => {
        if false == $collection.into_iter().any($predicate) {
            panic!("assertion failed: (any element of collection matches predicate)\n  predicate: {}\n collection: {:?}",
                format_args!($($arg)+),
                $collection,
            )
        }
    }
}

//=============================================================================================
// Unit Tests
//=============================================================================================

#[cfg(test)]
mod contains {
    use std::collections::HashMap;
    #[test]
    fn contains_item_in_vec() {
        let vec = vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
        let x = 5;
        assert_contains!(&vec, &x);
    }

    #[test]
    fn contains_item_in_map() {
        let mut map = HashMap::new();
        map.insert("a", 1);
        map.insert("b", 3);
        let pair = (&"a", &1);
        assert_contains!(&map, pair);
    }

    #[test]
    #[should_panic]
    fn does_not_contain_item() {
        let vec = vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
        let x = 2;
        assert_contains!(&vec, &x);
    }
}

#[cfg(test)]
mod all {
    use std::collections::HashMap;
    #[test]
    fn all_items_match() {
        let vec = vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
        assert_all!(&vec, |&x| x > 0, "all > 0");
    }

    #[test]
    #[should_panic]
    fn one_item_matches() {
        let mut map = HashMap::new();
        map.insert("a", 1);
        map.insert("b", 3);
        map.insert("c", 5);
        map.insert("d", 7);
        let pair = (&"a", &1);
        assert_all!(&map, |x| x == pair, r#"all == ("a", 1)"#);
    }
}


#[cfg(test)]
mod any {
    use std::collections::HashMap;
    #[test]
    fn all_items_match() {
        let vec = vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
        assert_any!(&vec, |&x| x > 0, "any > 0");
    }

    #[test]
    fn one_item_matches() {
        let mut map = HashMap::new();
        map.insert("a", 1);
        map.insert("b", 3);
        map.insert("c", 5);
        map.insert("d", 7);
        let pair = (&"a", &1);
        assert_any!(&map, |x| x == pair, r#"any == ("a", 1)"#);
    }

    #[test]
    #[should_panic]
    fn no_items_match() {
        let vec = vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
        assert_any!(&vec, |&x| x < 0, "x < 0");
    }
}