-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from slnowak/support-for-either
Fixed for comprehension for javaslang.control.Either
- Loading branch information
Showing
2 changed files
with
127 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
cyclops-javaslang/src/test/java/com/aol/cyclops/javaslang/EitherComprehensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.aol.cyclops.javaslang; | ||
|
||
import com.aol.cyclops.control.For; | ||
import javaslang.control.Either; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class EitherComprehensionTest { | ||
|
||
/* | ||
def prepareCappuccino(): Either[Exception, String] = for { | ||
ground <- grind("arabica beans") | ||
water <- heatWater(Water(25)) | ||
espresso <- brew(ground, water) | ||
foam <- frothMilk("milk") | ||
} yield combine(espresso, foam) | ||
*/ | ||
|
||
private static final Exception AN_EXCEPTION = new RuntimeException(); | ||
|
||
@Test | ||
public void shouldComprehendEitherExpressions() throws Exception { | ||
// when | ||
final Either<Exception, String> wrapsCappuccino = For | ||
.iterable(grind("arabica beans")) | ||
.iterable(ground -> heatWater(new Water(25))) | ||
.iterable(ground -> water -> brew(ground, water)) | ||
.iterable(ground -> water -> espresso -> frothMilk("milk")) | ||
.yield(ground -> water -> espresso -> foam -> combine(espresso, foam)) | ||
.unwrap(); | ||
|
||
// then | ||
assertThat( | ||
wrapsCappuccino.get(), | ||
equalTo("cappuccino") | ||
); | ||
} | ||
|
||
@Test | ||
public void shouldComprehendEitherExpressionsWithFailedOne() throws Exception { | ||
// when | ||
final Either<Exception, String> wrapsException = For | ||
.iterable(grind("arabica beans")) | ||
.iterable(ground -> failToHeatWater(new Water(25))) | ||
.iterable(ground -> water -> brew(ground, water)) | ||
.iterable(ground -> water -> espresso -> frothMilk("milk")) | ||
.yield(ground -> water -> espresso -> foam -> combine(espresso, foam)) | ||
.unwrap(); | ||
|
||
// then | ||
assertThat( | ||
wrapsException.getLeft(), | ||
equalTo(AN_EXCEPTION) | ||
); | ||
} | ||
|
||
@Test | ||
public void shouldComprehendEitherProjectionExpressions() throws Exception { | ||
// when | ||
final Either.RightProjection<Exception, String> wrapsCappuccino = For | ||
.iterable(rightProjectionOf(grind("arabica beans"))) | ||
.iterable(ground -> rightProjectionOf(heatWater(new Water(25)))) | ||
.iterable(ground -> water -> rightProjectionOf(brew(ground, water))) | ||
.iterable(ground -> water -> espresso -> rightProjectionOf(frothMilk("milk"))) | ||
.yield(ground -> water -> espresso -> foam -> combine(espresso, foam)) | ||
.unwrap(); | ||
|
||
// then | ||
assertThat( | ||
wrapsCappuccino.get(), | ||
equalTo("cappuccino") | ||
); | ||
} | ||
|
||
@Test | ||
public void shouldComprehendEitherExpressionsMixedWithEitherProjectionExpressions() throws Exception { | ||
// when | ||
final Either<Exception, String> wrapsCappuccino = For | ||
.iterable(grind("arabica beans")) | ||
.iterable(ground -> heatWater(new Water(25))) | ||
.iterable(ground -> water -> brew(ground, water)) | ||
.iterable(ground -> water -> espresso -> rightProjectionOf(frothMilk("milk"))) | ||
.yield(ground -> water -> espresso -> foam -> combine(espresso, foam)) | ||
.unwrap(); | ||
|
||
// then | ||
assertThat( | ||
wrapsCappuccino.get(), | ||
equalTo("cappuccino") | ||
); | ||
} | ||
|
||
|
||
Either<Exception, String> grind(String beans) { | ||
return Either.right("ground " + beans); | ||
} | ||
|
||
Either<Exception, Water> heatWater(Water water) { | ||
return Either.right(water.withTemperature(85)); | ||
} | ||
|
||
Either<Exception, Water> failToHeatWater(Water water) { | ||
return Either.left(AN_EXCEPTION); | ||
} | ||
|
||
Either<Exception, String> brew(String coffee, Water heatedWater) { | ||
return Either.right("espresso"); | ||
} | ||
|
||
Either<Exception, String> frothMilk(String milk) { | ||
return Either.right("frothed " + milk); | ||
} | ||
|
||
String combine(String espresso, String frothedMilk) { | ||
return "cappuccino"; | ||
} | ||
|
||
private static <L, R> Either.RightProjection<L, R> rightProjectionOf(Either<L, R> either) { | ||
return either.right(); | ||
} | ||
} |