| 1 |
import io |
| 2 |
|
| 3 |
from rich.console import Console |
| 4 |
from rich.prompt import Confirm, IntPrompt, Prompt |
| 5 |
|
| 6 |
|
| 7 |
def test_prompt_str(): |
| 8 |
INPUT = "egg\nfoo" |
| 9 |
console = Console(file=io.StringIO()) |
| 10 |
name = Prompt.ask( |
| 11 |
"what is your name", |
| 12 |
console=console, |
| 13 |
choices=["foo", "bar"], |
| 14 |
default="baz", |
| 15 |
stream=io.StringIO(INPUT), |
| 16 |
) |
| 17 |
assert name == "foo" |
| 18 |
expected = "what is your name [foo/bar] (baz): Please select one of the available options\nwhat is your name [foo/bar] (baz): " |
| 19 |
output = console.file.getvalue() |
| 20 |
print(repr(output)) |
| 21 |
assert output == expected |
| 22 |
|
| 23 |
|
| 24 |
def test_prompt_str_case_insensitive(): |
| 25 |
INPUT = "egg\nFoO" |
| 26 |
console = Console(file=io.StringIO()) |
| 27 |
name = Prompt.ask( |
| 28 |
"what is your name", |
| 29 |
console=console, |
| 30 |
choices=["foo", "bar"], |
| 31 |
default="baz", |
| 32 |
case_sensitive=False, |
| 33 |
stream=io.StringIO(INPUT), |
| 34 |
) |
| 35 |
assert name == "foo" |
| 36 |
expected = "what is your name [foo/bar] (baz): Please select one of the available options\nwhat is your name [foo/bar] (baz): " |
| 37 |
output = console.file.getvalue() |
| 38 |
print(repr(output)) |
| 39 |
assert output == expected |
| 40 |
|
| 41 |
|
| 42 |
def test_prompt_str_default(): |
| 43 |
INPUT = "" |
| 44 |
console = Console(file=io.StringIO()) |
| 45 |
name = Prompt.ask( |
| 46 |
"what is your name", |
| 47 |
console=console, |
| 48 |
default="Will", |
| 49 |
stream=io.StringIO(INPUT), |
| 50 |
) |
| 51 |
assert name == "Will" |
| 52 |
expected = "what is your name (Will): " |
| 53 |
output = console.file.getvalue() |
| 54 |
print(repr(output)) |
| 55 |
assert output == expected |
| 56 |
|
| 57 |
|
| 58 |
def test_prompt_int(): |
| 59 |
INPUT = "foo\n100" |
| 60 |
console = Console(file=io.StringIO()) |
| 61 |
number = IntPrompt.ask( |
| 62 |
"Enter a number", |
| 63 |
console=console, |
| 64 |
stream=io.StringIO(INPUT), |
| 65 |
) |
| 66 |
assert number == 100 |
| 67 |
expected = "Enter a number: Please enter a valid integer number\nEnter a number: " |
| 68 |
output = console.file.getvalue() |
| 69 |
print(repr(output)) |
| 70 |
assert output == expected |
| 71 |
|
| 72 |
|
| 73 |
def test_prompt_confirm_no(): |
| 74 |
INPUT = "foo\nNO\nn" |
| 75 |
console = Console(file=io.StringIO()) |
| 76 |
answer = Confirm.ask( |
| 77 |
"continue", |
| 78 |
console=console, |
| 79 |
stream=io.StringIO(INPUT), |
| 80 |
) |
| 81 |
assert answer is False |
| 82 |
expected = "continue [y/n]: Please enter Y or N\ncontinue [y/n]: Please enter Y or N\ncontinue [y/n]: " |
| 83 |
output = console.file.getvalue() |
| 84 |
print(repr(output)) |
| 85 |
assert output == expected |
| 86 |
|
| 87 |
|
| 88 |
def test_prompt_confirm_yes(): |
| 89 |
INPUT = "foo\nNO\ny" |
| 90 |
console = Console(file=io.StringIO()) |
| 91 |
answer = Confirm.ask( |
| 92 |
"continue", |
| 93 |
console=console, |
| 94 |
stream=io.StringIO(INPUT), |
| 95 |
) |
| 96 |
assert answer is True |
| 97 |
expected = "continue [y/n]: Please enter Y or N\ncontinue [y/n]: Please enter Y or N\ncontinue [y/n]: " |
| 98 |
output = console.file.getvalue() |
| 99 |
print(repr(output)) |
| 100 |
assert output == expected |
| 101 |
|
| 102 |
|
| 103 |
def test_prompt_confirm_default(): |
| 104 |
INPUT = "foo\nNO\ny" |
| 105 |
console = Console(file=io.StringIO()) |
| 106 |
answer = Confirm.ask( |
| 107 |
"continue", console=console, stream=io.StringIO(INPUT), default=True |
| 108 |
) |
| 109 |
assert answer is True |
| 110 |
expected = "continue [y/n] (y): Please enter Y or N\ncontinue [y/n] (y): Please enter Y or N\ncontinue [y/n] (y): " |
| 111 |
output = console.file.getvalue() |
| 112 |
print(repr(output)) |
| 113 |
assert output == expected |
| 114 |
|
| 115 |
|
| 116 |
def test_prompt_confirm_markup(): |
| 117 |
INPUT = "foo\nNO\ny" |
| 118 |
console = Console(file=io.StringIO(), markup=False) |
| 119 |
answer = Confirm.ask( |
| 120 |
"continue", console=console, stream=io.StringIO(INPUT), default=True |
| 121 |
) |
| 122 |
assert answer is True |
| 123 |
expected = "continue [y/n] (y): Please enter Y or N\ncontinue [y/n] (y): Please enter Y or N\ncontinue [y/n] (y): " |
| 124 |
output = console.file.getvalue() |
| 125 |
print(repr(output)) |
| 126 |
assert output == expected |