forked from hamcrest/JavaHamcrest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsIterableContainingParallelRunsTest.java
More file actions
169 lines (137 loc) · 5.87 KB
/
IsIterableContainingParallelRunsTest.java
File metadata and controls
169 lines (137 loc) · 5.87 KB
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 static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.hamcrest.collection.IsIterableContainingParallelRuns.containsParallelRunsOf;
import static org.hamcrest.core.IsEqual.equalTo;
import java.util.List;
import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
public class IsIterableContainingParallelRunsTest extends AbstractMatcherTest {
@Override
protected Matcher<?> createMatcher() {
return containsParallelRunsOf(1, 1, 2);
}
//
// ---- SINGLE RUN TESTS ---- (same tests cases as IsIterableContainingInRelativeOrderTest) ----
//
public void testMatchingSingleItemIterable() {
assertMatches("Single item iterable",
containsParallelRunsOf(1, 1), singletonList(1));
}
public void testMatchingMultipleItemIterable() {
assertMatches("Multiple item iterable",
containsParallelRunsOf(1, 1, 2, 3), asList(1, 2, 3));
}
public void testMatchesWithMoreElementsThanExpectedAtBeginning() {
assertMatches("More elements at beginning",
containsParallelRunsOf(1, 2, 3, 4), asList(1, 2, 3, 4));
}
public void testMatchesWithMoreElementsThanExpectedAtEnd() {
assertMatches("More elements at end",
containsParallelRunsOf(1, 1, 2, 3), asList(1, 2, 3, 4));
}
public void testMatchesWithMoreElementsThanExpectedInBetween() {
assertMatches("More elements in between",
containsParallelRunsOf(1, 1, 3), asList(1, 2, 3));
}
public void testMatchesSubSection() {
assertMatches("Sub section of iterable",
containsParallelRunsOf(1, 2, 3), asList(1, 2, 3, 4));
}
public void testMatchesWithSingleGapAndNotFirstOrLast() {
assertMatches("Sub section with single gaps without a first or last match",
containsParallelRunsOf(1, 2, 4), asList(1, 2, 3, 4, 5));
}
public void testMatchingSubSectionWithManyGaps() {
assertMatches("Sub section with many gaps iterable",
containsParallelRunsOf(1, 2, 4, 6), asList(1, 2, 3, 4, 5, 6, 7));
}
public void testDoesNotMatchWithFewerElementsThanExpected() {
List<WithValue> valueList = asList(make(1), make(2));
assertMismatchDescription("value with <3> was not found after <WithValue 2>",
containsParallelRunsOf(1, value(1), value(2), value(3)), valueList);
}
public void testDoesNotMatchIfSingleItemNotFound() {
assertMismatchDescription("value with <4> was not found",
containsParallelRunsOf(1, value(4)), singletonList(make(3)));
}
public void testDoesNotMatchIfOneOfMultipleItemsNotFound() {
assertMismatchDescription("value with <3> was not found after <WithValue 2>",
containsParallelRunsOf(1, value(1), value(2), value(3)),
asList(make(1), make(2), make(4)));
}
public void testDoesNotMatchEmptyIterable() {
assertMismatchDescription("value with <4> was not found",
containsParallelRunsOf(1, value(4)), emptyList());
}
public void testHasAReadableDescription() {
assertDescription(
"iterable containing [<1>, <2>] in relative order",
containsParallelRunsOf(1, 1, 2));
}
//
// ---- MULTIPLE PARALLEL RUN TESTS ------------------------------------------------------------
//
public void testMultiMatchesWithoutUnexpectedElements() {
assertMatches("Multiple runs without unexpected elements",
containsParallelRunsOf(2, 1, 2, 3), asList(1, 1, 2, 3, 2, 3));
}
public void testMultiMatchesWithRepeatedElements() {
assertMatches("Multiple runs with repeated elements",
containsParallelRunsOf(2, 1, 2, 1), asList(1, 2, 1, 1, 2, 1));
}
public void testMultiMatchesWithGaps() {
assertMatches("Multiple runs with gaps",
containsParallelRunsOf(4, 1), asList(2, 1, 2, 1, 1, 2, 1, 2));
}
public void testMultiDoesNotMatchIfSingleItemNotFound() {
assertMismatchDescription("value with <2> was not found after <WithValue 1> in run <2>",
containsParallelRunsOf(2, value(1), value(2)),
asList(make(1), make(2), make(1)));
}
public void testMultiDoesNotMatchIfSingleItemNotFoundAtStart() {
assertMismatchDescription("value with <1> was not found in run <2>",
containsParallelRunsOf(2, value(1), value(2)),
asList(make(1), make(2)));
}
public void testMultiDoesNotMatchAndReportsAllMismatchedRuns() {
assertMismatchDescription("<3> was not found after <2> in run <1>; and "
+ "<2> was not found after <1> in run <2>; and "
+ "<1> was not found in run <3>",
containsParallelRunsOf(3, 1, 2, 3),
asList(1, 1, 2));
}
public void testMultiDoesNotMatchEmptyIterable() {
assertMismatchDescription("value with <4> was not found in run <1>; and "
+ "value with <4> was not found in run <2>",
containsParallelRunsOf(2, value(4)), emptyList());
}
public void testMultiHasAReadableDescription() {
assertDescription(
"iterable containing <2> parallel runs of [<1>, <2>] in relative order",
containsParallelRunsOf(2, 1, 2));
assertDescription(
"iterable containing <901> parallel runs of [<1>, <2>] in relative order",
containsParallelRunsOf(901, 1, 2));
}
// ---- TEST UTILITIES -------------------------------------------------------------------------
public static class WithValue {
private final int value;
public WithValue(int value) { this.value = value; }
public int getValue() { return value; }
@Override public String toString() { return "WithValue " + value; }
}
public static WithValue make(int value) {
return new WithValue(value);
}
public static Matcher<WithValue> value(int value) {
return new FeatureMatcher<WithValue, Integer>(equalTo(value), "value with", "value") {
@Override
protected Integer featureValueOf(WithValue actual) {
return actual.getValue();
}
};
}
}