Merge pull request #3599 from blongden/test_mixin_fix

Check msg from at_pre_cmd
This commit is contained in:
Griatch 2025-12-18 11:28:43 +01:00 committed by GitHub
commit 7cd713d0c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 34 deletions

View file

@ -2263,3 +2263,16 @@ class TestSystemCommands(BaseEvenniaCommandTest):
multimatch.matches = matches multimatch.matches = matches
self.call(multimatch, "look", "") self.call(multimatch, "look", "")
class TestPreCmdOutputTestable(BaseEvenniaCommandTest):
def test_pre_cmd(self):
class CmdTest(Command):
def at_pre_cmd(self):
self.msg("This should be testable")
return True
def func(self):
self.msg("This should never be executed")
return
self.call(CmdTest(), "test", "This should be testable")

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):
@ -458,42 +457,40 @@ class EvenniaCommandTestMixin:
# 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: if not cmdobj.at_pre_cmd():
if cmdobj.at_pre_cmd(): try:
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:
pass pass
for inp in inputs: for inp in inputs:
# if there are any inputs left, we may have a non-generator # if there are any inputs left, we may have a non-generator
# input to handle (get_input/ask_yes_no that uses a separate # input to handle (get_input/ask_yes_no that uses a separate
# cmdset rather than a yield # cmdset rather than a yield
caller.execute_cmd(inp) caller.execute_cmd(inp)
# At this point the mocked .msg methods on each receiver will have # At this point the mocked .msg methods on each receiver will have
# stored all calls made to them (that's a basic function of the Mock # stored all calls made to them (that's a basic function of the Mock