@Test public void shouldBimapRight() { final Either<Integer, String> actual = Either.<Integer, String>right("1").bimap(i -> i + 1, s -> s + "1"); final Either<Integer, String> expected = Either.right("11"); assertThat(actual).isEqualTo(expected); }
@Test public void shouldFlatMapRightProjectionOfLeftOnRightProjectionOfRight() { final Either<String, String> good = Either.right("good"); final Either<String, String> bad = Either.left("bad"); final RightProjection<String, Tuple2<String, String>> actual = good.right().flatMap(g -> bad.right().map(b -> Tuple.of(g, b))); assertThat(actual.toEither()).isEqualTo(Either.left("bad")); }
@Test public void shouldPeekOnRightProjectionOfRight() { final List<Integer> actual = new ArrayList<>(); final Either<String, Integer> testee = Either.<String, Integer>right(1).right().peek(actual::add).toEither(); assertThat(actual).isEqualTo(Collections.singletonList(1)); assertThat(testee).isEqualTo(Either.right(1)); }
@Test public void shouldPeekOnLeftProjectionOfRight() { final List<Integer> actual = new ArrayList<>(); final Either<Integer, String> testee = Either.<Integer, String>right("1").left().peek(actual::add).toEither(); assertThat(actual.isEmpty()).isTrue(); assertThat(testee).isEqualTo(Either.right("1")); }
@Test public void shouldFlatMapOnLeftProjectionOfRight() { final Either<Integer, String> actual = Either.<Integer, String>right("1") .left() .flatMap(i -> Either.<Integer, String>left(i + 1).left()) .toEither(); assertThat(actual).isEqualTo(Either.right("1")); }
@Test public void shouldFlatMapOnRightProjectionOfRight() { final Either<String, Integer> actual = Either.<String, Integer>right(1) .right() .flatMap(i -> Either.<String, Integer>right(i + 1).right()) .toEither(); assertThat(actual).isEqualTo(Either.right(2)); }
@Test public void right_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); Option<Integer> actual = target.right(); assertThat(actual.isDefined(), is(false)); assertThat(actual.getOrNull(), is(nullValue())); }
@Test public void transformingJustYieldsRight() { final Function<Optional<O>, Either<O, O>> maybeToEither = new OptionalToEither<O, O>(new ConstantSupplier<O>(O.ANOTHER)); final Either<O, O> got = maybeToEither.apply(Optional.<O>of(O.ONE)); Assert.assertEquals(Either.right(O.ONE), got); }
@Test public void swap_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); Either<Integer, String> swapped = target.swap(); Option<String> right = swapped.right(); assertThat(right.isDefined(), is(true)); assertThat(right.getOrNull(), is(equalTo("foo"))); }
@Test public void shouldReturnRightWhenOrElseRunOnRightProjectionOfRight() { final boolean[] actual = new boolean[] {true}; Either.<String, Integer>right(1) .right() .orElseRun( s -> { actual[0] = false; }); assertThat(actual[0]).isTrue(); }
@Test public void shouldReturnOtherWhenOrElseRunOnLeftProjectionOfRight() { final boolean[] actual = new boolean[] {false}; Either.right("1") .left() .orElseRun( s -> { actual[0] = true; }); assertThat(actual[0]).isTrue(); }
@SuppressWarnings("unchecked") @Test public void left() throws Exception { new Random() .ints(20, 0, 2) .mapToObj( i -> i == 0 ? Either.<String, Integer>left("left value (String)") : Either.<String, Integer>right(42)) .forEach( either -> either.apply( left -> System.out.println("received left value: " + left.substring(11)), right -> System.out.println( "received right value: 0x" + Integer.toHexString(right)))); }
@Test public void shouldConvertRightProjectionOfRightToEither() { final Either<Integer, String> self = Either.right("1"); assertThat(self.right().toEither()).isEqualTo(self); }
@Test public void shouldConvertRightProjectionOfRightToString() { assertThat(Either.right(1).right().toString()).isEqualTo("RightProjection(Right(1))"); }
@Test public void shouldHashRightProjectionOfRight() { assertThat(Either.right(1).right().hashCode()).isEqualTo(Objects.hashCode(Either.right(1))); }
@Test public void shouldEqualRightProjectionOfRight() { assertThat(Either.right(1).right()).isEqualTo(Either.right(1).right()); }
@Test public void shouldNotEqualRightProjectionOfRightIfObjectIsOfDifferentType() { assertThat(Either.right(1).right().equals(new Object())).isFalse(); }
@Test public void shouldBeAwareOfPropertyThatHoldsExistsOfRightProjectionOfRight() { assertThat(Either.right(1).right().exists(i -> i == 1)).isTrue(); }
@Test public void shouldReturnIteratorOfRightOfRightProjection() { assertThat((Iterator<Integer>) Either.right(1).right().iterator()).isNotNull(); }
@Test public void shouldForEachOnRightProjectionOfRight() { final List<Integer> actual = new ArrayList<>(); Either.<String, Integer>right(1).right().forEach(actual::add); assertThat(actual).isEqualTo(Collections.singletonList(1)); }
@Test public void shouldConvertRightProjectionOfRightToJavaOptional() { assertThat(Either.<Integer, String>right("1").right().toJavaOptional()) .isEqualTo(Optional.of("1")); }
@Test public void shouldFilterSomeOnRightProjectionOfRightIfPredicateMatches() { final boolean actual = Either.<String, Integer>right(1).right().filter(i -> true).toOption().isDefined(); assertThat(actual).isTrue(); }
@Test public void shouldBeAwareOfPropertyThatNotHoldsForAllOfRightProjectionOfRight() { assertThat(Either.right(1).right().forAll(i -> i == 2)).isFalse(); }
@Test public void shouldNotHoldPropertyExistsOfRightProjectionOfLeft() { assertThat(Either.right(1).left().exists(e -> true)).isFalse(); }
@Test public void shouldReturnRightWhenOrElseThrowWithFunctionOnRightProjectionOfRight() { final Integer actual = Either.<String, Integer>right(1).right().orElseThrow(s -> new RuntimeException(s)); assertThat(actual).isEqualTo(1); }
@Test public void shouldEqualRightProjectionOfRightIfObjectIsSame() { final RightProjection<?, ?> r = Either.right(1).right(); assertThat(r.equals(r)).isTrue(); }
public <T> Either<T, E> either(Supplier<T> nothing) { if (hasValue) { return Either.right(element); } return Either.left(nothing.get()); }
@Test public void shouldNotEqualRightProjectionOfRightIfObjectIsNull() { assertThat(Either.right(1).right().equals(null)).isFalse(); }
@Test // a property holds for all elements of no elements public void shouldNotHoldPropertyForAllOfRightProjectionOfLeft() { assertThat(Either.right(1).left().forAll(e -> true)).isTrue(); }
@Test public void shouldFilterNoneOnRightProjectionOfRightIfPredicateNotMatches() { assertThat(Either.<String, Integer>right(1).right().filter(i -> false)) .isEqualTo(Option.none()); }