Skip to content

Commit

Permalink
test: eliminate for...in loops in test_irc_utils suite
Browse files Browse the repository at this point in the history
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]`.
  • Loading branch information
dgw committed Sep 15, 2024
1 parent 1cd02c4 commit 0e44186
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions test/irc/test_irc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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')

0 comments on commit 0e44186

Please sign in to comment.