Skip to content

Commit a6b00c4

Browse files
author
smgfreeman
committed
sf. More template madness. Moved any() from IsAnything to IsInstanceOf. Moved MatcherAssert to core.
1 parent 7f0ef63 commit a6b00c4

10 files changed

Lines changed: 68 additions & 53 deletions

File tree

hamcrest-integration/src/main/java/org/hamcrest/MatcherAssert.java renamed to hamcrest-core/src/main/java/org/hamcrest/MatcherAssert.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55

66
public class MatcherAssert {
7-
8-
public static <T,K extends T,V extends T> void assertThat(K actual, Matcher<V> matcher) {
7+
public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
98
assertThat("", actual, matcher);
109
}
11-
12-
public static<T,K extends T,V extends T> void assertThat(String reason, K actual, Matcher<V> matcher) {
10+
11+
public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
1312
if (!matcher.matches(actual)) {
1413
Description description = new StringDescription();
1514
description.appendText(reason)

‎hamcrest-core/src/main/java/org/hamcrest/core/IsAnything.java‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,4 @@ public static <T> Matcher<T> anything() {
4848
public static <T> Matcher<T> anything(String description) {
4949
return new IsAnything<T>(description);
5050
}
51-
52-
/**
53-
* This matcher always evaluates to true. With type inference.
54-
*/
55-
@Factory
56-
public static <T> Matcher<T> any(@SuppressWarnings("unused")Class<T> type) {
57-
return new IsAnything<T>();
58-
}
5951
}

‎hamcrest-core/src/main/java/org/hamcrest/core/IsCollectionContaining.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
import org.hamcrest.Matcher;
1212
import org.hamcrest.TypeSafeMatcher;
1313

14-
public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<T>> {
14+
public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<? super T>> {
1515
private final Matcher<? super T> elementMatcher;
1616

1717
public IsCollectionContaining(Matcher<? super T> elementMatcher) {
1818
this.elementMatcher = elementMatcher;
1919
}
2020

2121
@Override
22-
public boolean matchesSafely(Iterable<T> collection) {
23-
for (T item : collection) {
22+
public boolean matchesSafely(Iterable<? super T> collection) {
23+
for (Object item : collection) {
2424
if (elementMatcher.matches(item)){
2525
return true;
2626
}
@@ -35,22 +35,23 @@ public void describeTo(Description description) {
3535
}
3636

3737
@Factory
38-
public static <T> Matcher<Iterable<T>> hasItem(Matcher<? super T> elementMatcher) {
39-
return new IsCollectionContaining<T>(elementMatcher);
38+
public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> elementMatcher) {
39+
return new IsCollectionContaining<T>(elementMatcher);
4040
}
4141

4242
@Factory
43-
public static <T> Matcher<Iterable<T>> hasItem(T element) {
44-
return hasItem(equalTo(element));
43+
public static <T> Matcher<Iterable<? super T>> hasItem(T element) {
44+
// Doesn't forward to hasItem() method so compiler can sort out generics.
45+
return new IsCollectionContaining<T>(equalTo(element));
4546
}
4647

4748
@Factory
4849
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) {
4950
List<Matcher<? super Iterable<T>>> all = new ArrayList<Matcher<? super Iterable<T>>>(elementMatchers.length);
5051

5152
for (Matcher<? super T> elementMatcher : elementMatchers) {
52-
Matcher<? super Iterable<T>> itemMatcher = hasItem(elementMatcher);
53-
all.add(itemMatcher);
53+
// Doesn't forward to hasItem() method so compiler can sort out generics.
54+
all.add(new IsCollectionContaining<T>(elementMatcher));
5455
}
5556

5657
return allOf(all);
@@ -59,7 +60,6 @@ public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMat
5960
@Factory
6061
public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
6162
List<Matcher<? super Iterable<T>>> all = new ArrayList<Matcher<? super Iterable<T>>>(elements.length);
62-
6363
for (T element : elements) {
6464
all.add(hasItem(element));
6565
}

‎hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,26 @@ public void describeTo(Description description) {
3434
}
3535

3636
/**
37-
* Is the value an instance of a particular type?
37+
* Is the value an instance of a particular type?
38+
* This version assumes no relationship between the required type and
39+
* the signature of the method that sets it up, for example in
40+
* <code>assertThat(anObject, instanceOf(Thing.class));</code>
3841
*/
3942
@SuppressWarnings("unchecked")
4043
@Factory
41-
public static <T> Matcher<T> instanceOf(Class<T> type) {
44+
public static <T> Matcher<T> instanceOf(Class<?> type) {
4245
return (Matcher<T>) new IsInstanceOf(type);
4346
}
47+
48+
/**
49+
* Is the value an instance of a particular type?
50+
* Use this version to make generics conform, for example in
51+
* the JMock clause <code>with(any(Thing.class))</code>
52+
*/
53+
@SuppressWarnings("unchecked")
54+
@Factory
55+
public static <T> Matcher<T> any(Class<T> type) {
56+
return (Matcher<T>) new IsInstanceOf(type);
57+
}
58+
4459
}

‎hamcrest-library/src/main/java/org/hamcrest/collection/IsMapContainingKey.java‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
import org.hamcrest.TypeSafeMatcher;
99
import static org.hamcrest.core.IsEqual.equalTo;
1010

11-
public class IsMapContainingKey<K> extends TypeSafeMatcher<Map<? extends K,?>> {
11+
public class IsMapContainingKey<K> extends TypeSafeMatcher<Map<? super K,?>> {
1212
private final Matcher<? super K> keyMatcher;
1313

1414
public IsMapContainingKey(Matcher<? super K> keyMatcher) {
1515
this.keyMatcher = keyMatcher;
1616
}
1717

1818
@Override
19-
public boolean matchesSafely(Map<? extends K, ? > item) {
20-
for (K key : item.keySet()) {
19+
public boolean matchesSafely(Map<? super K, ?> item) {
20+
for (Object key : item.keySet()) {
2121
if (keyMatcher.matches(key)) {
2222
return true;
2323
}
@@ -31,12 +31,13 @@ public void describeTo(Description description) {
3131
}
3232

3333
@Factory
34-
public static <K> Matcher<Map<? extends K,?>> hasKey(K key) {
35-
return IsMapContainingKey.<K>hasKey(equalTo(key));
34+
public static <K> Matcher<Map<? super K,?>> hasKey(K key) {
35+
// does not forward to other hasKey method to help compiler cope with type inference
36+
return new IsMapContainingKey<K>(equalTo(key));
3637
}
3738

3839
@Factory
39-
public static <K> Matcher<Map<? extends K,?>> hasKey(Matcher<? super K> keyMatcher) {
40-
return new IsMapContainingKey<K>(keyMatcher);
40+
public static <K> Matcher<Map<? super K,?>> hasKey(Matcher<? super K> keyMatcher) {
41+
return new IsMapContainingKey<K>(keyMatcher);
4142
}
4243
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/MatcherAssertTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
8080
assertEquals(expectedMessage, e.getMessage());
8181
}
8282
}
83+
84+
85+
public void testCanAssertSubtypes() {
86+
Integer aSub = new Integer(1);
87+
Number aSuper = aSub;
88+
89+
assertThat(aSub, equalTo(aSuper));
90+
}
8391
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsCollectionContainingTest.java‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hamcrest.collection;
22

33
import static java.util.Arrays.asList;
4+
import static org.hamcrest.MatcherAssert.assertThat;
45
import static org.hamcrest.core.IsCollectionContaining.hasItem;
56
import static org.hamcrest.core.IsCollectionContaining.hasItems;
67
import static org.hamcrest.core.IsEqual.equalTo;
@@ -11,7 +12,8 @@
1112

1213
import org.hamcrest.AbstractMatcherTest;
1314
import org.hamcrest.Matcher;
14-
import org.hamcrest.MatcherAssert;
15+
import org.hamcrest.core.IsCollectionContaining;
16+
import org.hamcrest.core.IsEqual;
1517

1618
public class IsCollectionContainingTest extends AbstractMatcherTest {
1719
@Override
@@ -20,19 +22,19 @@ protected Matcher<?> createMatcher() {
2022
}
2123

2224
public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher() {
23-
Matcher<Iterable<String>> itemMatcher = hasItem(equalTo("a"));
25+
Matcher<Iterable<? super String>> itemMatcher = hasItem(equalTo("a"));
2426

2527
assertMatches("should match list that contains 'a'",
2628
itemMatcher, asList("a", "b", "c"));
2729
}
2830

2931
public void testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher() {
30-
final Matcher<Iterable<String>> matcher1 = hasItem(equalTo("a"));
32+
final Matcher<Iterable<? super String>> matcher1 = hasItem(equalTo("a"));
3133
assertDoesNotMatch("should not match list that doesn't contain 'a'",
3234
matcher1, asList("b", "c"));
3335

3436

35-
final Matcher<Iterable<String>> matcher2 = hasItem(equalTo("a"));
37+
final Matcher<Iterable<? super String>> matcher2 = hasItem(equalTo("a"));
3638
assertDoesNotMatch("should not match the empty list",
3739
matcher2, new ArrayList<String>());
3840
}
@@ -49,7 +51,8 @@ public void testCanMatchItemWhenCollectionHoldsSuperclass() // Issue 24
4951
{
5052
final Set<Number> s = new HashSet<Number>();
5153
s.add(Integer.valueOf(2));
52-
MatcherAssert.assertThat(s, hasItem(Integer.valueOf(2)));
54+
assertThat(s, new IsCollectionContaining<Number>(new IsEqual<Object>(Integer.valueOf(2))));
55+
assertThat(s, IsCollectionContaining.hasItem(Integer.valueOf(2)));
5356
}
5457

5558
@SuppressWarnings("unchecked")

‎hamcrest-unit-test/src/main/java/org/hamcrest/collection/IsMapContainingKeyTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public void testMatchesMapContainingKeyWithNumberKeys() throws Exception {
6262
// very ugly version!
6363
assertThat(map, IsMapContainingKey.<Number>hasKey(Integer.valueOf(1)));
6464

65-
// this can be complex to compile! :)
6665
assertThat(map, hasKey(Integer.valueOf(1)));
6766
}
6867

‎hamcrest-unit-test/src/main/java/org/hamcrest/core/IsAnythingTest.java‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
*/
33
package org.hamcrest.core;
44

5-
import static org.hamcrest.core.IsAnything.any;
6-
import org.hamcrest.AbstractMatcherTest;
7-
import org.hamcrest.Matcher;
85
import static org.hamcrest.MatcherAssert.assertThat;
96
import static org.hamcrest.core.IsAnything.anything;
107

8+
import org.hamcrest.AbstractMatcherTest;
9+
import org.hamcrest.Matcher;
10+
1111
public class IsAnythingTest extends AbstractMatcherTest {
1212

1313
@Override
@@ -30,11 +30,4 @@ public void testCanOverrideDescription() {
3030
assertDescription(description, anything(description));
3131
}
3232

33-
public void testSupportsStaticTyping() {
34-
requiresStringMatcher(any(String.class));
35-
}
36-
37-
private void requiresStringMatcher(Matcher<String> arg) {
38-
// no-op
39-
}
4033
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/core/IsInstanceOfTest.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.hamcrest.AbstractMatcherTest;
66
import org.hamcrest.Matcher;
77
import static org.hamcrest.MatcherAssert.assertThat;
8-
import static org.hamcrest.core.IsInstanceOf.instanceOf;
8+
import static org.hamcrest.core.IsInstanceOf.*;
99
import static org.hamcrest.core.IsNot.not;
1010

1111
public class IsInstanceOfTest extends AbstractMatcherTest {
@@ -19,22 +19,27 @@ public void testEvaluatesToTrueIfArgumentIsInstanceOfASpecificClass() {
1919
assertThat(1, instanceOf(Number.class));
2020
assertThat(1.0, instanceOf(Number.class));
2121
assertThat(null, not(instanceOf(Number.class)));
22-
assertThat("hello", not(instanceOf(Number.class)));
22+
assertThat(new Object(), not(instanceOf(Number.class)));
2323
}
2424

2525
public void testHasAReadableDescription() {
2626
assertDescription("an instance of java.lang.Number", instanceOf(Number.class));
2727
}
28-
29-
public void testWillReturnCorrectTypeForUseInJMock() {
28+
29+
public void testInstanceOfRequiresACastToReturnTheCorrectTypeForUseInJMock() {
30+
@SuppressWarnings("unused")
31+
Integer anInteger = (Integer)with(instanceOf(Integer.class));
32+
}
33+
34+
public void testAnyWillReturnTheCorrectTypeForUseInJMock() {
3035
@SuppressWarnings("unused")
31-
Integer anInteger = with(instanceOf(Integer.class));
36+
Integer anInteger = with(any(Integer.class));
3237
}
3338

3439

3540
private static <T> T with(Matcher<T> matcher) {
3641
return null;
37-
}
42+
}
3843

3944
}
4045

0 commit comments

Comments
 (0)