Skip to content

Commit 39145e5

Browse files
Matcher that allows the user to match the entries of a map
1 parent e20e58d commit 39145e5

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

hamcrest-library/src/main/java/org/hamcrest/Matchers.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,19 @@ public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>
10681068
return org.hamcrest.collection.IsMapContaining.<K,V>hasEntry(key, value);
10691069
}
10701070

1071+
/**
1072+
* Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map}'s set of entries
1073+
* satisfies the specified <code>entriesMatcher</code>.
1074+
* For example:
1075+
* <pre>assertThat(myMap, hasEntries(hasSize(2)))</pre>
1076+
*
1077+
* @param entriesMatcher
1078+
* the matcher that must be satisfied by the set of entries
1079+
*/
1080+
public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> hasEntries(Matcher<? super java.util.Set<? extends java.util.Map.Entry<? extends K, ? extends V>>> entriesMatcher) {
1081+
return org.hamcrest.collection.IsMapWithEntries.hasEntries(entriesMatcher);
1082+
}
1083+
10711084
/**
10721085
* Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains
10731086
* at least one key that satisfies the specified matcher.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.FeatureMatcher;
4+
import org.hamcrest.Matcher;
5+
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
public class IsMapWithEntries<K, V> extends FeatureMatcher<Map<? extends K, ? extends V>, Set<? extends Map.Entry<? extends K, ? extends V>>> {
10+
11+
public IsMapWithEntries(Matcher<? super Set<? extends Map.Entry<? extends K, ? extends V>>> entriesMatcher) {
12+
super(entriesMatcher, "a map with entries", "map entries");
13+
}
14+
15+
@Override
16+
protected Set<? extends Map.Entry<? extends K, ? extends V>> featureValueOf(Map<? extends K, ? extends V> actual) {
17+
return actual.entrySet();
18+
}
19+
20+
/**
21+
* Creates a matcher for {@link Map}s matching when the examined {@link Map}'s set of entries
22+
* satisfies the specified <code>entriesMatcher</code>.
23+
* For example:
24+
* <pre>assertThat(myMap, hasEntries(hasSize(2)))</pre>
25+
*
26+
* @param entriesMatcher
27+
* the matcher that must be satisfied by the set of entries
28+
*/
29+
public static <K, V> Matcher<Map<? extends K, ? extends V>> hasEntries(Matcher<? super Set<? extends Map.Entry<? extends K, ? extends V>>> entriesMatcher) {
30+
return new IsMapWithEntries<>(entriesMatcher);
31+
}
32+
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.Matchers;
6+
7+
import java.util.Collections;
8+
9+
import static org.hamcrest.collection.IsMapWithEntries.hasEntries;
10+
11+
public class IsMapWithEntriesTest extends AbstractMatcherTest {
12+
13+
@Override
14+
protected Matcher<?> createMatcher() {
15+
return hasEntries(Matchers.empty());
16+
}
17+
18+
public void testDoesNotMatchNull() {
19+
assertMismatchDescription("was null", hasEntries(Matchers.empty()), null);
20+
}
21+
22+
public void testDoesNotMatchAMapWhoseEntriesDoNotSatisfyTheEntriesMatcher() {
23+
assertMismatchDescription("map entries collection size was <0>", hasEntries(Matchers.hasSize(1)), Collections.emptyMap());
24+
}
25+
26+
public void testMatchesAMapWhoseEntriesSatisfyTheEntriesMatcher() {
27+
assertMatches(hasEntries(Matchers.hasSize(1)), Collections.singletonMap("k", "v"));
28+
}
29+
30+
public void testHasReadableDescription() {
31+
assertDescription("a map with entries an empty collection", hasEntries(Matchers.empty()));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)