forked from palatable/lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOMatcher.java
More file actions
61 lines (49 loc) · 2.35 KB
/
IOMatcher.java
File metadata and controls
61 lines (49 loc) · 2.35 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
package testsupport.matchers;
import com.jnape.palatable.lambda.adt.Either;
import com.jnape.palatable.lambda.io.IO;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import java.util.concurrent.atomic.AtomicReference;
import static com.jnape.palatable.lambda.adt.Either.left;
import static com.jnape.palatable.lambda.adt.Either.right;
import static com.jnape.palatable.lambda.io.IO.io;
import static org.hamcrest.Matchers.anything;
public final class IOMatcher<A> extends TypeSafeMatcher<IO<A>> {
private final Either<Matcher<? super Throwable>, Matcher<? super A>> matcher;
private final AtomicReference<Either<Throwable, A>> resultRef;
private IOMatcher(Either<Matcher<? super Throwable>, Matcher<? super A>> matcher) {
this.matcher = matcher;
resultRef = new AtomicReference<>();
}
@Override
protected boolean matchesSafely(IO<A> io) {
Either<Throwable, A> res = io.safe().unsafePerformIO();
resultRef.set(res);
return res.match(t -> matcher.match(tMatcher -> tMatcher.matches(t),
aMatcher -> false),
a -> matcher.match(tMatcher -> false,
aMatcher -> aMatcher.matches(a)));
}
@Override
public void describeTo(Description description) {
matcher.match(m -> io(() -> m.describeTo(description.appendText("IO throwing exception matching "))),
m -> io(() -> m.describeTo(description.appendText("IO yielding value matching "))))
.unsafePerformIO();
}
@Override
protected void describeMismatchSafely(IO<A> item, Description mismatchDescription) {
resultRef.get().match(t -> io(() -> mismatchDescription.appendText("IO threw " + t)),
a -> io(() -> mismatchDescription.appendText("IO yielded value " + a)))
.unsafePerformIO();
}
public static <A> IOMatcher<A> yieldsValue(Matcher<? super A> matcher) {
return new IOMatcher<>(right(matcher));
}
public static <A> IOMatcher<A> completesNormally() {
return yieldsValue(anything());
}
public static <A> IOMatcher<A> throwsException(Matcher<? super Throwable> throwableMatcher) {
return new IOMatcher<>(left(throwableMatcher));
}
}