Skip to content

Commit 78226a6

Browse files
author
npryce
committed
Added appendList method to Description to append a list of self-describing objects
Factored out some common utility classes from StringDescription into the internal package Added the IsArray matcher
1 parent e48d2ae commit 78226a6

12 files changed

Lines changed: 174 additions & 19 deletions

File tree

‎src/api/org/hamcrest/Description.java‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,31 @@
88
public interface Description {
99

1010
/**
11-
* Append some plain text to the description.
11+
* Appends some plain text to the description.
1212
*/
1313
Description appendText(String text);
1414

1515
/**
16-
* Append an arbitary value to the description.
16+
* Appends an arbitary value to the description.
1717
*/
1818
Description appendValue(Object value);
1919

2020
/**
21-
* Append a list of values to the description.
21+
* Appends a list of values to the description.
2222
*/
2323
<T> Description appendValueList(String start, String separator, String end,
2424
T... values);
2525

2626
/**
27-
* Append a list of values to the description.
27+
* Appends a list of values to the description.
2828
*/
2929
<T> Description appendValueList(String start, String separator, String end,
3030
Iterable<T> values);
31+
32+
/**
33+
* Appends a list of {@link org.hamcrest.SelfDescribing} objects
34+
* to the description.
35+
*/
36+
Description appendList(String start, String separator, String end,
37+
Iterable<? extends SelfDescribing> values);
3138
}

‎src/integration/org/hamcrest/MatcherAssert.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
*/
33
package org.hamcrest;
44

5-
import org.hamcrest.internal.StringDescription;
65

76
public class MatcherAssert {
87

‎src/integration/org/hamcrest/integration/EasyMockAdapter.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import org.easymock.IArgumentMatcher;
44
import org.easymock.EasyMock;
5-
import org.hamcrest.internal.StringDescription;
65
import org.hamcrest.Matcher;
6+
import org.hamcrest.StringDescription;
77

88
/**
99
* An adapter to allowing a Hamcrest {@link org.hamcrest.Matcher}

‎src/integration/org/hamcrest/integration/JMockAdapter.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.jmock.core.Constraint;
44
import org.hamcrest.Matcher;
5-
import org.hamcrest.internal.StringDescription;
5+
import org.hamcrest.StringDescription;
66

77
/**
88
* An adapter to allowing a Hamcrest {@link org.hamcrest.Matcher}

src/library/org/hamcrest/internal/StringDescription.java renamed to src/library/org/hamcrest/StringDescription.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
package org.hamcrest.internal;
1+
package org.hamcrest;
22

33
import java.util.Arrays;
44
import java.util.Iterator;
55

66
import org.hamcrest.Description;
7+
import org.hamcrest.SelfDescribing;
8+
import org.hamcrest.internal.ArrayIterator;
9+
import org.hamcrest.internal.SelfDescribingValueIterator;
710

811
public class StringDescription implements Description {
9-
1012
private final StringBuffer buffer;
1113

1214
public StringDescription() {
@@ -54,18 +56,25 @@ public <T> Description appendValueList(String start, String separator, String en
5456
}
5557

5658
private <T> Description appendValueList(String start, String separator, String end, Iterator<T> values) {
57-
boolean separate = false;
58-
59+
return appendList(start, separator, end, new SelfDescribingValueIterator<T>(values));
60+
}
61+
62+
public Description appendList(String start, String separator, String end, Iterable<? extends SelfDescribing> values) {
63+
return appendList(start, separator, end, values.iterator());
64+
}
65+
66+
private Description appendList(String start, String separator, String end, Iterator<? extends SelfDescribing> i) {
67+
boolean separate = false;
5968
buffer.append(start);
60-
while (values.hasNext()) {
61-
if (separate) buffer.append(separator);
62-
appendValue(values.next());
63-
separate = true;
69+
while (i.hasNext()) {
70+
if (separate) buffer.append(separator);
71+
i.next().describeTo(this);
72+
separate = true;
6473
}
6574
buffer.append(end);
6675

6776
return this;
68-
}
77+
}
6978

7079
private void toJavaSyntax(StringBuffer buffer, String unformatted) {
7180
buffer.append('"');
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.hamcrest.collection;
2+
3+
import java.util.Arrays;
4+
5+
import org.hamcrest.Description;
6+
import org.hamcrest.Matcher;
7+
8+
public class IsArray<T> implements Matcher<T[]> {
9+
private final Matcher<T>[] elementMatchers;
10+
11+
public IsArray(Matcher<T>[] elementMatchers) {
12+
this.elementMatchers = elementMatchers.clone();
13+
}
14+
15+
public boolean match(T[] array) {
16+
if (array == null) return false;
17+
18+
if (array.length != elementMatchers.length) return false;
19+
20+
for (int i = 0; i < array.length; i++) {
21+
if (!elementMatchers[i].match(array[i])) return false;
22+
}
23+
24+
return true;
25+
}
26+
27+
public void describeTo(Description description) {
28+
description.appendList(descriptionStart(), descriptionSeparator(), descriptionEnd(),
29+
Arrays.asList(elementMatchers));
30+
}
31+
32+
/**
33+
* Returns the string that starts the description.
34+
*
35+
* Can be overridden in subclasses to customise how the matcher is
36+
* described.
37+
*/
38+
protected String descriptionStart() {
39+
return "[";
40+
}
41+
42+
/**
43+
* Returns the string that separates the elements in the description.
44+
*
45+
* Can be overridden in subclasses to customise how the matcher is
46+
* described.
47+
*/
48+
protected String descriptionSeparator() {
49+
return ", ";
50+
}
51+
52+
/**
53+
* Returns the string that ends the description.
54+
*
55+
* Can be overridden in subclasses to customise how the matcher is
56+
* described.
57+
*/
58+
protected String descriptionEnd() {
59+
return "]";
60+
}
61+
62+
public static <T> IsArray<T> array(Matcher<T>... elementMatchers) {
63+
return new IsArray<T>(elementMatchers);
64+
}
65+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.hamcrest.internal;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.SelfDescribing;
5+
6+
public class SelfDescribingValue<T> implements SelfDescribing {
7+
private T value;
8+
9+
public SelfDescribingValue(T value) {
10+
this.value = value;
11+
}
12+
13+
public void describeTo(Description description) {
14+
description.appendValue(value);
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.hamcrest.internal;
2+
3+
import java.util.Iterator;
4+
5+
import org.hamcrest.SelfDescribing;
6+
7+
public class SelfDescribingValueIterator<T> implements Iterator<SelfDescribing> {
8+
private Iterator<T> values;
9+
10+
public SelfDescribingValueIterator(Iterator<T> values) {
11+
this.values = values;
12+
}
13+
14+
public boolean hasNext() {
15+
return values.hasNext();
16+
}
17+
18+
public SelfDescribing next() {
19+
return new SelfDescribingValue<T>(values.next());
20+
}
21+
22+
public void remove() {
23+
values.remove();
24+
}
25+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import junit.framework.AssertionFailedError;
66
import junit.framework.TestCase;
77
import org.hamcrest.core.Always;
8-
import org.hamcrest.internal.StringDescription;
98

109
public abstract class AbstractMatcherTest extends TestCase {
1110
protected static final Matcher TRUE_MATCHER = new Always(true);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.hamcrest.AbstractMatcherTest;
1616
import org.hamcrest.Description;
1717
import org.hamcrest.Matcher;
18-
import org.hamcrest.internal.StringDescription;
18+
import org.hamcrest.StringDescription;
1919

2020
/**
2121
* @author Iain McGinniss

0 commit comments

Comments
 (0)