Skip to content

Commit 1de3808

Browse files
Augment the matcher that allows the user to match the entries of a map with a convenience factory method which takes a variable number of matchers for individual map entries
1 parent 9b28bba commit 1de3808

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,20 @@ public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>
10791079
return org.hamcrest.collection.IsMapWithEntries.hasEntries(entriesMatcher);
10801080
}
10811081

1082+
/**
1083+
* Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map}'s set of entries
1084+
* contains, in any order, entries satisfying the specified <code>entriesMatchers</code>.
1085+
* For example:
1086+
* <pre>assertThat(myMap, hasEntries(entry("a key"), entry("another key")))</pre>
1087+
*
1088+
* @param entriesMatchers
1089+
* the matchers that must be satisfied by the entries
1090+
*/
1091+
@SafeVarargs
1092+
public static <K, V> Matcher<java.util.Map<? extends K, ? extends V>> hasEntries(Matcher<? super java.util.Map.Entry<? extends K, ? extends V>>... entriesMatchers) {
1093+
return org.hamcrest.collection.IsMapWithEntries.hasEntries(entriesMatchers);
1094+
}
1095+
10821096
/**
10831097
* Creates a matcher for {@link java.util.Map.Entry}s matching when the examined {@link java.util.Map.Entry} has a key which satisfies
10841098
* the specified <code>keyMatcher</code>, and a value which satisfies the specified <code>valueMatcher</code>.

hamcrest-library/src/main/java/org/hamcrest/collection/IsMapWithEntries.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ public IsMapWithEntries(Matcher<? super Set<? extends Map.Entry<? extends K, ? e
3030
return new IsMapWithEntries<>(entriesMatcher);
3131
}
3232

33+
/**
34+
* Creates a matcher for {@link Map}s matching when the examined {@link Map}'s set of entries
35+
* contains, in any order, entries satisfying the specified <code>entriesMatchers</code>.
36+
* For example:
37+
* <pre>assertThat(myMap, hasEntries(entry("a key"), entry("another key")))</pre>
38+
*
39+
* @param entriesMatchers
40+
* the matchers that must be satisfied by the entries
41+
*/
42+
@SafeVarargs
43+
public static <K, V> Matcher<Map<? extends K, ? extends V>> hasEntries(Matcher<? super Map.Entry<? extends K, ? extends V>>... entriesMatchers) {
44+
return new IsMapWithEntries<>(IsIterableContainingInAnyOrder.containsInAnyOrder(entriesMatchers));
45+
}
46+
3347
}

hamcrest-library/src/test/java/org/hamcrest/collection/IsMapWithEntriesTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import org.hamcrest.Matchers;
66

77
import java.util.Collections;
8+
import java.util.LinkedHashMap;
9+
import java.util.Map;
810

11+
import static org.hamcrest.collection.IsMapEntry.entry;
912
import static org.hamcrest.collection.IsMapWithEntries.hasEntries;
1013

1114
public class IsMapWithEntriesTest extends AbstractMatcherTest {
@@ -31,4 +34,13 @@ public void testHasReadableDescription() {
3134
assertDescription("a map with entries an empty collection", hasEntries(Matchers.empty()));
3235
}
3336

37+
public void testMatchesANumberOfExplicitEntriesInAnyOrder() {
38+
Map<String, Integer> map = new LinkedHashMap<>();
39+
map.put("c", 3);
40+
map.put("b", 2);
41+
map.put("a", 1);
42+
43+
assertMatches(hasEntries(entry("a", Matchers.equalTo(1)), entry("b", Matchers.equalTo(2)), entry("c", Matchers.equalTo(3))), map);
44+
}
45+
3446
}

0 commit comments

Comments
 (0)