-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathtest_checkbox.py
More file actions
460 lines (337 loc) · 14.1 KB
/
Copy pathtest_checkbox.py
File metadata and controls
460 lines (337 loc) · 14.1 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import unittest
from readchar import key
import inquirer.questions as questions
import tests.integration.console_render.helper as helper
from inquirer.render import ConsoleRender
class CheckboxRenderTest(unittest.TestCase, helper.BaseTestCase):
def setUp(self):
self.base_setup()
def tearDown(self):
self.base_teardown()
def test_all_choices_are_shown(self):
stdin = helper.event_factory(key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
sut.render(question)
self.assertInStdout(message)
for choice in choices:
self.assertInStdout(choice)
def test_one_choice(self):
stdin = helper.event_factory(key.SPACE, key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
self.assertInStdout(message)
assert result == ["foo"]
def test_choose_the_second(self):
stdin = helper.event_factory(key.DOWN, key.SPACE, key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
self.assertInStdout(message)
assert result == ["bar"]
def test_choose_with_long_choices(self):
stdin = helper.event_factory(
key.DOWN,
key.DOWN,
key.DOWN,
key.DOWN,
key.DOWN,
key.DOWN,
key.DOWN,
key.DOWN,
key.SPACE,
key.DOWN,
key.DOWN,
key.ENTER,
)
message = "Number message"
variable = "Number variable"
choices = list(range(15))
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == [8]
def test_can_move(self):
stdin = helper.event_factory(key.DOWN, key.DOWN, key.UP, key.SPACE, key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["bar"]
def test_cannot_move_beyond_upper_limit(self):
stdin = helper.event_factory(
key.UP,
key.UP,
key.UP,
key.SPACE,
key.ENTER,
)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["foo"]
def test_cannot_move_beyond_lower_limit(self):
stdin = helper.event_factory(
key.DOWN, key.DOWN, key.DOWN, key.DOWN, key.DOWN, key.DOWN, key.DOWN, key.SPACE, key.ENTER
)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
self.printStdout()
assert result == ["bazz"]
def test_move_down_carousel(self):
stdin = helper.event_factory(key.DOWN, key.DOWN, key.DOWN, key.DOWN, key.SPACE, key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices, carousel=True)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["bar"]
def test_move_up_carousel(self):
stdin = helper.event_factory(key.UP, key.SPACE, key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices, carousel=True)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["bazz"]
def test_ctrl_c_breaks_execution(self):
stdin_array = [key.CTRL_C]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
with self.assertRaises(KeyboardInterrupt):
sut.render(question)
def test_deselection(self):
stdin_array = [key.SPACE, key.SPACE, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == []
def test_right_cursor_selects_too(self):
stdin_array = [key.RIGHT, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["foo"]
def test_right_cursor_do_not_unselect(self):
stdin_array = [key.RIGHT, key.RIGHT, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["foo"]
def test_left_cursor_unselect(self):
stdin_array = [key.SPACE, key.LEFT, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == []
def test_left_cursor_do_not_select(self):
stdin_array = [key.SPACE, key.LEFT, key.LEFT, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == []
def test_select_all(self):
stdin_array = [key.CTRL_A, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == choices
def test_reset_one_selection(self):
stdin_array = [key.SPACE, key.CTRL_R, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == []
def test_reset_all_selection(self):
stdin_array = [key.CTRL_A, key.CTRL_R, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == []
def test_invert_one_selection(self):
stdin_array = [key.SPACE, key.CTRL_I, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["bar", "bazz"]
def test_invert_all(self):
stdin_array = [key.CTRL_I, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == choices
def test_double_invert_all(self):
stdin_array = [key.CTRL_I, key.CTRL_I, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == []
def test_unselect_locked_space(self):
stdin_array = [key.SPACE, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices, locked=["foo"])
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["foo"]
def test_unselect_locked_left(self):
stdin_array = [key.LEFT, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices, locked=["foo"])
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["foo"]
def test_two_locked_options(self):
stdin_array = [key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices, locked=["foo", "bazz"])
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["foo", "bazz"]
def test_locked_with_typo(self):
stdin_array = [key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices, locked=["fooo"])
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == []
def test_locked_with_default(self):
stdin_array = [key.DOWN, key.SPACE, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]
question = questions.Checkbox(variable, message, choices=choices, locked=["bar"], default=["bar"])
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
assert result == ["bar"]
def test_first_hint_is_shown(self):
stdin = helper.event_factory(key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = {
"foo": "Foo",
"bar": "Bar",
"bazz": "Bazz",
}
question = questions.Checkbox(variable, message, choices=choices.keys(), hints=choices)
sut = ConsoleRender(event_generator=stdin)
sut.render(question)
self.assertInStdout("Foo")
def test_second_hint_is_shown(self):
stdin = helper.event_factory(key.DOWN, key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = {
"foo": "Foo",
"bar": "Bar",
"bazz": "Bazz",
}
question = questions.Checkbox(variable, message, choices=choices.keys(), hints=choices)
sut = ConsoleRender(event_generator=stdin)
sut.render(question)
self.assertInStdout("Bar")
def test_max_options_displayed_at_once_shows_more_choices(self):
stdin = helper.event_factory(key.ENTER)
message = "Pick some"
variable = "many"
choices = [f"opt-{i:03d}" for i in range(40)]
question = questions.Checkbox(
variable, message, choices=choices, max_options_displayed_at_once=25
)
sut = ConsoleRender(event_generator=stdin)
sut.render(question)
self.assertInStdout("opt-000")
self.assertInStdout("opt-024")
self.assertNotInStdout("opt-025")
def test_max_options_displayed_at_once_default_unchanged(self):
stdin = helper.event_factory(key.ENTER)
message = "Pick some"
variable = "many"
choices = [f"opt-{i:03d}" for i in range(40)]
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
sut.render(question)
self.assertInStdout("opt-000")
self.assertInStdout("opt-012")
self.assertNotInStdout("opt-013")