Skip to content
Myr

test_types.py

This file

Path

tests/test_types.py

80 lines · 2.7 KiB · Python

Last changed by

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

History of this file

Take it

1import pytest23import click456@pytest.mark.parametrize(7    ("type", "value", "expect"),8    [9        (click.IntRange(0, 5), "3", 3),10        (click.IntRange(5), "5", 5),11        (click.IntRange(5), "100", 100),12        (click.IntRange(max=5), "5", 5),13        (click.IntRange(max=5), "-100", -100),14        (click.IntRange(0, clamp=True), "-1", 0),15        (click.IntRange(max=5, clamp=True), "6", 5),16        (click.IntRange(0, min_open=True, clamp=True), "0", 1),17        (click.IntRange(max=5, max_open=True, clamp=True), "5", 4),18        (click.FloatRange(0.5, 1.5), "1.2", 1.2),19        (click.FloatRange(0.5, min_open=True), "0.51", 0.51),20        (click.FloatRange(max=1.5, max_open=True), "1.49", 1.49),21        (click.FloatRange(0.5, clamp=True), "-0.0", 0.5),22        (click.FloatRange(max=1.5, clamp=True), "inf", 1.5),23    ],24)25def test_range(type, value, expect):26    assert type.convert(value, None, None) == expect272829@pytest.mark.parametrize(30    ("type", "value", "expect"),31    [32        (click.IntRange(0, 5), "6", "6 is not in the range 0<=x<=5."),33        (click.IntRange(5), "4", "4 is not in the range x>=5."),34        (click.IntRange(max=5), "6", "6 is not in the range x<=5."),35        (click.IntRange(0, 5, min_open=True), 0, "0<x<=5"),36        (click.IntRange(0, 5, max_open=True), 5, "0<=x<5"),37        (click.FloatRange(0.5, min_open=True), 0.5, "x>0.5"),38        (click.FloatRange(max=1.5, max_open=True), 1.5, "x<1.5"),39    ],40)41def test_range_fail(type, value, expect):42    with pytest.raises(click.BadParameter) as exc_info:43        type.convert(value, None, None)4445    assert expect in exc_info.value.message464748def test_float_range_no_clamp_open():49    with pytest.raises(TypeError):50        click.FloatRange(0, 1, max_open=True, clamp=True)5152    sneaky = click.FloatRange(0, 1, max_open=True)53    sneaky.clamp = True5455    with pytest.raises(RuntimeError):56        sneaky.convert("1.5", None, None)575859@pytest.mark.parametrize(60    ("nargs", "multiple", "default", "expect"),61    [62        (2, False, None, None),63        (2, False, (None, None), (None, None)),64        (None, True, None, ()),65        (None, True, (None, None), (None, None)),66        (2, True, None, ()),67        (2, True, [(None, None)], ((None, None),)),68        (-1, None, None, ()),69    ],70)71def test_cast_multi_default(runner, nargs, multiple, default, expect):72    if nargs == -1:73        param = click.Argument(["a"], nargs=nargs, default=default)74    else:75        param = click.Option(["-a"], nargs=nargs, multiple=multiple, default=default)7677    cli = click.Command("cli", params=[param], callback=lambda a: a)78    result = runner.invoke(cli, standalone_mode=False)79    assert result.exception is None80    assert result.return_value == expect