From 2e376a32cbb3d178621f8c752ee377efe04c38fc Mon Sep 17 00:00:00 2001 From: Henddher Pedroza Date: Fri, 14 Sep 2018 23:28:56 -0500 Subject: [PATCH] Enforce parts and results to be DefaultObject not DefaultCharacter, DefaultRoom nor DefaultExit with tests. Tests for @lspuzzlerecipes and @lsarmedpuzzles --- evennia/contrib/puzzles.py | 21 +++++++++- evennia/contrib/tests.py | 81 +++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/evennia/contrib/puzzles.py b/evennia/contrib/puzzles.py index e0423105f5..cf89a4834c 100644 --- a/evennia/contrib/puzzles.py +++ b/evennia/contrib/puzzles.py @@ -75,6 +75,7 @@ from evennia import DefaultObject from evennia import DefaultScript from evennia import DefaultCharacter from evennia import DefaultRoom +from evennia import DefaultExit from evennia.commands.default.muxcommand import MuxCommand from evennia.utils.utils import inherits_from from evennia.utils import search, utils, logger @@ -193,12 +194,28 @@ class CmdCreatePuzzleRecipe(MuxCommand): def is_valid_result_location(part): return is_valid_obj_location(part) + def is_valid_inheritance(obj): + valid = not inherits_from(obj, DefaultCharacter) \ + and not inherits_from(obj, DefaultRoom) \ + and not inherits_from(obj, DefaultExit) + if not valid: + caller.msg('Invalid typeclass for %s' % (obj)) + return valid + + def is_valid_part(part): + return is_valid_inheritance(part) \ + and is_valid_part_location(part) + + def is_valid_result(result): + return is_valid_inheritance(result) \ + and is_valid_result_location(result) + parts = [] for objname in self.lhslist[1:]: obj = caller.search(objname) if not obj: return - if not is_valid_part_location(obj): + if not is_valid_part(obj): return parts.append(obj) @@ -207,7 +224,7 @@ class CmdCreatePuzzleRecipe(MuxCommand): obj = caller.search(objname) if not obj: return - if not is_valid_result_location(obj): + if not is_valid_result(obj): return results.append(obj) diff --git a/evennia/contrib/tests.py b/evennia/contrib/tests.py index ab206dc632..0e61a66f33 100644 --- a/evennia/contrib/tests.py +++ b/evennia/contrib/tests.py @@ -1215,7 +1215,7 @@ class TestPuzzles(CommandTest): def _assert_no_recipes(self): self.assertEqual( 0, - len(search.search_tag('', category=puzzles._PUZZLES_TAG_CATEGORY)) + len(search.search_script_tag('', category=puzzles._PUZZLES_TAG_CATEGORY)) ) # good recipes @@ -1294,6 +1294,11 @@ class TestPuzzles(CommandTest): _bad_recipe('name', ['nothing'], ['neither'], r"Could not find 'nothing'.") _bad_recipe('name', ['stone'], ['nothing'], r"Could not find 'nothing'.") _bad_recipe('', ['stone', 'fire'], ['stone', 'fire'], r"^Invalid puzzle name ''.") + self.stone.location = self.char1 + _bad_recipe('name', ['stone'], ['fire'], r"^Invalid location for stone$") + _bad_recipe('name', ['flint'], ['stone'], r"^Invalid location for stone$") + _bad_recipe('name', ['self'], ['fire'], r"^Invalid typeclass for Char$") + _bad_recipe('name', ['here'], ['fire'], r"^Invalid typeclass for Room$") self._assert_no_recipes() @@ -1352,3 +1357,77 @@ class TestPuzzles(CommandTest): # and the parts were destroyed _use('stone, flint', 'There is no stone around') _use('flint, stone', 'There is no flint around') + + def test_lspuzzlerecipes_lsarmedpuzzles(self): + msg = self.call( + puzzles.CmdListPuzzleRecipes(), + '', + caller=self.char1 + ) + self._assert_msg_matched( + msg, + [ + r"^-+$", + r"^-+$", + ], + re.MULTILINE | re.DOTALL + ) + + recipe_dbref = self._good_recipe( + 'makefire', ['stone', 'flint'], ['fire'], + and_destroy_it=False) + + msg = self.call( + puzzles.CmdListPuzzleRecipes(), + '', + caller=self.char1 + ) + self._assert_msg_matched( + msg, + [ + r"^-+$", + r"^Puzzle 'makefire'.*$", + r"^Parts$", + r"^.*key: stone$", + r"^.*key: flint$", + r"^Results$", + r"^.*key: fire$", + r"^.*key: stone$", + r"^.*key: flint$", + r"^-+$", + ], + re.MULTILINE | re.DOTALL + ) + + msg = self.call( + puzzles.CmdListArmedPuzzles(), + '', + caller=self.char1 + ) + self._assert_msg_matched( + msg, + [ + r"^-+$", + r"^-+$" + ], + re.MULTILINE | re.DOTALL + ) + + self._arm(recipe_dbref, 'makefire', ['stone', 'flint']) + + msg = self.call( + puzzles.CmdListArmedPuzzles(), + '', + caller=self.char1 + ) + self._assert_msg_matched( + msg, + [ + r"^-+$", + r"^Puzzle name: makefire$", + r"^.*stone.* at \s+ Room.*$", + r"^.*flint.* at \s+ Room.*$", + r"^-+$", + ], + re.MULTILINE | re.DOTALL + )