Beispiel #1
0
def test_zio_map_1() -> None:
    count = 0

    def _impure_function(x: int) -> int:
        nonlocal count
        count += 1
        return x + 1

    assert (ZIO.succeed(100).map(_impure_function).map(_impure_function).map(
        _impure_function).map(lambda x: f"The result is: {x}")._run(
            ())) == Right("The result is: 103")
    assert count == 3
Beispiel #2
0
def test_zio_effect_catch_1() -> None:
    x: Optional[int] = None

    def _impure_function() -> int:
        nonlocal x
        x = 100
        return 42

    program = ZIO.effect_catch(_impure_function, Bippy)
    assert x is None
    assert program._run(()) == Right(42)
    assert x == 100
Beispiel #3
0
def test_zio_flatten_1() -> None:
    count = 0

    def _impure_function(x: int) -> ZIO[object, NoReturn, int]:
        nonlocal count
        count += 1
        return ZIO.succeed(x + 1)

    assert (ZIO.succeed(100).flat_map(_impure_function).flat_map(
        _impure_function).flat_map(_impure_function).map(
            lambda x: ZIO.succeed(f"The result is: {x}")).flatten()._run(
                ())) == Right("The result is: 103")
    assert count == 3
Beispiel #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")
Beispiel #5
0
def test_zio_either_2() -> None:
    assert ZIO.fail("a").either()._run(()) == Right(Left("a"))
Beispiel #6
0
def test_zio_or_die_1() -> None:
    assert ZIO.succeed(42).or_die()._run(()) == Right(42)
Beispiel #7
0
 def succeed(a: AA) -> "ZIO[object, NoReturn, AA]":
     return ZIO(lambda _: Right(a))
Beispiel #8
0
def test_zio_either_1() -> None:
    assert ZIO.succeed("a").either()._run(()) == Right(Right("a"))
Beispiel #9
0

@pytest.mark.parametrize("input,expected", [
    (42, Left(42)),
    ("asdf", Left("asdf")),
])
def test_either_left(input: Either[A, NoReturn], expected: Left[A]) -> None:
    x = Either.left(input)
    assert x == x.to_left() == expected

    with pytest.raises(TypeError):
        x.to_right()  # type: ignore


@pytest.mark.parametrize("input,expected", [
    (42, Right(42)),
    ("asdf", Right("asdf")),
])
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


@pytest.mark.parametrize("value,left_type,right_type", [(42, int, str),
                                                        ("asdf", str, int)])
