Skip to content

Commit 749ef4b

Browse files
committed
add new isMapWithSize matcher, addressing googlecode issue no 131
1 parent 8d25c2c commit 749ef4b

3 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.hamcrest.collection;
2+
3+
import java.util.Map;
4+
5+
import org.hamcrest.Factory;
6+
import org.hamcrest.FeatureMatcher;
7+
import org.hamcrest.Matcher;
8+
9+
import static org.hamcrest.core.IsEqual.equalTo;
10+
11+
/**
12+
* Matches if map size satisfies a nested matcher.
13+
*/
14+
public final class IsMapWithSize<K, V> extends FeatureMatcher<Map<? extends K, ? extends V>, Integer> {
15+
public IsMapWithSize(Matcher<? super Integer> sizeMatcher) {
16+
super(sizeMatcher, "a map with size", "map size");
17+
}
18+
19+
@Override
20+
protected Integer featureValueOf(Map<? extends K, ? extends V> actual) {
21+
return actual.size();
22+
}
23+
24+
/**
25+
* Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns
26+
* a value that satisfies the specified matcher.
27+
* <p/>
28+
* For example:
29+
* <pre>assertThat(myMap, is(aMapWithSize(equalTo(2))))</pre>
30+
*
31+
* @param sizeMatcher
32+
* a matcher for the size of an examined {@link java.util.Map}
33+
*/
34+
@Factory
35+
public static <K, V> Matcher<Map<? extends K, ? extends V>> aMapWithSize(Matcher<? super Integer> sizeMatcher) {
36+
return new IsMapWithSize<K, V>(sizeMatcher);
37+
}
38+
39+
/**
40+
* Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns
41+
* a value equal to the specified <code>size</code>.
42+
* <p/>
43+
* For example:
44+
* <pre>assertThat(myMap, is(aMapWithSize(2)))</pre>
45+
*
46+
* @param size
47+
* the expected size of an examined {@link java.util.Map}
48+
*/
49+
@Factory
50+
public static <K, V> Matcher<Map<? extends K, ? extends V>> aMapWithSize(int size) {
51+
Matcher<? super Integer> matcher = equalTo(size);
52+
return IsMapWithSize.<K, V>aMapWithSize(matcher);
53+
}
54+
55+
/**
56+
* Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns
57+
* zero.
58+
* <p/>
59+
* For example:
60+
* <pre>assertThat(myMap, is(anEmptyMap()))</pre>
61+
*
62+
*/
63+
@Factory
64+
public static <K, V> Matcher<Map<? extends K, ? extends V>> anEmptyMap() {
65+
return IsMapWithSize.<K, V>aMapWithSize(equalTo(0));
66+
}
67+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

‎matchers.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<factory class="org.hamcrest.collection.IsArrayContainingInOrder"/>
2525
<factory class="org.hamcrest.collection.IsArrayContainingInAnyOrder"/>
2626
<factory class="org.hamcrest.collection.IsArrayWithSize"/>
27+
<factory class="org.hamcrest.collection.IsMapWithSize"/>
2728
<factory class="org.hamcrest.collection.IsCollectionWithSize"/>
2829
<factory class="org.hamcrest.collection.IsEmptyCollection"/>
2930
<factory class="org.hamcrest.collection.IsEmptyIterable"/>

0 commit comments

Comments
 (0)