From ec833c21d6e069614e4229d28c7d5cf7ef02dde8 Mon Sep 17 00:00:00 2001 From: Griatch Date: Fri, 9 Aug 2019 22:41:37 +0200 Subject: [PATCH] Extend new unit tests in main cmdparser --- evennia/commands/cmdparser.py | 8 +++- evennia/commands/tests.py | 86 +++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/evennia/commands/cmdparser.py b/evennia/commands/cmdparser.py index 2c78a247ab..5bbc12878b 100644 --- a/evennia/commands/cmdparser.py +++ b/evennia/commands/cmdparser.py @@ -59,11 +59,11 @@ def build_matches(raw_string, cmdset, include_prefixes=False): matches (list) A list of match tuples created by `cmdparser.create_match`. """ - l_raw_string = raw_string.lower() matches = [] try: if include_prefixes: # use the cmdname as-is + l_raw_string = raw_string.lower() for cmd in cmdset: matches.extend([create_match(cmdname, raw_string, cmd, cmdname) for cmdname in [cmd.key] + cmd.aliases @@ -72,9 +72,13 @@ def build_matches(raw_string, cmdset, include_prefixes=False): cmd.arg_regex.match(l_raw_string[len(cmdname):]))]) else: # strip prefixes set in settings + raw_string = (raw_string.lstrip(_CMD_IGNORE_PREFIXES) + if len(raw_string) > 1 else l_raw_string) + l_raw_string = raw_string.lower() for cmd in cmdset: for raw_cmdname in [cmd.key] + cmd.aliases: - cmdname = raw_cmdname.lstrip(_CMD_IGNORE_PREFIXES) if len(raw_cmdname) > 1 else raw_cmdname + cmdname = (raw_cmdname.lstrip(_CMD_IGNORE_PREFIXES) + if len(raw_cmdname) > 1 else raw_cmdname) if cmdname and l_raw_string.startswith(cmdname.lower()) and \ (not cmd.arg_regex or cmd.arg_regex.match(l_raw_string[len(cmdname):])): matches.append(create_match(cmdname, raw_string, cmd, raw_cmdname)) diff --git a/evennia/commands/tests.py b/evennia/commands/tests.py index b2f903996e..64fd8cebd7 100644 --- a/evennia/commands/tests.py +++ b/evennia/commands/tests.py @@ -3,9 +3,11 @@ Unit testing for the Command system itself. """ +from django.test import override_settings from evennia.utils.test_resources import EvenniaTest, TestCase from evennia.commands.cmdset import CmdSet from evennia.commands.command import Command +from evennia.commands import cmdparser # Testing-command sets @@ -376,3 +378,87 @@ class TestGetAndMergeCmdSets(TwistedTestCase, EvenniaTest): self.assertEqual(len(cmdset.commands), 9) deferred.addCallback(_callback) return deferred + + +class AccessableCommand(Command): + def access(*args, **kwargs): + return True + +class _CmdTest1(AccessableCommand): + key = "test1" + + +class _CmdTest2(AccessableCommand): + key = "another command" + + +class _CmdTest3(AccessableCommand): + key = "&the third command" + + +class _CmdTest4(AccessableCommand): + key = "test2" + + +class _CmdSetTest(CmdSet): + key = "test_cmdset" + def at_cmdset_creation(self): + self.add(_CmdTest1) + self.add(_CmdTest2) + self.add(_CmdTest3) + + +class TestCmdParser(TestCase): + + def test_create_match(self): + class DummyCmd: + pass + dummy = DummyCmd() + + self.assertEqual( + cmdparser.create_match("look at", "look at target", dummy, "look"), + ("look at", " target", dummy, 7, 0.5, "look")) + + @override_settings(CMD_IGNORE_PREFIXES="@&/+") + def test_build_matches(self): + a_cmdset = _CmdSetTest() + bcmd = [cmd for cmd in a_cmdset.commands if cmd.key == "test1"][0] + + # normal parsing + self.assertEqual( + cmdparser.build_matches("test1 rock", a_cmdset, include_prefixes=False), + [("test1", " rock", bcmd, 5, 0.5, 'test1')] + ) + + # test prefix exclusion + bcmd = [cmd for cmd in a_cmdset.commands if cmd.key == "another command"][0] + self.assertEqual( + cmdparser.build_matches("@another command smiles to me ", + a_cmdset, include_prefixes=False), + [("another command", " smiles to me ", bcmd, 15, 0.5, 'another command')] + ) + # test prefix exclusion on the cmd class + bcmd = [cmd for cmd in a_cmdset.commands if cmd.key == "&the third command"][0] + self.assertEqual( + cmdparser.build_matches("the third command", + a_cmdset, include_prefixes=False), + [("the third command", "", bcmd, 17, 1.0, '&the third command')] + ) + + @override_settings(SEARCH_MULTIMATCH_REGEX=r"(?P[0-9]+)-(?P.*)") + def test_num_prefixes(self): + self.assertEqual(cmdparser.try_num_prefixes("look me"), + (None, None)) + self.assertEqual(cmdparser.try_num_prefixes("3-look me"), + ('3', "look me")) + self.assertEqual(cmdparser.try_num_prefixes("567-look me"), + ('567', "look me")) + + @override_settings(SEARCH_MULTIMATCH_REGEX=r"(?P[0-9]+)-(?P.*)", + CMD_IGNORE_PREFIXES="@&/+") + def test_cmdparser(self): + a_cmdset = _CmdSetTest() + bcmd = [cmd for cmd in a_cmdset.commands if cmd.key == "test1"][0] + + self.assertEqual(cmdparser.cmdparser("test1hello", a_cmdset, None), + [("test1", "hello", bcmd, 5, 0.5, 'test1')])