1
|
|
|
"""Tests around prompting for and handling of choice variables.""" |
2
|
|
|
import click |
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
from cookiecutter.prompt import read_user_choice |
6
|
|
|
|
7
|
|
|
OPTIONS = ['hello', 'world', 'foo', 'bar'] |
8
|
|
|
|
9
|
|
|
EXPECTED_PROMPT = """Select varname: |
10
|
|
|
1 - hello |
11
|
|
|
2 - world |
12
|
|
|
3 - foo |
13
|
|
|
4 - bar |
14
|
|
|
Choose from 1, 2, 3, 4""" |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
@pytest.mark.parametrize('user_choice, expected_value', enumerate(OPTIONS, 1)) |
18
|
|
|
def test_click_invocation(mocker, user_choice, expected_value): |
19
|
|
|
"""Test click function called correctly by cookiecutter. |
20
|
|
|
|
21
|
|
|
Test for choice type invocation. |
22
|
|
|
""" |
23
|
|
|
choice = mocker.patch('click.Choice') |
24
|
|
|
choice.return_value = click.Choice(OPTIONS) |
25
|
|
|
|
26
|
|
|
prompt = mocker.patch('click.prompt') |
27
|
|
|
prompt.return_value = f'{user_choice}' |
28
|
|
|
|
29
|
|
|
assert read_user_choice('varname', OPTIONS) == expected_value |
30
|
|
|
|
31
|
|
|
prompt.assert_called_once_with( |
32
|
|
|
EXPECTED_PROMPT, type=click.Choice(OPTIONS), default='1', show_choices=False |
33
|
|
|
) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def test_raise_if_options_is_not_a_non_empty_list(): |
37
|
|
|
"""Test function called by cookiecutter raise expected errors. |
38
|
|
|
|
39
|
|
|
Test for choice type invocation. |
40
|
|
|
""" |
41
|
|
|
with pytest.raises(TypeError): |
42
|
|
|
read_user_choice('foo', 'NOT A LIST') |
43
|
|
|
|
44
|
|
|
with pytest.raises(ValueError): |
45
|
|
|
read_user_choice('foo', []) |
46
|
|
|
|