-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathRightMatcher.java
More file actions
43 lines (34 loc) · 1.38 KB
/
RightMatcher.java
File metadata and controls
43 lines (34 loc) · 1.38 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 RightMatcher<L, R> extends TypeSafeMatcher<Either<L, R>> {
private final Matcher<R> rMatcher;
private RightMatcher(Matcher<R> rMatcher) {
this.rMatcher = rMatcher;
}
@Override
protected boolean matchesSafely(Either<L, R> actual) {
return actual.match(constantly(false), rMatcher::matches);
}
@Override
public void describeTo(Description description) {
description.appendText("Right value of ");
rMatcher.describeTo(description);
}
@Override
protected void describeMismatchSafely(Either<L, R> item, Description mismatchDescription) {
mismatchDescription.appendText("was ");
item.peek(l -> io(() -> mismatchDescription.appendValue(item)),
r -> io(() -> {
mismatchDescription.appendText("Right value of ");
rMatcher.describeMismatch(r, mismatchDescription);
}));
}
public static <L, R> RightMatcher<L, R> isRightThat(Matcher<R> rMatcher) {
return new RightMatcher<>(rMatcher);
}
}