def test_from_union_left(value: Union[A, B], left_type: Type[A],
                         right_type: Type[B]) -> None:
    assert Either.from_union(value, left_type,
Beispiel #10
0
def test_raise_errors_no_errors() -> None:
    assert Either.right("hello").raise_errors() == Right("hello")
Beispiel #11
0
def test_zio_provide() -> None:
    assert (ZIO(Right[int]).map(lambda x: x + 1).map(lambda x: x + 1).map(
        lambda x: x + 1).provide(100)._run(())) == Right(103)
Beispiel #12
0
def test_zio_succeed() -> None:
    assert ZIO.succeed(42)._run(()) == Right(42)
Beispiel #13
0
def test_zio_access() -> None:
    accessor: Callable[[str], int] = len
    assert ZIO.access(accessor)._run("hello") == Right(5)
Beispiel #14
0
def test_zio_access_m_1() -> None:
    accessor: Callable[[str], ZIO[object, NoReturn,
                                  int]] = lambda s: ZIO.succeed(len(s))
    assert ZIO.access_m(accessor)._run("hello") == Right(5)
Beispiel #15
0
def test_environment_1() -> None:
    assert Environment[int]()._run(42) == Right(42)


def test_environment_2() -> None:
    assert Environment[int]().provide(42)._run(()) == Right(42)


def test_environment_3() -> None:
    assert Environment[int]().provide(42).provide("asdf")._run(()) == Right(42)


@pytest.mark.parametrize(
    "program,environment,expected_result",
    [
        (ZIO.succeed(42), None, Right(42)),
        (ZIO.fail(Bippy()), None, Left(Bippy)),
        (ZIO.effect(lambda: 42), None, Right(42)),
        (ZIO.effect(lambda: _raise(Bippy())), None,
         Left(Bippy)),  # type: ignore
        (ZIO.effect_catch(lambda: 42, Bippy), None, Right(42)),
        (ZIO.effect_catch(lambda: _raise(Bippy()),
                          Bippy), None, Left(Bippy)),  # type: ignore
        (ZIO.effect_catch(lambda: _raise(Bippy()),
                          NotBippy), None, Left(Bippy)),  # type: ignore
        (ZIO.succeed(42).catch(Bippy), None, Right(42)),
        (ZIO.access(len), "hello", Right(5)),
        (ZIO.access_m(lambda s: ZIO.succeed(len(s))), "hello",
         Right(5)),  # type: ignore
        (ZIO.access_m(lambda s: ZIO.fail(Bippy())), "hello",
         Left(Bippy)),  # type: ignore
Beispiel #16
0
 def effect_total(side_effect: Thunk[A]) -> "ZIO[object, NoReturn, A]":
     return ZIO(lambda _: Right(side_effect()))
Beispiel #17
0
 def access(f: Callable[[R], A]) -> "ZIO[R, NoReturn, A]":
     return ZIO(lambda r: Right(f(r)))
Beispiel #18
0
def test_environment_1() -> None:
    assert Environment[int]()._run(42) == Right(42)
Beispiel #19
0
 def or_else(
         self: "ZIO[R, EE, AA]",
         other: "ZIO[R, E2, A2]") -> "ZIO[R, Union[EE, E2], Union[AA, A2]]":
     return ZIO(lambda r: self._run(r).fold(lambda e: other._run(r), lambda
                                            a: Right(a)))
Beispiel #20
0
def test_environment_3() -> None:
    assert Environment[int]().provide(42).provide("asdf")._run(()) == Right(42)
Beispiel #21
0
 def either(self) -> "ZIO[R, NoReturn, Either[E, A]]":
     return ZIO(lambda r: Right(self._run(r)))
Beispiel #22
0
 def or_die(self: "ZIO[R, X, AA]") -> "ZIO[R, NoReturn, AA]":
     return ZIO(lambda r: self._run(r).fold(_raise, lambda a: Right(a)))
Beispiel #23
0
def test_zio_lshift_1() -> None:
    assert (
        ZIO.succeed(100) << ZIO.succeed(1) << ZIO.succeed(2) << ZIO.succeed(3)
    ).flat_map(lambda x: ZIO.succeed(f"The result is: {x}"))._run(
        ()) == Right("The result is: 3")
Beispiel #24
0
def test_flatten_right() -> None:
    x: Either[str, Either[str, int]] = Either.right(Either.right(42))
    assert x.flatten() == Right(42)
Beispiel #25
0
def test_zio_zip_1() -> None:
    assert ZIO.succeed("a").zip(ZIO.succeed(42))._run(()) == Right(("a", 42))
Beispiel #26
0
    environment: R
    expected_output: Either[E, A]

    def is_satisfied(self) -> bool:
        output_p = unsafe_run(self.p.either().provide(self.environment))
        output_q = unsafe_run(self.q.either().provide(self.environment))
        return output_p == output_q == self.expected_output


@monadic
def _q1(do: ZIOMonad[object, NoReturn]) -> ZIO[object, NoReturn, int]:
    result = do << ZIO.succeed(42)
    return ZIO.succeed(result)


EQ1: Final = Equiv(ZIO.succeed(42), _q1(), (), Right(42))


@monadic
def _q2(do: ZIOMonad[object, NoReturn]) -> ZIO[object, NoReturn, int]:
    result = do << ZIO.succeed(42)
    return ZIO.succeed(result)


EQ2: Final = Equiv(ZIO.succeed(42), _q2(), (), Right(42))


@monadic
def _q3(do: ZIOMonad[object, Bippy]) -> ZIO[object, Bippy, int]:
    do << ZIO.fail(Bippy())
    return ZIO.succeed(42)
Beispiel #27
0
 def __init__(self) -> None:
     self._run = lambda r: Right(r)