Esempio 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"
Esempio 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")
Esempio 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"
Esempio n. 4
0
def test_match_types_complex() -> None:
    case_1 = Environment[str]().map(lambda s: s + "1")
    case_2 = Environment[str]().map(lambda s: s + "2")
    case_3 = Environment[str]().map(lambda s: s + "3")

    with pytest.raises(TypeMatchException):
        unsafe_run(Environment[int]().match_types().provide(42))

    # Obviously grotesque use of `match_types`...but we should understand
    # its semantics.
    assert unsafe_run(Environment[str]().match_types().at_type(
        str, case_1).at_type(str, case_2).map(lambda s: s + "foo").at_type(
            str, case_3).provide("")) == "1foo"

    with pytest.raises(TypeMatchException):
        unsafe_run(Environment[dict]().match_types().at_type(
            str, case_1).at_type(str, case_2).map(lambda s: s + "foo").at_type(
                str, case_3).provide({'ka': 'boom'}))

    with pytest.raises(TypeMatchException):
        unsafe_run(Environment[str]().match_types().provide(""))

    with pytest.raises(TypeMatchException):
        unsafe_run(Environment[str]().match_types().at_type(
            int, Environment[int]()).provide(""))

    assert unsafe_run(Environment[str]().require(
        lambda s: len(s) > 5, lambda s: f"String '{s}' is too short.").at_type(
            str, case_1).either().provide("foo")) == Either.left(
                "String 'foo' is too short.")
Esempio n. 5
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")
Esempio n. 6
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
Esempio n. 7
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)
Esempio n. 8
0
def test_fold_right() -> None:
    x = Either.right("hello")
    assert x.fold(lambda x: x + 1, lambda y: y + "!") == "hello!"
Esempio n. 9
0
def test_match_right() -> None:
    x = Either.right("hello")
    assert x.match(lambda x: x.value + 1, lambda y: y.value + "!") == "hello!"
Esempio n. 10
0
def test_from_union_fails() -> None:
    with pytest.raises(TypeError):
        Either.from_union([1, 2, 3], int, str)  # type: ignore
Esempio n. 11
0
def test_raise_errors_regular_value() -> None:
    with pytest.raises(EitherException) as exc:
        Either.left(42).raise_errors()
    assert exc.value == EitherException(value=42)
Esempio n. 12
0
def test_flatten_right() -> None:
    x: Either[str, Either[str, int]] = Either.right(Either.right(42))
    assert x.flatten() == Right(42)
Esempio n. 13
0
def test_flatten_left() -> None:
    x: Either[str, Either[str, int]] = Either.left("asdf")
    assert x.flatten() == Left("asdf")
Esempio n. 14
0
 def _f(i: int) -> Either[Exception, str]:
     return Either.right(str(i))
Esempio n. 15
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))])
Esempio n. 16
0
 def _f(r: R) -> Either[Union[E, X], AA]:
     try:
         return self._run(r)
     except exc as e:
         return Either.left(e)
Esempio n. 17
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)
Esempio n. 18
0
def test_raise_errors_no_errors() -> None:
    assert Either.right("hello").raise_errors() == Right("hello")
Esempio n. 19
0
def test_match_left() -> None:
    x: Either[int, str] = Either.left(42)
    assert x.match(lambda x: x.value + 1, lambda y: y.value + "!") == 43
Esempio n. 20
0
def test_raise_errors_exception_on_left() -> None:
    with pytest.raises(BippyException) as exc:
        Either.left(BippyException("Oh noes!")).raise_errors()
    assert str(exc.value) == "Oh noes!"
Esempio n. 21
0
def test_fold_left() -> None:
    x: Either[int, str] = Either.left(42)
    assert x.fold(lambda x: x + 1, lambda y: y + "!") == 43
Esempio n. 22
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!"
Esempio n. 23
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()
Esempio n. 24
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)