Skip to content

Commit e6b1bfe

Browse files
author
joeretro
committed
Java5ified core matchers.
1 parent 4ef2fa6 commit e6b1bfe

25 files changed

Lines changed: 291 additions & 225 deletions

‎TODO.txt‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
- iterables (jdk 1.5)
1414

1515
* String matchers
16-
- case insensitive
17-
- whitespace insensitive
1816
- reg exp matching
1917

2018
* XML
@@ -33,4 +31,7 @@
3331

3432
* Sugar for new matchers
3533

36-
* NOT_NULL, IS_ANYTHING, etc -> notNull(), isAnything(), etc.
34+
* And/Or(Matcher... )
35+
36+
* and(hasProperty(),eq())
37+

‎src/library/org/hamcrest/Matchers.java‎

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,8 @@
22
*/
33
package org.hamcrest;
44

5-
import org.hamcrest.core.IsAnything;
6-
import org.hamcrest.core.IsCompatibleType;
7-
import org.hamcrest.core.IsIn;
8-
import org.hamcrest.core.IsInstanceOf;
9-
import org.hamcrest.core.IsNot;
10-
import org.hamcrest.core.IsNull;
11-
12-
import java.util.Collection;
13-
145
public class Matchers {
156

16-
public static final Matcher ANYTHING = new IsAnything();
17-
public static final Matcher NULL = new IsNull();
18-
public static final Matcher NOT_NULL = new IsNot(NULL);
19-
20-
public static IsInstanceOf isA(Class operandClass) {
21-
return new IsInstanceOf(operandClass);
22-
}
23-
24-
public static IsCompatibleType compatibleType(Class baseType) {
25-
return new IsCompatibleType(baseType);
26-
}
27-
28-
public static IsIn isIn(Collection collection) {
29-
return new IsIn(collection);
30-
}
31-
32-
public static IsIn isIn(Object[] array) {
33-
return new IsIn(array);
34-
}
35-
367
/**
378
* Temporary Matcher... don't use this.. it will go away!
389
*/

‎src/library/org/hamcrest/core/Always.java‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,50 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89
/**
9-
* Always evaluate to true or false (depending on what's passed into the constructor).
10+
* Always passes or failes, ignoring what value is passed to it.
1011
*/
11-
public class Always implements Matcher {
12+
public class Always<T> implements Matcher<T> {
1213

13-
private final boolean matches;
14+
private final boolean pass;
1415
private final String description;
1516

16-
public Always(boolean matches) {
17-
this(matches, "always evaluates to " + matches);
17+
public Always(boolean pass) {
18+
this(pass, "always " + (pass ? "passes" : "fails"));
1819
}
1920

2021
public Always(boolean matches, String description) {
21-
this.matches = matches;
22+
this.pass = matches;
2223
this.description = description;
2324
}
2425

25-
public boolean match(Object o) {
26-
return matches;
26+
public boolean match(T o) {
27+
return pass;
2728
}
2829

2930
public void describeTo(Description description) {
3031
description.appendText(this.description);
3132
}
3233

34+
@Factory
35+
public static <T> Matcher<T> alwaysPasses() {
36+
return new Always<T>(true);
37+
}
38+
39+
@Factory
40+
public static <T> Matcher<T> alwaysPasses(String description) {
41+
return new Always<T>(true, description);
42+
}
43+
44+
@Factory
45+
public static <T> Matcher<T> alwaysFails() {
46+
return new Always<T>(false);
47+
}
48+
49+
@Factory
50+
public static <T> Matcher<T> alwaysFails(String description) {
51+
return new Always<T>(false, description);
52+
}
3353
}

‎src/library/org/hamcrest/core/And.java‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,39 @@
1313
* matcher returns <code>false</code>.
1414
*/
1515
public class And<T> implements Matcher<T> {
16-
private final Matcher<T> left;
17-
private final Matcher<T> right;
16+
private final Matcher<T>[] matchers;
1817

19-
public And(Matcher<T> left, Matcher<T> right) {
20-
this.left = left;
21-
this.right = right;
18+
19+
public And(Matcher<T>[] matchers) {
20+
this.matchers = matchers;
2221
}
2322

2423
public boolean match(T o) {
25-
return left.match(o) && right.match(o);
24+
for (Matcher<T> matcher : matchers) {
25+
if (!matcher.match(o)) {
26+
return false;
27+
}
28+
}
29+
return true;
2630
}
2731

2832
public void describeTo(Description description) {
2933
description.appendText("(");
30-
left.describeTo(description);
31-
description.appendText(" and ");
32-
right.describeTo(description);
34+
boolean seenFirst = false;
35+
for (Matcher<T> matcher : matchers) {
36+
if (seenFirst) {
37+
description.appendText(" and ");
38+
} else {
39+
seenFirst = true;
40+
}
41+
matcher.describeTo(description);
42+
}
3343
description.appendText(")");
3444
}
3545

3646
@Factory
37-
public static <T> Matcher<T> and(Matcher<T> left, Matcher<T> right) {
38-
return new And<T>(left, right);
47+
public static <T> Matcher<T> and(Matcher<T>... matchers) {
48+
return new And<T>(matchers);
3949
}
4050

4151
}

‎src/library/org/hamcrest/core/DescribedAs.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89
/**
910
* Provides a custom description to another matcher.
@@ -25,4 +26,9 @@ public boolean match(T o) {
2526
public void describeTo(Description description) {
2627
description.appendText(this.description);
2728
}
29+
30+
@Factory
31+
public static <T> Matcher<T> describedAs(String description, Matcher<T> matcher) {
32+
return new DescribedAs<T>(description, matcher);
33+
}
2834
}

‎src/library/org/hamcrest/core/IsCompatibleType.java‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
5+
import org.hamcrest.Factory;
56

6-
public class IsCompatibleType implements Matcher<Class> {
7-
private final Class type;
7+
public class IsCompatibleType<T extends Class<T>> implements Matcher<Class<T>> {
8+
private final Class<? extends T> type;
89

9-
public IsCompatibleType(Class type) {
10+
public IsCompatibleType(Class<? extends T> type) {
1011
this.type = type;
1112
}
1213

@@ -17,4 +18,10 @@ public boolean match(Class cls) {
1718
public void describeTo(Description description) {
1819
description.appendText("type < ").appendText(type.getName());
1920
}
21+
22+
@Factory
23+
public static <T> Matcher<Class<? extends T>> compatibleType(Class<? extends T> baseType) {
24+
return new IsCompatibleType(baseType);
25+
}
26+
2027
}

‎src/library/org/hamcrest/core/IsEqual.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,26 @@ private static boolean isArray(Object o) {
6060
return o.getClass().isArray();
6161
}
6262

63+
/**
64+
* Is the value equal to another value, as tested by the
65+
* {@link java.lang.Object#equals} invokedMethod?
66+
*/
6367
@Factory
6468
public static <T> Matcher<T> eq(T operand) {
6569
return new IsEqual<T>(operand);
6670
}
6771

72+
/**
73+
* Is the value true?
74+
*/
6875
@Factory
6976
public static Matcher<Boolean> isTrue() {
7077
return eq(true);
7178
}
7279

80+
/**
81+
* Is the value false?
82+
*/
7383
@Factory
7484
public static Matcher<Boolean> isFalse() {
7585
return eq(false);

‎src/library/org/hamcrest/core/IsIn.java‎

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

33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
5+
import org.hamcrest.Factory;
56

67
import java.util.Arrays;
78
import java.util.Collection;
@@ -13,10 +14,6 @@ public IsIn(Collection<T> collection) {
1314
this.collection = collection;
1415
}
1516

16-
public IsIn(T... elements) {
17-
collection = Arrays.asList(elements);
18-
}
19-
2017
public boolean match(T o) {
2118
return collection.contains(o);
2219
}
@@ -34,4 +31,15 @@ public void describeTo(Description description) {
3431
}
3532
description.appendText("}");
3633
}
34+
35+
@Factory
36+
public static <T> Matcher<T> isInCollection(Collection<T> elements) {
37+
return new IsIn<T>(elements);
38+
}
39+
40+
@Factory
41+
public static <T> Matcher<T> isIn(T... elements) {
42+
return isInCollection(Arrays.asList(elements));
43+
}
44+
3745
}

‎src/library/org/hamcrest/core/IsInstanceOf.java‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,37 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89

910
/**
1011
* Tests whether the value is an instance of a class.
1112
*/
12-
public class IsInstanceOf implements Matcher<Class> {
13-
private final Class theClass;
13+
public class IsInstanceOf<T> implements Matcher<T> {
14+
private final Class<T> theClass;
1415

1516
/**
1617
* Creates a new instance of IsInstanceOf
1718
*
1819
* @param theClass The predicate evaluates to true for instances of this class
1920
* or one of its subclasses.
2021
*/
21-
public IsInstanceOf(Class theClass) {
22+
public IsInstanceOf(Class<T> theClass) {
2223
this.theClass = theClass;
2324
}
2425

25-
public boolean match(Class cls) {
26-
return theClass.isInstance(cls);
26+
public boolean match(T item) {
27+
return theClass.isInstance(item);
2728
}
2829

2930
public void describeTo(Description description) {
3031
description.appendText("an instance of ")
3132
.appendText(theClass.getName());
3233
}
34+
35+
@Factory
36+
public static <T> Matcher<T> isA(Class<T> instanceOf) {
37+
return new IsInstanceOf<T>(instanceOf);
38+
}
39+
3340
}

‎src/library/org/hamcrest/core/IsNot.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public void describeTo(Description description) {
2727
}
2828

2929
@Factory
30-
public static <T> Matcher<T> not(Matcher<T> c) {
31-
return new IsNot<T>(c);
30+
public static <T> Matcher<T> not(Matcher<T> matcher) {
31+
return new IsNot<T>(matcher);
3232
}
3333

3434
}

0 commit comments

Comments
 (0)