-
Notifications
You must be signed in to change notification settings - Fork 376
/
AllOf.java
142 lines (128 loc) · 5 KB
/
AllOf.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
package org.hamcrest.core;
import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Calculates the logical conjunction of multiple matchers. Evaluation is shortcut, so
* subsequent matchers are not called if an earlier matcher returns <code>false</code>.
*/
public class AllOf<T> extends DiagnosingMatcher<T> {
private final Iterable<Matcher<? super T>> matchers;
public AllOf(Iterable<Matcher<? super T>> matchers) {
this.matchers = matchers;
}
@Override
public boolean matches(Object o, Description mismatch) {
for (Matcher<? super T> matcher : matchers) {
if (!matcher.matches(o)) {
mismatch.appendDescriptionOf(matcher).appendText(" ");
matcher.describeMismatch(o, mismatch);
return false;
}
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendList("(", " " + "and" + " ", ")", matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* <p/>
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
@Factory
public static <T> Matcher<T> allOf(Iterable<Matcher<? super T>> matchers) {
return new AllOf<T>(matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* <p/>
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
@Factory
public static <T> Matcher<T> allOf(Matcher<? super T>... matchers) {
return allOf(Arrays.asList(matchers));
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* <p/>
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
@Factory
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second) {
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(2);
matchers.add(first);
matchers.add(second);
return allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* <p/>
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
@Factory
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third) {
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(3);
matchers.add(first);
matchers.add(second);
matchers.add(third);
return allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* <p/>
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
@Factory
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(4);
matchers.add(first);
matchers.add(second);
matchers.add(third);
matchers.add(fourth);
return allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* <p/>
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
@Factory
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(5);
matchers.add(first);
matchers.add(second);
matchers.add(third);
matchers.add(fourth);
matchers.add(fifth);
return allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
* <p/>
* For example:
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
*/
@Factory
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth) {
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(6);
matchers.add(first);
matchers.add(second);
matchers.add(third);
matchers.add(fourth);
matchers.add(fifth);
matchers.add(sixth);
return allOf(matchers);
}
}