qutebrowser/tests/utils/test_qtutils.py

123 lines
4.4 KiB
Python
Raw Normal View History

2014-06-23 07:45:04 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2015-01-03 15:51:31 +01:00
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-06-23 07:45:04 +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/>.
2014-08-26 20:25:11 +02:00
"""Tests for qutebrowser.utils.qtutils."""
2014-06-23 07:45:04 +02:00
import sys
2015-04-09 01:23:52 +02:00
import pytest
2014-06-23 07:45:04 +02:00
from qutebrowser import qutebrowser
2014-08-26 20:25:11 +02:00
from qutebrowser.utils import qtutils
2014-06-23 07:45:04 +02:00
2015-04-09 01:23:52 +02:00
class TestCheckOverflow:
2015-04-09 07:35:33 +02:00
2014-06-23 07:45:04 +02:00
"""Test check_overflow.
Class attributes:
INT32_MIN: Minimum valid value for a signed int32.
INT32_MAX: Maximum valid value for a signed int32.
INT64_MIN: Minimum valid value for a signed int64.
INT64_MAX: Maximum valid value for a signed int64.
GOOD_VALUES: A dict of types mapped to a list of good values.
BAD_VALUES: A dict of types mapped to a list of bad values.
"""
INT32_MIN = -(2 ** 31)
INT32_MAX = 2 ** 31 - 1
INT64_MIN = -(2 ** 63)
INT64_MAX = 2 ** 63 - 1
GOOD_VALUES = {
'int': [-1, 0, 1, 23.42, INT32_MIN, INT32_MAX],
'int64': [-1, 0, 1, 23.42, INT64_MIN, INT64_MAX],
}
BAD_VALUES = {
'int': [(INT32_MIN - 1, INT32_MIN),
(INT32_MAX + 1, INT32_MAX),
(float(INT32_MAX + 1), INT32_MAX)],
'int64': [(INT64_MIN - 1, INT64_MIN),
(INT64_MAX + 1, INT64_MAX),
(float(INT64_MAX + 1), INT64_MAX)],
}
2015-04-09 01:23:52 +02:00
@pytest.mark.parametrize('ctype, val', [(ctype, val) for ctype, vals in
GOOD_VALUES.items() for val in
vals])
def test_good_values(self, ctype, val):
2014-06-23 07:45:04 +02:00
"""Test values which are inside bounds."""
2015-04-09 01:23:52 +02:00
qtutils.check_overflow(val, ctype)
2014-06-23 07:45:04 +02:00
2015-04-09 01:23:52 +02:00
@pytest.mark.parametrize('ctype, val',
2015-04-09 07:35:33 +02:00
[(ctype, val) for ctype, vals in
BAD_VALUES.items() for (val, _) in vals])
2015-04-09 01:23:52 +02:00
def test_bad_values_fatal(self, ctype, val):
2014-06-23 07:45:04 +02:00
"""Test values which are outside bounds with fatal=True."""
2015-04-09 01:23:52 +02:00
with pytest.raises(OverflowError):
qtutils.check_overflow(val, ctype)
2014-06-23 07:45:04 +02:00
2015-04-09 01:23:52 +02:00
@pytest.mark.parametrize('ctype, val, repl',
[(ctype, val, repl) for ctype, vals in
BAD_VALUES.items() for (val, repl) in vals])
def test_bad_values_nonfatal(self, ctype, val, repl):
2014-06-23 07:45:04 +02:00
"""Test values which are outside bounds with fatal=False."""
2015-04-09 01:23:52 +02:00
newval = qtutils.check_overflow(val, ctype, fatal=False)
assert newval == repl
2015-04-09 01:23:52 +02:00
class TestGetQtArgs:
2015-04-09 07:35:33 +02:00
"""Tests for get_args."""
2014-06-23 07:45:04 +02:00
2015-04-09 01:23:52 +02:00
@pytest.fixture
def parser(self, mocker):
2015-04-09 07:35:33 +02:00
"""Fixture to provide an argparser.
Monkey-patches .exit() of the argparser so it doesn't exit on errors.
"""
2015-04-09 01:23:52 +02:00
parser = qutebrowser.get_argparser()
mocker.patch.object(parser, 'exit', side_effect=Exception)
return parser
2014-06-23 07:45:04 +02:00
2015-04-09 01:23:52 +02:00
def test_no_qt_args(self, parser):
2014-06-23 07:45:04 +02:00
"""Test commandline with no Qt arguments given."""
2015-04-09 01:23:52 +02:00
args = parser.parse_args(['--debug'])
assert qtutils.get_args(args) == [sys.argv[0]]
2014-06-23 07:45:04 +02:00
2015-04-09 01:23:52 +02:00
def test_qt_flag(self, parser):
2014-06-23 07:45:04 +02:00
"""Test commandline with a Qt flag."""
2015-04-09 01:23:52 +02:00
args = parser.parse_args(['--debug', '--qt-reverse', '--nocolor'])
assert qtutils.get_args(args) == [sys.argv[0], '-reverse']
2014-06-23 07:45:04 +02:00
2015-04-09 01:23:52 +02:00
def test_qt_arg(self, parser):
2014-06-23 07:45:04 +02:00
"""Test commandline with a Qt argument."""
2015-04-09 01:23:52 +02:00
args = parser.parse_args(['--qt-stylesheet', 'foobar'])
assert qtutils.get_args(args) == [sys.argv[0], '-stylesheet', 'foobar']
2014-06-23 07:45:04 +02:00
2015-04-09 01:23:52 +02:00
def test_qt_both(self, parser):
2014-06-23 07:45:04 +02:00
"""Test commandline with a Qt argument and flag."""
2015-04-09 01:23:52 +02:00
args = parser.parse_args(['--qt-stylesheet', 'foobar', '--qt-reverse'])
qt_args = qtutils.get_args(args)
2015-04-09 01:23:52 +02:00
assert qt_args[0] == sys.argv[0]
assert '-reverse' in qt_args
assert '-stylesheet' in qt_args
assert 'foobar' in qt_args