clearcheck 0.0.2

Elegant and extensible assertions in rust.
Documentation
use std::collections::HashMap;
use std::hash::Hash;

use crate::matchers::{Matcher, MatcherResult};

/// MapEmptyMatcher offers a flexible way to assert whether a HashMap is empty.
///
/// # Example
///```
/// use std::collections::HashMap;
/// use clearcheck::matchers::map::empty::be_empty;
/// use clearcheck::matchers::Matcher;
///
/// let key_value: HashMap<i32, i32> = HashMap::new();
/// let matcher = be_empty();
///
/// assert!(matcher.test(&key_value).passed());
/// ```
pub enum MapEmptyMatcher {
    Empty,
    NotEmpty,
}

impl<K: Hash + Eq, V> Matcher<HashMap<K, V>> for MapEmptyMatcher {
    fn test(&self, collection: &HashMap<K, V>) -> MatcherResult {
        match self {
            MapEmptyMatcher::Empty => MatcherResult::new(
                collection.is_empty(),
                "Map should be empty",
                "Map should not be empty",
            ),
            MapEmptyMatcher::NotEmpty => MatcherResult::new(
                !collection.is_empty(),
                "Map should not be empty",
                "Map should be empty",
            ),
        }
    }
}

/// Creates a MapEmptyMatcher that asserts whether a HashMap is empty.
pub fn be_empty() -> MapEmptyMatcher {
    MapEmptyMatcher::Empty
}

/// Creates a MapEmptyMatcher that asserts whether a HashMap is not empty.
pub fn not_be_empty() -> MapEmptyMatcher {
    MapEmptyMatcher::NotEmpty
}

#[cfg(test)]
mod map_tests {
    use std::collections::HashMap;

    use crate::assertions::bool::TrueFalseAssertion;
    use crate::matchers::map::empty::{be_empty, not_be_empty};
    use crate::matchers::Matcher;

    #[test]
    fn should_be_empty() {
        let key_value: HashMap<i32, i32> = HashMap::new();
        let matcher = be_empty();
        matcher.test(&key_value).passed.should_be_true();
    }

    #[test]
    #[should_panic]
    fn should_be_empty_but_was_not() {
        let mut key_value: HashMap<&str, &str> = HashMap::new();
        key_value.insert("java", "junit");

        let matcher = be_empty();
        matcher.test(&key_value).passed.should_be_true();
    }

    #[test]
    fn should_not_be_empty() {
        let mut key_value: HashMap<&str, &str> = HashMap::new();
        key_value.insert("java", "junit");

        let matcher = not_be_empty();
        matcher.test(&key_value).passed.should_be_true();
    }

    #[test]
    #[should_panic]
    fn should_not_be_empty_but_was() {
        let key_value: HashMap<&str, &str> = HashMap::new();

        let matcher = not_be_empty();
        matcher.test(&key_value).passed.should_be_true();
    }
}