Repository: cookiecutter/cookiecutter
File: tests/test_prompt.py (Python)

Code before:
from past.builtins import basestring

from cookiecutter import prompt
from jinja2.environment import Environment


@pytest.mark.parametrize('raw_var, rendered_var', [
    (1, '1'),
    (True, 'True'),
    ('foo', 'foo'),
    ('{{cookiecutter.project}}', 'foobar')
])
def test_convert_to_str(mocker, raw_var, rendered_var):
    env = Environment()
    from_string = mocker.patch(
        'cookiecutter.prompt.Environment.from_string',
        wraps=env.from_string
    )
    context = {'project': 'foobar'}

    result = prompt.render_variable(env, raw_var, context)
    assert result == rendered_var

    # Make sure that non str variables are conerted beforehand
    if not isinstance(raw_var, basestring):
        raw_var = str(raw_var)
    from_string.assert_called_once_with(raw_var)


@pytest.fixture(autouse=True)
def patch_readline_on_win(monkeypatch):
    if 'windows' in platform.platform().lower():
        monkeypatch.setattr('sys.stdin.readline', lambda: '\n')


class TestPrompt(object):
    def test_prompt_for_config_simple(self, monkeypatch):
        monkeypatch.setattr(
            'cookiecutter.prompt.read_user_variable',
            lambda var, default: u'Audrey Roy'
        )
        context = {'cookiecutter': {'full_name': 'Your Name'}}

        cookiecutter_dict = prompt.prompt_for_config(context)
        assert cookiecutter_dict == {'full_name': u'Audrey Roy'}

    def test_prompt_for_config_unicode(self, monkeypatch):
        monkeypatch.setattr(
            'cookiecutter.prompt.read_user_variable',
            lambda var, default: u'Pizzä ïs Gööd'
        )

Review comment (suggestion):
Right, because the assertion doesn't apply anymore (there's still the assertion above checking the return value).

I could assert that it's not called I suppose..

This test does feel like it's basically replicating the same logic as the function it's testing.. but that's probably hard to avoid with such small functions.


Code after:
from past.builtins import basestring

from cookiecutter import prompt
from jinja2.environment import Environment


@pytest.mark.parametrize('raw_var, rendered_var', [
    (1, '1'),
    (True, 'True'),
    ('foo', 'foo'),
    ('{{cookiecutter.project}}', 'foobar'),
    (None, None),
])
def test_convert_to_str(mocker, raw_var, rendered_var):
    env = Environment()
    from_string = mocker.patch(
        'cookiecutter.prompt.Environment.from_string',
        wraps=env.from_string
    )
    context = {'project': 'foobar'}

    result = prompt.render_variable(env, raw_var, context)
    assert result == rendered_var

    # Make sure that non None non str variables are conerted beforehand
    if raw_var is not None:
        if not isinstance(raw_var, basestring):
            raw_var = str(raw_var)
        from_string.assert_called_once_with(raw_var)
    else:
        assert not from_string.called


@pytest.fixture(autouse=True)
def patch_readline_on_win(monkeypatch):
    if 'windows' in platform.platform().lower():
        monkeypatch.setattr('sys.stdin.readline', lambda: '\n')


class TestPrompt(object):
    def test_prompt_for_config_simple(self, monkeypatch):
        monkeypatch.setattr(
            'cookiecutter.prompt.read_user_variable',
            lambda var, default: u'Audrey Roy'
        )
        context = {'cookiecutter': {'full_name': 'Your Name'}}

        cookiecutter_dict = prompt.prompt_for_config(context)
        assert cookiecutter_dict == {'full_name': u'Audrey Roy'}

    def test_prompt_for_config_unicode(self, monkeypatch):