|
| 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 org.hamcrest.MatcherAssert; |
| 9 | + |
| 10 | +import static org.hamcrest.collection.IsMapWithSize.aMapWithSize; |
| 11 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 12 | + |
| 13 | +public final class IsMapWithSizeTest extends AbstractMatcherTest { |
| 14 | + |
| 15 | + @Override |
| 16 | + protected Matcher<?> createMatcher() { |
| 17 | + return aMapWithSize(7); |
| 18 | + } |
| 19 | + |
| 20 | + public void testMatchesWhenSizeIsCorrect() { |
| 21 | + assertMatches("correct size", aMapWithSize(equalTo(2)), mapWithKeys("a", "b")); |
| 22 | + assertMismatchDescription("map size was <3>", aMapWithSize(equalTo(2)), mapWithKeys("a", "b", "c")); |
| 23 | + } |
| 24 | + |
| 25 | + public void testMatchesMapWhenSizeIsCorrectUsingObjectElementType() { |
| 26 | + Map<Object, Object> map = mapWithKeys(new Object(), new Object()); |
| 27 | + assertMatches("correct size", aMapWithSize(equalTo(2)), map); |
| 28 | + assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), map); |
| 29 | + } |
| 30 | + |
| 31 | + public void testMatchesMapWhenSizeIsCorrectUsingStringElementType() { |
| 32 | + Map<String, Integer> map = mapWithKeys("a", "b"); |
| 33 | + assertMatches("correct size", aMapWithSize(equalTo(2)), map); |
| 34 | + assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), map); |
| 35 | + } |
| 36 | + |
| 37 | + public void testMatchesMapWhenSizeIsCorrectUsingWildcardElementType() { |
| 38 | + Map<?, ?> map = mapWithKeys("a", "b"); |
| 39 | + assertMatches("correct size", aMapWithSize(equalTo(2)), map); |
| 40 | + assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), map); |
| 41 | + } |
| 42 | + |
| 43 | + public void testMatchesListWhenSizeIsCorrectUsingObjectElementType() { |
| 44 | + Map<Object, Object> map = mapWithKeys(new Object(), new Object()); |
| 45 | + assertMatches("correct size", aMapWithSize(equalTo(2)), map); |
| 46 | + assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), map); |
| 47 | + } |
| 48 | + |
| 49 | + public void testMatchesListWhenSizeIsCorrectUsingStringElementType() { |
| 50 | + Map<String, Integer> list = mapWithKeys("a", "b"); |
| 51 | + assertMatches("correct size", aMapWithSize(equalTo(2)), list); |
| 52 | + assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), list); |
| 53 | + } |
| 54 | + |
| 55 | + public void testMatchesListWhenSizeIsCorrectUsingWildcardElementType() { |
| 56 | + Map<?, ?> list = mapWithKeys("a", "b"); |
| 57 | + assertMatches("correct size", aMapWithSize(equalTo(2)), list); |
| 58 | + assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), list); |
| 59 | + } |
| 60 | + |
| 61 | + public void testProvidesConvenientShortcutForHasSizeEqualTo() { |
| 62 | + assertMatches("correct size", aMapWithSize(2), mapWithKeys(new Object(), new Object())); |
| 63 | + assertMismatchDescription("map size was <3>", aMapWithSize(2), mapWithKeys(new Object(), new Object(), new Object())); |
| 64 | + } |
| 65 | + |
| 66 | + public void testHasAReadableDescription() { |
| 67 | + assertDescription("a map with size <3>", aMapWithSize(equalTo(3))); |
| 68 | + } |
| 69 | + |
| 70 | + public void testCompilesWithATypedMap() { |
| 71 | + Map<String, Integer> arrayList = new HashMap<String, Integer>(); |
| 72 | + MatcherAssert.assertThat(arrayList, aMapWithSize(0)); |
| 73 | + } |
| 74 | + |
| 75 | + private static final <K, V> Map<K, V> mapWithKeys(K... keys) { |
| 76 | + final Map<K, V> result = new HashMap<K, V>(); |
| 77 | + for (K key : keys) { |
| 78 | + result.put(key, null); |
| 79 | + } |
| 80 | + return result; |
| 81 | + } |
| 82 | +} |
0 commit comments