Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new isMapWithSize matcher, addressing googlecode issue no 131
- Loading branch information
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
hamcrest-library/src/main/java/org/hamcrest/collection/IsMapWithSize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.hamcrest.collection; | ||
|
||
import java.util.Map; | ||
|
||
import org.hamcrest.Factory; | ||
import org.hamcrest.FeatureMatcher; | ||
import org.hamcrest.Matcher; | ||
|
||
import static org.hamcrest.core.IsEqual.equalTo; | ||
|
||
/** | ||
* Matches if map size satisfies a nested matcher. | ||
*/ | ||
public final class IsMapWithSize<K, V> extends FeatureMatcher<Map<? extends K, ? extends V>, Integer> { | ||
public IsMapWithSize(Matcher<? super Integer> sizeMatcher) { | ||
super(sizeMatcher, "a map with size", "map size"); | ||
} | ||
|
||
@Override | ||
protected Integer featureValueOf(Map<? extends K, ? extends V> actual) { | ||
return actual.size(); | ||
} | ||
|
||
/** | ||
* Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns | ||
* a value that satisfies the specified matcher. | ||
* <p/> | ||
* For example: | ||
* <pre>assertThat(myMap, is(aMapWithSize(equalTo(2))))</pre> | ||
* | ||
* @param sizeMatcher | ||
* a matcher for the size of an examined {@link java.util.Map} | ||
*/ | ||
@Factory | ||
public static <K, V> Matcher<Map<? extends K, ? extends V>> aMapWithSize(Matcher<? super Integer> sizeMatcher) { | ||
return new IsMapWithSize<K, V>(sizeMatcher); | ||
} | ||
|
||
/** | ||
* Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns | ||
* a value equal to the specified <code>size</code>. | ||
* <p/> | ||
* For example: | ||
* <pre>assertThat(myMap, is(aMapWithSize(2)))</pre> | ||
* | ||
* @param size | ||
* the expected size of an examined {@link java.util.Map} | ||
*/ | ||
@Factory | ||
public static <K, V> Matcher<Map<? extends K, ? extends V>> aMapWithSize(int size) { | ||
Matcher<? super Integer> matcher = equalTo(size); | ||
return IsMapWithSize.<K, V>aMapWithSize(matcher); | ||
} | ||
|
||
/** | ||
* Creates a matcher for {@link java.util.Map}s that matches when the <code>size()</code> method returns | ||
* zero. | ||
* <p/> | ||
* For example: | ||
* <pre>assertThat(myMap, is(anEmptyMap()))</pre> | ||
* | ||
*/ | ||
@Factory | ||
public static <K, V> Matcher<Map<? extends K, ? extends V>> anEmptyMap() { | ||
return IsMapWithSize.<K, V>aMapWithSize(equalTo(0)); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsMapWithSizeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.hamcrest.collection; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.hamcrest.AbstractMatcherTest; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.MatcherAssert; | ||
|
||
import static org.hamcrest.collection.IsMapWithSize.aMapWithSize; | ||
import static org.hamcrest.core.IsEqual.equalTo; | ||
|
||
public final class IsMapWithSizeTest extends AbstractMatcherTest { | ||
|
||
@Override | ||
protected Matcher<?> createMatcher() { | ||
return aMapWithSize(7); | ||
} | ||
|
||
public void testMatchesWhenSizeIsCorrect() { | ||
assertMatches("correct size", aMapWithSize(equalTo(2)), mapWithKeys("a", "b")); | ||
assertMismatchDescription("map size was <3>", aMapWithSize(equalTo(2)), mapWithKeys("a", "b", "c")); | ||
} | ||
|
||
public void testMatchesMapWhenSizeIsCorrectUsingObjectElementType() { | ||
Map<Object, Object> map = mapWithKeys(new Object(), new Object()); | ||
assertMatches("correct size", aMapWithSize(equalTo(2)), map); | ||
assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), map); | ||
} | ||
|
||
public void testMatchesMapWhenSizeIsCorrectUsingStringElementType() { | ||
Map<String, Integer> map = mapWithKeys("a", "b"); | ||
assertMatches("correct size", aMapWithSize(equalTo(2)), map); | ||
assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), map); | ||
} | ||
|
||
public void testMatchesMapWhenSizeIsCorrectUsingWildcardElementType() { | ||
Map<?, ?> map = mapWithKeys("a", "b"); | ||
assertMatches("correct size", aMapWithSize(equalTo(2)), map); | ||
assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), map); | ||
} | ||
|
||
public void testMatchesListWhenSizeIsCorrectUsingObjectElementType() { | ||
Map<Object, Object> map = mapWithKeys(new Object(), new Object()); | ||
assertMatches("correct size", aMapWithSize(equalTo(2)), map); | ||
assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), map); | ||
} | ||
|
||
public void testMatchesListWhenSizeIsCorrectUsingStringElementType() { | ||
Map<String, Integer> list = mapWithKeys("a", "b"); | ||
assertMatches("correct size", aMapWithSize(equalTo(2)), list); | ||
assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), list); | ||
} | ||
|
||
public void testMatchesListWhenSizeIsCorrectUsingWildcardElementType() { | ||
Map<?, ?> list = mapWithKeys("a", "b"); | ||
assertMatches("correct size", aMapWithSize(equalTo(2)), list); | ||
assertMismatchDescription("map size was <2>", aMapWithSize(equalTo(3)), list); | ||
} | ||
|
||
public void testProvidesConvenientShortcutForHasSizeEqualTo() { | ||
assertMatches("correct size", aMapWithSize(2), mapWithKeys(new Object(), new Object())); | ||
assertMismatchDescription("map size was <3>", aMapWithSize(2), mapWithKeys(new Object(), new Object(), new Object())); | ||
} | ||
|
||
public void testHasAReadableDescription() { | ||
assertDescription("a map with size <3>", aMapWithSize(equalTo(3))); | ||
} | ||
|
||
public void testCompilesWithATypedMap() { | ||
Map<String, Integer> arrayList = new HashMap<String, Integer>(); | ||
MatcherAssert.assertThat(arrayList, aMapWithSize(0)); | ||
} | ||
|
||
private static final <K, V> Map<K, V> mapWithKeys(K... keys) { | ||
final Map<K, V> result = new HashMap<K, V>(); | ||
for (K key : keys) { | ||
result.put(key, null); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters