Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.List(variable, message, choices=choices)
sut = ConsoleRender(event_generator=stdin)
sut.render(question)
self.assertInStdout(message)
for choice in choices:
self.assertInStdout(choice)
def test_default_input(self):
self.sut.send(key.ENTER)
self.sut.expect("{'interests': \['Computers', 'Books'\]}.*", timeout=1) # noqa
def test_no_as_default(self):
stdin = helper.event_factory(key.ENTER)
message = 'Foo message'
variable = 'Bar variable'
expected = False
question = questions.Confirm(variable,
message=message)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
self.assertEqual(expected, result)
self.assertInStdout(message)
self.assertInStdout('(y/N)')
'UP',
'DOWN',
'ENTER',
'CTRL_C',
'CTRL_D'
]
all_keys = [getattr(readchar.key, k) for k in dir(readchar.key) if k not in exclude]
all_keys.extend(string.printable)
expected_calls = [
(('',),),
(('\x1b[2A',),),
(('\x1b[K\x1b[1m[\x1b[32;1mx\x1b[0;1m]\x1b[0m foo',),),
]
for key in all_keys:
with InputContext(readchar.key.DOWN, key, readchar.key.ENTER):
selindex = cutie.select(["foo"])
self.assertEqual(selindex, 0)
self.assertEqual(mock_print.call_args_list[:3], expected_calls)
mock_print.reset_mock()
def test_can_move(self):
stdin = (key.DOWN
+ key.DOWN
+ key.UP
+ key.SPACE
+ key.ENTER)
message = 'Foo message'
variable = 'Bar variable'
choices = ['foo', 'bar', 'bazz']
sys.stdin = StringIO(stdin)
question = questions.Checkbox(variable, message, choices=choices)
sut = ConsoleRender(key_generator=fake_key_generator)
result = sut.render(question)
self.assertEqual(['bar'], result)
def test_default_input(self):
self.sut.expect(".*What's.*", timeout=1)
self.sut.send('abcde')
self.sut.send(key.ENTER)
self.sut.expect("{'password': 'abcde'}", timeout=1)
def test_deselect_on_min_sufficient(self, mock_print):
call_args = ["foo", "bar"]
expected_calls = [
print_call("foo"),
print_call("bar", "selected"),
print_call(state="confirm-active"),
PRINT_CALL_END
]
with InputContext(" ", readchar.key.DOWN, readchar.key.DOWN, readchar.key.ENTER):
selected_indices = cutie.select_multiple(call_args, minimal_count=1, ticked_indices=[0, 1])
self.assertEqual(mock_print.call_args_list[-4:], expected_calls)
self.assertEqual(selected_indices, [1])
def process_input(self, pressed):
if pressed == key.CTRL_C:
raise KeyboardInterrupt()
if pressed.lower() == key.ENTER:
raise errors.EndOfInput(self.question.default)
if pressed in 'yY':
print(pressed)
raise errors.EndOfInput(True)
if pressed in 'nN':
print(pressed)
raise errors.EndOfInput(False)
'\033[1m[\033[32;1m x \033[0;1m]\033[0m ' if i == selected_index else
'\033[1m[ ]\033[0m ', option))
keypress = readchar.readkey()
if keypress == readchar.key.UP:
new_index = selected_index
while new_index > 0:
new_index -= 1
selected_index = new_index
break
elif keypress == readchar.key.DOWN:
new_index = selected_index
while new_index < len(options) - 1:
new_index += 1
selected_index = new_index
break
elif keypress == readchar.key.ENTER:
break
elif keypress == readchar.key.CTRL_C:
raise KeyboardInterrupt
return selected_index