blob: 074e449540dfc53662b496a4b78fd3308a24304b (
plain)
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
|
package org.zwobble.couscous.tests.util;
import org.hamcrest.*;
import java.util.List;
import java.util.function.Function;
import static org.hamcrest.Matchers.equalTo;
import static org.zwobble.couscous.util.ExtraLists.eagerMap;
public class ExtraMatchers {
public static <T, U> Matcher<T> isInstance(Class<U> type, Matcher<? super U> downcastMatcher) {
return new DiagnosingMatcher<T>() {
@Override
protected boolean matches(Object item, Description mismatch) {
if (null == item) {
mismatch.appendText("null");
return false;
}
if (!type.isInstance(item)) {
mismatch.appendValue(item).appendText(" is a " + item.getClass().getName());
return false;
}
if (!downcastMatcher.matches(item)) {
downcastMatcher.describeMismatch(item, mismatch);
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
description
.appendText(type.getSimpleName())
.appendText(" with ");
downcastMatcher.describeTo(description);
}
};
}
public static <T, U> Matcher<T> hasFeature(
String name,
Function<? super T, U> extract,
Matcher<? super U> subMatcher) {
return new FeatureMatcher<T, U>(subMatcher, name, name) {
@Override
protected U featureValueOf(T actual) {
return extract.apply(actual);
}
};
}
public static <T> Matcher<Iterable<? extends T>> containsExactly(List<Matcher<? super T>> matchers) {
if (matchers.isEmpty()) {
return Matchers.emptyIterable();
} else {
return Matchers.contains(matchers);
}
}
public static <T> Matcher<Iterable<? extends T>> containsExactElements(List<T> values) {
return containsExactly(eagerMap(values, value -> equalTo(value)));
}
}
|