mirror of
https://github.com/progval/irctest.git
synced 2025-04-05 06:49:47 +00:00
Add _IrcTestCase.messageDiffers to allow matching messages without using assertions.
This commit is contained in:
@ -69,7 +69,24 @@ class _IrcTestCase(unittest.TestCase):
|
|||||||
if self.show_io:
|
if self.show_io:
|
||||||
print("---- new test ----")
|
print("---- new test ----")
|
||||||
|
|
||||||
def assertMessageEqual(
|
def assertMessageEqual(self, msg, **kwargs):
|
||||||
|
"""Helper for partially comparing a message.
|
||||||
|
|
||||||
|
Takes the message as first arguments, and comparisons to be made
|
||||||
|
as keyword arguments.
|
||||||
|
|
||||||
|
Deals with subcommands (eg. `CAP`) if any of `subcommand`,
|
||||||
|
`subparams`, and `target` are given."""
|
||||||
|
error = self.messageDiffers(msg, **kwargs)
|
||||||
|
if error:
|
||||||
|
self.failureException(msg)
|
||||||
|
|
||||||
|
def messageEqual(self, msg, **kwargs):
|
||||||
|
"""Boolean negation of `messageDiffers` (returns a boolean,
|
||||||
|
not an optional string)."""
|
||||||
|
return not self.messageDiffers(msg, **kwargs)
|
||||||
|
|
||||||
|
def messageDiffers(
|
||||||
self,
|
self,
|
||||||
msg,
|
msg,
|
||||||
subcommand=None,
|
subcommand=None,
|
||||||
@ -80,34 +97,65 @@ class _IrcTestCase(unittest.TestCase):
|
|||||||
extra_format=(),
|
extra_format=(),
|
||||||
**kwargs,
|
**kwargs,
|
||||||
):
|
):
|
||||||
"""Helper for partially comparing a message.
|
"""Returns an error message if the message doesn't match the given arguments,
|
||||||
|
or None if it matches."""
|
||||||
Takes the message as first arguments, and comparisons to be made
|
|
||||||
as keyword arguments.
|
|
||||||
|
|
||||||
Deals with subcommands (eg. `CAP`) if any of `subcommand`,
|
|
||||||
`subparams`, and `target` are given."""
|
|
||||||
fail_msg = fail_msg or "{msg}"
|
|
||||||
for (key, value) in kwargs.items():
|
for (key, value) in kwargs.items():
|
||||||
self.assertEqual(
|
if getattr(msg, key) != value:
|
||||||
getattr(msg, key), value, msg, fail_msg, extra_format=extra_format
|
fail_msg = (
|
||||||
)
|
fail_msg or "expected {param} to be {expects}, got {got}: {msg}"
|
||||||
|
)
|
||||||
|
return fail_msg.format(
|
||||||
|
*extra_format,
|
||||||
|
got=getattr(msg, key),
|
||||||
|
expects=value,
|
||||||
|
param=key,
|
||||||
|
msg=msg,
|
||||||
|
)
|
||||||
if nick:
|
if nick:
|
||||||
self.assertNotEqual(msg.prefix, None, msg, fail_msg)
|
got_nick = msg.prefix.split("!")[0]
|
||||||
self.assertEqual(msg.prefix.split("!")[0], nick, msg, fail_msg)
|
if msg.prefix is None:
|
||||||
|
fail_msg = (
|
||||||
|
fail_msg or "expected nick to be {expects}, got {got} prefix: {msg}"
|
||||||
|
)
|
||||||
|
return fail_msg.format(
|
||||||
|
*extra_format, got=got_nick, expects=nick, param=key, msg=msg
|
||||||
|
)
|
||||||
|
|
||||||
if subcommand is not None or subparams is not None:
|
if subcommand is not None or subparams is not None:
|
||||||
self.assertGreater(len(msg.params), 2, fail_msg)
|
self.assertGreater(len(msg.params), 2, fail_msg)
|
||||||
|
if len(msg.params) <= 2:
|
||||||
|
fail_msg = (
|
||||||
|
fail_msg or "expected subcommand with params, got only {params}"
|
||||||
|
)
|
||||||
|
return fail_msg.format(
|
||||||
|
*extra_format,
|
||||||
|
got=msg.params,
|
||||||
|
expects=[subcommand] + subparams,
|
||||||
|
params=msg.params,
|
||||||
|
msg=msg,
|
||||||
|
)
|
||||||
|
|
||||||
# msg_target = msg.params[0]
|
# msg_target = msg.params[0]
|
||||||
msg_subcommand = msg.params[1]
|
msg_subcommand = msg.params[1]
|
||||||
msg_subparams = msg.params[2:]
|
msg_subparams = msg.params[2:]
|
||||||
if subcommand:
|
if subcommand:
|
||||||
self.assertEqual(
|
if msg_subcommand != subcommand:
|
||||||
msg_subcommand, subcommand, msg, fail_msg, extra_format=extra_format
|
fail_msg = (
|
||||||
)
|
fail_msg or "expected subcommand {expects}, got {got}: {msg}"
|
||||||
|
)
|
||||||
|
return fail_msg.format(
|
||||||
|
*extra_format, got=msg_subcommand, expects=subcommand, msg=msg
|
||||||
|
)
|
||||||
if subparams is not None:
|
if subparams is not None:
|
||||||
self.assertEqual(
|
if msg_subparams != subparams:
|
||||||
msg_subparams, subparams, msg, fail_msg, extra_format=extra_format
|
fail_msg = (
|
||||||
)
|
fail_msg or "expected subparams {expects}, got {got}: {msg}"
|
||||||
|
)
|
||||||
|
return fail_msg.format(
|
||||||
|
*extra_format, got=msg_subparams, expects=subparams, msg=msg
|
||||||
|
)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def assertIn(self, item, list_, msg=None, fail_msg=None, extra_format=()):
|
def assertIn(self, item, list_, msg=None, fail_msg=None, extra_format=()):
|
||||||
if fail_msg:
|
if fail_msg:
|
||||||
|
Reference in New Issue
Block a user