From 253f3b2cd7e99776aa33c3e2ad165554fa5942cd Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 9 Apr 2015 18:40:56 -0300 Subject: [PATCH] Use namedtuple and parametrized fixture for TestSplit As discussed in the PR, this greatly improves legibility --- tests/misc/test_split.py | 58 ++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/tests/misc/test_split.py b/tests/misc/test_split.py index e926368e0..284a406be 100644 --- a/tests/misc/test_split.py +++ b/tests/misc/test_split.py @@ -18,6 +18,7 @@ # along with qutebrowser. If not, see . """Tests for qutebrowser.misc.split.""" +import collections import pytest @@ -29,7 +30,7 @@ from qutebrowser.misc import split # Format: input/split|output|without|keep/split|output|with|keep/ -test_data = r""" +test_data_str = r""" one two/one|two/one| two/ one "two three" four/one|two three|four/one| "two three"| four/ one 'two three' four/one|two three|four/one| 'two three'| four/ @@ -103,36 +104,53 @@ foo\ bar/foo bar/foo\ bar/ áéíóú/áéíóú/áéíóú/ """ -test_data_lines = test_data.strip().splitlines() + +def _parse_split_test_data_str(): + """ + Parse the test data set into a namedtuple to use in tests. + + Returns: + A list of namedtuples with str attributes: input, keep, no_keep + """ + tuple_class = collections.namedtuple('TestCase', 'input, keep, no_keep') + + result = [] + for line in test_data_str.splitlines(): + if not line: + continue + data = line.split('/') + item = tuple_class(input=data[0], keep=data[1].split('|'), + no_keep=data[2].split('|')) + result.append(item) + return result class TestSplit: """Test split.""" - @pytest.mark.parametrize('cmd, out', - [case.split('/')[:-2] - for case in test_data_lines]) - def test_split(self, cmd, out): + @pytest.fixture(params=_parse_split_test_data_str()) + def split_test_case(self, request): + """ + Fixture that will automatically parametrize all tests the depend on it + using the parsed test case data. + """ + return request.param + + def test_split(self, split_test_case): """Test splitting.""" - items = split.split(cmd) - assert items == out.split('|') + items = split.split(split_test_case.input) + assert items == split_test_case.keep - @pytest.mark.parametrize('cmd', - [case.split('/')[0] - for case in test_data_lines]) - def test_split_keep_original(self, cmd): + def test_split_keep_original(self, split_test_case): """Test if splitting with keep=True yields the original string.""" - items = split.split(cmd, keep=True) - assert ''.join(items) == cmd + items = split.split(split_test_case.input, keep=True) + assert ''.join(items) == split_test_case.input - @pytest.mark.parametrize('cmd, _mid, out', - [case.split('/')[:-1] - for case in test_data_lines]) - def test_split_keep(self, cmd, _mid, out): + def test_split_keep(self, split_test_case): """Test splitting with keep=True.""" - items = split.split(cmd, keep=True) - assert items == out.split('|') + items = split.split(split_test_case.input, keep=True) + assert items == split_test_case.no_keep class TestSimpleSplit: