Skip to content

Commit 237b808

Browse files
author
nat.pryce
committed
applied the appendDescriptionOf method all over the place
1 parent 868ddb5 commit 237b808

File tree

17 files changed

+44
-70
lines changed

17 files changed

+44
-70
lines changed

hamcrest-core/src/main/java/org/hamcrest/BaseDescription.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private Description appendList(String start, String separator, String end, Itera
8484
}
8585

8686

87-
/** Append the <var>str</var> to the description.
87+
/** Append the String <var>str</var> to the description.
8888
* The default implementation passes every character to {@link #append(char)}.
8989
* Override in subclasses to provide an efficient implementation.
9090
*/
@@ -94,6 +94,8 @@ protected void append(String str) {
9494
}
9595
}
9696

97+
/** Append the char <var>c</var> to the description.
98+
*/
9799
protected abstract void append(char c);
98100

99101
private void toJavaSyntax(String unformatted) {

hamcrest-core/src/main/java/org/hamcrest/StringDescription.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ public StringDescription(Appendable out) {
2424
* @return
2525
* The description of the object.
2626
*/
27-
public static String toString(SelfDescribing selfDescribing) {
28-
StringDescription description = new StringDescription();
29-
selfDescribing.describeTo(description);
30-
return description.toString();
27+
public static String toString(SelfDescribing value) {
28+
return new StringDescription().appendDescriptionOf(value).toString();
3129
}
3230

3331
/**
@@ -37,7 +35,6 @@ public static String asString(SelfDescribing selfDescribing) {
3735
return toString(selfDescribing);
3836
}
3937

40-
4138
protected void append(String str) {
4239
try {
4340
out.append(str);

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,7 @@ public boolean matches(Object o) {
2929
}
3030

3131
public void describeTo(Description description) {
32-
description.appendText("(");
33-
boolean seenFirst = false;
34-
for (Matcher<? extends T> matcher : matchers) {
35-
if (seenFirst) {
36-
description.appendText(" and ");
37-
} else {
38-
seenFirst = true;
39-
}
40-
matcher.describeTo(description);
41-
}
42-
description.appendText(")");
32+
description.appendList("(", " and ", ")", matchers);
4333
}
4434

4535
/**

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,7 @@ public boolean matches(Object o) {
3030
}
3131

3232
public void describeTo(Description description) {
33-
description.appendText("(");
34-
boolean seenFirst = false;
35-
for (Matcher<? extends T> matcher : matchers) {
36-
if (seenFirst) {
37-
description.appendText(" or ");
38-
} else {
39-
seenFirst = true;
40-
}
41-
matcher.describeTo(description);
42-
}
43-
description.appendText(")");
33+
description.appendList("(", " or ", ")", matchers);
4434
}
4535

4636
/**

hamcrest-core/src/main/java/org/hamcrest/core/Is.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ public boolean matches(Object arg) {
2727
}
2828

2929
public void describeTo(Description description) {
30-
description.appendText("is ");
31-
matcher.describeTo(description);
30+
description.appendText("is ").appendDescriptionOf(matcher);
3231
}
33-
32+
3433
/**
3534
* Decorates another Matcher, retaining the behavior but allowing tests
3635
* to be slightly more expressive.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public boolean matches(Object arg) {
2424
}
2525

2626
public void describeTo(Description description) {
27-
description.appendText("not ");
28-
matcher.describeTo(description);
27+
description.appendText("not ").appendDescriptionOf(matcher);
2928
}
3029

3130
/**

hamcrest-integration/src/main/java/org/hamcrest/MatcherAssert.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ public static <T> void assertThat(T actual, Matcher<T> matcher) {
1111
public static <T> void assertThat(String reason, T actual, Matcher<T> matcher) {
1212
if (!matcher.matches(actual)) {
1313
Description description = new StringDescription();
14-
description.appendText(reason);
15-
description.appendText("\nExpected: ");
16-
matcher.describeTo(description);
17-
description.appendText("\n got: ").appendValue(actual).appendText("\n");
14+
description.appendText(reason)
15+
.appendText("\nExpected: ")
16+
.appendDescriptionOf(matcher)
17+
.appendText("\n got: ")
18+
.appendValue(actual)
19+
.appendText("\n");
20+
1821
throw new java.lang.AssertionError(description.toString());
1922
}
2023
}

hamcrest-integration/src/main/java/org/hamcrest/integration/EasyMock2Adapter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ public boolean matches(Object argument) {
3939
public void appendTo(StringBuffer buffer) {
4040
hamcrestMatcher.describeTo(new StringDescription(buffer));
4141
}
42-
4342
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void describeTo(Description description) {
102102
description.appendText("hasProperty(");
103103
description.appendValue(propertyName);
104104
description.appendText(", ");
105-
value.describeTo(description);
105+
description.appendDescriptionOf(value);
106106
description.appendText(")");
107107
}
108108

hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public boolean matchesSafely(T[] array) {
2424
}
2525

2626
public void describeTo(Description description) {
27-
description.appendText("an array containing ");
28-
elementMatcher.describeTo(description);
27+
description
28+
.appendText("an array containing ")
29+
.appendDescriptionOf(elementMatcher);
2930
}
3031

3132
@Factory

hamcrest-library/src/main/java/org/hamcrest/collection/IsCollectionContaining.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public boolean matchesSafely(Iterable<T> collection) {
2727
}
2828

2929
public void describeTo(Description description) {
30-
description.appendText("a collection containing ");
31-
elementMatcher.describeTo(description);
30+
description
31+
.appendText("a collection containing ")
32+
.appendDescriptionOf(elementMatcher);
3233
}
3334

3435
@Factory

hamcrest-library/src/main/java/org/hamcrest/collection/IsMapContaining.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public boolean matchesSafely(Map<K, V> map) {
3131
}
3232

3333
public void describeTo(Description description) {
34-
description.appendText("map containing [");
35-
keyMatcher.describeTo(description);
36-
description.appendText("->");
37-
valueMatcher.describeTo(description);
38-
description.appendText("]");
34+
description.appendText("map containing [")
35+
.appendDescriptionOf(keyMatcher)
36+
.appendText("->")
37+
.appendDescriptionOf(valueMatcher)
38+
.appendText("]");
3939
}
4040

4141
@Factory

hamcrest-library/src/main/java/org/hamcrest/object/HasToString.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ public boolean matches(Object item) {
1818
}
1919

2020
public void describeTo(Description description) {
21-
description.appendText("asString(");
22-
toStringMatcher.describeTo(description);
23-
description.appendText(")");
21+
description
22+
.appendText("asString(")
23+
.appendDescriptionOf(toStringMatcher)
24+
.appendText(")");
2425
}
2526

2627
@Factory

hamcrest-library/src/main/java/org/hamcrest/xml/HasXPath.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ public boolean matchesSafely(Node item) {
5757
public void describeTo(Description description) {
5858
description.appendText("an XML document with XPath ").appendText(xpathString);
5959
if (valueMatcher != null) {
60-
description.appendText(" ");
61-
valueMatcher.describeTo(description);
60+
description.appendText(" ").appendDescriptionOf(valueMatcher);
6261
}
6362
}
64-
63+
6564
@Factory
6665
public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher) {
6766
return new HasXPath(xPath, valueMatcher);

hamcrest-unit-test/src/main/java/org/hamcrest/AbstractMatcherTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public <T> void assertDoesNotMatch(String message, Matcher<T> c, T arg) {
2424

2525
public void assertDescription(String expected, Matcher matcher) {
2626
Description description = new StringDescription();
27-
matcher.describeTo(description);
27+
description.appendDescriptionOf(matcher);
2828
assertEquals(expected, description.toString());
2929
}
3030

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

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

1515
import org.hamcrest.AbstractMatcherTest;
16-
import org.hamcrest.Description;
1716
import org.hamcrest.Matcher;
1817
import org.hamcrest.StringDescription;
1918

@@ -52,11 +51,9 @@ public void testDoesNotMatchWriteOnlyProperty() {
5251

5352
public void testDescribeTo() {
5453
Matcher matcher = equalTo(true);
55-
Description isEqualDescription = new StringDescription();
56-
matcher.describeTo(isEqualDescription);
57-
58-
assertDescription("hasProperty(\"property\", " + isEqualDescription + ")",
59-
hasProperty("property", matcher));
54+
55+
assertDescription("hasProperty(\"property\", " + StringDescription.asString(matcher) + ")",
56+
hasProperty("property", matcher));
6057
}
6158

6259
public static class BeanWithoutInfo {

hamcrest-unit-test/src/main/java/org/hamcrest/object/HasToStringTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package org.hamcrest.object;
22

3-
import org.hamcrest.Description;
4-
import org.hamcrest.Matcher;
5-
import org.hamcrest.StringDescription;
6-
import org.hamcrest.AbstractMatcherTest;
7-
83
import static org.hamcrest.MatcherAssert.assertThat;
94
import static org.hamcrest.core.IsEqual.equalTo;
105
import static org.hamcrest.core.IsNot.not;
116
import static org.hamcrest.object.HasToString.hasToString;
127

8+
import org.hamcrest.AbstractMatcherTest;
9+
import org.hamcrest.Matcher;
10+
import org.hamcrest.StringDescription;
11+
1312
public class HasToStringTest extends AbstractMatcherTest {
1413

1514
private static final String TO_STRING_RESULT = "toString result";
@@ -36,9 +35,6 @@ public void testHasReadableDescription() {
3635
}
3736

3837
private String descriptionOf(Matcher matcher) {
39-
Description description = new StringDescription();
40-
matcher.describeTo(description);
41-
return description.toString();
38+
return StringDescription.asString(matcher);
4239
}
43-
4440
}

0 commit comments

Comments
 (0)