diff --git a/tests/test_cliutil.py b/tests/test_cliutil.py index 510d6152c6d8d2f9ca7723a379d580c018774a9f..b0425ed277af21864fb26b3d250ea8da9454680b 100644 --- a/tests/test_cliutil.py +++ b/tests/test_cliutil.py @@ -298,3 +298,36 @@ def test_enum_arg_choices_str_defaults(arg_enum): def test_enum_arg_choices_str_args(arg_enum): sep = '/' assert arg_enum.choices_str(sep, '{}') == sep.join(c.value for c in ArgChoices) + +@pytest.mark.parametrize('values,sep', testutil.combine_values( + [['foo'], ['bar', 'baz'], ['qu', 'quu', 'quux']], + [',', ', ', ' ,', ' , '], +)) +def test_extend_action_once(values, sep): + action = cliutil.ExtendAction(['-t'], 'result') + args = argparse.Namespace() + action(None, args, sep.join(values), '-t') + assert args.result == values + +def test_extend_action_multi(): + action = cliutil.ExtendAction(['-t'], 'result') + args = argparse.Namespace() + action(None, args, 'foo,bar', '-t') + action(None, args, 'baz, quux', '-t') + assert args.result == ['foo', 'bar', 'baz', 'quux'] + +def test_extend_action_from_default(): + action = cliutil.ExtendAction(['-t'], 'result') + args = argparse.Namespace(result=['foo']) + action(None, args, 'bar , baz', '-t') + assert args.result == ['foo', 'bar', 'baz'] + +@pytest.mark.parametrize('pattern,expected', [ + (',', ['foo', ' bar']), + (r'\s+', ['foo,', 'bar']), +]) +def test_extend_action_custom_pattern(pattern, expected): + action = cliutil.ExtendAction(['-t'], 'result', const=pattern) + args = argparse.Namespace() + action(None, args, 'foo, bar', '-t') + assert args.result == expected