def test_live_console_input_no_prompt_with_running() -> None: with patch('builtins.input', return_value="Hello") as mock_input: program = console.input().provide(ConsoleEnvironment(LiveConsole())) output = unsafe_run(program) assert output == "Hello" mock_input.assert_called_with()
def test_live_console_print_with_running() -> None: with patch('builtins.print', return_value=None) as mock_print: program = console.print("Hello world").provide( ConsoleEnvironment(LiveConsole())) output = unsafe_run(program) assert output is None mock_print.assert_called_with("Hello world")
def test_live_console_input_with_keyboard_interrupt() -> None: with patch('builtins.input', side_effect=KeyboardInterrupt) as mock_input: program = console.input("Prompt").provide( ConsoleEnvironment(LiveConsole())) with pytest.raises(KeyboardInterrupt): unsafe_run(program) mock_input.assert_called_with("Prompt")
def test_live_console_input_with_eof_error() -> None: with patch('builtins.input', side_effect=EOFError) as mock_input: program = console.input("Prompt").provide( ConsoleEnvironment(LiveConsole())) with pytest.raises(EOFError): unsafe_run(program) mock_input.assert_called_with("Prompt")
def test_live_console_input_no_prompt_without_running() -> None: with patch('builtins.input') as mock_input: console.input().provide(ConsoleEnvironment(LiveConsole())) mock_input.assert_not_called()
def test_live_console_print_without_running() -> None: with patch('builtins.print') as mock_print: console.print("Hello world").provide(ConsoleEnvironment(LiveConsole())) mock_print.assert_not_called()
do << con.print("Hello, what is your name?") name = do << con.input() do << con.print(f"Your name is: {name}") x = do << ZIO.succeed(1) while x < 20: x = do << ( ZIO.succeed(x).map(lambda p: p + 1).flat_map(lambda q: ZIO.succeed( q - 1)).flat_map(lambda r: ZIO.succeed(r + 1))) do << con.print(f"The value of x is: {x}") return ZIO.succeed(f"Hello, {name}!") p = program().provide(LiveConsole()) final_result = unsafe_run(p) print(f"Final result (1) is: {final_result}") # You can run the same program (value) over and over again. final_result_2 = unsafe_run(p) print(f"Final result (2) is: {final_result_2}") @monadic def prog( do: ZIOMonad[ConsoleSystemEnvironment, NoReturn] ) -> ZIO[ConsoleSystemEnvironment, NoReturn, int]: age = do << console.get_input_from_console( prompt="How old are you?\n", parse_value=ZIO.from_callable(str).map(int).catch(