File: test_question.py

package info (click to toggle)
clikit 0.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 984 kB
  • sloc: python: 9,575; sh: 6; makefile: 3
file content (83 lines) | stat: -rw-r--r-- 1,946 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os
import subprocess

import pytest

from clikit.ui.components import Question


def has_tty_available():
    devnull = open(os.devnull, "w")
    exit_code = subprocess.call(["stty", "2"], stdout=devnull, stderr=devnull)

    return exit_code == 0


TTY_AVAILABLE = has_tty_available()


def test_ask(io):
    question = Question("What time is it?", "2PM")
    io.set_input("\n8AM\n")

    assert "2PM" == question.ask(io)

    io.clear_error()

    assert "8AM" == question.ask(io)
    assert "What time is it? " == io.fetch_error()


@pytest.mark.skipif(
    not TTY_AVAILABLE, reason="`stty` is required to test hidden response functionality"
)
def test_ask_hidden_response(io):
    question = Question("What time is it?", "2PM")
    question.hide()
    io.set_input("8AM\n")

    assert "8AM" == question.ask(io)
    assert "What time is it? " == io.fetch_error()


def test_ask_and_validate(io):
    error = "This is not a color!"

    def validator(color):
        if color not in ["white", "black"]:
            raise Exception(error)

        return color

    question = Question("What color was the white horse of Henry IV?", "white")
    question.set_validator(validator)
    question.set_max_attempts(2)

    io.set_input("\nblack\n")
    assert "white" == question.ask(io)
    assert "black" == question.ask(io)

    io.set_input("green\nyellow\norange\n")

    with pytest.raises(Exception) as e:
        question.ask(io)

    assert error == str(e.value)


def test_no_interaction(io):
    io.set_interactive(False)

    question = Question("Do you have a job?", "not yet")
    assert "not yet" == question.ask(io)


def test_ask_question_with_special_characters(io):
    question = Question("What time is it, Sébastien?", "2PMë")
    io.set_input("\n")

    assert "2PMë" == question.ask(io)
    assert "What time is it, Sébastien? " == io.fetch_error()