Use namedtuple and parametrized fixture for TestSplit
As discussed in the PR, this greatly improves legibility
This commit is contained in:
parent
55e3645131
commit
253f3b2cd7
@ -18,6 +18,7 @@
|
|||||||
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""Tests for qutebrowser.misc.split."""
|
"""Tests for qutebrowser.misc.split."""
|
||||||
|
import collections
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ from qutebrowser.misc import split
|
|||||||
|
|
||||||
# Format: input/split|output|without|keep/split|output|with|keep/
|
# 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/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/
|
||||||
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:
|
class TestSplit:
|
||||||
|
|
||||||
"""Test split."""
|
"""Test split."""
|
||||||
|
|
||||||
@pytest.mark.parametrize('cmd, out',
|
@pytest.fixture(params=_parse_split_test_data_str())
|
||||||
[case.split('/')[:-2]
|
def split_test_case(self, request):
|
||||||
for case in test_data_lines])
|
"""
|
||||||
def test_split(self, cmd, out):
|
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."""
|
"""Test splitting."""
|
||||||
items = split.split(cmd)
|
items = split.split(split_test_case.input)
|
||||||
assert items == out.split('|')
|
assert items == split_test_case.keep
|
||||||
|
|
||||||
@pytest.mark.parametrize('cmd',
|
def test_split_keep_original(self, split_test_case):
|
||||||
[case.split('/')[0]
|
|
||||||
for case in test_data_lines])
|
|
||||||
def test_split_keep_original(self, cmd):
|
|
||||||
"""Test if splitting with keep=True yields the original string."""
|
"""Test if splitting with keep=True yields the original string."""
|
||||||
items = split.split(cmd, keep=True)
|
items = split.split(split_test_case.input, keep=True)
|
||||||
assert ''.join(items) == cmd
|
assert ''.join(items) == split_test_case.input
|
||||||
|
|
||||||
@pytest.mark.parametrize('cmd, _mid, out',
|
def test_split_keep(self, split_test_case):
|
||||||
[case.split('/')[:-1]
|
|
||||||
for case in test_data_lines])
|
|
||||||
def test_split_keep(self, cmd, _mid, out):
|
|
||||||
"""Test splitting with keep=True."""
|
"""Test splitting with keep=True."""
|
||||||
items = split.split(cmd, keep=True)
|
items = split.split(split_test_case.input, keep=True)
|
||||||
assert items == out.split('|')
|
assert items == split_test_case.no_keep
|
||||||
|
|
||||||
|
|
||||||
class TestSimpleSplit:
|
class TestSimpleSplit:
|
||||||
|
Loading…
Reference in New Issue
Block a user