From f4c46ec1c5b699f1b9c2bf9be247ba1a35a6e143 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 10 Apr 2015 18:22:02 -0300 Subject: [PATCH] Improve test legibility in TestCheckOverflow Created OverflowTestCases which is responsible to provide data for the tests --- tests/utils/test_qtutils.py | 44 +++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/tests/utils/test_qtutils.py b/tests/utils/test_qtutils.py index e3c5478f8..290f6dad3 100644 --- a/tests/utils/test_qtutils.py +++ b/tests/utils/test_qtutils.py @@ -27,9 +27,9 @@ from qutebrowser import qutebrowser from qutebrowser.utils import qtutils -class TestCheckOverflow: - - """Test check_overflow. +class OverflowTestCases: + """ + Provides test data for overflow checking. Class attributes: INT32_MIN: Minimum valid value for a signed int32. @@ -59,24 +59,47 @@ class TestCheckOverflow: (float(INT64_MAX + 1), INT64_MAX)], } - @pytest.mark.parametrize('ctype, val', [(ctype, val) for ctype, vals in - GOOD_VALUES.items() for val in - vals]) + @classmethod + def iter_good_values(cls): + """ + 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): """Test values which are inside bounds.""" qtutils.check_overflow(val, ctype) @pytest.mark.parametrize('ctype, val', - [(ctype, val) for ctype, vals in - BAD_VALUES.items() for (val, _) in vals]) + [(ctype, val) for (ctype, val, _) in + OverflowTestCases.iter_bad_values()]) def test_bad_values_fatal(self, ctype, val): """Test values which are outside bounds with fatal=True.""" with pytest.raises(OverflowError): qtutils.check_overflow(val, ctype) @pytest.mark.parametrize('ctype, val, repl', - [(ctype, val, repl) for ctype, vals in - BAD_VALUES.items() for (val, repl) in vals]) + OverflowTestCases.iter_bad_values()) def test_bad_values_nonfatal(self, ctype, val, repl): """Test values which are outside bounds with fatal=False.""" newval = qtutils.check_overflow(val, ctype, fatal=False) @@ -84,7 +107,6 @@ class TestCheckOverflow: class TestGetQtArgs: - """Tests for get_args.""" @pytest.fixture