1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
from prompt_toolkit.output import DummyOutput
from pytest import fail
import questionary
from questionary import form
from tests.utils import KeyInputs
from tests.utils import execute_with_input_pipe
def example_form(inp):
return form(
q1=questionary.confirm("Hello?", input=inp, output=DummyOutput()),
q2=questionary.select(
"World?", choices=["foo", "bar"], input=inp, output=DummyOutput()
),
)
def example_form_with_skip(inp):
return form(
q1=questionary.confirm("Hello?", input=inp, output=DummyOutput()),
q2=questionary.select(
"World?", choices=["foo", "bar"], input=inp, output=DummyOutput()
).skip_if(True, 42),
)
def test_form_creation():
text = "Y" + KeyInputs.ENTER + "\r"
def run(inp):
inp.send_text(text)
f = example_form(inp)
result = f.unsafe_ask()
assert result == {"q1": True, "q2": "foo"}
execute_with_input_pipe(run)
def test_form_skips_questions():
text = "Y" + KeyInputs.ENTER + "\r"
def run(inp):
inp.send_text(text)
f = example_form_with_skip(inp)
result = f.ask()
assert result == {"q1": True, "q2": 42}
execute_with_input_pipe(run)
def test_form_skips_questions_unsafe_ask():
text = "Y" + KeyInputs.ENTER + "\r"
def run(inp):
inp.send_text(text)
f = example_form_with_skip(inp)
result = f.unsafe_ask()
assert result == {"q1": True, "q2": 42}
execute_with_input_pipe(run)
def test_ask_should_catch_keyboard_exception():
def run(inp):
try:
inp.send_text(KeyInputs.CONTROLC)
f = example_form(inp)
result = f.ask()
assert result == {}
except KeyboardInterrupt:
fail("Keyboard Interrupt should be caught by `ask()`")
execute_with_input_pipe(run)
|