Exemplo n.º 1
0
def test_multiple_raise_errors() -> None:
    with pytest.raises(BippyException) as exc:
        (Either.right(42).raise_errors().flat_map(
            lambda _: Either.left(BippyException("kaboom 1"))).
         flat_map(lambda _: Either.left(BippyException("kaboom 2"))).flat_map(
             lambda _: Either.left(BippyException("kaboom 3"))).raise_errors())
    assert str(exc.value) == "kaboom 1"
Exemplo n.º 2
0
def test_tap() -> None:
    x = 0

    def foo(arg: Either[int, str]) -> None:
        nonlocal x
        x = arg.map(len).fold(lambda x: x, lambda x: x)

    e: Either[int, str] = Either.left(42)
    assert e.tap(foo) == e
    assert x == 42

    e = Either.right("hello")
    assert e.tap(foo) == e
    assert x == len("hello")
Exemplo n.º 3
0
def test_display(capsys: Any) -> None:
    e: Either[int, str] = Either.left(42)
    assert e.display() == e
    captured = capsys.readouterr()
    assert captured.out == str(e) + "\n"
    assert e.display("Bippy") == e
    captured = capsys.readouterr()
    assert captured.out == "Bippy:\n" + str(e) + "\n"

    e = Either.right("hello")
    assert e.display() == e
    captured = capsys.readouterr()
    assert captured.out == str(e) + "\n"
    assert e.display("Bippy") == e
    captured = capsys.readouterr()
    assert captured.out == "Bippy:\n" + str(e) + "\n"
Exemplo n.º 4
0
def test_to_union() -> None:
    # The point of this test is to ensure that mypy's type inference works
    # properly, ergo we prefer to not parametrize it.

    def foo(x: Union[int, str]) -> bool:
        return isinstance(x, int) or isinstance(x, str)

    e: Either[int, str] = Left(42)
    assert e.to_union() == 42
    assert foo(e.to_union())

    e = Right("hello")
    assert e.to_union() == "hello"
    assert foo(e.to_union())

    # mypy should properly unify Union[NoReturn, X] for all types X.
    assert Either.left(42).to_union() + 1 == 43
    assert len(Either.right("hello").to_union()) == len("hello")
Exemplo n.º 5
0
def test_swap_left(input: A, left_type: Type[A], right_type: Type[B]) -> None:
    x: Either[A, B] = Either.left(input)
    assert x.swap().swap() == x == Either.right(input).swap()
Exemplo n.º 6
0
def test_fold_right() -> None:
    x = Either.right("hello")
    assert x.fold(lambda x: x + 1, lambda y: y + "!") == "hello!"
Exemplo n.º 7
0
def test_match_right() -> None:
    x = Either.right("hello")
    assert x.match(lambda x: x.value + 1, lambda y: y.value + "!") == "hello!"
Exemplo n.º 8
0
def test_from_union_right(value: Union[A, B], left_type: Type[A],
                          right_type: Type[B]) -> None:
    assert Either.from_union(value, right_type,
                             left_type) == Either.right(value)
Exemplo n.º 9
0
def test_either_right(input: Either[A, NoReturn], expected: Right[A]) -> None:
    x = Either.right(input)
    assert x == x.to_right() == expected

    with pytest.raises(TypeError):
        x.to_left()  # type: ignore
Exemplo n.º 10
0
def test_raise_errors_no_errors() -> None:
    assert Either.right("hello").raise_errors() == Right("hello")
Exemplo n.º 11
0
def test_raise_errors_right_flat_map() -> None:
    with pytest.raises(BippyException) as exc:
        (Either.right(42).flat_map(
            lambda _: Either.left(BippyException("Oh noes!"))).raise_errors())
    assert str(exc.value) == "Oh noes!"
Exemplo n.º 12
0
def test_flatten_right() -> None:
    x: Either[str, Either[str, int]] = Either.right(Either.right(42))
    assert x.flatten() == Right(42)
Exemplo n.º 13
0
 def _f(i: int) -> Either[Exception, str]:
     return Either.right(str(i))
Exemplo n.º 14
0
    eqr.EQ2,
    eqr.EQ3,
    eqr.EQ4,
    eqr.EQ5,
    eqr.EQ6,
    eqr.EQ7,
])
def test_do_notation(equivalence_relation: eqr.Equiv[R, E, A]) -> None:
    output_p = unsafe_run(equivalence_relation.p.either().provide(
        equivalence_relation.environment))
    output_q = unsafe_run(equivalence_relation.q.either().provide(
        equivalence_relation.environment))
    assert output_p == output_q == equivalence_relation.expected_output


@pytest.mark.parametrize("input", [Either.left(42), Either.right("hello")])
def test_from_either(input: Either[int, str]) -> None:
    assert ZIO.from_either(input)._run(()) == input


@pytest.mark.parametrize("input,expected",
                         [(ZIO.fail("oh noes"), Right(Left("oh noes"))),
                          (ZIO.succeed(42), Right(Right(42)))])
def test_either(input: ZIO[object, str, int], expected: Either[str,
                                                               int]) -> None:
    assert input.either()._run(()) == expected


@pytest.mark.parametrize("input,expected",
                         [(ZIO.succeed(Left("oh noes")), Left("oh noes")),
                          (ZIO.succeed(Right(42)), Right(42))])
Exemplo n.º 15
0
 def effect_catch(side_effect: Thunk[A],
                  exception_type: Type[X]) -> "ZIO[object, X, A]":
     a = ZIO[object, NoReturn, A](lambda _: Either.right(side_effect()))
     return a.catch(exception_type)
Exemplo n.º 16
0
 def effect(side_effect: Thunk[A]) -> "ZIO[object, Exception, A]":
     a = ZIO[object, NoReturn, A](lambda _: Either.right(side_effect()))
     return a.catch(Exception)