* Code for BAEL-1040

* Amended code for BAEL-1040

* Amended tests for BAEL-1040
This commit is contained in:
Alessio Stalla 2018-04-04 01:44:06 +02:00 committed by maibin
parent a81bd92cef
commit 266c285533
1 changed files with 43 additions and 7 deletions

View File

@ -5,8 +5,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.*;
import java.util.function.Function;
import static org.junit.Assert.*;
@ -14,10 +13,6 @@ import static io.atlassian.fugue.Unit.Unit;
public class FugueTest {
@Before
public void setup() {
}
@Test
public void whenSome_thenDefined() {
Option<String> some = Option.some("value");
@ -47,7 +42,11 @@ public class FugueTest {
@Test
public void whenNullOption_thenSome() {
Option<Object> some = Option.some("value").map(x -> null);
Option<String> some = Option.some("value") .map(String::toUpperCase);
assertEquals("VALUE", some.get());
some = some.map(x -> null);
assertNull(some.get());
some.forEach(Assert::assertNull);
@ -74,6 +73,33 @@ public class FugueTest {
}
@Test
public void whenOption_thenIterable() {
Option<String> some = Option.some("value");
Iterable<String> strings = Iterables.concat(some, Arrays.asList("a", "b", "c"));
List<String> stringList = new ArrayList<>();
Iterables.addAll(stringList, strings);
assertEquals(4, stringList.size());
}
@Test
public void whenOption_thenStream() {
assertEquals(0, Option.none().toStream().count());
assertEquals(1, Option.some("value").toStream().count());
}
@Test
public void whenLift_thenPartialFunction() {
Function<Integer, Integer> f = (Integer x) -> x > 0 ? x + 1 : null;
Function<Option<Integer>, Option<Integer>> lifted = Options.lift(f);
assertEquals(2, (long) lifted.apply(Option.some(1)).get());
assertTrue(lifted.apply(Option.none()).isEmpty());
assertEquals(null, lifted.apply(Option.some(0)).get());
}
@Test
public void whenLeft_thenEither() {
Either<Integer, String> right = Either.right("value");
Either<Integer, String> left = Either.left(-1);
@ -92,6 +118,8 @@ public class FugueTest {
assertTrue(either.isRight());
assertEquals("value", either.right().get());
either.right().forEach(x -> assertEquals("value", x));
}
private static String decodeSQLErrorCode(Integer x) {
@ -193,4 +221,12 @@ public class FugueTest {
return Unit();
}
@Test
public void whenPair_thenLeftAndRight() {
Pair<Integer, String> pair = Pair.pair(1, "a");
assertEquals(1, (int) pair.left());
assertEquals("a", pair.right());
}
}