File: test_examples.py

package info (click to toggle)
python-questionary 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 960 kB
  • sloc: python: 3,917; makefile: 66
file content (128 lines) | stat: -rw-r--r-- 3,806 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from prompt_toolkit.output import DummyOutput

from tests.utils import KeyInputs
from tests.utils import execute_with_input_pipe


def ask_with_patched_input(q, text):
    def run(inp):
        inp.send_text(text)
        return q(input=inp, output=DummyOutput())

    return execute_with_input_pipe(run)


def test_confirm_example():
    from examples.confirm_continue import ask_dictstyle
    from examples.confirm_continue import ask_pystyle

    text = "n" + KeyInputs.ENTER + "\r"

    result_dict = ask_with_patched_input(ask_dictstyle, text)
    result_py = ask_with_patched_input(ask_pystyle, text)

    assert result_dict == {"continue": False}
    assert result_dict["continue"] == result_py


def test_text_example():
    from examples.text_phone_number import ask_dictstyle
    from examples.text_phone_number import ask_pystyle

    text = "1234567890" + KeyInputs.ENTER + "\r"

    result_dict = ask_with_patched_input(ask_dictstyle, text)
    result_py = ask_with_patched_input(ask_pystyle, text)

    assert result_dict == {"phone": "1234567890"}
    assert result_dict["phone"] == result_py


def test_select_example():
    from examples.select_restaurant import ask_dictstyle
    from examples.select_restaurant import ask_pystyle

    text = KeyInputs.DOWN + KeyInputs.ENTER + KeyInputs.ENTER + "\r"

    result_dict = ask_with_patched_input(ask_dictstyle, text)
    result_py = ask_with_patched_input(ask_pystyle, text)

    assert result_dict == {"theme": "Make a reservation"}
    assert result_dict["theme"] == result_py


def test_rawselect_example():
    from examples.rawselect_separator import ask_dictstyle
    from examples.rawselect_separator import ask_pystyle

    text = "3" + KeyInputs.ENTER + KeyInputs.ENTER + "\r"

    result_dict = ask_with_patched_input(ask_dictstyle, text)
    result_py = ask_with_patched_input(ask_pystyle, text)

    assert result_dict == {"theme": "Ask opening hours"}
    assert result_dict["theme"] == result_py


def test_checkbox_example():
    from examples.checkbox_separators import ask_dictstyle
    from examples.checkbox_separators import ask_pystyle

    text = "n" + KeyInputs.ENTER + KeyInputs.ENTER + KeyInputs.ENTER + "\r"

    result_dict = ask_with_patched_input(ask_dictstyle, text)
    result_py = ask_with_patched_input(ask_pystyle, text)

    assert result_dict == {"toppings": ["foo"]}
    assert result_dict["toppings"] == result_py


def test_password_example():
    from examples.password_git import ask_dictstyle
    from examples.password_git import ask_pystyle

    text = "asdf" + KeyInputs.ENTER + "\r"

    result_dict = ask_with_patched_input(ask_dictstyle, text)
    result_py = ask_with_patched_input(ask_pystyle, text)

    assert result_dict == {"password": "asdf"}
    assert result_dict["password"] == result_py


def test_autocomplete_example():
    from examples.autocomplete_ants import ask_dictstyle
    from examples.autocomplete_ants import ask_pystyle

    text = "Polyergus lucidus" + KeyInputs.ENTER + "\r"

    result_dict = ask_with_patched_input(ask_dictstyle, text)
    result_py = ask_with_patched_input(ask_pystyle, text)

    assert result_dict == {"ants": "Polyergus lucidus"}
    assert result_py == "Polyergus lucidus"


def test_advanced_workflow_example():
    from examples.advanced_workflow import ask_dictstyle

    text = (
        KeyInputs.ENTER
        + "questionary"
        + KeyInputs.ENTER
        + KeyInputs.DOWN
        + KeyInputs.DOWN
        + KeyInputs.ENTER
        + "Hello World"
        + KeyInputs.ENTER
        + "\r"
    )

    result_dict = ask_with_patched_input(ask_dictstyle, text)

    assert result_dict == {
        "intro": None,
        "conditional_step": True,
        "next_question": "questionary",
        "second_question": "Hello World",
    }