-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathLeftMatcher.java
More file actions
43 lines (34 loc) · 1.37 KB
/
LeftMatcher.java
File metadata and controls
43 lines (34 loc) · 1.37 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
package testsupport.matchers;
import com.jnape.palatable.lambda.adt.Either;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
import static com.jnape.palatable.lambda.io.IO.io;
public final class LeftMatcher<L, R> extends TypeSafeMatcher<Either<L, R>> {
private final Matcher<L> lMatcher;
private LeftMatcher(Matcher<L> lMatcher) {
this.lMatcher = lMatcher;
}
@Override
protected boolean matchesSafely(Either<L, R> actual) {
return actual.match(lMatcher::matches, constantly(false));
}
@Override
public void describeTo(Description description) {
description.appendText("Left value of ");
lMatcher.describeTo(description);
}
@Override
protected void describeMismatchSafely(Either<L, R> item, Description mismatchDescription) {
mismatchDescription.appendText("was ");
item.peek(l -> io(() -> {
mismatchDescription.appendText("Left value of ");
lMatcher.describeMismatch(l, mismatchDescription);
}),
r -> io(() -> mismatchDescription.appendValue(item)));
}
public static <L, R> LeftMatcher<L, R> isLeftThat(Matcher<L> lMatcher) {
return new LeftMatcher<>(lMatcher);
}
}