Skip to content

Commit 03e3571

Browse files
author
joe.walnes
committed
Moved assertThat() back into hamcrest-integration.
1 parent 1c819c8 commit 03e3571

10 files changed

Lines changed: 86 additions & 30 deletions

File tree

‎CHANGES.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* Relaxed isInstanceOf() matcher generic type so it can be used on any kind of
2121
object. e.g. assertThat(someUnknownObject, isInstanceOf(String.class));
2222

23+
* Modified anyOf() and allOf() to accept mixed-types.
24+
2325
* TypeSafeMatcher.matchesSafely() is now public.
2426

2527
* Generator recognizes @Factory methods that return subclass of Matcher.

‎build.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
</classpath>
129129
<batchtest>
130130
<fileset dir="hamcrest-unit-test/src/main/java">
131+
<include name="**/*Test*"/>
131132
<exclude name="**/Abstract*"/>
132133
</fileset>
133134
</batchtest>

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
* matcher returns <code>false</code>.
1414
*/
1515
public class AllOf<T> extends BaseMatcher<T> {
16-
private final Iterable<Matcher<T>> matchers;
16+
private final Iterable<Matcher<? extends T>> matchers;
1717

18-
public AllOf(Iterable<Matcher<T>> matchers) {
18+
public AllOf(Iterable<Matcher<? extends T>> matchers) {
1919
this.matchers = matchers;
2020
}
2121

2222
public boolean matches(Object o) {
23-
for (Matcher<T> matcher : matchers) {
23+
for (Matcher<? extends T> matcher : matchers) {
2424
if (!matcher.matches(o)) {
2525
return false;
2626
}
@@ -31,7 +31,7 @@ public boolean matches(Object o) {
3131
public void describeTo(Description description) {
3232
description.appendText("(");
3333
boolean seenFirst = false;
34-
for (Matcher<T> matcher : matchers) {
34+
for (Matcher<? extends T> matcher : matchers) {
3535
if (seenFirst) {
3636
description.appendText(" and ");
3737
} else {
@@ -43,18 +43,18 @@ public void describeTo(Description description) {
4343
}
4444

4545
/**
46-
* MM Evaluates to true only if ALL of the passed in matchers evaluate to true.
46+
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
4747
*/
4848
@Factory
49-
public static <T> Matcher<T> allOf(Matcher<T>... matchers) {
49+
public static <T> Matcher<T> allOf(Matcher<? extends T>... matchers) {
5050
return allOf(Arrays.asList(matchers));
5151
}
5252

5353
/**
54-
* IM Evaluates to true only if ALL of the passed in matchers evaluate to true.
54+
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
5555
*/
5656
@Factory
57-
public static <T> Matcher<T> allOf(Iterable<Matcher<T>> matchers) {
57+
public static <T> Matcher<T> allOf(Iterable<Matcher<? extends T>> matchers) {
5858
return new AllOf<T>(matchers);
5959
}
6060

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
*/
1515
public class AnyOf<T> extends BaseMatcher<T> {
1616

17-
private final Iterable<Matcher<T>> matchers;
17+
private final Iterable<Matcher<? extends T>> matchers;
1818

19-
public AnyOf(Iterable<Matcher<T>> matchers) {
19+
public AnyOf(Iterable<Matcher<? extends T>> matchers) {
2020
this.matchers = matchers;
2121
}
2222

2323
public boolean matches(Object o) {
24-
for (Matcher<T> matcher : matchers) {
24+
for (Matcher<? extends T> matcher : matchers) {
2525
if (matcher.matches(o)) {
2626
return true;
2727
}
@@ -32,7 +32,7 @@ public boolean matches(Object o) {
3232
public void describeTo(Description description) {
3333
description.appendText("(");
3434
boolean seenFirst = false;
35-
for (Matcher<T> matcher : matchers) {
35+
for (Matcher<? extends T> matcher : matchers) {
3636
if (seenFirst) {
3737
description.appendText(" or ");
3838
} else {
@@ -47,15 +47,15 @@ public void describeTo(Description description) {
4747
* Evaluates to true if ANY of the passed in matchers evaluate to true.
4848
*/
4949
@Factory
50-
public static <T> Matcher<T> anyOf(Matcher<T>... matchers) {
50+
public static <T> Matcher<T> anyOf(Matcher<? extends T>... matchers) {
5151
return anyOf(Arrays.asList(matchers));
5252
}
5353

5454
/**
5555
* Evaluates to true if ANY of the passed in matchers evaluate to true.
5656
*/
5757
@Factory
58-
public static <T> Matcher<T> anyOf(Iterable<Matcher<T>> matchers) {
58+
public static <T> Matcher<T> anyOf(Iterable<Matcher<? extends T>> matchers) {
5959
return new AnyOf<T>(matchers);
6060
}
6161
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import java.util.ArrayList;
1212

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

16-
public IsCollectionContaining(Matcher<T> elementMatcher) {
16+
public IsCollectionContaining(Matcher<? extends T> elementMatcher) {
1717
this.elementMatcher = elementMatcher;
1818
}
1919

@@ -32,7 +32,7 @@ public void describeTo(Description description) {
3232
}
3333

3434
@Factory
35-
public static <T> Matcher<Iterable<T>> hasItem(Matcher<T> elementMatcher) {
35+
public static <T> Matcher<Iterable<T>> hasItem(Matcher<? extends T> elementMatcher) {
3636
return new IsCollectionContaining<T>(elementMatcher);
3737
}
3838

@@ -42,17 +42,19 @@ public static <T> Matcher<Iterable<T>> hasItem(T element) {
4242
}
4343

4444
@Factory
45-
public static <T> Matcher<Iterable<T>> hasItems(Matcher<T>... elementMatchers) {
46-
Collection<Matcher<Iterable<T>>> all = new ArrayList<Matcher<Iterable<T>>>(elementMatchers.length);
47-
for (Matcher<T> elementMatcher : elementMatchers) {
45+
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? extends T>... elementMatchers) {
46+
Collection<Matcher<? extends Iterable<T>>> all
47+
= new ArrayList<Matcher<? extends Iterable<T>>>(elementMatchers.length);
48+
for (Matcher<? extends T> elementMatcher : elementMatchers) {
4849
all.add(hasItem(elementMatcher));
4950
}
5051
return allOf(all);
5152
}
5253

5354
@Factory
5455
public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
55-
Collection<Matcher<Iterable<T>>> all = new ArrayList<Matcher<Iterable<T>>>(elements.length);
56+
Collection<Matcher<? extends Iterable<T>>> all
57+
= new ArrayList<Matcher<? extends Iterable<T>>>(elements.length);
5658
for (T element : elements) {
5759
all.add(hasItem(element));
5860
}

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

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

3+
import static org.hamcrest.collection.IsCollectionContaining.hasItem;
34
import org.hamcrest.AbstractMatcherTest;
45
import org.hamcrest.Matcher;
56
import static org.hamcrest.collection.IsCollectionContaining.hasItems;
@@ -11,27 +12,27 @@
1112
public class IsCollectionContainingTest extends AbstractMatcherTest {
1213

1314
protected Matcher<?> createMatcher() {
14-
return IsCollectionContaining.hasItem(equalTo("irrelevant"));
15+
return hasItem(equalTo("irrelevant"));
1516
}
1617

1718
public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher() {
1819
assertMatches("should matches list that contains 'a'",
19-
IsCollectionContaining.hasItem(equalTo("a")), asList("a", "b", "c"));
20+
hasItem(equalTo("a")), asList("a", "b", "c"));
2021
}
2122

2223
public void testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher() {
2324
assertDoesNotMatch("should not matches list that doesn't contain 'a'",
24-
IsCollectionContaining.hasItem(equalTo("a")), asList("b", "c"));
25+
hasItem(equalTo("a")), asList("b", "c"));
2526
assertDoesNotMatch("should not matches empty list",
26-
IsCollectionContaining.hasItem(equalTo("a")), new ArrayList<String>());
27+
hasItem(equalTo("a")), new ArrayList<String>());
2728
}
2829

2930
public void testDoesNotMatchNull() {
30-
assertDoesNotMatch("should not matches null", IsCollectionContaining.hasItem(equalTo("a")), null);
31+
assertDoesNotMatch("should not matches null", hasItem(equalTo("a")), null);
3132
}
3233

3334
public void testHasAReadableDescription() {
34-
assertDescription("a collection containing \"a\"", IsCollectionContaining.hasItem(equalTo("a")));
35+
assertDescription("a collection containing \"a\"", hasItem(equalTo("a")));
3536
}
3637

3738
public void testMatchesAllItemsInCollection() {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.hamcrest.AbstractMatcherTest;
88
import org.hamcrest.Matcher;
99

10-
@SuppressWarnings("unchecked")
1110
public class AllOfTest extends AbstractMatcherTest {
1211

1312
protected Matcher<?> createMatcher() {
@@ -27,4 +26,17 @@ public void testEvaluatesToTheTheLogicalConjunctionOfManyOtherMatchers() {
2726
assertThat("good", not(allOf(equalTo("good"), equalTo("good"), equalTo("bad"), equalTo("good"), equalTo("good"))));
2827
}
2928

29+
public void testSupportsMixedTypes() {
30+
assertThat(new SampleBaseClass("good"), not(allOf(
31+
equalTo(new SampleBaseClass("bad")),
32+
equalTo(new SampleBaseClass("good")),
33+
equalTo(new SampleSubClass("ugly")))
34+
));
35+
assertThat(new SampleSubClass("good"), not(allOf(
36+
equalTo(new SampleBaseClass("bad")),
37+
equalTo(new SampleBaseClass("good")),
38+
equalTo(new SampleSubClass("ugly")))
39+
));
40+
}
41+
3042
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import static org.hamcrest.core.IsEqual.equalTo;
88
import static org.hamcrest.core.IsNot.not;
99

10-
@SuppressWarnings("unchecked")
1110
public class AnyOfTest extends AbstractMatcherTest {
1211

13-
protected Matcher<?> createMatcher() {
12+
protected Matcher<?> createMatcher() {
1413
return anyOf(equalTo("irrelevant"));
1514
}
1615

@@ -27,4 +26,17 @@ public void testEvaluatesToTheTheLogicalDisjunctionOfManyOtherMatchers() {
2726
assertThat("good", not(anyOf(equalTo("bad"), equalTo("bad"), equalTo("bad"), equalTo("bad"), equalTo("bad"))));
2827
}
2928

29+
public void testSupportsMixedTypes() {
30+
assertThat(new SampleBaseClass("good"), anyOf(
31+
equalTo(new SampleBaseClass("bad")),
32+
equalTo(new SampleBaseClass("good")),
33+
equalTo(new SampleSubClass("ugly"))
34+
));
35+
assertThat(new SampleSubClass("good"), anyOf(
36+
equalTo(new SampleBaseClass("bad")),
37+
equalTo(new SampleBaseClass("good")),
38+
equalTo(new SampleSubClass("ugly"))
39+
));
40+
}
41+
3042
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.hamcrest.core;
2+
3+
public class SampleBaseClass {
4+
String value;
5+
6+
public SampleBaseClass(String value) {
7+
this.value = value;
8+
}
9+
10+
public String toString() {
11+
return value;
12+
}
13+
14+
public boolean equals(Object obj) {
15+
return obj instanceof SampleBaseClass && value.equals(((SampleBaseClass) obj).value);
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.hamcrest.core;
2+
3+
public class SampleSubClass extends SampleBaseClass {
4+
5+
public SampleSubClass(String value) {
6+
super(value);
7+
}
8+
9+
}

0 commit comments

Comments
 (0)