Use pytest parametrization instead of ad-hoc method generation

This commit is contained in:
Valentin Lorentz 2021-07-04 16:38:39 +02:00 committed by Val Lorentz
parent 0177c369dd
commit 92a73ad4a5
3 changed files with 47 additions and 55 deletions

View File

@ -3,6 +3,8 @@ correctly. Also checks truncation"""
import socket import socket
import pytest
from irctest import cases from irctest import cases
from irctest.irc_utils import message_parser from irctest.irc_utils import message_parser
from irctest.numerics import ERR_INPUTTOOLONG from irctest.numerics import ERR_INPUTTOOLONG
@ -26,8 +28,19 @@ def _sendBytePerByte(self, line):
self.clients[1].conn.sendall(bytes([byte])) self.clients[1].conn.sendall(bytes([byte]))
def _testNoTags(sender_function, colon): class BufferingTestCase(cases.BaseServerTestCase):
def f(self): @pytest.mark.parametrize(
"sender_function,colon",
[
pytest.param(_sendWhole, "", id="whole-no colon"),
pytest.param(_sendCharPerChar, "", id="charperchar-no colon"),
pytest.param(_sendBytePerByte, "", id="byteperbyte-no colon"),
pytest.param(_sendWhole, ":", id="whole-colon"),
pytest.param(_sendCharPerChar, ":", id="charperchar-colon"),
pytest.param(_sendBytePerByte, ":", id="byteperbyte-colon"),
],
)
def testNoTags(self, sender_function, colon):
self.connectClient("nick1") self.connectClient("nick1")
self.connectClient("nick2") self.connectClient("nick2")
@ -97,12 +110,6 @@ def _testNoTags(sender_function, colon):
f"but got {payload!r}", f"but got {payload!r}",
) )
return f
class BufferingTestCase(cases.BaseServerTestCase):
# show_io = False
def get_overhead(self, client1, client2, colon): def get_overhead(self, client1, client2, colon):
self.sendLine(client1, f"PRIVMSG nick2 {colon}a\r\n") self.sendLine(client1, f"PRIVMSG nick2 {colon}a\r\n")
line = self._getLine(client2) line = self._getLine(client2)
@ -118,10 +125,3 @@ class BufferingTestCase(cases.BaseServerTestCase):
line += data line += data
if not data or data.endswith(b"\r\n"): if not data or data.endswith(b"\r\n"):
return line return line
testNoTagsWholeNoColon = _testNoTags(_sendWhole, colon="")
testNoTagsCharPerCharNoColon = _testNoTags(_sendCharPerChar, colon="")
testNoTagsBytePerByteNoColon = _testNoTags(_sendBytePerByte, colon="")
testNoTagsWholeColon = _testNoTags(_sendWhole, colon=":")
testNoTagsCharPerCharColon = _testNoTags(_sendCharPerChar, colon=":")
testNoTagsBytePerByteColon = _testNoTags(_sendBytePerByte, colon=":")

View File

@ -6,6 +6,8 @@ Section 3.2 of RFC 2812
import math import math
import time import time
import pytest
from irctest import cases, client_mock, runner from irctest import cases, client_mock, runner
from irctest.irc_utils import ambiguities from irctest.irc_utils import ambiguities
from irctest.irc_utils.junkdrawer import ircv3_timestamp_to_unixtime from irctest.irc_utils.junkdrawer import ircv3_timestamp_to_unixtime
@ -708,11 +710,18 @@ class JoinTestCase(cases.BaseServerTestCase):
) )
def _testChannelsEquivalent(casemapping, name1, name2): class TestChannelCaseSensitivity(cases.BaseServerTestCase):
"""Generates test functions""" @pytest.mark.parametrize(
"casemapping,name1,name2",
[
("ascii", "#Foo", "#foo"),
("rfc1459", "#Foo", "#foo"),
("rfc1459", "#F]|oo{", "#f}\\oo["),
("rfc1459", "#F}o\\o[", "#f]o|o{"),
],
)
@cases.mark_specifications("RFC1459", "RFC2812", strict=True) @cases.mark_specifications("RFC1459", "RFC2812", strict=True)
def f(self): def testChannelsEquivalent(self, casemapping, name1, name2):
self.connectClient("foo") self.connectClient("foo")
self.connectClient("bar") self.connectClient("bar")
if self.server_support["CASEMAPPING"] != casemapping: if self.server_support["CASEMAPPING"] != casemapping:
@ -729,15 +738,15 @@ def _testChannelsEquivalent(casemapping, name1, name2):
"Channel names {} and {} are not equivalent.".format(name1, name2) "Channel names {} and {} are not equivalent.".format(name1, name2)
) )
f.__name__ = "testEquivalence__{}__{}".format(name1, name2) @pytest.mark.parametrize(
return f "casemapping,name1,name2",
[
("ascii", "#Foo", "#fooa"),
def _testChannelsNotEquivalent(casemapping, name1, name2): ("rfc1459", "#Foo", "#fooa"),
"""Generates test functions""" ],
)
@cases.mark_specifications("RFC1459", "RFC2812", strict=True) @cases.mark_specifications("RFC1459", "RFC2812", strict=True)
def f(self): def testChannelsNotEquivalent(self, casemapping, name1, name2):
self.connectClient("foo") self.connectClient("foo")
self.connectClient("bar") self.connectClient("bar")
if self.server_support["CASEMAPPING"] != casemapping: if self.server_support["CASEMAPPING"] != casemapping:
@ -758,21 +767,6 @@ def _testChannelsNotEquivalent(casemapping, name1, name2):
"Channel names {} and {} are equivalent.".format(name1, name2) "Channel names {} and {} are equivalent.".format(name1, name2)
) )
f.__name__ = "testEquivalence__{}__{}".format(name1, name2)
return f
class testChannelCaseSensitivity(cases.BaseServerTestCase):
testAsciiSimpleEquivalent = _testChannelsEquivalent("ascii", "#Foo", "#foo")
testAsciiSimpleNotEquivalent = _testChannelsNotEquivalent("ascii", "#Foo", "#fooa")
testRfcSimpleEquivalent = _testChannelsEquivalent("rfc1459", "#Foo", "#foo")
testRfcSimpleNotEquivalent = _testChannelsNotEquivalent("rfc1459", "#Foo", "#fooa")
testRfcFancyEquivalent = _testChannelsEquivalent("rfc1459", "#F]|oo{", "#f}\\oo[")
testRfcFancyNotEquivalent = _testChannelsEquivalent(
"rfc1459", "#F}o\\o[", "#f]o|o{"
)
class InviteTestCase(cases.BaseServerTestCase): class InviteTestCase(cases.BaseServerTestCase):
@cases.mark_specifications("Modern") @cases.mark_specifications("Modern")

View File

@ -10,11 +10,18 @@ from irctest.irc_utils.junkdrawer import random_name
from irctest.patma import ANYDICT from irctest.patma import ANYDICT
def _testEchoMessage(command, solo, server_time): class EchoMessageTestCase(cases.BaseServerTestCase):
"""Generates test functions""" @pytest.mark.parametrize(
"command,solo,server_time",
[
("PRIVMSG", False, False),
("PRIVMSG", True, True),
("PRIVMSG", False, True),
("NOTICE", False, True),
],
)
@cases.mark_capabilities("echo-message") @cases.mark_capabilities("echo-message")
def f(self): def testEchoMessage(self, command, solo, server_time):
"""<http://ircv3.net/specs/extensions/echo-message-3.2.html>""" """<http://ircv3.net/specs/extensions/echo-message-3.2.html>"""
self.addClient() self.addClient()
self.sendLine(1, "CAP LS 302") self.sendLine(1, "CAP LS 302")
@ -95,10 +102,6 @@ def _testEchoMessage(command, solo, server_time):
extra_format=(m2,), extra_format=(m2,),
) )
return f
class EchoMessageTestCase(cases.BaseServerTestCase):
@pytest.mark.arbitrary_client_tags @pytest.mark.arbitrary_client_tags
@cases.mark_capabilities( @cases.mark_capabilities(
"batch", "labeled-response", "echo-message", "message-tags" "batch", "labeled-response", "echo-message", "message-tags"
@ -144,8 +147,3 @@ class EchoMessageTestCase(cases.BaseServerTestCase):
# Either both messages have a msgid, or neither does # Either both messages have a msgid, or neither does
self.assertEqual(delivery.tags.get("msgid"), echo.tags.get("msgid")) self.assertEqual(delivery.tags.get("msgid"), echo.tags.get("msgid"))
testEchoMessagePrivmsgNoServerTime = _testEchoMessage("PRIVMSG", False, False)
testEchoMessagePrivmsgSolo = _testEchoMessage("PRIVMSG", True, True)
testEchoMessagePrivmsg = _testEchoMessage("PRIVMSG", False, True)
testEchoMessageNotice = _testEchoMessage("NOTICE", False, True)