patma: Fix inconsistencies between ANYSTR and AnyOptStr

This commit is contained in:
Valentin Lorentz 2022-03-05 09:52:18 +01:00 committed by Val Lorentz
parent 66c457f6ce
commit 2a4e71eccd
3 changed files with 27 additions and 24 deletions

View File

@ -13,18 +13,18 @@ class Operator:
pass
class AnyStr(Operator):
class _AnyStr(Operator):
"""Wildcard matching any string"""
def __repr__(self) -> str:
return "AnyStr"
return "ANYSTR"
class AnyOptStr(Operator):
class _AnyOptStr(Operator):
"""Wildcard matching any string as well as None"""
def __repr__(self) -> str:
return "AnyOptStr()"
return "ANYOPTSTR"
@dataclasses.dataclass(frozen=True)
@ -62,10 +62,13 @@ class RemainingKeys(Operator):
return f"RemainingKeys({self.key!r})"
ANYSTR = AnyStr()
ANYSTR = _AnyStr()
"""Singleton, spares two characters"""
ANYDICT = {RemainingKeys(ANYSTR): AnyOptStr()}
ANYOPTSTR = _AnyOptStr()
"""Singleton, spares two characters"""
ANYDICT = {RemainingKeys(ANYSTR): ANYOPTSTR}
"""Matches any dictionary; useful to compare tags dict, eg.
`match_dict(got_tags, {"label": "foo", **ANYDICT})`"""
@ -87,9 +90,9 @@ ANYLIST = [ListRemainder(ANYSTR)]
def match_string(got: Optional[str], expected: Union[str, Operator, None]) -> bool:
if isinstance(expected, AnyOptStr):
if isinstance(expected, _AnyOptStr):
return True
elif isinstance(expected, AnyStr) and got is not None:
elif isinstance(expected, _AnyStr) and got is not None:
return True
elif isinstance(expected, StrRe):
if got is None or not re.match(expected.regexp, got):

View File

@ -7,8 +7,8 @@ from irctest.irc_utils.message_parser import parse_message
from irctest.patma import (
ANYDICT,
ANYLIST,
ANYOPTSTR,
ANYSTR,
AnyOptStr,
ListRemainder,
NotStrRe,
RemainingKeys,
@ -142,9 +142,9 @@ MESSAGE_SPECS: List[Tuple[Dict, List[str], List[str], List[str]]] = [
],
# and they each error with:
[
"expected tags to match {'tag1': AnyStr}, got {'tag1': 'bar', 'tag2': ''}",
"expected tags to match {'tag1': AnyStr}, got {}",
"expected tags to match {'tag1': AnyStr}, got {}",
"expected tags to match {'tag1': ANYSTR}, got {'tag1': 'bar', 'tag2': ''}",
"expected tags to match {'tag1': ANYSTR}, got {}",
"expected tags to match {'tag1': ANYSTR}, got {}",
]
),
(
@ -171,16 +171,16 @@ MESSAGE_SPECS: List[Tuple[Dict, List[str], List[str], List[str]]] = [
# and they each error with:
[
"expected command to be PRIVMSG, got PRIVMG",
"expected tags to match {'tag1': 'bar', RemainingKeys(AnyStr): AnyOptStr()}, got {'tag1': 'value1'}",
"expected tags to match {'tag1': '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 {'tag1': 'bar', RemainingKeys(AnyStr): AnyOptStr()}, got {}",
"expected tags to match {'tag1': 'bar', RemainingKeys(ANYSTR): ANYOPTSTR}, got {}",
]
),
(
# the specification:
dict(
tags={"tag1": "bar", RemainingKeys(NotStrRe("tag2")): AnyOptStr()},
tags={"tag1": "bar", RemainingKeys(NotStrRe("tag2")): ANYOPTSTR},
command="PRIVMSG",
params=["#chan", "hello"],
),
@ -200,9 +200,9 @@ MESSAGE_SPECS: List[Tuple[Dict, List[str], List[str], List[str]]] = [
# and they each error with:
[
"expected command to be PRIVMSG, got PRIVMG",
"expected tags to match {'tag1': 'bar', RemainingKeys(NotStrRe(r'tag2')): AnyOptStr()}, got {'tag1': 'value1'}",
"expected tags to match {'tag1': 'bar', RemainingKeys(NotStrRe(r'tag2')): AnyOptStr()}, got {'tag1': 'bar', 'tag2': ''}",
"expected tags to match {'tag1': 'bar', RemainingKeys(NotStrRe(r'tag2')): AnyOptStr()}, got {'tag1': 'bar', 'tag2': 'baz'}",
"expected tags to match {'tag1': 'bar', RemainingKeys(NotStrRe(r'tag2')): ANYOPTSTR}, got {'tag1': 'value1'}",
"expected tags to match {'tag1': 'bar', RemainingKeys(NotStrRe(r'tag2')): ANYOPTSTR}, got {'tag1': 'bar', 'tag2': ''}",
"expected tags to match {'tag1': 'bar', RemainingKeys(NotStrRe(r'tag2')): ANYOPTSTR}, got {'tag1': 'bar', 'tag2': 'baz'}",
]
),
(
@ -223,8 +223,8 @@ MESSAGE_SPECS: List[Tuple[Dict, List[str], List[str], List[str]]] = [
],
# and they each error with:
[
"expected params to match ['nick', 'FOO=1', ListRemainder(AnyStr)], got ['nick']",
"expected params to match ['nick', 'FOO=1', ListRemainder(AnyStr)], got ['nick', 'BAR=2']",
"expected params to match ['nick', 'FOO=1', ListRemainder(ANYSTR)], got ['nick']",
"expected params to match ['nick', 'FOO=1', ListRemainder(ANYSTR)], got ['nick', 'BAR=2']",
]
),
(
@ -245,7 +245,7 @@ MESSAGE_SPECS: List[Tuple[Dict, List[str], List[str], List[str]]] = [
],
# and they each error with:
[
"expected params to match ['nick', ListRemainder(AnyStr, min_length=1)], got ['nick']",
"expected params to match ['nick', ListRemainder(ANYSTR, min_length=1)], got ['nick']",
]
),
(

View File

@ -11,7 +11,7 @@ import pytest
from irctest import cases
from irctest.numerics import ERR_UNKNOWNCOMMAND
from irctest.patma import ANYDICT, AnyOptStr, NotStrRe, RemainingKeys, StrRe
from irctest.patma import ANYDICT, ANYOPTSTR, NotStrRe, RemainingKeys, StrRe
class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper):
@ -299,7 +299,7 @@ class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper
tags={
"+draft/reply": msgid,
"+draft/react": "l😃l",
RemainingKeys(NotStrRe("label")): AnyOptStr(),
RemainingKeys(NotStrRe("label")): ANYOPTSTR,
},
)
self.assertNotIn(
@ -367,7 +367,7 @@ class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper
tags={
"+draft/reply": msgid,
"+draft/react": "l😃l",
RemainingKeys(NotStrRe("label")): AnyOptStr(),
RemainingKeys(NotStrRe("label")): ANYOPTSTR,
},
fail_msg="No TAGMSG received by the target after sending one out",
)