Improve cmdutils tests

This would have caught 12061b8bb1.
This commit is contained in:
Florian Bruhin 2016-08-25 22:13:12 +02:00
parent 943dc564b2
commit 8823ed2821

View File

@ -183,30 +183,38 @@ class TestRegister:
@cmdutils.register(star_args_optional=True) @cmdutils.register(star_args_optional=True)
def fun(*args): def fun(*args):
"""Blah.""" """Blah."""
pass assert not args
cmdutils.cmd_dict['fun'].parser.parse_args([]) cmd = cmdutils.cmd_dict['fun']
cmd.namespace = cmd.parser.parse_args([])
args, kwargs = cmd._get_call_args(win_id=0)
fun(*args, **kwargs)
def test_flag(self): @pytest.mark.parametrize('inp, expected', [
(['--arg'], True), (['-a'], True), ([], False)])
def test_flag(self, inp, expected):
@cmdutils.register() @cmdutils.register()
def fun(arg=False): def fun(arg=False):
"""Blah.""" """Blah."""
pass assert arg == expected
parser = cmdutils.cmd_dict['fun'].parser cmd = cmdutils.cmd_dict['fun']
assert parser.parse_args(['--arg']).arg cmd.namespace = cmd.parser.parse_args(inp)
assert parser.parse_args(['-a']).arg assert cmd.namespace.arg == expected
assert not parser.parse_args([]).arg
def test_flag_argument(self): def test_flag_argument(self):
@cmdutils.register() @cmdutils.register()
@cmdutils.argument('arg', flag='b') @cmdutils.argument('arg', flag='b')
def fun(arg=False): def fun(arg=False):
"""Blah.""" """Blah."""
pass assert arg
parser = cmdutils.cmd_dict['fun'].parser cmd = cmdutils.cmd_dict['fun']
assert parser.parse_args(['-b']).arg
with pytest.raises(argparser.ArgumentParserError): with pytest.raises(argparser.ArgumentParserError):
parser.parse_args(['-a']) cmd.parser.parse_args(['-a'])
cmd.namespace = cmd.parser.parse_args(['-b'])
assert cmd.namespace.arg
args, kwargs = cmd._get_call_args(win_id=0)
fun(*args, **kwargs)
def test_partial_arg(self): def test_partial_arg(self):
"""Test with only some arguments decorated with @cmdutils.argument.""" """Test with only some arguments decorated with @cmdutils.argument."""
@ -284,7 +292,7 @@ class TestRegister:
@cmdutils.argument('arg', choices=choices) @cmdutils.argument('arg', choices=choices)
def fun(arg: typ): def fun(arg: typ):
"""Blah.""" """Blah."""
pass assert arg == expected
cmd = cmdutils.cmd_dict['fun'] cmd = cmdutils.cmd_dict['fun']
cmd.namespace = cmd.parser.parse_args([inp]) cmd.namespace = cmd.parser.parse_args([inp])
@ -293,7 +301,10 @@ class TestRegister:
with pytest.raises(cmdexc.ArgumentTypeError): with pytest.raises(cmdexc.ArgumentTypeError):
cmd._get_call_args(win_id=0) cmd._get_call_args(win_id=0)
else: else:
assert cmd._get_call_args(win_id=0) == ([expected], {}) args, kwargs = cmd._get_call_args(win_id=0)
assert args == [expected]
assert kwargs == {}
fun(*args, **kwargs)
def test_choices_no_annotation(self): def test_choices_no_annotation(self):
# https://github.com/The-Compiler/qutebrowser/issues/1871 # https://github.com/The-Compiler/qutebrowser/issues/1871