Check msg from at_pre_cmd

It's possible to display a msg from at_pre_cmd but it's not possible to
test it. Rather than return when at_pre_cmd returns True, the handler
should still run the msg checks is they're present.
This commit is contained in:
Ben Longden 2024-08-19 14:18:48 +01:00 committed by Ben Longden
parent 27f0ccec4a
commit 1d746db2dd

View file

@ -433,7 +433,6 @@ class EvenniaCommandTestMixin:
cmdobj.raw_string = raw_string if raw_string is not None else cmdobj.key + " " + input_args cmdobj.raw_string = raw_string if raw_string is not None else cmdobj.key + " " + input_args
cmdobj.obj = obj or (caller if caller else self.char1) cmdobj.obj = obj or (caller if caller else self.char1)
inputs = inputs or [] inputs = inputs or []
# set up receivers # set up receivers
receiver_mapping = {} receiver_mapping = {}
if isinstance(msg, dict): if isinstance(msg, dict):
@ -453,37 +452,34 @@ class EvenniaCommandTestMixin:
unmocked_msg_methods[receiver] = receiver.msg unmocked_msg_methods[receiver] = receiver.msg
# replace normal `.msg` with a mock # replace normal `.msg` with a mock
receiver.msg = Mock() receiver.msg = Mock()
# Run the methods of the Command. This mimics what happens in the # Run the methods of the Command. This mimics what happens in the
# cmdhandler. This will have the mocked .msg be called as part of the # cmdhandler. This will have the mocked .msg be called as part of the
# execution. Mocks remembers what was sent to them so we will be able # execution. Mocks remembers what was sent to them so we will be able
# to retrieve what was sent later. # to retrieve what was sent later.
try: try:
if cmdobj.at_pre_cmd(): if not cmdobj.at_pre_cmd():
return cmdobj.parse()
cmdobj.parse() ret = cmdobj.func()
ret = cmdobj.func() # handle func's with yield in them (making them generators)
if isinstance(ret, types.GeneratorType):
# handle func's with yield in them (making them generators) while True:
if isinstance(ret, types.GeneratorType): try:
while True: inp = inputs.pop() if inputs else None
try: if inp:
inp = inputs.pop() if inputs else None try:
if inp: # this mimics a user's reply to a prompt
try: ret.send(inp)
# this mimics a user's reply to a prompt except TypeError:
ret.send(inp) next(ret)
except TypeError: ret = ret.send(inp)
else:
# non-input yield, like yield(10). We don't pause
# but fire it immediately.
next(ret) next(ret)
ret = ret.send(inp) except StopIteration:
else: break
# non-input yield, like yield(10). We don't pause
# but fire it immediately.
next(ret)
except StopIteration:
break
cmdobj.at_post_cmd() cmdobj.at_post_cmd()
except StopIteration: except StopIteration:
pass pass
except InterruptCommand: except InterruptCommand: