Skip to content

Commit 9b1d9f0

Browse files
author
nat.pryce
committed
working on Issue 17 - added introspection to core matchers and common introspection interfaces
1 parent ef9a7f6 commit 9b1d9f0

14 files changed

Lines changed: 147 additions & 43 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.hamcrest.Matcher;
55
import org.hamcrest.Description;
66
import org.hamcrest.Factory;
7+
import org.hamcrest.introspection.Combination;
78

89
import java.util.Arrays;
910

@@ -12,7 +13,7 @@
1213
* shortcut, so that the second matcher is not called if the first
1314
* matcher returns <code>false</code>.
1415
*/
15-
public class AllOf<T> extends BaseMatcher<T> {
16+
public class AllOf<T> extends BaseMatcher<T> implements Combination {
1617
private final Iterable<? extends Matcher<? extends T>> matchers;
1718

1819
public AllOf(Iterable<? extends Matcher<? extends T>> matchers) {
@@ -31,8 +32,12 @@ public boolean matches(Object o) {
3132
public void describeTo(Description description) {
3233
description.appendList("(", " and ", ")", matchers);
3334
}
35+
36+
public Iterable<? extends Matcher<?>> combined() {
37+
return matchers;
38+
}
3439

35-
/**
40+
/**
3641
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
3742
*/
3843
@Factory

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

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

3+
import java.util.Arrays;
4+
35
import org.hamcrest.BaseMatcher;
4-
import org.hamcrest.Matcher;
56
import org.hamcrest.Description;
67
import org.hamcrest.Factory;
7-
8-
import java.util.Arrays;
8+
import org.hamcrest.Matcher;
9+
import org.hamcrest.introspection.Combination;
910

1011
/**
1112
* Calculates the logical disjunction of two matchers. Evaluation is
1213
* shortcut, so that the second matcher is not called if the first
1314
* matcher returns <code>true</code>.
1415
*/
15-
public class AnyOf<T> extends BaseMatcher<T> {
16-
16+
public class AnyOf<T> extends BaseMatcher<T> implements Combination {
1717
private final Iterable<? extends Matcher<? extends T>> matchers;
1818

1919
public AnyOf(Iterable<? extends Matcher<? extends T>> matchers) {
@@ -28,12 +28,16 @@ public boolean matches(Object o) {
2828
}
2929
return false;
3030
}
31-
31+
3232
public void describeTo(Description description) {
3333
description.appendList("(", " or ", ")", matchers);
3434
}
35-
36-
/**
35+
36+
public Iterable<? extends Matcher<?>> combined() {
37+
return matchers;
38+
}
39+
40+
/**
3741
* Evaluates to true if ANY of the passed in matchers evaluate to true.
3842
*/
3943
@Factory

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
import java.util.regex.Pattern;
66

7+
import org.hamcrest.BaseMatcher;
78
import org.hamcrest.Description;
8-
import org.hamcrest.Matcher;
99
import org.hamcrest.Factory;
10-
import org.hamcrest.BaseMatcher;
10+
import org.hamcrest.Matcher;
11+
import org.hamcrest.introspection.Modifier;
1112

1213
/**
1314
* Provides a custom description to another matcher.
1415
*/
15-
public class DescribedAs<T> extends BaseMatcher<T> {
16+
public class DescribedAs<T> extends BaseMatcher<T> implements Modifier {
1617
private final String descriptionTemplate;
1718
private final Matcher<T> matcher;
1819
private final Object[] values;
@@ -45,6 +46,10 @@ public void describeTo(Description description) {
4546
}
4647
}
4748

49+
public Matcher<?> modified() {
50+
return matcher;
51+
}
52+
4853
/**
4954
* Wraps an existing matcher and overrides the description when it fails.
5055
*/

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
*/
33
package org.hamcrest.core;
44

5+
import java.lang.reflect.Array;
6+
7+
import org.hamcrest.BaseMatcher;
58
import org.hamcrest.Description;
6-
import org.hamcrest.Matcher;
79
import org.hamcrest.Factory;
8-
import org.hamcrest.BaseMatcher;
9-
10-
import java.lang.reflect.Array;
10+
import org.hamcrest.Matcher;
11+
import org.hamcrest.introspection.Comparison;
1112

1213

1314
/**
1415
* Is the value equal to another value, as tested by the
1516
* {@link java.lang.Object#equals} invokedMethod?
1617
*/
17-
public class IsEqual<T> extends BaseMatcher<T> {
18+
public class IsEqual<T> extends BaseMatcher<T> implements Comparison {
1819
private final Object object;
1920

2021
public IsEqual(T equalArg) {
@@ -28,8 +29,12 @@ public boolean matches(Object arg) {
2829
public void describeTo(Description description) {
2930
description.appendValue(object);
3031
}
32+
33+
public Object operand() {
34+
return object;
35+
}
3136

32-
private static boolean areEqual(Object o1, Object o2) {
37+
private static boolean areEqual(Object o1, Object o2) {
3338
if (o1 == null || o2 == null) {
3439
return o1 == null && o2 == null;
3540
} else if (isArray(o1)) {

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
*/
33
package org.hamcrest.core;
44

5+
import org.hamcrest.BaseMatcher;
56
import org.hamcrest.Description;
6-
import org.hamcrest.Matcher;
77
import org.hamcrest.Factory;
8-
import org.hamcrest.BaseMatcher;
8+
import org.hamcrest.Matcher;
9+
import org.hamcrest.introspection.Comparison;
910

1011

1112
/**
1213
* Tests whether the value is an instance of a class.
1314
*/
14-
public class IsInstanceOf extends BaseMatcher<Object> {
15+
public class IsInstanceOf extends BaseMatcher<Object> implements Comparison {
1516
private final Class<?> theClass;
1617

1718
/**
@@ -33,7 +34,11 @@ public void describeTo(Description description) {
3334
.appendText(theClass.getName());
3435
}
3536

36-
/**
37+
public Class<?> operand() {
38+
return theClass;
39+
}
40+
41+
/**
3742
* Is the value an instance of a particular type?
3843
*/
3944
@Factory

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
package org.hamcrest.core;
44

55
import static org.hamcrest.core.IsEqual.equalTo;
6+
7+
import org.hamcrest.BaseMatcher;
68
import org.hamcrest.Description;
7-
import org.hamcrest.Matcher;
89
import org.hamcrest.Factory;
9-
import org.hamcrest.BaseMatcher;
10+
import org.hamcrest.Matcher;
11+
import org.hamcrest.introspection.Modifier;
1012

1113

1214
/**
1315
* Calculates the logical negation of a matcher.
1416
*/
15-
public class IsNot<T> extends BaseMatcher<T> {
17+
public class IsNot<T> extends BaseMatcher<T> implements Modifier {
1618
private final Matcher<T> matcher;
1719

1820
public IsNot(Matcher<T> matcher) {
@@ -27,6 +29,11 @@ public void describeTo(Description description) {
2729
description.appendText("not ").appendDescriptionOf(matcher);
2830
}
2931

32+
33+
public Matcher<?> modified() {
34+
return matcher;
35+
}
36+
3037
/**
3138
* Inverts the rule.
3239
*/
@@ -45,5 +52,4 @@ public static <T> Matcher<T> not(Matcher<T> matcher) {
4552
public static <T> Matcher<T> not(T value) {
4653
return not(equalTo(value));
4754
}
48-
4955
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
import org.hamcrest.Matcher;
77
import org.hamcrest.Factory;
88
import org.hamcrest.BaseMatcher;
9+
import org.hamcrest.introspection.Comparison;
910

1011

1112
/**
1213
* Is the value the same object as another value?
1314
*/
14-
public class IsSame<T> extends BaseMatcher<T> {
15+
public class IsSame<T> extends BaseMatcher<T> implements Comparison {
1516
private final T object;
16-
17+
1718
public IsSame(T object) {
1819
this.object = object;
1920
}
@@ -25,8 +26,12 @@ public boolean matches(Object arg) {
2526
public void describeTo(Description description) {
2627
description.appendText("same(") .appendValue(object) .appendText(")");
2728
}
28-
29-
/**
29+
30+
public Object operand() {
31+
return object;
32+
}
33+
34+
/**
3035
* Creates a new instance of IsSame
3136
*
3237
* @param object The predicate evaluates to true only when the argument is
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.hamcrest.introspection;
2+
3+
import org.hamcrest.Matcher;
4+
5+
public interface Combination {
6+
Iterable<? extends Matcher<?>> combined();
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.hamcrest.introspection;
2+
3+
public interface Comparison {
4+
Object operand();
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.hamcrest.introspection;
2+
3+
import org.hamcrest.Matcher;
4+
5+
public interface Modifier {
6+
Matcher<?> modified();
7+
}

0 commit comments

Comments
 (0)