|
| 1 | +package testsupport.assertion; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.adt.Maybe; |
| 4 | +import com.jnape.palatable.lambda.adt.hlist.Tuple2; |
| 5 | +import com.jnape.palatable.lambda.functions.Fn2; |
| 6 | +import com.jnape.palatable.lambda.functions.builtin.fn2.Map; |
| 7 | +import com.jnape.palatable.lambda.io.IO; |
| 8 | +import com.jnape.palatable.lambda.monoid.builtin.Present; |
| 9 | +import com.jnape.palatable.lambda.optics.Prism; |
| 10 | + |
| 11 | +import java.util.Objects; |
| 12 | + |
| 13 | +import static com.jnape.palatable.lambda.adt.Either.left; |
| 14 | +import static com.jnape.palatable.lambda.adt.Maybe.just; |
| 15 | +import static com.jnape.palatable.lambda.adt.Maybe.nothing; |
| 16 | +import static com.jnape.palatable.lambda.functions.Fn2.fn2; |
| 17 | +import static com.jnape.palatable.lambda.functions.builtin.fn1.CatMaybes.catMaybes; |
| 18 | +import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly; |
| 19 | +import static com.jnape.palatable.lambda.functions.builtin.fn2.CartesianProduct.cartesianProduct; |
| 20 | +import static com.jnape.palatable.lambda.functions.builtin.fn2.Into.into; |
| 21 | +import static com.jnape.palatable.lambda.functions.builtin.fn2.ReduceLeft.reduceLeft; |
| 22 | +import static com.jnape.palatable.lambda.optics.functions.Matching.matching; |
| 23 | +import static com.jnape.palatable.lambda.optics.functions.Pre.pre; |
| 24 | +import static com.jnape.palatable.lambda.optics.functions.Re.re; |
| 25 | +import static com.jnape.palatable.lambda.optics.functions.View.view; |
| 26 | +import static java.lang.String.format; |
| 27 | +import static java.lang.String.join; |
| 28 | +import static java.util.Arrays.asList; |
| 29 | + |
| 30 | +public final class PrismAssert { |
| 31 | + |
| 32 | + public static <S, A> void assertPrismLawfulness(Prism<S, S, A, A> prism, |
| 33 | + Iterable<S> ss, |
| 34 | + Iterable<A> bs) { |
| 35 | + Iterable<Tuple2<S, A>> cases = cartesianProduct(ss, bs); |
| 36 | + Present.<String>present((x, y) -> join("\n\n", x, y)) |
| 37 | + .reduceLeft(asList(falsify("The result of a review can always be successfully previewed:", |
| 38 | + (s, b) -> view(pre(prism), view(re(prism), b)), (s, b) -> just(b), cases), |
| 39 | + falsify("If I can preview a value from an input, I can review the input to the value", |
| 40 | + (s, b) -> new PrismResult<>(view(pre(prism), s).fmap(constantly(s))), |
| 41 | + (s, b) -> new PrismResult<>(just(view(re(prism), b))), cases), |
| 42 | + falsify("A non-match result can always be converted back to an input", |
| 43 | + (s, b) -> new PrismResult<>(matching(prism, s).projectA().fmap(matching(prism))), |
| 44 | + (s, b) -> new PrismResult<>(just(left(s))), cases))) |
| 45 | + .peek(failures -> IO.throwing(new AssertionError("Lens law failures\n\n" + failures))); |
| 46 | + } |
| 47 | + |
| 48 | + private static <S, A, X> Maybe<String> falsify(String label, Fn2<S, A, X> l, Fn2<S, A, X> r, |
| 49 | + Iterable<Tuple2<S, A>> cases) { |
| 50 | + return Map.<Tuple2<S, A>, Maybe<String>>map(into((s, b) -> { |
| 51 | + X x = l.apply(s, b); |
| 52 | + X y = r.apply(s, b); |
| 53 | + return Objects.equals(x, y) ? nothing() : just(format("S <%s>, B <%s> (%s != %s)", s, b, x, y)); |
| 54 | + })) |
| 55 | + .fmap(catMaybes()) |
| 56 | + .fmap(reduceLeft((x, y) -> x + "\n\t - " + y)) |
| 57 | + .fmap(maybeFailures -> maybeFailures.fmap(failures -> "\"" + label + "\" failed for the following cases:\n\n\t - " + failures)) |
| 58 | + .apply(cases); |
| 59 | + } |
| 60 | + |
| 61 | + private static final class PrismResult<S> { |
| 62 | + private final Maybe<S> maybeS; |
| 63 | + |
| 64 | + private PrismResult(Maybe<S> maybeS) { |
| 65 | + this.maybeS = maybeS; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean equals(Object other) { |
| 70 | + if (other instanceof PrismResult) { |
| 71 | + return maybeS.zip(((PrismResult<?>) other).maybeS.fmap(fn2(Objects::equals))).orElse(true); |
| 72 | + } |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public int hashCode() { |
| 78 | + return Objects.hash(maybeS); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String toString() { |
| 83 | + return maybeS.toString(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments