diff options
| author | Peter Kelley <peter@knewton.com> | 2016-08-19 20:45:05 -0400 |
|---|---|---|
| committer | Peter Kelley <peter@knewton.com> | 2017-03-28 22:59:12 -0400 |
| commit | 6131e96559e3a29aeb91129ac2a155af435a9433 (patch) | |
| tree | 410e0028144154a05783c7e844d84d64a4879fec /tests/test_bashcomplete.py | |
| parent | c3da86160ff25617d60947ed81c4c520fb495145 (diff) | |
| download | click-6131e96559e3a29aeb91129ac2a155af435a9433.tar.gz | |
Issue #535: Bash completion for click.Choice()
Added support for bash completion of
type=click.Choice Options and Arguments.
Added comprehensive tests for bash completion.
There are many scenarios present for arguments.
Diffstat (limited to 'tests/test_bashcomplete.py')
| -rw-r--r-- | tests/test_bashcomplete.py | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/tests/test_bashcomplete.py b/tests/test_bashcomplete.py index fea096c..42d2255 100644 --- a/tests/test_bashcomplete.py +++ b/tests/test_bashcomplete.py @@ -14,6 +14,42 @@ def test_single_command(): assert list(get_choices(cli, 'lol', [], '')) == [] +def test_boolean_flag(): + @click.command() + @click.option('--shout/--no-shout', default=False) + def cli(local_opt): + pass + + assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout'] + + +def test_multi_value_option(): + @click.group() + @click.option('--pos', nargs=2, type=float) + def cli(local_opt): + pass + + @cli.command() + @click.option('--local-opt') + def sub(local_opt): + pass + + assert list(get_choices(cli, 'lol', [], '-')) == ['--pos'] + assert list(get_choices(cli, 'lol', ['--pos'], '')) == [] + assert list(get_choices(cli, 'lol', ['--pos', '1.0'], '')) == [] + assert list(get_choices(cli, 'lol', ['--pos', '1.0', '1.0'], '')) == ['sub'] + + +def test_multi_option(): + @click.command() + @click.option('--message', '-m', multiple=True) + def cli(local_opt): + pass + + assert list(get_choices(cli, 'lol', [], '-')) == ['--message', '-m'] + assert list(get_choices(cli, 'lol', ['-m'], '')) == [] + + def test_small_chain(): @click.group() @click.option('--global-opt') @@ -60,3 +96,134 @@ def test_long_chain(): assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '')) == ['csub'] assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '-')) == ['--csub-opt'] assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '')) == [] + + +def test_argument_choice(): + @click.command() + @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12'])) + @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22'])) + @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument'])) + def cli(): + pass + + assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12'] + assert list(get_choices(cli, 'lol', [], 'arg')) == ['arg11', 'arg12'] + assert list(get_choices(cli, 'lol', ['arg11'], '')) == ['arg21', 'arg22'] + assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], '')) == ['arg', 'argument'] + assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], 'argu')) == ['argument'] + + +def test_option_choice(): + @click.command() + @click.option('--opt1', type=click.Choice(['opt11', 'opt12'])) + @click.option('--opt2', type=click.Choice(['opt21', 'opt22'])) + @click.option('--opt3', type=click.Choice(['opt', 'option'])) + def cli(): + pass + + assert list(get_choices(cli, 'lol', [], '-')) == ['--opt1', '--opt2', '--opt3'] + assert list(get_choices(cli, 'lol', [], '--opt')) == ['--opt1', '--opt2', '--opt3'] + assert list(get_choices(cli, 'lol', [], '--opt1=')) == ['opt11', 'opt12'] + assert list(get_choices(cli, 'lol', [], '--opt2=')) == ['opt21', 'opt22'] + assert list(get_choices(cli, 'lol', ['--opt2'], '=')) == ['opt21', 'opt22'] + assert list(get_choices(cli, 'lol', ['--opt2', '='], 'opt')) == ['opt21', 'opt22'] + assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12'] + assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22'] + assert list(get_choices(cli, 'lol', ['--opt1', 'opt11', '--opt2'], '')) == ['opt21', 'opt22'] + assert list(get_choices(cli, 'lol', ['--opt2', 'opt21'], '-')) == ['--opt1', '--opt3'] + assert list(get_choices(cli, 'lol', ['--opt1', 'opt11'], '-')) == ['--opt2', '--opt3'] + assert list(get_choices(cli, 'lol', ['--opt1'], 'opt')) == ['opt11', 'opt12'] + assert list(get_choices(cli, 'lol', ['--opt3'], 'opti')) == ['option'] + + assert list(get_choices(cli, 'lol', ['--opt1', 'invalid_opt'], '-')) == ['--opt2', '--opt3'] + + +def test_option_and_arg_choice(): + @click.command() + @click.option('--opt1', type=click.Choice(['opt11', 'opt12'])) + @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12'])) + @click.option('--opt2', type=click.Choice(['opt21', 'opt22'])) + def cli(): + pass + + assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12'] + assert list(get_choices(cli, 'lol', [''], '--opt1=')) == ['opt11', 'opt12'] + assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12'] + assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22'] + + +def test_boolean_flag_choice(): + @click.command() + @click.option('--shout/--no-shout', default=False) + @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2'])) + def cli(local_opt): + pass + + assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout'] + assert list(get_choices(cli, 'lol', ['--shout'], '')) == ['arg1', 'arg2'] + + +def test_multi_value_option_choice(): + @click.command() + @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2'])) + @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2'])) + def cli(local_opt): + pass + + assert list(get_choices(cli, 'lol', ['--pos'], '')) == ['pos1', 'pos2'] + assert list(get_choices(cli, 'lol', ['--pos', 'pos1'], '')) == ['pos1', 'pos2'] + assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2'], '')) == ['arg1', 'arg2'] + assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2', 'arg1'], '')) == [] + + +def test_multi_option_choice(): + @click.command() + @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2'])) + @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2'])) + def cli(local_opt): + pass + + assert list(get_choices(cli, 'lol', ['-m'], '')) == ['m1', 'm2'] + assert list(get_choices(cli, 'lol', ['-m', 'm1', '-m'], '')) == ['m1', 'm2'] + assert list(get_choices(cli, 'lol', ['-m', 'm1'], '')) == ['arg1', 'arg2'] + + +def test_variadic_argument_choice(): + @click.command() + @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2'])) + def cli(local_opt): + pass + + assert list(get_choices(cli, 'lol', ['src1', 'src2'], '')) == ['src1', 'src2'] + + +def test_long_chain_choice(): + @click.group() + def cli(): + pass + + @cli.group('sub') + @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2'])) + @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2'])) + def sub(sub_opt): + pass + + @sub.command('bsub') + @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2'])) + @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2'])) + @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2'])) + def bsub(bsub_opt): + pass + + assert list(get_choices(cli, 'lol', ['sub'], '')) == ['subarg1', 'subarg2'] + assert list(get_choices(cli, 'lol', ['sub', '--sub-opt'], '')) == ['subopt1', 'subopt2'] + assert list(get_choices(cli, 'lol', ['sub', '--sub-opt', 'subopt1'], '')) == \ + ['subarg1', 'subarg2'] + assert list(get_choices(cli, 'lol', + ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-')) == ['--bsub-opt'] + assert list(get_choices(cli, 'lol', + ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '')) == \ + ['bsubopt1', 'bsubopt2'] + assert list(get_choices(cli, 'lol', + ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'], + '')) == ['bbsubarg1', 'bbsubarg2'] |
