|
| 1 | +package org.hamcrest.collection; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.hamcrest.AbstractMatcherTest; |
| 7 | +import org.hamcrest.Matcher; |
| 8 | +import static org.hamcrest.collection.IsMapContainingKey.hasKey; |
| 9 | + |
| 10 | +public class IsMapContainingKeyTest extends AbstractMatcherTest { |
| 11 | + |
| 12 | + public void testMatchesSingletonMapContainingKey() { |
| 13 | + Map<String,Integer> map = new HashMap<String, Integer>(); |
| 14 | + map.put("a", 1); |
| 15 | + |
| 16 | + assertMatches("Matches single key", hasKey("a"), map); |
| 17 | + } |
| 18 | + |
| 19 | + public void testMatchesMapContainingKey() { |
| 20 | + Map<String,Integer> map = new HashMap<String, Integer>(); |
| 21 | + map.put("a", 1); |
| 22 | + map.put("b", 2); |
| 23 | + map.put("c", 3); |
| 24 | + |
| 25 | + assertMatches("Matches a", hasKey("a"), map); |
| 26 | + assertMatches("Matches c", hasKey("c"), map); |
| 27 | + } |
| 28 | + |
| 29 | + public void testHasReadableDescription() { |
| 30 | + assertDescription("map with key \"a\"", hasKey("a")); |
| 31 | + } |
| 32 | + |
| 33 | + public void testDoesNotMatchEmptyMap() { |
| 34 | + assertDoesNotMatch("Empty map", hasKey("Foo"), new HashMap<String,Integer>()); |
| 35 | + } |
| 36 | + |
| 37 | + public void testDoesNotMatchMapMissingKey() { |
| 38 | + Map<String,Integer> map = new HashMap<String, Integer>(); |
| 39 | + map.put("a", 1); |
| 40 | + map.put("b", 2); |
| 41 | + map.put("c", 3); |
| 42 | + |
| 43 | + assertDoesNotMatch("Map without matching key", hasKey("d"), map); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + @Override |
| 48 | + protected Matcher<?> createMatcher() { |
| 49 | + return hasKey("foo"); |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments