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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
import asyncio
import platform
import pytest
from prompt_toolkit.output import DummyOutput
from pytest import fail
from questionary import text
from questionary.utils import is_prompt_toolkit_3
from tests.utils import KeyInputs
from tests.utils import execute_with_input_pipe
def test_ask_should_catch_keyboard_exception():
def run(inp):
inp.send_text(KeyInputs.CONTROLC)
question = text("Hello?", input=inp, output=DummyOutput())
try:
result = question.ask()
assert result is None
except KeyboardInterrupt:
fail("Keyboard Interrupt should be caught by `ask()`")
execute_with_input_pipe(run)
def test_skipping_of_questions():
def run(inp):
question = text("Hello?", input=inp, output=DummyOutput()).skip_if(
condition=True, default=42
)
response = question.ask()
assert response == 42
execute_with_input_pipe(run)
def test_skipping_of_questions_unsafe():
def run(inp):
question = text("Hello?", input=inp, output=DummyOutput()).skip_if(
condition=True, default=42
)
response = question.unsafe_ask()
assert response == 42
execute_with_input_pipe(run)
def test_skipping_of_skipping_of_questions():
def run(inp):
inp.send_text("World" + KeyInputs.ENTER + "\r")
question = text("Hello?", input=inp, output=DummyOutput()).skip_if(
condition=False, default=42
)
response = question.ask()
assert response == "World" and not response == 42
execute_with_input_pipe(run)
def test_skipping_of_skipping_of_questions_unsafe():
def run(inp):
inp.send_text("World" + KeyInputs.ENTER + "\r")
question = text("Hello?", input=inp, output=DummyOutput()).skip_if(
condition=False, default=42
)
response = question.unsafe_ask()
assert response == "World" and not response == 42
execute_with_input_pipe(run)
@pytest.mark.skipif(
not is_prompt_toolkit_3() and platform.system() == "Windows",
reason="requires prompt_toolkit >= 3",
)
def test_async_ask_question():
loop = asyncio.new_event_loop()
def run(inp):
inp.send_text("World" + KeyInputs.ENTER + "\r")
question = text("Hello?", input=inp, output=DummyOutput())
response = loop.run_until_complete(question.ask_async())
assert response == "World"
execute_with_input_pipe(run)
def test_multiline_text():
def run(inp):
inp.send_text(f"Hello{KeyInputs.ENTER}world{KeyInputs.ESCAPE}{KeyInputs.ENTER}")
question = text("Hello?", input=inp, output=DummyOutput(), multiline=True)
response = question.ask()
assert response == "Hello\nworld"
execute_with_input_pipe(run)
|