2015-08-20 07:09:49 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2017-05-09 21:37:03 +02:00
|
|
|
# Copyright 2015-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2015-08-20 07:09:49 +02:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Tests for qutebrowser.commands.argparser."""
|
|
|
|
|
2016-05-11 08:01:25 +02:00
|
|
|
import inspect
|
|
|
|
|
2015-08-20 07:09:49 +02:00
|
|
|
import pytest
|
|
|
|
from PyQt5.QtCore import QUrl
|
|
|
|
|
|
|
|
from qutebrowser.commands import argparser, cmdexc
|
|
|
|
from qutebrowser.utils import usertypes, objreg
|
|
|
|
|
|
|
|
|
|
|
|
Enum = usertypes.enum('Enum', ['foo', 'foo_bar'])
|
|
|
|
|
|
|
|
|
|
|
|
class FakeTabbedBrowser:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.opened_url = None
|
|
|
|
|
|
|
|
def tabopen(self, url):
|
|
|
|
self.opened_url = url
|
|
|
|
|
|
|
|
|
|
|
|
class TestArgumentParser:
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def parser(self):
|
|
|
|
return argparser.ArgumentParser('foo')
|
|
|
|
|
2016-08-22 07:40:24 +02:00
|
|
|
@pytest.fixture
|
2015-08-20 07:09:49 +02:00
|
|
|
def tabbed_browser(self, win_registry):
|
|
|
|
tb = FakeTabbedBrowser()
|
|
|
|
objreg.register('tabbed-browser', tb, scope='window', window=0)
|
|
|
|
yield tb
|
|
|
|
objreg.delete('tabbed-browser', scope='window', window=0)
|
|
|
|
|
|
|
|
def test_name(self, parser):
|
|
|
|
assert parser.name == 'foo'
|
|
|
|
|
|
|
|
def test_exit(self, parser):
|
|
|
|
parser.add_argument('--help', action='help')
|
|
|
|
|
|
|
|
with pytest.raises(argparser.ArgumentParserExit) as excinfo:
|
|
|
|
parser.parse_args(['--help'])
|
|
|
|
|
|
|
|
assert excinfo.value.status == 0
|
|
|
|
|
|
|
|
def test_error(self, parser):
|
|
|
|
with pytest.raises(argparser.ArgumentParserError) as excinfo:
|
|
|
|
parser.parse_args(['--foo'])
|
|
|
|
assert str(excinfo.value) == "Unrecognized arguments: --foo"
|
|
|
|
|
|
|
|
def test_help(self, parser, tabbed_browser):
|
|
|
|
parser.add_argument('--help', action=argparser.HelpAction, nargs=0)
|
|
|
|
|
|
|
|
with pytest.raises(argparser.ArgumentParserExit):
|
|
|
|
parser.parse_args(['--help'])
|
|
|
|
|
|
|
|
expected_url = QUrl('qute://help/commands.html#foo')
|
|
|
|
assert tabbed_browser.opened_url == expected_url
|
|
|
|
|
|
|
|
|
2016-05-11 08:01:25 +02:00
|
|
|
@pytest.mark.parametrize('types, value, expected', [
|
|
|
|
([Enum], 'foo', Enum.foo),
|
|
|
|
([Enum], 'foo-bar', Enum.foo_bar),
|
|
|
|
|
|
|
|
([int], '2', 2),
|
|
|
|
([int, str], 'foo', 'foo'),
|
2015-08-20 07:09:49 +02:00
|
|
|
])
|
2016-05-11 08:01:25 +02:00
|
|
|
@pytest.mark.parametrize('multi', [True, False])
|
2016-05-17 18:53:48 +02:00
|
|
|
def test_type_conv_valid(types, value, expected, multi):
|
2016-05-11 08:01:25 +02:00
|
|
|
param = inspect.Parameter('foo', inspect.Parameter.POSITIONAL_ONLY)
|
|
|
|
|
|
|
|
if multi:
|
2016-05-17 18:53:48 +02:00
|
|
|
assert argparser.multitype_conv(param, types, value) == expected
|
2016-05-11 08:01:25 +02:00
|
|
|
elif len(types) == 1:
|
2016-05-17 18:53:48 +02:00
|
|
|
assert argparser.type_conv(param, types[0], value) == expected
|
2015-08-20 07:09:49 +02:00
|
|
|
|
2016-05-11 08:01:25 +02:00
|
|
|
|
2016-05-17 18:53:48 +02:00
|
|
|
@pytest.mark.parametrize('typ, value', [
|
|
|
|
(Enum, 'blubb'),
|
|
|
|
(Enum, 'foo_bar'),
|
|
|
|
(int, '2.5'),
|
|
|
|
(int, 'foo'),
|
|
|
|
])
|
|
|
|
@pytest.mark.parametrize('multi', [True, False])
|
|
|
|
def test_type_conv_invalid(typ, value, multi):
|
|
|
|
param = inspect.Parameter('foo', inspect.Parameter.POSITIONAL_ONLY)
|
|
|
|
|
|
|
|
with pytest.raises(cmdexc.ArgumentTypeError) as excinfo:
|
2016-05-11 08:01:25 +02:00
|
|
|
if multi:
|
2016-05-17 18:53:48 +02:00
|
|
|
argparser.multitype_conv(param, [typ], value)
|
2016-05-11 08:01:25 +02:00
|
|
|
else:
|
2016-05-17 18:53:48 +02:00
|
|
|
argparser.type_conv(param, typ, value)
|
|
|
|
|
|
|
|
if multi:
|
|
|
|
msg = 'foo: Invalid value {}'.format(value)
|
|
|
|
elif typ is Enum:
|
|
|
|
msg = ('foo: Invalid value {} - expected one of: foo, '
|
|
|
|
'foo-bar'.format(value))
|
|
|
|
else:
|
|
|
|
msg = 'foo: Invalid {} value {}'.format(typ.__name__, value)
|
|
|
|
assert str(excinfo.value) == msg
|
2015-08-20 07:09:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_multitype_conv_invalid_type():
|
2016-05-11 08:01:25 +02:00
|
|
|
"""Test using an invalid type with a multitype converter."""
|
|
|
|
param = inspect.Parameter('foo', inspect.Parameter.POSITIONAL_ONLY)
|
2015-08-20 07:09:49 +02:00
|
|
|
with pytest.raises(ValueError) as excinfo:
|
2016-05-17 18:53:48 +02:00
|
|
|
argparser.multitype_conv(param, [None], '')
|
2016-05-11 08:01:25 +02:00
|
|
|
assert str(excinfo.value) == "foo: Unknown type None!"
|
|
|
|
|
|
|
|
|
2016-05-18 06:59:26 +02:00
|
|
|
@pytest.mark.parametrize('value, typ', [(None, None), (42, int)])
|
|
|
|
def test_conv_default_param(value, typ):
|
2016-05-11 08:01:25 +02:00
|
|
|
"""The default value should always be a valid choice."""
|
2016-05-18 06:59:26 +02:00
|
|
|
def func(foo=value):
|
2016-05-11 08:01:25 +02:00
|
|
|
pass
|
|
|
|
param = inspect.signature(func).parameters['foo']
|
2016-05-18 06:59:26 +02:00
|
|
|
assert argparser.type_conv(param, typ, value, str_choices=['val']) == value
|
2016-05-11 08:01:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_conv_str_type():
|
|
|
|
"""Using a str literal as type used to mean exactly that's a valid value.
|
|
|
|
|
|
|
|
This got replaced by @cmdutils.argument(..., choices=...), so we make sure
|
|
|
|
no string annotations are there anymore.
|
|
|
|
"""
|
|
|
|
param = inspect.Parameter('foo', inspect.Parameter.POSITIONAL_ONLY)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
2016-05-17 18:53:48 +02:00
|
|
|
argparser.type_conv(param, 'val', None)
|
2016-05-11 08:01:25 +02:00
|
|
|
assert str(excinfo.value) == 'foo: Legacy string type!'
|
|
|
|
|
|
|
|
|
|
|
|
def test_conv_str_choices_valid():
|
|
|
|
"""Calling str type with str_choices and valid value."""
|
|
|
|
param = inspect.Parameter('foo', inspect.Parameter.POSITIONAL_ONLY)
|
2016-05-17 18:53:48 +02:00
|
|
|
converted = argparser.type_conv(param, str, 'val1',
|
|
|
|
str_choices=['val1', 'val2'])
|
|
|
|
assert converted == 'val1'
|
2016-05-11 08:01:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_conv_str_choices_invalid():
|
|
|
|
"""Calling str type with str_choices and invalid value."""
|
|
|
|
param = inspect.Parameter('foo', inspect.Parameter.POSITIONAL_ONLY)
|
|
|
|
with pytest.raises(cmdexc.ArgumentTypeError) as excinfo:
|
2016-05-17 18:53:48 +02:00
|
|
|
argparser.type_conv(param, str, 'val3', str_choices=['val1', 'val2'])
|
2016-05-11 08:01:25 +02:00
|
|
|
msg = 'foo: Invalid value val3 - expected one of: val1, val2'
|
|
|
|
assert str(excinfo.value) == msg
|