Skip to content

Commit 1860732

Browse files
author
neildunn
committed
* Added Array, Iterable and Collection variants of:
- contains in order - contains in any order - is empty - has size * Added multiple argument versions of AnyOf and AllOf to stop compiler warnings at use sites of allOf and anyOf (due to generic type arrays created from a var args)
1 parent 2528c3a commit 1860732

28 files changed

Lines changed: 1279 additions & 219 deletions

‎.classpath‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
<classpathentry kind="lib" path="lib/integration/jmock-1.10RC1.jar"/>
1414
<classpathentry kind="lib" path="lib/integration/junit-4.0.jar"/>
1515
<classpathentry kind="lib" path="lib/integration/testng-4.6-jdk15.jar"/>
16-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.5.0_08"/>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
1717
<classpathentry kind="output" path="classes"/>
1818
</classpath>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @see CoreMatchers
1818
* @see BaseMatcher
1919
*/
20-
@SuppressWarnings({"unused", "UnusedDeclaration"})
20+
@SuppressWarnings({"unused"})
2121
public interface Matcher<T> extends SelfDescribing {
2222

2323
/**
@@ -26,7 +26,7 @@ public interface Matcher<T> extends SelfDescribing {
2626
* This method matches against Object, instead of the generic type T. This is
2727
* because the caller of the Matcher does not know at runtime what the type is
2828
* (because of type erasure with Java generics). It is down to the implementations
29-
* to check the correct type.
29+
* to check the correct type.
3030
*
3131
* @param item the object against which the matcher is evaluated.
3232
* @return <code>true</code> if <var>item</var> matches, otherwise <code>false</code>.

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

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

3+
import java.util.ArrayList;
34
import java.util.Arrays;
5+
import java.util.List;
46

57
import org.hamcrest.Description;
68
import org.hamcrest.Factory;
@@ -16,17 +18,16 @@ public AllOf(Iterable<Matcher<? super T>> matchers) {
1618
super(matchers);
1719
}
1820

19-
2021
@Override
2122
public boolean matches(Object o) {
2223
return matches(o, false);
2324
}
24-
25+
2526
@Override
2627
public void describeTo(Description description) {
2728
describeTo(description, "and");
2829
}
29-
30+
3031
/**
3132
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
3233
*/
@@ -42,4 +43,68 @@ public static <T> Matcher<T> allOf(Matcher<? super T>... matchers) {
4243
public static <T> Matcher<T> allOf(Iterable<Matcher<? super T>> matchers) {
4344
return new AllOf<T>(matchers);
4445
}
46+
47+
/**
48+
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
49+
*/
50+
@Factory
51+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second) {
52+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(2);
53+
matchers.add(first);
54+
matchers.add(second);
55+
return allOf(matchers);
56+
}
57+
58+
/**
59+
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
60+
*/
61+
@Factory
62+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
63+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(3);
64+
matchers.add(first);
65+
matchers.add(second);
66+
matchers.add(third);
67+
return allOf(matchers);
68+
}
69+
70+
/**
71+
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
72+
*/
73+
@Factory
74+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> forth) {
75+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(4);
76+
matchers.add(first);
77+
matchers.add(second);
78+
matchers.add(third);
79+
matchers.add(forth);
80+
return allOf(matchers);
81+
}
82+
83+
/**
84+
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
85+
*/
86+
@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) {
88+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(5);
89+
matchers.add(first);
90+
matchers.add(second);
91+
matchers.add(third);
92+
matchers.add(forth);
93+
matchers.add(fifth);
94+
return allOf(matchers);
95+
}
96+
97+
/**
98+
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
99+
*/
100+
@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) {
102+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(6);
103+
matchers.add(first);
104+
matchers.add(second);
105+
matchers.add(third);
106+
matchers.add(forth);
107+
matchers.add(sixth);
108+
return allOf(matchers);
109+
}
45110
}

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

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

