test_raise_if_options_is_not_a_non_empty_list()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nop 0
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