Exemplo n.º 1
0
 def input(
     self,
     prompt: Optional[str] = None
 ) -> ZIO[object, Union[EOFError, KeyboardInterrupt], str]:
     user_input = self._user_input.pop(0)
     self._effects.append(console_effect.Input(prompt, user_input))
     if isinstance(user_input, str):
         return ZIO.succeed(user_input)
     else:
         result: Union[EOFError, KeyboardInterrupt] = user_input
         return ZIO.fail(result)
Exemplo n.º 2
0
def test_zio_map_error_1() -> None:
    count = 0

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

    assert (ZIO.fail(100).map_error(_impure_function).map_error(
        _impure_function).map_error(_impure_function).map_error(
            lambda x: f"The result is: {x}")._run(
                ())) == Left("The result is: 103")
    assert count == 3
Exemplo n.º 3
0
def test_zio_map_error_2() -> None:
    count = 0

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

    def _kaboom(x: int) -> int:
        raise Bippy

    assert (ZIO.fail(100).map_error(_impure_function).map_error(_kaboom).
            map_error(_impure_function).map_error(_impure_function).map_error(
                lambda x: f"The result is: {x}").catch(Bippy)._run(
                    ())) == Left(Bippy())
    assert count == 1
Exemplo n.º 4
0
 def exit(
         self,
         exit_code: Optional[int] = None
 ) -> ZIO[object, NoReturn, NoReturn]:
     self._effects.append(system_effect.Exit(exit_code))
     return ZIO.fail(SystemExit(exit_code)).or_die()
Exemplo n.º 5
0
def test_zio_fail() -> None:
    assert ZIO.fail(42)._run(()) == Left(42)
Exemplo n.º 6
0
def test_zio_or_die_2() -> None:
    with pytest.raises(Bippy):
        ZIO.fail(Bippy()).or_die()._run(())
Exemplo n.º 7
0
def test_zio_either_2() -> None:
    assert ZIO.fail("a").either()._run(()) == Right(Left("a"))
Exemplo n.º 8
0
def test_zio_zip_3() -> None:
    assert ZIO.succeed("a").zip(ZIO.fail(42))._run(()) == Left(42)
Exemplo n.º 9
0
def test_zio_access_m_2() -> None:
    accessor: Callable[[str], ZIO[object, str,
                                  int]] = lambda s: ZIO.fail("oops")
    assert ZIO.access_m(accessor)._run("hello") == Left("oops")
Exemplo n.º 10
0
    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
        (ZIO.effect_total(lambda: 42), None, Right(42)),
Exemplo n.º 11
0
def _q3(do: ZIOMonad[object, Bippy]) -> ZIO[object, Bippy, int]:
    do << ZIO.fail(Bippy())
    return ZIO.succeed(42)
Exemplo n.º 12
0
@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)


EQ3: Final = Equiv(ZIO.fail(Bippy()), _q3(), (), Left(Bippy()))


@monadic
def _q4(do: ZIOMonad[object, Exception]) -> ZIO[object, Exception, int]:
    result = do << ZIO.effect(lambda: 42)
    return ZIO.succeed(result)


EQ4: Final = Equiv(ZIO.effect(lambda: 42), _q4(), (), Right(42))


@monadic
def _q5(do: ZIOMonad[object, Exception]) -> ZIO[object, Exception, int]:
    do << ZIO.effect(lambda: _raise(Bippy()))  # type: ignore
    return ZIO.succeed(42)