summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2020-10-07 22:53:24 +0200
committerFlorian Bruhin <me@the-compiler.org>2021-01-26 20:32:46 +0100
commit61ce42fc74193958582ac055d8dd7c9d1dc618ea (patch)
tree543e6fe9e62624d2ccfe90fa362a832db82082fd /tests
parent273734c9aa4ac80c7fa29b47255c741841ac9864 (diff)
downloadqutebrowser-61ce42fc74193958582ac055d8dd7c9d1dc618ea.tar.gz
qutebrowser-61ce42fc74193958582ac055d8dd7c9d1dc618ea.zip
Update test_typed_args for lazy annotations
This unfortunately gets quite a bit more cumbersome, as we can't easily parametrize an annotation anymore - the former `typ` will just be lazily evaluated as the string "typ". Closes #5769
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/api/test_cmdutils.py49
1 files changed, 30 insertions, 19 deletions
diff --git a/tests/unit/api/test_cmdutils.py b/tests/unit/api/test_cmdutils.py
index af70b0379..179b2e95f 100644
--- a/tests/unit/api/test_cmdutils.py
+++ b/tests/unit/api/test_cmdutils.py
@@ -25,7 +25,7 @@ import sys
import logging
import types
import enum
-from typing import Union
+import textwrap
import pytest
@@ -296,32 +296,42 @@ class TestRegister:
x = enum.auto()
y = enum.auto()
- @pytest.mark.parametrize('typ, inp, choices, expected', [
- (int, '42', None, 42),
- (int, 'x', None, cmdexc.ArgumentTypeError),
- (str, 'foo', None, 'foo'),
+ @pytest.mark.parametrize('annotation, inp, choices, expected', [
+ ('int', '42', None, 42),
+ ('int', 'x', None, cmdexc.ArgumentTypeError),
+ ('str', 'foo', None, 'foo'),
- (Union[str, int], 'foo', None, 'foo'),
- (Union[str, int], '42', None, 42),
+ ('Union[str, int]', 'foo', None, 'foo'),
+ ('Union[str, int]', '42', None, 42),
# Choices
- (str, 'foo', ['foo'], 'foo'),
- (str, 'bar', ['foo'], cmdexc.ArgumentTypeError),
+ ('str', 'foo', ['foo'], 'foo'),
+ ('str', 'bar', ['foo'], cmdexc.ArgumentTypeError),
# Choices with Union: only checked when it's a str
- (Union[str, int], 'foo', ['foo'], 'foo'),
- (Union[str, int], 'bar', ['foo'], cmdexc.ArgumentTypeError),
- (Union[str, int], '42', ['foo'], 42),
+ ('Union[str, int]', 'foo', ['foo'], 'foo'),
+ ('Union[str, int]', 'bar', ['foo'], cmdexc.ArgumentTypeError),
+ ('Union[str, int]', '42', ['foo'], 42),
- (Enum, 'x', None, Enum.x),
- (Enum, 'z', None, cmdexc.ArgumentTypeError),
+ ('Enum', 'x', None, Enum.x),
+ ('Enum', 'z', None, cmdexc.ArgumentTypeError),
])
- def test_typed_args(self, typ, inp, choices, expected):
+ def test_typed_args(self, annotation, inp, choices, expected):
+ src = textwrap.dedent("""
+ from typing import Union
+ from qutebrowser.api import cmdutils
+
@cmdutils.register()
@cmdutils.argument('arg', choices=choices)
- def fun(arg: typ):
- """Blah."""
- assert arg == expected
+ def fun(arg: {annotation}):
+ '''Blah.'''
+ return arg
+ """.format(annotation=annotation))
+ code = compile(src, '<string>', 'exec')
+ print(src)
+ ns = {'choices': choices, 'Enum': self.Enum}
+ exec(code, ns, ns)
+ fun = ns['fun']
cmd = objects.commands['fun']
cmd.namespace = cmd.parser.parse_args([inp])
@@ -333,7 +343,8 @@ class TestRegister:
args, kwargs = cmd._get_call_args(win_id=0)
assert args == [expected]
assert kwargs == {}
- fun(*args, **kwargs)
+ ret = fun(*args, **kwargs)
+ assert ret == expected
def test_choices_no_annotation(self):
# https://github.com/qutebrowser/qutebrowser/issues/1871