|
| 1 | +package com.aol.cyclops.javaslang; |
| 2 | + |
| 3 | +import com.aol.cyclops.control.For; |
| 4 | +import javaslang.control.Either; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import static org.hamcrest.Matchers.equalTo; |
| 8 | +import static org.junit.Assert.assertThat; |
| 9 | + |
| 10 | +public class EitherComprehensionTest { |
| 11 | + |
| 12 | + /* |
| 13 | + def prepareCappuccino(): Either[Exception, String] = for { |
| 14 | + ground <- grind("arabica beans") |
| 15 | + water <- heatWater(Water(25)) |
| 16 | + espresso <- brew(ground, water) |
| 17 | + foam <- frothMilk("milk") |
| 18 | + } yield combine(espresso, foam) |
| 19 | + */ |
| 20 | + |
| 21 | + private static final Exception AN_EXCEPTION = new RuntimeException(); |
| 22 | + |
| 23 | + @Test |
| 24 | + public void shouldComprehendEitherExpressions() throws Exception { |
| 25 | + // when |
| 26 | + final Either<Exception, String> wrapsCappuccino = For |
| 27 | + .iterable(grind("arabica beans")) |
| 28 | + .iterable(ground -> heatWater(new Water(25))) |
| 29 | + .iterable(ground -> water -> brew(ground, water)) |
| 30 | + .iterable(ground -> water -> espresso -> frothMilk("milk")) |
| 31 | + .yield(ground -> water -> espresso -> foam -> combine(espresso, foam)) |
| 32 | + .unwrap(); |
| 33 | + |
| 34 | + // then |
| 35 | + assertThat( |
| 36 | + wrapsCappuccino.get(), |
| 37 | + equalTo("cappuccino") |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void shouldComprehendEitherExpressionsWithFailedOne() throws Exception { |
| 43 | + // when |
| 44 | + final Either<Exception, String> wrapsException = For |
| 45 | + .iterable(grind("arabica beans")) |
| 46 | + .iterable(ground -> failToHeatWater(new Water(25))) |
| 47 | + .iterable(ground -> water -> brew(ground, water)) |
| 48 | + .iterable(ground -> water -> espresso -> frothMilk("milk")) |
| 49 | + .yield(ground -> water -> espresso -> foam -> combine(espresso, foam)) |
| 50 | + .unwrap(); |
| 51 | + |
| 52 | + // then |
| 53 | + assertThat( |
| 54 | + wrapsException.getLeft(), |
| 55 | + equalTo(AN_EXCEPTION) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void shouldComprehendEitherProjectionExpressions() throws Exception { |
| 61 | + // when |
| 62 | + final Either.RightProjection<Exception, String> wrapsCappuccino = For |
| 63 | + .iterable(rightProjectionOf(grind("arabica beans"))) |
| 64 | + .iterable(ground -> rightProjectionOf(heatWater(new Water(25)))) |
| 65 | + .iterable(ground -> water -> rightProjectionOf(brew(ground, water))) |
| 66 | + .iterable(ground -> water -> espresso -> rightProjectionOf(frothMilk("milk"))) |
| 67 | + .yield(ground -> water -> espresso -> foam -> combine(espresso, foam)) |
| 68 | + .unwrap(); |
| 69 | + |
| 70 | + // then |
| 71 | + assertThat( |
| 72 | + wrapsCappuccino.get(), |
| 73 | + equalTo("cappuccino") |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void shouldComprehendEitherExpressionsMixedWithEitherProjectionExpressions() throws Exception { |
| 79 | + // when |
| 80 | + final Either<Exception, String> wrapsCappuccino = For |
| 81 | + .iterable(grind("arabica beans")) |
| 82 | + .iterable(ground -> heatWater(new Water(25))) |
| 83 | + .iterable(ground -> water -> brew(ground, water)) |
| 84 | + .iterable(ground -> water -> espresso -> rightProjectionOf(frothMilk("milk"))) |
| 85 | + .yield(ground -> water -> espresso -> foam -> combine(espresso, foam)) |
| 86 | + .unwrap(); |
| 87 | + |
| 88 | + // then |
| 89 | + assertThat( |
| 90 | + wrapsCappuccino.get(), |
| 91 | + equalTo("cappuccino") |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + Either<Exception, String> grind(String beans) { |
| 97 | + return Either.right("ground " + beans); |
| 98 | + } |
| 99 | + |
| 100 | + Either<Exception, Water> heatWater(Water water) { |
| 101 | + return Either.right(water.withTemperature(85)); |
| 102 | + } |
| 103 | + |
| 104 | + Either<Exception, Water> failToHeatWater(Water water) { |
| 105 | + return Either.left(AN_EXCEPTION); |
| 106 | + } |
| 107 | + |
| 108 | + Either<Exception, String> brew(String coffee, Water heatedWater) { |
| 109 | + return Either.right("espresso"); |
| 110 | + } |
| 111 | + |
| 112 | + Either<Exception, String> frothMilk(String milk) { |
| 113 | + return Either.right("frothed " + milk); |
| 114 | + } |
| 115 | + |
| 116 | + String combine(String espresso, String frothedMilk) { |
| 117 | + return "cappuccino"; |
| 118 | + } |
| 119 | + |
| 120 | + private static <L, R> Either.RightProjection<L, R> rightProjectionOf(Either<L, R> either) { |
| 121 | + return either.right(); |
| 122 | + } |
| 123 | +} |
0 commit comments