Skip to content

Commit 7ca7be5

Browse files
author
jon.m.reid
committed
Clean-up/unification of AnyOf & AllOf, especially adding missing test of description.
1 parent dbf5d3d commit 7ca7be5

5 files changed

Lines changed: 41 additions & 30 deletions

File tree

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import org.hamcrest.Matcher;
1010

1111
/**
12-
* Calculates the logical conjunction of two matchers. Evaluation is
13-
* shortcut, so that the second matcher is not called if the first
14-
* matcher returns <code>false</code>.
12+
* Calculates the logical conjunction of multiple matchers. Evaluation is shortcut, so
13+
* subsequent matchers are not called if an earlier matcher returns <code>false</code>.
1514
*/
1615
public class AllOf<T> extends ShortcutCombination<T> {
16+
1717
public AllOf(Iterable<Matcher<? super T>> matchers) {
1818
super(matchers);
1919
}
@@ -71,25 +71,25 @@ public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second,
7171
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
7272
*/
7373
@Factory
74-
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> forth) {
74+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
7575
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(4);
7676
matchers.add(first);
7777
matchers.add(second);
7878
matchers.add(third);
79-
matchers.add(forth);
79+
matchers.add(fourth);
8080
return allOf(matchers);
8181
}
8282

8383
/**
8484
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
8585
*/
8686
@Factory
87-
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> forth, Matcher<? super T> fifth) {
87+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
8888
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(5);
8989
matchers.add(first);
9090
matchers.add(second);
9191
matchers.add(third);
92-
matchers.add(forth);
92+
matchers.add(fourth);
9393
matchers.add(fifth);
9494
return allOf(matchers);
9595
}
@@ -98,12 +98,12 @@ public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second,
9898
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
9999
*/
100100
@Factory
101-
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> forth, Matcher<? super T> fifth, Matcher<? super T> sixth) {
101+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth) {
102102
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(6);
103103
matchers.add(first);
104104
matchers.add(second);
105105
matchers.add(third);
106-
matchers.add(forth);
106+
matchers.add(fourth);
107107
matchers.add(sixth);
108108
return allOf(matchers);
109109
}

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import org.hamcrest.Matcher;
1010

1111
/**
12-
* Calculates the logical disjunction of two matchers. Evaluation is
13-
* shortcut, so that the second matcher is not called if the first
14-
* matcher returns <code>true</code>.
12+
* Calculates the logical disjunction of multiple matchers. Evaluation is shortcut, so
13+
* subsequent matchers are not called if an earlier matcher returns <code>true</code>.
1514
*/
1615
public class AnyOf<T> extends ShortcutCombination<T> {
16+
1717
public AnyOf(Iterable<Matcher<? super T>> matchers) {
1818
super(matchers);
1919
}
@@ -36,6 +36,14 @@ public static <T> Matcher<T> anyOf(Matcher<? super T>... matchers) {
3636
return anyOf(Arrays.asList(matchers));
3737
}
3838

39+
/**
40+
* Evaluates to true if ANY of the passed in matchers evaluate to true.
41+
*/
42+
@Factory
43+
public static <T> Matcher<T> anyOf(Iterable<Matcher<? super T>> matchers) {
44+
return new AnyOf<T>(matchers);
45+
}
46+
3947
/**
4048
* Evaluates to true if ANY of the passed in matchers evaluate to true.
4149
*/
@@ -63,25 +71,25 @@ public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second,
6371
* Evaluates to true if ANY of the passed in matchers evaluate to true.
6472
*/
6573
@Factory
66-
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> forth) {
74+
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
6775
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
6876
matchers.add(first);
6977
matchers.add(second);
7078
matchers.add(third);
71-
matchers.add(forth);
79+
matchers.add(fourth);
7280
return anyOf(matchers);
7381
}
7482

7583
/**
7684
* Evaluates to true if ANY of the passed in matchers evaluate to true.
7785
*/
7886
@Factory
79-
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> forth, Matcher<? super T> fifth) {
87+
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
8088
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
8189
matchers.add(first);
8290
matchers.add(second);
8391
matchers.add(third);
84-
matchers.add(forth);
92+
matchers.add(fourth);
8593
matchers.add(fifth);
8694
return anyOf(matchers);
8795
}
@@ -90,22 +98,14 @@ public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second,
9098
* Evaluates to true if ANY of the passed in matchers evaluate to true.
9199
*/
92100
@Factory
93-
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> forth, Matcher<? super T> fifth, Matcher<? super T> sixth) {
101+
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth) {
94102
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
95103
matchers.add(first);
96104
matchers.add(second);
97105
matchers.add(third);
98-
matchers.add(forth);
106+
matchers.add(fourth);
99107
matchers.add(fifth);
100108
matchers.add(sixth);
101109
return anyOf(matchers);
102110
}
103-
104-
/**
105-
* Evaluates to true if ANY of the passed in matchers evaluate to true.
106-
*/
107-
@Factory
108-
public static <T> Matcher<T> anyOf(Iterable<Matcher<? super T>> matchers) {
109-
return new AnyOf<T>(matchers);
110-
}
111111
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.hamcrest.Matcher;
66

77
abstract class ShortcutCombination<T> extends BaseMatcher<T> {
8+
89
private final Iterable<Matcher<? super T>> matchers;
910

1011
public ShortcutCombination(Iterable<Matcher<? super T>> matchers) {
@@ -15,7 +16,6 @@ public ShortcutCombination(Iterable<Matcher<? super T>> matchers) {
1516

1617
public abstract void describeTo(Description description);
1718

18-
1919
protected boolean matches(Object o, boolean shortcut) {
2020
for (Matcher<? super T> matcher : matchers) {
2121
if (matcher.matches(o) == shortcut) {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
import org.hamcrest.Matcher;
1010

1111
public class AllOfTest extends AbstractMatcherTest {
12+
13+
@SuppressWarnings("unchecked")
14+
protected Matcher<?> createMatcher() {
15+
return allOf(equalTo("irrelevant"));
16+
}
17+
1218
@SuppressWarnings("unchecked")
1319
public void testEvaluatesToTheTheLogicalConjunctionOfTwoOtherMatchers() {
1420
assertThat("good", allOf(equalTo("good"), equalTo("good")));
@@ -35,8 +41,8 @@ public void testSupportsMixedTypes() {
3541
assertThat(new SampleSubClass("good"), negated);
3642
}
3743

38-
@SuppressWarnings("unchecked")
39-
protected Matcher<?> createMatcher() {
40-
return allOf(equalTo("irrelevant"), equalTo("irrelevant"));
44+
public void testHasAReadableDescription() {
45+
assertDescription("(\"good\" and \"bad\" and \"ugly\")",
46+
allOf(equalTo("good"), equalTo("bad"), equalTo("ugly")));
4147
}
4248
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public void testSupportsMixedTypes() {
4040

4141
assertThat(new SampleSubClass("good"), combined);
4242
}
43+
44+
public void testHasAReadableDescription() {
45+
assertDescription("(\"good\" or \"bad\" or \"ugly\")",
46+
anyOf(equalTo("good"), equalTo("bad"), equalTo("ugly")));
47+
}
4348
}

0 commit comments

Comments
 (0)