-
Notifications
You must be signed in to change notification settings - Fork 376
/
IsIterableContainingInOrder.java
169 lines (148 loc) · 6.52 KB
/
IsIterableContainingInOrder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package org.hamcrest.collection;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.core.IsNull;
import java.util.ArrayList;
import java.util.List;
import static java.util.Arrays.asList;
import static org.hamcrest.core.IsEqual.equalTo;
public class IsIterableContainingInOrder<E> extends TypeSafeDiagnosingMatcher<Iterable<? extends E>> {
private final List<Matcher<? super E>> matchers;
public IsIterableContainingInOrder(List<Matcher<? super E>> matchers) {
this.matchers = matchers;
}
@Override
protected boolean matchesSafely(Iterable<? extends E> iterable, Description mismatchDescription) {
final MatchSeries<E> matchSeries = new MatchSeries<E>(matchers, mismatchDescription);
for (E item : iterable) {
if (!matchSeries.matches(item)) {
return false;
}
}
return matchSeries.isFinished();
}
@Override
public void describeTo(Description description) {
description.appendText("iterable containing ").appendList("[", ", ", "]", matchers);
}
private static class MatchSeries<F> {
private final List<Matcher<? super F>> matchers;
private final Description mismatchDescription;
private int nextMatchIx = 0;
public MatchSeries(List<Matcher<? super F>> matchers, Description mismatchDescription) {
this.mismatchDescription = mismatchDescription;
if (matchers.isEmpty()) {
throw new IllegalArgumentException("Should specify at least one expected element");
}
this.matchers = matchers;
}
public boolean matches(F item) {
if (matchers.size() <= nextMatchIx) {
mismatchDescription.appendText("not matched: ").appendValue(item);
return false;
}
return isMatched(item);
}
public boolean isFinished() {
if (nextMatchIx < matchers.size()) {
mismatchDescription.appendText("no item was ").appendDescriptionOf(matchers.get(nextMatchIx));
return false;
}
return true;
}
private boolean isMatched(F item) {
final Matcher<? super F> matcher = matchers.get(nextMatchIx);
if (!matcher.matches(item)) {
describeMismatch(matcher, item);
return false;
}
nextMatchIx++;
return true;
}
private void describeMismatch(Matcher<? super F> matcher, F item) {
mismatchDescription.appendText("item " + nextMatchIx + ": ");
matcher.describeMismatch(item, mismatchDescription);
}
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields a series of items, each logically equal to the
* corresponding item in the specified items. For a positive match, the examined iterable
* must be of the same length as the number of specified items.
* <p/>
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"))</pre>
*
* @param items
* the items that must equal the items provided by an examined {@link Iterable}
*/
@Factory
public static <E> Matcher<Iterable<? extends E>> contains(E... items) {
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
for (E item : items) {
matchers.add(equalTo(item));
}
return contains(matchers);
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields a single item that satisfies the specified matcher.
* For a positive match, the examined iterable must only yield one item.
* <p/>
* For example:
* <pre>assertThat(Arrays.asList("foo"), contains(equalTo("foo")))</pre>
*
* @param itemMatcher
* the matcher that must be satisfied by the single item provided by an
* examined {@link Iterable}
*/
@SuppressWarnings("unchecked")
@Factory
public static <E> Matcher<Iterable<? extends E>> contains(final Matcher<? super E> itemMatcher) {
return contains(new ArrayList<Matcher<? super E>>(asList(itemMatcher)));
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields a series of items, each satisfying the corresponding
* matcher in the specified matchers. For a positive match, the examined iterable
* must be of the same length as the number of specified matchers.
* <p/>
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")))</pre>
*
* @param itemMatchers
* the matchers that must be satisfied by the items provided by an examined {@link Iterable}
*/
@Factory
public static <E> Matcher<Iterable<? extends E>> contains(Matcher<? super E>... itemMatchers) {
final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = nullSafe(itemMatchers);
return contains(nullSafeWithExplicitTypeMatchers);
}
/**
* Creates a matcher for {@link Iterable}s that matches when a single pass over the
* examined {@link Iterable} yields a series of items, each satisfying the corresponding
* matcher in the specified list of matchers. For a positive match, the examined iterable
* must be of the same length as the specified list of matchers.
* <p/>
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre>
*
* @param itemMatchers
* a list of matchers, each of which must be satisfied by the corresponding item provided by
* an examined {@link Iterable}
*/
@Factory
public static <E> Matcher<Iterable<? extends E>> contains(List<Matcher<? super E>> itemMatchers) {
return new IsIterableContainingInOrder<E>(itemMatchers);
}
@SuppressWarnings("unchecked")
private static <E> List<Matcher<? super E>> nullSafe(Matcher<? super E>[] itemMatchers) {
final List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(itemMatchers.length);
for (final Matcher<? super E> itemMatcher : itemMatchers) {
matchers.add((Matcher<? super E>) (itemMatcher == null ? IsNull.nullValue() : itemMatcher));
}
return matchers;
}
}