Skip to content

Commit 28e4921

Browse files
author
neildunn
committed
Fixed implementation of hasKey and hasValue re. generics
1 parent 9b1d9f0 commit 28e4921

13 files changed

Lines changed: 195 additions & 27 deletions

File tree

hamcrest-core/src/main/java/org/hamcrest/introspection/Combination.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
import org.hamcrest.Matcher;
44

5-
public interface Combination {
6-
Iterable<? extends Matcher<?>> combined();
5+
public interface Combination {
6+
Iterable<? extends Matcher<?>> combined();
77
}

hamcrest-library/src/main/java/org/hamcrest/collection/IsMapContaining.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,4 @@ public static <K,V> Matcher<Map<K,V>> hasEntry(K key, V value) {
4848
return hasEntry(equalTo(key), equalTo(value));
4949
}
5050

51-
@Factory
52-
public static <K,V> Matcher<Map<K,V>> hasKey(Matcher<K> keyMatcher) {
53-
return hasEntry(keyMatcher, IsAnything.<V>anything());
54-
}
55-
56-
@Factory
57-
public static <K,V> Matcher<Map<K,V>> hasKey(K key) {
58-
return hasKey(equalTo(key));
59-
}
60-
61-
@Factory
62-
public static <K,V> Matcher<Map<K,V>> hasValue(Matcher<V> valueMatcher) {
63-
return hasEntry(IsAnything.<K>anything(), valueMatcher);
64-
}
65-
66-
@Factory
67-
public static <K,V> Matcher<Map<K,V>> hasValue(V value) {
68-
return hasValue(equalTo(value));
69-
}
7051
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.hamcrest.collection;
2+
3+
import java.util.Map;
4+
5+
import org.hamcrest.Description;
6+
import org.hamcrest.Factory;
7+
import org.hamcrest.Matcher;
8+
import org.hamcrest.TypeSafeMatcher;
9+
import static org.hamcrest.core.IsEqual.equalTo;
10+
11+
public class IsMapContainingKey<K> extends TypeSafeMatcher<Map<K,?>> {
12+
13+
private final Matcher<K> keyMatcher;
14+
15+
public IsMapContainingKey(Matcher<K> keyMatcher) {
16+
this.keyMatcher = keyMatcher;
17+
}
18+
19+
@Override
20+
public boolean matchesSafely(Map<K, ?> item) {
21+
for (K key : item.keySet()) {
22+
if (keyMatcher.matches(key)) {
23+
return true;
24+
}
25+
}
26+
return false;
27+
}
28+
29+
public void describeTo(Description description) {
30+
description.appendText("map with key ")
31+
.appendDescriptionOf(keyMatcher);
32+
}
33+
34+
@Factory
35+
public static <K> Matcher<Map<K,?>> hasKey(K key) {
36+
return hasKey(equalTo(key));
37+
}
38+
39+
@Factory
40+
public static <K> Matcher<Map<K,?>> hasKey(Matcher<K> keyMatcher) {
41+
return new IsMapContainingKey<K>(keyMatcher);
42+
}
43+
44+
45+
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Factory;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeMatcher;
7+
import static org.hamcrest.core.IsEqual.equalTo;
8+
import java.util.Map;
9+
10+
public class IsMapContainingValue<V> extends TypeSafeMatcher<Map<?,V>>{
11+
12+
private final Matcher<V> valueMatcher;
13+
14+
public IsMapContainingValue(Matcher<V> valueMatcher) {
15+
this.valueMatcher = valueMatcher;
16+
}
17+
18+
@Override
19+
public boolean matchesSafely(Map<?, V> item) {
20+
for (V value : item.values()) {
21+
if (valueMatcher.matches(value)) {
22+
return true;
23+
}
24+
}
25+
return false;
26+
}
27+
28+
public void describeTo(Description description) {
29+
description.appendText("map with value ")
30+
.appendDescriptionOf(valueMatcher);
31+
}
32+
33+
@Factory
34+
public static <V> Matcher<Map<?,V>> hasValue(V value) {
35+
return hasValue(equalTo(value));
36+
}
37+
38+
@Factory
39+
public static <V> Matcher<Map<?,V>> hasValue(Matcher<V> valueMatcher) {
40+
return new IsMapContainingValue<V>(valueMatcher);
41+
}
42+
43+
}

hamcrest-unit-test/src/main/java/org/hamcrest/AbstractMatcherTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public <T> void assertDoesNotMatch(String message, Matcher<T> c, T arg) {
2222
assertFalse(message, c.matches(arg));
2323
}
2424

25-
public void assertDescription(String expected, Matcher matcher) {
25+
public void assertDescription(String expected, Matcher<?> matcher) {
2626
Description description = new StringDescription();
2727
description.appendDescriptionOf(matcher);
28-
assertEquals(expected, description.toString());
28+
assertEquals("Expected description", expected, description.toString());
2929
}
3030

3131
public void testIsNullSafe() {

hamcrest-unit-test/src/main/java/org/hamcrest/BaseMatcherTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class BaseMatcherTest extends TestCase {
66

77
public void testDescribesItselfWithToStringMethod() {
8-
Matcher someMatcher = new BaseMatcher() {
8+
Matcher<Object> someMatcher = new BaseMatcher<Object>() {
99
public boolean matches(Object item) {
1010
throw new UnsupportedOperationException();
1111
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsMapContainingTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.hamcrest.core.IsAnything.anything;
77
import static org.hamcrest.core.IsEqual.equalTo;
88

9+
910
import java.util.HashMap;
1011
import java.util.Map;
1112

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.IsMapContainingValue.hasValue;
9+
10+
public class IsMapContainingValueTest extends AbstractMatcherTest {
11+
12+
public void testHasReadableDescription() {
13+
assertDescription("map with value \"a\"", hasValue("a"));
14+
}
15+
16+
public void testDoesNotMatchEmptyMap() {
17+
Map<String,Integer> map = new HashMap<String,Integer>();
18+
assertDoesNotMatch("Empty map", hasValue(1), map);
19+
}
20+
21+
public void testMatchesSingletonMapContainingValue() {
22+
Map<String,Integer> map = new HashMap<String,Integer>();
23+
map.put("a", 1);
24+
25+
assertMatches("Singleton map", hasValue(1), map);
26+
}
27+
28+
public void testMatchesMapContainingValue() {
29+
Map<String,Integer> map = new HashMap<String,Integer>();
30+
map.put("a", 1);
31+
map.put("b", 2);
32+
map.put("c", 3);
33+
34+
assertMatches("hasValue 1", hasValue(1), map);
35+
assertMatches("hasValue 3", hasValue(3), map);
36+
}
37+
38+
@Override
39+
protected Matcher<?> createMatcher() {
40+
return hasValue("foo");
41+
}
42+
43+
}

hamcrest-unit-test/src/main/java/org/hamcrest/generator/ReflectiveFactoryReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public void testReadsGenericTypeParameters() {
248248

249249
public static class SubclassOfMatcher {
250250
@Factory
251-
public static BaseMatcher subclassMethod() {
251+
public static BaseMatcher<?> subclassMethod() {
252252
return null;
253253
}
254254
}

0 commit comments

Comments
 (0)