File: test_interaction.py

package info (click to toggle)
python-argh 0.26.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 296 kB
  • sloc: python: 1,586; makefile: 4
file content (70 lines) | stat: -rw-r--r-- 1,741 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
# coding: utf-8
"""
Interaction Tests
~~~~~~~~~~~~~~~~~
"""
import sys
import mock

import argh


def parse_choice(choice, **kwargs):
    argh.io._input = lambda prompt: choice
    return argh.confirm('test', **kwargs)


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

    assert True == parse_choice('y')
    assert True == parse_choice('y', default=True)
    assert True == parse_choice('y', default=False)
    assert True == parse_choice('y', default=None)

    assert False == parse_choice('n')
    assert False == parse_choice('n', default=True)
    assert False == parse_choice('n', default=False)
    assert False == parse_choice('n', default=None)

    assert None == parse_choice('x') == None


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

    def raw_input_mock(prompt):
        prompts.append(prompt)
    argh.io._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)'


def test_encoding():
    "Unicode is accepted as prompt message"
    raw_input_mock = mock.MagicMock()

    argh.io._input = raw_input_mock

    msg = 'привет'
    if sys.version_info <= (3,0):
        msg = msg.decode('utf-8')

    argh.confirm(msg)

    # bytes in Python 2.x, Unicode in Python 3.x
    raw_input_mock.assert_called_once_with('привет? (y/n)')