diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:18:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:18:28 +0000 |
commit | f8363b456f1ab31ee56abad579b215af195093d5 (patch) | |
tree | b1500c675c2e0a55fb75721a854e1510acf7c862 /tests/test_prompt.py | |
parent | Initial commit. (diff) | |
download | rich-f8363b456f1ab31ee56abad579b215af195093d5.tar.xz rich-f8363b456f1ab31ee56abad579b215af195093d5.zip |
Adding upstream version 9.11.0.upstream/9.11.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/test_prompt.py')
-rw-r--r-- | tests/test_prompt.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/test_prompt.py b/tests/test_prompt.py new file mode 100644 index 0000000..9a41cc3 --- /dev/null +++ b/tests/test_prompt.py @@ -0,0 +1,95 @@ +import io + +from rich.console import Console +from rich.prompt import Prompt, IntPrompt, Confirm + + +def test_prompt_str(): + INPUT = "egg\nfoo" + console = Console(file=io.StringIO()) + name = Prompt.ask( + "what is your name", + console=console, + choices=["foo", "bar"], + default="baz", + stream=io.StringIO(INPUT), + ) + assert name == "foo" + expected = "what is your name [foo/bar] (baz): Please select one of the available options\nwhat is your name [foo/bar] (baz): " + output = console.file.getvalue() + print(repr(output)) + assert output == expected + + +def test_prompt_str_default(): + INPUT = "" + console = Console(file=io.StringIO()) + name = Prompt.ask( + "what is your name", + console=console, + default="Will", + stream=io.StringIO(INPUT), + ) + assert name == "Will" + expected = "what is your name (Will): " + output = console.file.getvalue() + print(repr(output)) + assert output == expected + + +def test_prompt_int(): + INPUT = "foo\n100" + console = Console(file=io.StringIO()) + number = IntPrompt.ask( + "Enter a number", + console=console, + stream=io.StringIO(INPUT), + ) + assert number == 100 + expected = "Enter a number: Please enter a valid integer number\nEnter a number: " + output = console.file.getvalue() + print(repr(output)) + assert output == expected + + +def test_prompt_confirm_no(): + INPUT = "foo\nNO\nn" + console = Console(file=io.StringIO()) + answer = Confirm.ask( + "continue", + console=console, + stream=io.StringIO(INPUT), + ) + assert answer is False + expected = "continue [y/n]: Please enter Y or N\ncontinue [y/n]: Please enter Y or N\ncontinue [y/n]: " + output = console.file.getvalue() + print(repr(output)) + assert output == expected + + +def test_prompt_confirm_yes(): + INPUT = "foo\nNO\ny" + console = Console(file=io.StringIO()) + answer = Confirm.ask( + "continue", + console=console, + stream=io.StringIO(INPUT), + ) + assert answer is True + expected = "continue [y/n]: Please enter Y or N\ncontinue [y/n]: Please enter Y or N\ncontinue [y/n]: " + output = console.file.getvalue() + print(repr(output)) + assert output == expected + + +def test_prompt_confirm_default(): + INPUT = "foo\nNO\ny" + console = Console(file=io.StringIO()) + answer = Confirm.ask( + "continue", console=console, stream=io.StringIO(INPUT), default=True + ) + assert answer is True + expected = "continue [y/n] (y): Please enter Y or N\ncontinue [y/n] (y): Please enter Y or N\ncontinue [y/n] (y): " + output = console.file.getvalue() + print(repr(output)) + assert output == expected |