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_backspace_limit(self):
self.sut.expect("What's.*", timeout=1)
self.sut.send('a')
self.sut.send(key.BACKSPACE)
self.sut.send(key.BACKSPACE)
self.sut.send('b')
self.sut.send(key.ENTER)
self.sut.expect("{'password': 'b'}", timeout=1)
def test_backspace_limit(self):
self.sut.expect("What's.*", timeout=1)
self.sut.send('a')
self.sut.send(key.BACKSPACE)
self.sut.send(key.BACKSPACE)
self.sut.send('b')
self.sut.send(key.ENTER)
self.sut.expect("{'password': 'b'}", timeout=1)
def test_backspace_delete_char(self, mock_print):
expected_calls = [
(tuple(),),
print_call("Yes", "selected"),
print_call("No"),
(('\x1b[3A\r\x1b[Kfoo (Y/N) Ye',), {"end": '', "flush": True},),
(('\x1b[K\n\x1b[K\n\x1b[K\n\x1b[3A',),),
]
with InputContext(readchar.key.UP, readchar.key.BACKSPACE, "\r"):
cutie.prompt_yes_or_no("foo")
self.assertEqual(mock_print.call_args_list[-5:], expected_calls)
def test_allows_deletion(self):
stdin_array = ['a', key.BACKSPACE, 'b', key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = 'Foo message'
variable = 'Bar variable'
question = questions.Password(variable, message)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
self.assertEqual('b', result)
def test_validation_fails(self):
stdin_array = [x for x in
'Invalid' + key.ENTER +
key.BACKSPACE*20 +
'9999' + key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = 'Insert number'
variable = 'foo'
expected = '9999'
question = questions.Text(variable,
validate=lambda _, x: re.match(r'\d+', x),
message=message)
sut = ConsoleRender(event_generator=stdin)
result = sut.render(question)
self.assertEqual(expected, result)
self.assertInStdout(message)
self.assertInStdout('"Invalid" is not a valid foo')
def test_invalid_phone(self):
self.set_name()
self.set_surname()
self.set_phone('abcde')
self.sut.expect('I don\'t like your phone number!', timeout=1)
self.sut.sendline(5*key.BACKSPACE + '12345')
self.sut.expect_list([re.compile(b"'name': 'foo'"),
re.compile(b"'surname': 'bar'"),
re.compile(b"'phone': '12345'")],
timeout=1)
class DefaultKeys:
"""List of default keybindings.
Attributes:
interrupt(List[str]): Keys that cause a keyboard interrupt.
select(List[str]): Keys that trigger list element selection.
confirm(List[str]): Keys that trigger list confirmation.
delete(List[str]): Keys that trigger character deletion.
down(List[str]): Keys that select the element below.
up(List[str]): Keys that select the element above.
"""
interrupt: List[str] = [readchar.key.CTRL_C, readchar.key.CTRL_D]
select: List[str] = [readchar.key.SPACE]
confirm: List[str] = [readchar.key.ENTER]
delete: List[str] = [readchar.key.BACKSPACE]
down: List[str] = [readchar.key.DOWN, 'j']
up: List[str] = [readchar.key.UP, 'k']
def get_number(
prompt: str,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
allow_float: bool = True) -> float:
"""Get a number from user input.
If an invalid number is entered the user will be prompted again.
Args:
prompt (str): The prompt asking the user to input.
min_value (float, optional): The [inclusive] minimum value.
max_value (float, optional): The [inclusive] maximum value.
def process_input(self, pressed):
if pressed == key.CTRL_C:
raise KeyboardInterrupt()
if pressed in (key.CR, key.LF, key.ENTER):
raise errors.EndOfInput(self.current)
if pressed == key.BACKSPACE:
if self.current and self.cursor_offset != len(self.current):
if self.cursor_offset > 0:
self.current = (self.current[:-self.cursor_offset - 1] +
self.current[-self.cursor_offset:])
else:
self.current = self.current[:-1]
elif pressed == key.LEFT:
if self.cursor_offset < len(self.current):
self.cursor_offset += 1
elif pressed == key.RIGHT:
self.cursor_offset = max(self.cursor_offset - 1, 0)
elif len(pressed) != 1:
return
else:
if self.cursor_offset == 0:
self.current += pressed