From 0e4418691ba4a6af1fdf56e6224b61270af8643f Mon Sep 17 00:00:00 2001 From: dgw Date: Sun, 15 Sep 2024 14:20:14 -0500 Subject: [PATCH] test: eliminate for...in loops in test_irc_utils suite Using `pytest.mark.parametrize()` instead. Two test cases become 12. Note: Yes, it is possible to have pytest treat each tuple generated by `itertools.permutations()` as a single argument. Doing so yields useless generated test-case names like `test_safe[seq0]`, though. Using three separate test parameters of simple types (`str` or `bytes`) yields more informative names like `test_safe[\n-\r-\x00]`. --- test/irc/test_irc_utils.py | 56 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/test/irc/test_irc_utils.py b/test/irc/test_irc_utils.py index 3204444e2..47f2e3d9a 100644 --- a/test/irc/test_irc_utils.py +++ b/test/irc/test_irc_utils.py @@ -8,21 +8,21 @@ from sopel.irc import utils -def test_safe(): +@pytest.mark.parametrize('s1, s2, s3', permutations(('\n', '\r', '\x00'))) +def test_safe(s1, s2, s3): text = 'some text' - variants = permutations(('\n', '\r', '\x00')) - for variant in variants: - seq = ''.join(variant) - assert utils.safe(text + seq) == text - assert utils.safe(seq + text) == text - assert utils.safe('some ' + seq + 'text') == text - assert utils.safe( - variant[0] - + 'some ' - + variant[1] - + 'text' - + variant[2] - ) == text + seq = ''.join((s1, s2, s3)) + + assert utils.safe(text + seq) == text + assert utils.safe(seq + text) == text + assert utils.safe('some ' + seq + 'text') == text + assert utils.safe( + s1 + + 'some ' + + s2 + + 'text' + + s3 + ) == text def test_safe_empty(): @@ -35,18 +35,18 @@ def test_safe_none(): utils.safe(None) -def test_safe_bytes(): +@pytest.mark.parametrize('b1, b2, b3', permutations((b'\n', b'\r', b'\x00'))) +def test_safe_bytes(b1, b2, b3): text = b'some text' - variants = permutations((b'\n', b'\r', b'\x00')) - for variant in variants: - seq = b''.join(variant) - assert utils.safe(text + seq) == text.decode('utf-8') - assert utils.safe(seq + text) == text.decode('utf-8') - assert utils.safe(b'some ' + seq + b'text') == text.decode('utf-8') - assert utils.safe( - variant[0] - + b'some ' - + variant[1] - + b'text' - + variant[2] - ) == text.decode('utf-8') + seq = b''.join((b1, b2, b3)) + + assert utils.safe(text + seq) == text.decode('utf-8') + assert utils.safe(seq + text) == text.decode('utf-8') + assert utils.safe(b'some ' + seq + b'text') == text.decode('utf-8') + assert utils.safe( + b1 + + b'some ' + + b2 + + b'text' + + b3 + ) == text.decode('utf-8')