Skip to content

Commit

Permalink
Sarthak | Adds key/value based matchers for HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Jan 2, 2024
1 parent a0b187d commit 321e1aa
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 10 deletions.
112 changes: 103 additions & 9 deletions src/matchers/map/contain.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hash;

use crate::panicking::{assert_failed_binary, AssertKind};

trait Contain<K, V> {
trait KeyContains<K, V> {
fn should_contain_key<Q>(&self, key: &Q) -> &Self
where K: Borrow<Q>,
Q: Hash + Eq + std::fmt::Debug;
Q: Hash + Eq + Debug;

fn should_not_contain_key<Q>(&self, key: &Q) -> &Self
where K: Borrow<Q>,
Q: Hash + Eq + std::fmt::Debug;
Q: Hash + Eq + Debug;
}

trait KeyValueContains<K, V> {
fn should_contain<Q, S>(&self, key: &Q, value: &S) -> &Self
where K: Borrow<Q>,
V: Borrow<S> + PartialEq<S>,
Q: Hash + Eq + Debug,
S: PartialOrd + Debug;

fn should_not_contain<Q, S>(&self, key: &Q, value: &S) -> &Self
where K: Borrow<Q>,
V: Borrow<S> + PartialEq<S>,
Q: Hash + Eq + Debug,
S: PartialOrd + Debug;
}

impl<K, V> Contain<K, V> for HashMap<K, V>
where K: Hash + Eq + PartialEq + std::fmt::Debug {
impl<K, V> KeyContains<K, V> for HashMap<K, V>
where K: Hash + Eq + PartialEq + Debug {
fn should_contain_key<Q>(&self, key: &Q) -> &Self
where K: Borrow<Q>,
Q: Hash + Eq + std::fmt::Debug {
Q: Hash + Eq + Debug {
let contains = self.contains_key(key);
if !contains {
assert_failed_binary(AssertKind::Contains, &self.keys(), key);
Expand All @@ -29,7 +43,7 @@ impl<K, V> Contain<K, V> for HashMap<K, V>

fn should_not_contain_key<Q>(&self, key: &Q) -> &Self
where K: Borrow<Q>,
Q: Hash + Eq + std::fmt::Debug {
Q: Hash + Eq + Debug {
let contains = self.contains_key(key);
if contains {
assert_failed_binary(AssertKind::NotContains, &self.keys(), key);
Expand All @@ -38,11 +52,46 @@ impl<K, V> Contain<K, V> for HashMap<K, V>
}
}

impl<K, V> KeyValueContains<K, V> for HashMap<K, V>
where K: Hash + Eq + PartialEq + Debug,
V: PartialOrd + Debug {
fn should_contain<Q, S>(&self, key: &Q, value: &S) -> &Self
where K: Borrow<Q>,
V: Borrow<S> + PartialEq<S>,
Q: Hash + Eq + Debug,
S: PartialOrd + Debug {
match self.get(key) {
None => {
assert_failed_binary(AssertKind::Contains, &self.keys(), key);
}
Some(existing) if existing != value => {
assert_failed_binary(AssertKind::ContainsValue, &self.values(), value);
}
_ => {}
}
self
}

fn should_not_contain<Q, S>(&self, key: &Q, value: &S) -> &Self
where K: Borrow<Q>,
V: Borrow<S> + PartialEq<S>,
Q: Hash + Eq + Debug,
S: PartialOrd + Debug {
match self.get(key) {
Some(existing) if existing == value => {
assert_failed_binary(AssertKind::NotContainsValue, &self.values(), value);
}
_ => {}
}
self
}
}

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

use crate::matchers::map::contain::Contain;
use crate::matchers::map::contain::KeyContains;

#[test]
fn should_contain_key() {
Expand Down Expand Up @@ -73,4 +122,49 @@ mod tests {
key_value.insert("rust", "assert");
key_value.should_not_contain_key(&"rust");
}
}

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

use crate::matchers::map::contain::KeyValueContains;

#[test]
fn should_contain_key_value() {
let mut key_value = HashMap::new();
key_value.insert("rust", "assert");
key_value.should_contain(&"rust", &"assert");
}

#[test]
#[should_panic]
fn should_contain_key_value_but_it_did_not_1() {
let mut key_value = HashMap::new();
key_value.insert("rust", "assert");
key_value.should_contain(&"rust", &"catch");
}

#[test]
#[should_panic]
fn should_contain_key_value_but_it_did_not_2() {
let mut key_value = HashMap::new();
key_value.insert("rust", "assert");
key_value.should_contain(&"java", &"catch");
}

#[test]
fn should_not_contain_key_value() {
let mut key_value = HashMap::new();
key_value.insert("rust", "assert");
key_value.should_not_contain(&"rust", &"catch");
}

#[test]
#[should_panic]
fn should_not_contain_key_value_but_it_did() {
let mut key_value = HashMap::new();
key_value.insert("rust", "assert");
key_value.should_not_contain(&"rust", &"assert");
}
}
6 changes: 5 additions & 1 deletion src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ pub(crate) enum AssertKind {
NotContainsDuplicates,
SortsAscending,
SortsDescending,
EqualSize,
NotEqualSize,
EqualSize,
AtleastSize,
AtmostSize,
ContainsValue,
NotContainsValue,
}

pub(crate) fn assert_failed_unary<T>(
Expand Down Expand Up @@ -57,6 +59,8 @@ fn assert_failed_inner(
AssertKind::NotEqualSize => "must not have the same size as",
AssertKind::AtleastSize => "must have at least the same size as",
AssertKind::AtmostSize => "must have at most the same size as",
AssertKind::ContainsValue => "must contain the value of",
AssertKind::NotContainsValue => "must not contain the value of",
};

match right {
Expand Down

0 comments on commit 321e1aa

Please sign in to comment.