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
|
# -*- coding: utf-8 -*-
import re
from prompt_toolkit.validation import ValidationError
from prompt_toolkit.validation import Validator
from tests.utils import feed_cli_with_input
def test_legacy_name():
message = "What is your name"
text = "bob\r"
result, cli = feed_cli_with_input("input", message, text)
assert result == "bob"
def test_text():
message = "What is your name"
text = "bob\r"
result, cli = feed_cli_with_input("text", message, text)
assert result == "bob"
def test_text_validate():
message = "What is your name"
text = "Doe\r"
result, cli = feed_cli_with_input(
"text",
message,
text,
validate=lambda val: val == "Doe" or "is your last name Doe?",
)
assert result == "Doe"
def test_text_validate_with_class():
class SimpleValidator(Validator):
def validate(self, document):
ok = re.match("[01][01][01]", document.text)
if not ok:
raise ValidationError(
message="Binary FTW", cursor_position=len(document.text)
)
message = "What is your name"
text = "001\r"
result, cli = feed_cli_with_input("text", message, text, validate=SimpleValidator)
assert result == "001"
|