1010
1111import static com .jnape .palatable .lambda .adt .Either .left ;
1212import static com .jnape .palatable .lambda .adt .Either .right ;
13+ import static com .jnape .palatable .lambda .io .IO .io ;
1314import static org .hamcrest .Matchers .anything ;
1415
1516public final class IOMatcher <A > extends TypeSafeMatcher <IO <A >> {
@@ -26,24 +27,35 @@ private IOMatcher(Either<Matcher<? super Throwable>, Matcher<? super A>> matcher
2627 protected boolean matchesSafely (IO <A > io ) {
2728 Either <Throwable , A > res = io .safe ().unsafePerformIO ();
2829 resultRef .set (res );
29- return res .match (t -> matcher .match (tMatcher -> tMatcher .matches (t ), aMatcher -> false ),
30- a -> matcher .match (tMatcher -> false , aMatcher -> aMatcher .matches (a )));
30+ return res .match (t -> matcher .match (tMatcher -> tMatcher .matches (t ),
31+ aMatcher -> false ),
32+ a -> matcher .match (tMatcher -> false ,
33+ aMatcher -> aMatcher .matches (a )));
3134 }
3235
3336 @ Override
3437 public void describeTo (Description description ) {
35- throw new UnsupportedOperationException ();
38+ matcher .match (m -> io (() -> m .describeTo (description .appendText ("IO throwing exception matching " ))),
39+ m -> io (() -> m .describeTo (description .appendText ("IO yielding value matching " ))))
40+ .unsafePerformIO ();
3641 }
3742
38- public static <A > IOMatcher <A > isIOThat (Matcher <? super A > matcher ) {
43+ @ Override
44+ protected void describeMismatchSafely (IO <A > item , Description mismatchDescription ) {
45+ resultRef .get ().match (t -> io (() -> mismatchDescription .appendText ("IO threw " + t )),
46+ a -> io (() -> mismatchDescription .appendText ("IO yielded value " + a )))
47+ .unsafePerformIO ();
48+ }
49+
50+ public static <A > IOMatcher <A > yieldsValue (Matcher <? super A > matcher ) {
3951 return new IOMatcher <>(right (matcher ));
4052 }
4153
42- public static <A > IOMatcher <A > isIOThatCompletesNormally () {
43- return isIOThat (anything ());
54+ public static <A > IOMatcher <A > completesNormally () {
55+ return yieldsValue (anything ());
4456 }
4557
46- public static <A > IOMatcher <A > isIOThatThrows (Matcher <? super Throwable > throwableMatcher ) {
58+ public static <A > IOMatcher <A > throwsException (Matcher <? super Throwable > throwableMatcher ) {
4759 return new IOMatcher <>(left (throwableMatcher ));
4860 }
4961}
0 commit comments