Improve test legibility in TestCheckOverflow

Created OverflowTestCases which is responsible to provide data for the tests
This commit is contained in:
Bruno Oliveira 2015-04-10 18:22:02 -03:00
parent 29c51c288b
commit f4c46ec1c5

View File

@ -27,9 +27,9 @@ from qutebrowser import qutebrowser
from qutebrowser.utils import qtutils from qutebrowser.utils import qtutils
class TestCheckOverflow: class OverflowTestCases:
"""
"""Test check_overflow. Provides test data for overflow checking.
Class attributes: Class attributes:
INT32_MIN: Minimum valid value for a signed int32. INT32_MIN: Minimum valid value for a signed int32.
@ -59,24 +59,47 @@ class TestCheckOverflow:
(float(INT64_MAX + 1), INT64_MAX)], (float(INT64_MAX + 1), INT64_MAX)],
} }
@pytest.mark.parametrize('ctype, val', [(ctype, val) for ctype, vals in @classmethod
GOOD_VALUES.items() for val in def iter_good_values(cls):
vals]) """
Yields pairs of (c data type, value) which should pass overflow
checking.
"""
for ctype, values in cls.GOOD_VALUES.items():
for value in values:
yield ctype, value
@classmethod
def iter_bad_values(cls):
"""
Yields pairs of (c type, value, repl) for values which don't pass
overflow checking, and value they should be replaced with if overflow
checking should not be fatal.
"""
for ctype, values in cls.BAD_VALUES.items():
for value, repl in values:
yield ctype, value, repl
class TestCheckOverflow:
"""Test check_overflow.
"""
@pytest.mark.parametrize('ctype, val', OverflowTestCases.iter_good_values())
def test_good_values(self, ctype, val): def test_good_values(self, ctype, val):
"""Test values which are inside bounds.""" """Test values which are inside bounds."""
qtutils.check_overflow(val, ctype) qtutils.check_overflow(val, ctype)
@pytest.mark.parametrize('ctype, val', @pytest.mark.parametrize('ctype, val',
[(ctype, val) for ctype, vals in [(ctype, val) for (ctype, val, _) in
BAD_VALUES.items() for (val, _) in vals]) OverflowTestCases.iter_bad_values()])
def test_bad_values_fatal(self, ctype, val): def test_bad_values_fatal(self, ctype, val):
"""Test values which are outside bounds with fatal=True.""" """Test values which are outside bounds with fatal=True."""
with pytest.raises(OverflowError): with pytest.raises(OverflowError):
qtutils.check_overflow(val, ctype) qtutils.check_overflow(val, ctype)
@pytest.mark.parametrize('ctype, val, repl', @pytest.mark.parametrize('ctype, val, repl',
[(ctype, val, repl) for ctype, vals in OverflowTestCases.iter_bad_values())
BAD_VALUES.items() for (val, repl) in vals])
def test_bad_values_nonfatal(self, ctype, val, repl): def test_bad_values_nonfatal(self, ctype, val, repl):
"""Test values which are outside bounds with fatal=False.""" """Test values which are outside bounds with fatal=False."""
newval = qtutils.check_overflow(val, ctype, fatal=False) newval = qtutils.check_overflow(val, ctype, fatal=False)
@ -84,7 +107,6 @@ class TestCheckOverflow:
class TestGetQtArgs: class TestGetQtArgs:
"""Tests for get_args.""" """Tests for get_args."""
@pytest.fixture @pytest.fixture