File: test_interaction.py

package info (click to toggle)
python-argh 0.31.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: python: 3,234; makefile: 4
file content (79 lines) | stat: -rw-r--r-- 2,040 bytes parent folder | download
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
"""
Interaction Tests
~~~~~~~~~~~~~~~~~
"""

import unittest.mock as mock

import argh


def parse_choice(choice, **kwargs):
    with mock.patch("argh.interaction.input", lambda prompt: choice):
        return argh.confirm("test", **kwargs)


def test_simple():
    assert parse_choice("") is None
    assert parse_choice("", default=None) is None
    assert parse_choice("", default=True) is True
    assert parse_choice("", default=False) is False

    assert parse_choice("y") is True
    assert parse_choice("y", default=True) is True
    assert parse_choice("y", default=False) is True
    assert parse_choice("y", default=None) is True

    assert parse_choice("n") is False
    assert parse_choice("n", default=True) is False
    assert parse_choice("n", default=False) is False
    assert parse_choice("n", default=None) is False

    assert parse_choice("x") is None


def test_prompt():
    "Prompt is properly formatted"
    prompts = []

    def raw_input_mock(prompt):
        prompts.append(prompt)

    with mock.patch("argh.interaction.input", raw_input_mock):
        argh.confirm("do smth")
        assert prompts[-1] == "do smth? (y/n)"

        argh.confirm("do smth", default=None)
        assert prompts[-1] == "do smth? (y/n)"

        argh.confirm("do smth", default=True)
        assert prompts[-1] == "do smth? (Y/n)"

        argh.confirm("do smth", default=False)
        assert prompts[-1] == "do smth? (y/N)"


@mock.patch("argh.interaction.input")
def test_encoding(mock_input):
    "Unicode is accepted as prompt message"

    msg = "привет"

    argh.confirm(msg)

    mock_input.assert_called_once_with("привет? (y/n)")


@mock.patch("argh.interaction.input")
def test_skip(mock_input):
    retval = argh.confirm("test", default=123, skip=True)

    assert retval == 123
    mock_input.assert_not_called()


@mock.patch("argh.interaction.input")
def test_keyboard_interrupt(mock_input):
    mock_input.side_effect = KeyboardInterrupt
    retval = argh.confirm("test")
    assert retval is None