File: test_text.py

package info (click to toggle)
python-questionary 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 980 kB
  • sloc: python: 3,920; makefile: 66
file content (52 lines) | stat: -rw-r--r-- 1,282 bytes parent folder | download | duplicates (2)
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"