3+
import java.util.ArrayList;
34
import java.util.Arrays;
5+
import java.util.List;
46

57
import org.hamcrest.Description;
68
import org.hamcrest.Factory;
@@ -15,19 +17,17 @@ public class AnyOf<T> extends ShortcutCombination<T> {
1517
public AnyOf(Iterable<Matcher<? super T>> matchers) {
1618
super(matchers);
1719
}
18-
20+
1921
@Override
2022
public boolean matches(Object o) {
2123
return matches(o, true);
2224
}
23-
25+
2426
@Override
2527
public void describeTo(Description description) {
2628
describeTo(description, "or");
2729
}
28-
2930

30-
3131
/**
3232
* Evaluates to true if ANY of the passed in matchers evaluate to true.
3333
*/
@@ -36,12 +36,76 @@ 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(Matcher<T> first, Matcher<? super T> second) {
44+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
45+
matchers.add(first);
46+
matchers.add(second);
47+
return anyOf(matchers);
48+
}
49+
50+
/**
51+
* Evaluates to true if ANY of the passed in matchers evaluate to true.
52+
*/
53+
@Factory
54+
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
55+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
56+
matchers.add(first);
57+
matchers.add(second);
58+
matchers.add(third);
59+
return anyOf(matchers);
60+
}
61+
62+
/**
63+
* Evaluates to true if ANY of the passed in matchers evaluate to true.
64+
*/
65+
@Factory
66+
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> forth) {
67+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
68+
matchers.add(first);
69+
matchers.add(second);
70+
matchers.add(third);
71+
matchers.add(forth);
72+
return anyOf(matchers);
73+
}
74+
75+
/**
76+
* Evaluates to true if ANY of the passed in matchers evaluate to true.
77+
*/
78+
@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) {
80+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
81+
matchers.add(first);
82+
matchers.add(second);
83+
matchers.add(third);
84+
matchers.add(forth);
85+
matchers.add(fifth);
86+
return anyOf(matchers);
87+
}
88+
89+
/**
90+
* Evaluates to true if ANY of the passed in matchers evaluate to true.
91+
*/
92+
@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) {
94+
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
95+
matchers.add(first);
96+
matchers.add(second);
97+
matchers.add(third);
98+
matchers.add(forth);
99+
matchers.add(fifth);
100+
matchers.add(sixth);
101+
return anyOf(matchers);
102+
}
103+
39104
/**
40105
* Evaluates to true if ANY of the passed in matchers evaluate to true.
41106
*/
42107
@Factory
43108
public static <T> Matcher<T> anyOf(Iterable<Matcher<? super T>> matchers) {
44109
return new AnyOf<T>(matchers);
45110
}
46-
47111
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package org.hamcrest.collection;
2+
3+
import static org.hamcrest.core.IsEqual.equalTo;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
import java.util.List;
9+
10+
import org.hamcrest.Description;
11+
import org.hamcrest.Factory;
12+
import org.hamcrest.Matcher;
13+
import org.hamcrest.TypeSafeMatcher;
14+
15+
public class IsArrayContainingInAnyOrder<E> extends TypeSafeMatcher<E[]> {
16+
private final IsIterableContainingInAnyOrder<E> iterableMatcher;
17+
private final Collection<Matcher<? super E>> matchers;
18+
19+
public IsArrayContainingInAnyOrder(Collection<Matcher<? super E>> matchers) {
20+
this.iterableMatcher = new IsIterableContainingInAnyOrder<E>(matchers);
21+
this.matchers = matchers;
22+
}
23+
24+
@Override
25+
public boolean matchesSafely(E[] item) {
26+
return iterableMatcher.matches(Arrays.asList(item));
27+
}
28+
29+
public void describeTo(Description description) {
30+
description.appendText("is array containing in any order: ")
31+
.appendList("[ ", ", ", " ]", matchers);
32+
}
33+
34+
@Factory
35+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<? super E>... matchers) {
36+
return arrayContainingInAnyOrder(Arrays.asList(matchers));
37+
}
38+
39+
40+
@Factory
41+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Collection<Matcher<? super E>> matchers) {
42+
return new IsArrayContainingInAnyOrder<E>(matchers);
43+
}
44+
45+
@Factory
46+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E... items) {
47+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
48+
for (E item : items) {
49+
matchers.add(equalTo(item));
50+
}
51+
return new IsArrayContainingInAnyOrder<E>(matchers);
52+
}
53+
54+
@Factory
55+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second) {
56+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
57+
matchers.add(equalTo(first));
58+
matchers.add(equalTo(second));
59+
return new IsArrayContainingInAnyOrder<E>(matchers);
60+
}
61+
62+
@Factory
63+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second, E third) {
64+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
65+
matchers.add(equalTo(first));
66+
matchers.add(equalTo(second));
67+
matchers.add(equalTo(third));
68+
return new IsArrayContainingInAnyOrder<E>(matchers);
69+
}
70+
71+
@Factory
72+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second, E third, E forth) {
73+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
74+
matchers.add(equalTo(first));
75+
matchers.add(equalTo(second));
76+
matchers.add(equalTo(third));
77+
matchers.add(equalTo(forth));
78+
return new IsArrayContainingInAnyOrder<E>(matchers);
79+
}
80+
81+
@Factory
82+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second, E third, E forth, E fifth) {
83+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
84+
matchers.add(equalTo(first));
85+
matchers.add(equalTo(second));
86+
matchers.add(equalTo(third));
87+
matchers.add(equalTo(forth));
88+
matchers.add(equalTo(fifth));
89+
return new IsArrayContainingInAnyOrder<E>(matchers);
90+
}
91+
92+
@Factory
93+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E first, E second, E third, E forth, E fifth, E sixth) {
94+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
95+
matchers.add(equalTo(first));
96+
matchers.add(equalTo(second));
97+
matchers.add(equalTo(third));
98+
matchers.add(equalTo(forth));
99+
matchers.add(equalTo(fifth));
100+
matchers.add(equalTo(sixth));
101+
return new IsArrayContainingInAnyOrder<E>(matchers);
102+
}
103+
104+
@Factory
105+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second) {
106+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
107+
matchers.add(first);
108+
matchers.add(second);
109+
return arrayContainingInAnyOrder(matchers);
110+
}
111+
112+
@Factory
113+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second, Matcher<? super E> third) {
114+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
115+
matchers.add(first);
116+
matchers.add(second);
117+
matchers.add(third);
118+
return arrayContainingInAnyOrder(matchers);
119+
}
120+
121+
@Factory
122+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second, Matcher<? super E> third, Matcher<? super E> forth) {
123+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
124+
matchers.add(first);
125+
matchers.add(second);
126+
matchers.add(third);
127+
matchers.add(forth);
128+
return arrayContainingInAnyOrder(matchers);
129+
}
130+
131+
@Factory
132+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second, Matcher<? super E> third, Matcher<? super E> forth, Matcher<? super E> fifth) {
133+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
134+
matchers.add(first);
135+
matchers.add(second);
136+
matchers.add(third);
137+
matchers.add(forth);
138+
matchers.add(fifth);
139+
return arrayContainingInAnyOrder(matchers);
140+
}
141+
142+
@Factory
143+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<E> first, Matcher<? super E> second, Matcher<? super E> third, Matcher<? super E> forth, Matcher<? super E> fifth, Matcher<? super E> sixth) {
144+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
145+
matchers.add(first);
146+
matchers.add(second);
147+
matchers.add(third);
148+
matchers.add(forth);
149+
matchers.add(fifth);
150+
matchers.add(sixth);
151+
return arrayContainingInAnyOrder(matchers);
152+
}
153+
}

0 commit comments

Comments
 (0)