Skip to content

Commit 8c92825

Browse files
author
smgfreeman
committed
exposed more of DiagnosingMatcher and expose classes in Matchers to allow composition within custom matchers.
Fixed mismatch messages in AllOf and HasPropertyWithValue
1 parent a6b00c4 commit 8c92825

6 files changed

Lines changed: 32 additions & 29 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ public final void describeMismatch(Object item, Description mismatchDescription)
1616
matches(item, mismatchDescription);
1717
}
1818

19-
protected abstract boolean matches(Object item, Description mismatchDescription);
19+
public abstract boolean matches(Object item, Description mismatchDescription);
2020
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AllOf(Iterable<Matcher<? super T>> matchers) {
2222
}
2323

2424
@Override
25-
protected boolean matches(Object o, Description mismatchDescription) {
25+
public boolean matches(Object o, Description mismatchDescription) {
2626
for (Matcher<? super T> matcher : matchers) {
2727
if (!matcher.matches(o)) {
2828
mismatchDescription.appendDescriptionOf(matcher).appendText(" ");
@@ -41,23 +41,23 @@ public void describeTo(Description description) {
4141
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
4242
*/
4343
@Factory
44-
public static <T> Matcher<T> allOf(Iterable<Matcher<? super T>> matchers) {
44+
public static <T> AllOf<T> allOf(Iterable<Matcher<? super T>> matchers) {
4545
return new AllOf<T>(matchers);
4646
}
4747

4848
/**
4949
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
5050
*/
5151
@Factory
52-
public static <T> Matcher<T> allOf(Matcher<? super T>... matchers) {
52+
public static <T> AllOf<T> allOf(Matcher<? super T>... matchers) {
5353
return allOf(Arrays.asList(matchers));
5454
}
5555

5656
/**
5757
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
5858
*/
5959
@Factory
60-
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second) {
60+
public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second) {
6161
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(2);
6262
matchers.add(first);
6363
matchers.add(second);
@@ -68,7 +68,7 @@ public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second)
6868
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
6969
*/
7070
@Factory
71-
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
71+
public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
7272
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(3);
7373
matchers.add(first);
7474
matchers.add(second);
@@ -80,7 +80,7 @@ public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second,
8080
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
8181
*/
8282
@Factory
83-
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
83+
public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
8484
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(4);
8585
matchers.add(first);
8686
matchers.add(second);
@@ -93,7 +93,7 @@ public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second,
9393
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
9494
*/
9595
@Factory
96-
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) {
96+
public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
9797
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(5);
9898
matchers.add(first);
9999
matchers.add(second);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ public void describeTo(Description description) {
3232
* Evaluates to true if ANY of the passed in matchers evaluate to true.
3333
*/
3434
@Factory
35-
public static <T> Matcher<T> anyOf(Iterable<Matcher<? super T>> matchers) {
35+
public static <T> AnyOf<T> anyOf(Iterable<Matcher<? super T>> matchers) {
3636
return new AnyOf<T>(matchers);
3737
}
3838

3939
/**
4040
* Evaluates to true if ANY of the passed in matchers evaluate to true.
4141
*/
4242
@Factory
43-
public static <T> Matcher<T> anyOf(Matcher<? super T>... matchers) {
43+
public static <T> AnyOf<T> anyOf(Matcher<? super T>... matchers) {
4444
return anyOf(Arrays.asList(matchers));
4545
}
4646

4747
/**
4848
* Evaluates to true if ANY of the passed in matchers evaluate to true.
4949
*/
5050
@Factory
51-
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second) {
51+
public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second) {
5252
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
5353
matchers.add(first);
5454
matchers.add(second);
@@ -59,7 +59,7 @@ public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second)
5959
* Evaluates to true if ANY of the passed in matchers evaluate to true.
6060
*/
6161
@Factory
62-
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
62+
public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
6363
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
6464
matchers.add(first);
6565
matchers.add(second);
@@ -71,7 +71,7 @@ public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second,
7171
* Evaluates to true if ANY of the passed in matchers evaluate to true.
7272
*/
7373
@Factory
74-
public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
74+
public static <T> AnyOf<T> anyOf(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>>();
7676
matchers.add(first);
7777
matchers.add(second);
@@ -84,7 +84,7 @@ public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second,
8484
* Evaluates to true if ANY of the passed in matchers evaluate to true.
8585
*/
8686
@Factory
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) {
87+
public static <T> AnyOf<T> anyOf(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>>();
8989
matchers.add(first);
9090
matchers.add(second);
@@ -98,7 +98,7 @@ public static <T> Matcher<T> anyOf(Matcher<T> first, Matcher<? super T> second,
9898
* Evaluates to true if ANY of the passed in matchers evaluate to true.
9999
*/
100100
@Factory
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) {
101+
public static <T> AnyOf<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) {
102102
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
103103
matchers.add(first);
104104
matchers.add(second);

‎hamcrest-library/src/main/java/org/hamcrest/beans/HasProperty.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import org.hamcrest.Description;
88
import org.hamcrest.Factory;
9-
import org.hamcrest.Matcher;
109
import org.hamcrest.TypeSafeMatcher;
1110

1211
/**
@@ -42,7 +41,7 @@ public void describeTo(Description description) {
4241
}
4342

4443
@Factory
45-
public static <T> Matcher<T> hasProperty(String propertyName) {
44+
public static <T> HasProperty<T> hasProperty(String propertyName) {
4645
return new HasProperty<T>(propertyName);
4746
}
4847

‎hamcrest-library/src/main/java/org/hamcrest/beans/HasPropertyWithValue.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ public HasPropertyWithValue(String propertyName, Matcher<?> value) {
8080
@Override
8181
public boolean matchesSafely(T argument, Description mismatchDescription) {
8282
try {
83-
mismatchDescription.appendText("property \"" + propertyName + "\" ");
8483
Method readMethod = getReadMethod(argument);
8584
if (readMethod == null) {
86-
mismatchDescription.appendText("was missing.");
85+
mismatchDescription.appendText("missing.");
8786
return false;
8887
}
8988
Object propertyValue = readMethod.invoke(argument, NO_ARGUMENTS);
@@ -118,7 +117,7 @@ public void describeTo(Description description) {
118117
}
119118

120119
@Factory
121-
public static <T> Matcher<T> hasProperty(String propertyName, Matcher<?> value) {
120+
public static <T> HasPropertyWithValue<T> hasProperty(String propertyName, Matcher<?> value) {
122121
return new HasPropertyWithValue<T>(propertyName, value);
123122
}
124123
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/beans/HasPropertyWithValueTest.java‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.beans.SimpleBeanInfo;
1414

1515
import org.hamcrest.AbstractMatcherTest;
16+
import org.hamcrest.Description;
1617
import org.hamcrest.Matcher;
1718
import org.hamcrest.StringDescription;
1819
import org.hamcrest.core.IsEqual;
@@ -38,17 +39,15 @@ protected Matcher<?> createMatcher() {
3839

3940
public void testMatchesInfolessBeanWithMatchedNamedProperty() {
4041
assertThat(shouldMatch, hasProperty("property", equalTo("is expected")));
41-
assertThat(shouldNotMatch, not(hasProperty("property",
42-
equalTo("is expected"))));
42+
assertThat(shouldNotMatch, not(hasProperty("property", equalTo("is expected"))));
4343
}
4444

4545
public void testMatchesBeanWithInfoWithMatchedNamedProperty() {
4646
assertThat(beanWithInfo, hasProperty("property", equalTo("with info")));
4747
}
4848

4949
public void testDoesNotMatchInfolessBeanWithoutMatchedNamedProperty() {
50-
assertThat(shouldNotMatch, not(hasProperty("nonExistentProperty",
51-
anything())));
50+
assertThat(shouldNotMatch, not(hasProperty("nonExistentProperty", anything())));
5251
}
5352

5453
public void testDoesNotMatchWriteOnlyProperty() {
@@ -60,17 +59,23 @@ public void testDescribeTo() {
6059
Matcher<?> matcher = equalTo(true);
6160

6261
assertDescription("hasProperty(\"property\", "
63-
+ StringDescription.asString(matcher) + ")", hasProperty("property",
64-
matcher));
62+
+ StringDescription.asString(matcher) + ")", hasProperty("property", matcher));
63+
}
64+
65+
public void testDoesNotWriteMismatchIfPropertyMatches() {
66+
HasPropertyWithValue<Object> matcher = hasProperty( "property", anything());
67+
assertTrue("Precondtion: Matcher should match item.", matcher.matches(beanWithInfo));
68+
Description description = new StringDescription();
69+
matcher.describeMismatch(beanWithInfo, description);
70+
assertEquals("Expected mismatch description", "", description.toString());
6571
}
6672

6773
public void testDescribesMissingPropertyMismatch() {
68-
assertMismatchDescription("property \"honk\" was missing.", hasProperty(
69-
"honk", anything()), shouldNotMatch);
74+
assertMismatchDescription("missing.", hasProperty( "honk", anything()), shouldNotMatch);
7075
}
7176

7277
public void testDescribesMismatchingPropertyValueMismatch() {
73-
assertMismatchDescription("property \"property\" was \"not expected\".",
78+
assertMismatchDescription("was \"not expected\".",
7479
hasProperty("property", equalTo("foo")), shouldNotMatch);
7580
}
7681

0 commit comments

Comments
 (0)