def test_float_range_option(runner): @click.command() @click.option('--x', type=click.FloatRange(0, 5)) def cli(x): click.echo(x) result = runner.invoke(cli, ['--x=5.0']) assert not result.exception assert result.output == '5.0\n' result = runner.invoke(cli, ['--x=6.0']) assert result.exit_code == 2 assert 'Invalid value for "--x": 6.0 is not in the valid range of 0 to 5.\n' \ in result.output @click.command() @click.option('--x', type=click.FloatRange(0, 5, clamp=True)) def clamp(x): click.echo(x) result = runner.invoke(clamp, ['--x=5.0']) assert not result.exception assert result.output == '5.0\n' result = runner.invoke(clamp, ['--x=6.0']) assert not result.exception assert result.output == '5\n' result = runner.invoke(clamp, ['--x=-1.0']) assert not result.exception assert result.output == '0\n'
def test_float_range_option(runner): @click.command() @click.option("--x", type=click.FloatRange(0, 5)) def cli(x): click.echo(x) result = runner.invoke(cli, ["--x=5.0"]) assert not result.exception assert result.output == "5.0\n" result = runner.invoke(cli, ["--x=6.0"]) assert result.exit_code == 2 assert ( "Invalid value for '--x': 6.0 is not in the valid range of 0 to 5.\n" in result.output) @click.command() @click.option("--x", type=click.FloatRange(0, 5, clamp=True)) def clamp(x): click.echo(x) result = runner.invoke(clamp, ["--x=5.0"]) assert not result.exception assert result.output == "5.0\n" result = runner.invoke(clamp, ["--x=6.0"]) assert not result.exception assert result.output == "5\n" result = runner.invoke(clamp, ["--x=-1.0"]) assert not result.exception assert result.output == "0\n"
def test_float_range_no_clamp_open(): with pytest.raises(TypeError): click.FloatRange(0, 1, max_open=True, clamp=True) sneaky = click.FloatRange(0, 1, max_open=True) sneaky.clamp = True with pytest.raises(RuntimeError): sneaky.convert("1.5", None, None)
"name": "integer range", "min": 0, "max": 10, "min_open": False, "max_open": False, "clamp": True, }, id="IntRange ParamType", ), pytest.param(click.FLOAT, { "param_type": "Float", "name": "float" }, id="FLOAT ParamType"), pytest.param( click.FloatRange(-0.5, 0.5), { "param_type": "FloatRange", "name": "float range", "min": -0.5, "max": 0.5, "min_open": False, "max_open": False, "clamp": False, }, id="FloatRange ParamType", ), pytest.param(*BOOL_PARAM_TYPE, id="Bool ParamType"), pytest.param(click.UUID, { "param_type": "UUID", "name": "uuid"
import asyncclick as click @pytest.mark.parametrize( ("type", "value", "expect"), [ (click.IntRange(0, 5), "3", 3), (click.IntRange(5), "5", 5), (click.IntRange(5), "100", 100), (click.IntRange(max=5), "5", 5), (click.IntRange(max=5), "-100", -100), (click.IntRange(0, clamp=True), "-1", 0), (click.IntRange(max=5, clamp=True), "6", 5), (click.IntRange(0, min_open=True, clamp=True), "0", 1), (click.IntRange(max=5, max_open=True, clamp=True), "5", 4), (click.FloatRange(0.5, 1.5), "1.2", 1.2), (click.FloatRange(0.5, min_open=True), "0.51", 0.51), (click.FloatRange(max=1.5, max_open=True), "1.49", 1.49), (click.FloatRange(0.5, clamp=True), "-0.0", 0.5), (click.FloatRange(max=1.5, clamp=True), "inf", 1.5), ], ) def test_range(type, value, expect): assert type.convert(value, None, None) == expect @pytest.mark.parametrize( ("type", "value", "expect"), [ (click.IntRange(0, 5), "6", "6 is not in the range 0<=x<=5."), (click.IntRange(5), "4", "4 is not in the range x>=5."),