JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

Add 'hasEntry' matchers for (key, valueMatcher) and (keyMatcher, value)

Open Shpota opened this issue 5 years ago • 1 comments

When I test maps I often need to apply a matcher for the value and check equality for the key. Currently there is no hasEntry matcher that would consume a key, and a matcher for the value so I have to do it somewhat like this:

Map<String, List<Integer>> map = singletonMap("key", asList(1, 2));

assertThat(map, hasEntry(equalTo("key"), contains(1, 2)));

It would be more convenient to do it like this:

assertThat(map, hasEntry("key", contains(1, 2)));

For that reason, I introduced two hasEntry matchers with the following signatures:

hasEntry(K key, Matcher<? super V> valueMatcher)
hasEntry(Matcher<? super K> keyMatcher, V value)

Note: I originally submitted this changes as a part of this PR but since its fork has gone I cannot rebase it anymore. That's why I had to opened this one.

Shpota avatar Jun 30 '20 15:06 Shpota

Going to try and kick start hamcrest, so if you want to get it merged, please rebase from the branch v2.3-candidates. Still trying to understand how has permissions to perform a release.

nhojpatrick avatar Feb 13 '22 12:02 nhojpatrick