File: test_text_question.py

package info (click to toggle)
python-briefcase 0.3.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,596 kB
  • sloc: python: 62,519; makefile: 60
file content (177 lines) | stat: -rw-r--r-- 4,907 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import pytest

from briefcase.exceptions import InputDisabled


def test_unvalidated_input(console):
    """If the user enters text and there's no validation, the text is returned."""
    console.values = ["hello"]

    value = console.text_question(
        intro="Some introduction",
        description="My variable",
        default="goodbye",
    )

    assert console.prompts == ["My variable [goodbye]: "]
    assert value == "hello"


def test_unvalidated_input_with_override(console):
    """If an override is provided and there's no validation, the override is
    returned."""
    console.values = ["hello"]

    value = console.text_question(
        intro="Some introduction",
        description="My variable",
        default="goodbye",
        override_value="override",
    )

    assert console.prompts == []
    assert value == "override"


def test_validated_input(console):
    """If the user enters text and there's validation, the user is prompted until valid
    text is entered."""
    console.values = ["bad", "hello"]

    def validator(text):
        if text == "bad":
            raise ValueError("That's bad...")
        return True

    value = console.text_question(
        intro="Some introduction",
        description="My variable",
        default="goodbye",
        validator=validator,
    )

    assert console.prompts == [
        "My variable [goodbye]: ",
        "My variable [goodbye]: ",
    ]
    assert value == "hello"


def test_validated_input_with_override(console):
    """If an override is provided and there's validation, the override is validated and
    returned."""

    def validator(text):
        if text == "bad":
            raise ValueError("That's bad...")
        return True

    value = console.text_question(
        intro="Some introduction",
        description="My variable",
        default="goodbye",
        validator=validator,
        override_value="override",
    )

    assert console.prompts == []
    assert value == "override"


def test_validated_input_with_invalid_override(console):
    """If an invalid override is provided and there's validation, the override is
    rejected and the user is prompted until valid text is entered."""
    console.values = ["bad", "hello"]

    def validator(text):
        if text in {"bad", "bad-override"}:
            return False
        return True

    value = console.text_question(
        intro="Some introduction",
        description="My variable",
        default="goodbye",
        validator=validator,
        override_value="bad-override",
    )

    assert console.prompts == [
        "My variable [goodbye]: ",
        "My variable [goodbye]: ",
    ]
    assert value == "hello"


def test_input_with_default(console):
    """If the user enters text and there's no validation, the text is returned."""
    console.values = [""]

    value = console.text_question(
        intro="Some introduction", description="My variable", default="goodbye"
    )

    assert console.prompts == ["My variable [goodbye]: "]
    assert value == "goodbye"


def test_input_disabled(disabled_console):
    """If input is disabled, the default is returned."""

    value = disabled_console.text_question(
        intro="Some introduction",
        description="My variable",
        default="goodbye",
    )

    assert disabled_console.prompts == []
    assert value == "goodbye"


def test_input_disabled_with_override(disabled_console):
    """If input is disabled, the override is returned."""
    value = disabled_console.text_question(
        intro="Some introduction",
        description="My variable",
        default="goodbye",
        override_value="override",
    )

    assert disabled_console.prompts == []
    assert value == "override"


def test_input_disabled_validation_failure(disabled_console):
    """If input is disabled, and validation fails, an error is raised."""

    with pytest.raises(InputDisabled, match=r"Well that won't work..."):

        def not_valid(text):
            raise ValueError("Well that won't work...")

        disabled_console.text_question(
            intro="Some introduction",
            description="My variable",
            default="goodbye",
            validator=not_valid,
        )

    assert disabled_console.prompts == []


def test_input_disabled_validation_failure_with_override(disabled_console):
    """If input is disabled, and validation fails for override, an error is raised."""
    with pytest.raises(InputDisabled, match=r"Well that won't work..."):

        def not_valid(text):
            raise ValueError("Well that won't work...")

        disabled_console.text_question(
            intro="Some introduction",
            description="My variable",
            default="goodbye",
            validator=not_valid,
            override_value="override",
        )

    assert disabled_console.prompts == []