From b8867cf4a22565149e905c0e95e8f77704bfed93 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sun, 28 Feb 2021 18:53:24 +0100 Subject: [PATCH] Use a new 'magic' class AnyStr instead of Ellipsis for pattern-matching messages. --- irctest/cases.py | 15 ++++++++++++--- irctest/server_tests/test_cap.py | 21 ++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/irctest/cases.py b/irctest/cases.py index 2a2a45b..78be536 100644 --- a/irctest/cases.py +++ b/irctest/cases.py @@ -65,6 +65,13 @@ TController = TypeVar("TController", bound=basecontrollers._BaseController) T = TypeVar("T") +class AnyStr: + """Used as a wildcard when matching message arguments + (see assertMessageMatch and listMatch)""" + + pass + + class ChannelJoinException(Exception): def __init__(self, code: str, params: List[str]): super().__init__(f"Failed to join channel ({code}): {params}") @@ -123,7 +130,7 @@ class _IrcTestCase(unittest.TestCase, Generic[TController]): def messageDiffers( self, msg: Message, - params: Optional[List[Any]] = None, + params: Optional[List[Union[str, Type[AnyStr]]]] = None, target: Optional[str] = None, nick: Optional[str] = None, fail_msg: Optional[str] = None, @@ -163,14 +170,16 @@ class _IrcTestCase(unittest.TestCase, Generic[TController]): return None - def listMatch(self, got: List[str], expected: List[Any]) -> bool: + def listMatch( + self, got: List[str], expected: List[Union[str, Type[AnyStr]]] + ) -> bool: """Returns True iff the list are equal. The ellipsis (aka. "..." aka triple dots) can be used on the 'expected' side as a wildcard, matching any *single* value.""" if len(got) != len(expected): return False for (got_value, expected_value) in zip(got, expected): - if expected_value is Ellipsis: + if expected_value is AnyStr: # wildcard continue if got_value != expected_value: diff --git a/irctest/server_tests/test_cap.py b/irctest/server_tests/test_cap.py index fa015fa..8726260 100644 --- a/irctest/server_tests/test_cap.py +++ b/irctest/server_tests/test_cap.py @@ -1,4 +1,5 @@ from irctest import cases +from irctest.cases import AnyStr from irctest.runner import CapabilityNotSupported, ImplementationChoice @@ -39,7 +40,7 @@ class CapTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.assertMessageMatch( m, command="CAP", - params=[..., "NAK", "foo"], + params=[AnyStr, "NAK", "foo"], fail_msg="Expected CAP NAK after requesting non-existing " "capability, got {msg}.", ) @@ -66,7 +67,7 @@ class CapTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.assertMessageMatch( m, command="CAP", - params=[..., "NAK", "foo qux bar baz qux quux"], + params=[AnyStr, "NAK", "foo qux bar baz qux quux"], fail_msg="Expected “CAP NAK :foo qux bar baz qux quux” after " "sending “CAP REQ :foo qux bar baz qux quux”, but got {msg}.", ) @@ -85,7 +86,7 @@ class CapTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.assertMessageMatch( m, command="CAP", - params=[..., "NAK", "foo multi-prefix bar"], + params=[AnyStr, "NAK", "foo multi-prefix bar"], fail_msg="Expected “CAP NAK :foo multi-prefix bar” after " "sending “CAP REQ :foo multi-prefix bar”, but got {msg}.", ) @@ -94,7 +95,7 @@ class CapTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.assertMessageMatch( m, command="CAP", - params=[..., "NAK", "multi-prefix bar"], + params=[AnyStr, "NAK", "multi-prefix bar"], fail_msg="Expected “CAP NAK :multi-prefix bar” after " "sending “CAP REQ :multi-prefix bar”, but got {msg}.", ) @@ -103,7 +104,7 @@ class CapTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.assertMessageMatch( m, command="CAP", - params=[..., "NAK", "foo multi-prefix"], + params=[AnyStr, "NAK", "foo multi-prefix"], fail_msg="Expected “CAP NAK :foo multi-prefix” after " "sending “CAP REQ :foo multi-prefix”, but got {msg}.", ) @@ -113,7 +114,7 @@ class CapTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.assertMessageMatch( m, command="CAP", - params=[..., "ACK", "multi-prefix"], + params=[AnyStr, "ACK", "multi-prefix"], fail_msg="Expected “CAP ACK :multi-prefix” after " "sending “CAP REQ :multi-prefix”, but got {msg}.", ) @@ -133,7 +134,7 @@ class CapTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.sendLine(1, "user user 0 * realname") self.sendLine(1, "CAP END") m = self.getRegistrationMessage(1) - self.assertMessageMatch(m, command="CAP", params=[..., "ACK", ...]) + self.assertMessageMatch(m, command="CAP", params=[AnyStr, "ACK", AnyStr]) self.assertEqual( set(m.params[2].split()), {cap1, cap2}, "Didn't ACK both REQed caps" ) @@ -151,8 +152,10 @@ class CapTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): self.sendLine(1, f"CAP REQ :-{cap2}") m = self.getMessage(1) # Must be either ACK or NAK - if self.messageDiffers(m, command="CAP", params=[..., "ACK", f"-{cap2}"]): - self.assertMessageMatch(m, command="CAP", params=[..., "NAK", f"-{cap2}"]) + if self.messageDiffers(m, command="CAP", params=[AnyStr, "ACK", f"-{cap2}"]): + self.assertMessageMatch( + m, command="CAP", params=[AnyStr, "NAK", f"-{cap2}"] + ) raise ImplementationChoice(f"Does not support CAP REQ -{cap2}") # server-time should be disabled