From 8c73ac2b757fc9564c58c04d36b5cc583ebabc3f Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Thu, 28 Apr 2022 20:12:18 +0200 Subject: [PATCH] patma: Add support for operators in keys Will be used to match either '@bot' or '@draft/bot'. --- irctest/patma.py | 14 ++++++++------ irctest/self_tests/cases.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/irctest/patma.py b/irctest/patma.py index 30ce278..b02c15e 100644 --- a/irctest/patma.py +++ b/irctest/patma.py @@ -155,13 +155,15 @@ def match_dict( for (expected_key, expected_value) in expected.items(): if isinstance(expected_key, RemainingKeys): remaining_keys_wildcard = (expected_key.key, expected_value) - elif isinstance(expected_key, Operator): - raise NotImplementedError(f"Unsupported operator: {expected_key}") else: - if expected_key not in got: - return False - got_value = got.pop(expected_key) - if not match_string(got_value, expected_value): + for key in got: + if match_string(key, expected_key) and match_string( + got[key], expected_value + ): + got.pop(key) + break + else: + # Found no (key, value) pair matching the request return False if remaining_keys_wildcard: diff --git a/irctest/self_tests/cases.py b/irctest/self_tests/cases.py index 63cf659..6f8920b 100644 --- a/irctest/self_tests/cases.py +++ b/irctest/self_tests/cases.py @@ -179,6 +179,39 @@ MESSAGE_SPECS: List[Tuple[Dict, List[str], List[str], List[str]]] = [ "expected tags to match {'tag1': 'bar', RemainingKeys(ANYSTR): ANYOPTSTR}, got {}", ] ), + ( + # the specification: + dict( + tags={StrRe("tag[12]"): "bar", **ANYDICT}, + command="PRIVMSG", + params=["#chan", "hello"], + ), + # matches: + [ + "@tag1=bar PRIVMSG #chan :hello", + "@tag1=bar;tag2= PRIVMSG #chan :hello", + "@tag1=bar :foo!baz@qux PRIVMSG #chan :hello", + "@tag2=bar PRIVMSG #chan :hello", + "@tag1=bar;tag2= PRIVMSG #chan :hello", + "@tag1=;tag2=bar PRIVMSG #chan :hello", + ], + # and does not match: + [ + "PRIVMG #chan :hello", + "@tag1=value1 PRIVMSG #chan :hello", + "PRIVMSG #chan hello2", + "PRIVMSG #chan2 hello", + ":foo!baz@qux PRIVMSG #chan hello", + ], + # and they each error with: + [ + "expected command to be PRIVMSG, got PRIVMG", + "expected tags to match {StrRe(r'tag[12]'): 'bar', RemainingKeys(ANYSTR): ANYOPTSTR}, got {'tag1': 'value1'}", + "expected params to match ['#chan', 'hello'], got ['#chan', 'hello2']", + "expected params to match ['#chan', 'hello'], got ['#chan2', 'hello']", + "expected tags to match {StrRe(r'tag[12]'): 'bar', RemainingKeys(ANYSTR): ANYOPTSTR}, got {}", + ] + ), ( # the specification: dict(