From 92a73ad4a5947d1b7b688d98aafb63a6e171e380 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sun, 4 Jul 2021 16:38:39 +0200 Subject: [PATCH] Use pytest parametrization instead of ad-hoc method generation --- irctest/server_tests/test_buffering.py | 30 ++++++------ .../server_tests/test_channel_operations.py | 48 ++++++++----------- irctest/server_tests/test_echo_message.py | 24 +++++----- 3 files changed, 47 insertions(+), 55 deletions(-) diff --git a/irctest/server_tests/test_buffering.py b/irctest/server_tests/test_buffering.py index c24e031..d90e449 100644 --- a/irctest/server_tests/test_buffering.py +++ b/irctest/server_tests/test_buffering.py @@ -3,6 +3,8 @@ correctly. Also checks truncation""" import socket +import pytest + from irctest import cases from irctest.irc_utils import message_parser from irctest.numerics import ERR_INPUTTOOLONG @@ -26,8 +28,19 @@ def _sendBytePerByte(self, line): self.clients[1].conn.sendall(bytes([byte])) -def _testNoTags(sender_function, colon): - def f(self): +class BufferingTestCase(cases.BaseServerTestCase): + @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("nick2") @@ -97,12 +110,6 @@ def _testNoTags(sender_function, colon): f"but got {payload!r}", ) - return f - - -class BufferingTestCase(cases.BaseServerTestCase): - # show_io = False - def get_overhead(self, client1, client2, colon): self.sendLine(client1, f"PRIVMSG nick2 {colon}a\r\n") line = self._getLine(client2) @@ -118,10 +125,3 @@ class BufferingTestCase(cases.BaseServerTestCase): line += data if not data or data.endswith(b"\r\n"): 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=":") diff --git a/irctest/server_tests/test_channel_operations.py b/irctest/server_tests/test_channel_operations.py index 6b66957..c33a077 100644 --- a/irctest/server_tests/test_channel_operations.py +++ b/irctest/server_tests/test_channel_operations.py @@ -6,6 +6,8 @@ Section 3.2 of RFC 2812 import math import time +import pytest + from irctest import cases, client_mock, runner from irctest.irc_utils import ambiguities from irctest.irc_utils.junkdrawer import ircv3_timestamp_to_unixtime @@ -708,11 +710,18 @@ class JoinTestCase(cases.BaseServerTestCase): ) -def _testChannelsEquivalent(casemapping, name1, name2): - """Generates test functions""" - +class TestChannelCaseSensitivity(cases.BaseServerTestCase): + @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) - def f(self): + def testChannelsEquivalent(self, casemapping, name1, name2): self.connectClient("foo") self.connectClient("bar") if self.server_support["CASEMAPPING"] != casemapping: @@ -729,15 +738,15 @@ def _testChannelsEquivalent(casemapping, name1, name2): "Channel names {} and {} are not equivalent.".format(name1, name2) ) - f.__name__ = "testEquivalence__{}__{}".format(name1, name2) - return f - - -def _testChannelsNotEquivalent(casemapping, name1, name2): - """Generates test functions""" - + @pytest.mark.parametrize( + "casemapping,name1,name2", + [ + ("ascii", "#Foo", "#fooa"), + ("rfc1459", "#Foo", "#fooa"), + ], + ) @cases.mark_specifications("RFC1459", "RFC2812", strict=True) - def f(self): + def testChannelsNotEquivalent(self, casemapping, name1, name2): self.connectClient("foo") self.connectClient("bar") if self.server_support["CASEMAPPING"] != casemapping: @@ -758,21 +767,6 @@ def _testChannelsNotEquivalent(casemapping, 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): @cases.mark_specifications("Modern") diff --git a/irctest/server_tests/test_echo_message.py b/irctest/server_tests/test_echo_message.py index 4226697..f183316 100644 --- a/irctest/server_tests/test_echo_message.py +++ b/irctest/server_tests/test_echo_message.py @@ -10,11 +10,18 @@ from irctest.irc_utils.junkdrawer import random_name from irctest.patma import ANYDICT -def _testEchoMessage(command, solo, server_time): - """Generates test functions""" - +class EchoMessageTestCase(cases.BaseServerTestCase): + @pytest.mark.parametrize( + "command,solo,server_time", + [ + ("PRIVMSG", False, False), + ("PRIVMSG", True, True), + ("PRIVMSG", False, True), + ("NOTICE", False, True), + ], + ) @cases.mark_capabilities("echo-message") - def f(self): + def testEchoMessage(self, command, solo, server_time): """""" self.addClient() self.sendLine(1, "CAP LS 302") @@ -95,10 +102,6 @@ def _testEchoMessage(command, solo, server_time): extra_format=(m2,), ) - return f - - -class EchoMessageTestCase(cases.BaseServerTestCase): @pytest.mark.arbitrary_client_tags @cases.mark_capabilities( "batch", "labeled-response", "echo-message", "message-tags" @@ -144,8 +147,3 @@ class EchoMessageTestCase(cases.BaseServerTestCase): # Either both messages have a msgid, or neither does 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)