|
|
@@ -0,0 +1,89 @@
|
|
|
+package org.leumasjaffe.container;
|
|
|
+
|
|
|
+import static org.hamcrest.CoreMatchers.*;
|
|
|
+import static org.junit.Assert.assertThat;
|
|
|
+import static org.junit.Assert.fail;
|
|
|
+
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+public class EitherFunctionalTest {
|
|
|
+ String str;
|
|
|
+ Integer i;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setUp() {
|
|
|
+ str = "testing";
|
|
|
+ i = Integer.valueOf(500);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void consumeLeftWithRightFailFunctionPasses() {
|
|
|
+ Either<String, Integer> either = Either.ofLeft(str);
|
|
|
+ either.consume(s -> assertThat(s, is(anything())),
|
|
|
+ i -> fail());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void consumeRightWithLeftFailFunctionPasses() {
|
|
|
+ Either<String, Integer> either = Either.ofRight(i);
|
|
|
+ either.consume(s -> fail(),
|
|
|
+ i -> assertThat(i, is(anything())));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void mapWithLeftDataChangesLeftSide() {
|
|
|
+ Either<String, Integer> either = Either.ofLeft(str);
|
|
|
+ Either<Integer, String> mapped = either.map(s -> i, i -> str);
|
|
|
+ assertThat(mapped.getLeft(), is(equalTo(i)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void mapWithRightDataChangesRightSide() {
|
|
|
+ Either<String, Integer> either = Either.ofRight(i);
|
|
|
+ Either<Integer, String> mapped = either.map(s -> i, i -> str);
|
|
|
+ assertThat(mapped.getRight(), is(equalTo(str)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void mapLeftWithLeftDataChanges() {
|
|
|
+ Either<String, Integer> either = Either.ofLeft(str);
|
|
|
+ Either<Integer, Integer> mapped = either.mapLeft(s -> i);
|
|
|
+ assertThat(mapped.getLeft(), is(equalTo(i)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void mapRightWithRightDataChanges() {
|
|
|
+ Either<String, Integer> either = Either.ofRight(i);
|
|
|
+ Either<String, String> mapped = either.mapRight(i -> str);
|
|
|
+ assertThat(mapped.getRight(), is(equalTo(str)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void mapLeftWithRightDataDoesNothin() {
|
|
|
+ Either<String, Integer> either = Either.ofRight(i);
|
|
|
+ Either<Integer, Integer> mapped = either.mapLeft(s -> i);
|
|
|
+ assertThat(mapped.getRight(), is(sameInstance(i)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void mapRightWithLeftDataDoesNothin() {
|
|
|
+ Either<String, Integer> either = Either.ofLeft(str);
|
|
|
+ Either<String, String> mapped = either.mapRight(i -> str);
|
|
|
+ assertThat(mapped.getLeft(), is(sameInstance(str)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void unifyOnLeftTerminatesEitherChain() {
|
|
|
+ Either<String, Integer> either = Either.ofLeft(str);
|
|
|
+ String data = either.unify(s -> "str:" + s, i -> "int:" + i);
|
|
|
+ assertThat(data, is(equalTo("str:testing")));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void unifyOnRightTerminatesEitherChain() {
|
|
|
+ Either<String, Integer> either = Either.ofRight(i);
|
|
|
+ String data = either.unify(s -> "str:" + s, i -> "int:" + i);
|
|
|
+ assertThat(data, is(equalTo("int:500")));
|
|
|
+ }
|
|
|
+}
|