From 789b8784d54541e7e89d74e519f9deb80aa9c41d Mon Sep 17 00:00:00 2001 From: Griatch Date: Mon, 7 Nov 2022 23:13:49 +0100 Subject: [PATCH] Testing for command duplication bug] --- evennia/commands/cmdset.py | 31 +++++++++++-------- evennia/commands/command.py | 2 +- evennia/commands/tests.py | 61 ++++++++++++++++++++++--------------- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/evennia/commands/cmdset.py b/evennia/commands/cmdset.py index 8607a4a372..ad30ee2d70 100644 --- a/evennia/commands/cmdset.py +++ b/evennia/commands/cmdset.py @@ -515,6 +515,11 @@ class CmdSet(object, metaclass=_CmdSetMeta): existing ones to make a unique set. """ + if hasattr(cmd, "key") and (cmd.key in ("say", "whisper")): + from evennia.utils import calledby + + print(calledby(2)) + print(f"cmdset.add {cmd.__class__}") if inherits_from(cmd, "evennia.commands.cmdset.CmdSet"): # cmd is a command set so merge all commands in that set @@ -536,31 +541,31 @@ class CmdSet(object, metaclass=_CmdSetMeta): cmds = [self._instantiate(c) for c in cmd] else: cmds = [self._instantiate(cmd)] + commands = self.commands system_commands = self.system_commands for cmd in cmds: # add all commands if not hasattr(cmd, "obj") or cmd.obj is None: cmd.obj = self.cmdsetobj - try: - ic = commands.index(cmd) - commands[ic] = cmd # replace - except ValueError: - commands.append(cmd) + + # remove duplicates and add new + for _dum in range(commands.count(cmd)): + commands.remove(cmd) + commands.append(cmd) # add system_command to separate list as well, - # for quick look-up + # for quick look-up. These have no if cmd.key.startswith("__"): - try: - ic = system_commands.index(cmd) - system_commands[ic] = cmd # replace - except ValueError: - system_commands.append(cmd) + # remove same-matches and add new + for _dum in range(system_commands.count(cmd)): + system_commands.remove(cmd) + system_commands.append(cmd) - self.commands = commands if not allow_duplicates: # extra run to make sure to avoid doublets - self.commands = list(set(self.commands)) + commands = list(set(commands)) + self.commands = commands def remove(self, cmd): """ diff --git a/evennia/commands/command.py b/evennia/commands/command.py index a683a8f789..6cc0996ab4 100644 --- a/evennia/commands/command.py +++ b/evennia/commands/command.py @@ -262,7 +262,7 @@ class Command(metaclass=CommandMeta): str, too. """ - return hash("\n".join(self._matchset)) + return hash("command") def __ne__(self, cmd): """ diff --git a/evennia/commands/tests.py b/evennia/commands/tests.py index c422a69a81..4e2ab2db40 100644 --- a/evennia/commands/tests.py +++ b/evennia/commands/tests.py @@ -4,45 +4,44 @@ Unit testing for the Command system itself. """ from django.test import override_settings -from evennia.utils.test_resources import BaseEvenniaTest, TestCase +from evennia.commands import cmdparser from evennia.commands.cmdset import CmdSet from evennia.commands.command import Command -from evennia.commands import cmdparser - +from evennia.utils.test_resources import BaseEvenniaTest, TestCase # Testing-command sets -class _CmdA(Command): +class _BaseCmd(Command): + def __init__(self, cmdset, *args, **kwargs): + super().__init__(*args, **kwargs) + self.from_cmdset = cmdset + + +class _CmdA(_BaseCmd): key = "A" - def __init__(self, cmdset, *args, **kwargs): - super().__init__(*args, **kwargs) - self.from_cmdset = cmdset - -class _CmdB(Command): +class _CmdB(_BaseCmd): key = "B" - def __init__(self, cmdset, *args, **kwargs): - super().__init__(*args, **kwargs) - self.from_cmdset = cmdset - -class _CmdC(Command): +class _CmdC(_BaseCmd): key = "C" - def __init__(self, cmdset, *args, **kwargs): - super().__init__(*args, **kwargs) - self.from_cmdset = cmdset - -class _CmdD(Command): +class _CmdD(_BaseCmd): key = "D" - def __init__(self, cmdset, *args, **kwargs): - super().__init__(*args, **kwargs) - self.from_cmdset = cmdset + +class _CmdEe(_BaseCmd): + key = "E" + aliases = ["ee"] + + +class _CmdEf(_BaseCmd): + key = "E" + aliases = ["ff"] class _CmdSetA(CmdSet): @@ -82,6 +81,14 @@ class _CmdSetD(CmdSet): self.add(_CmdD("D")) +class _CmdSetEe_Ef(CmdSet): + key = "Ee_Ef" + + def at_cmdset_creation(self): + self.add(_CmdEe("Ee")) + self.add(_CmdEf("Ee")) + + # testing Command Sets @@ -816,7 +823,7 @@ class TestDuplicateBehavior(TestCase): self.cmdset_d.priority = 0 self.cmdset_a.duplicates = True - def test_reverse_sameprio_duplicate(self): + def test_reverse_sameprio_duplicate__implicit(self): """ Test of `duplicates` transfer which does not propagate. Only A has duplicates=True. @@ -831,7 +838,7 @@ class TestDuplicateBehavior(TestCase): self.assertIsNone(cmdset_f.duplicates) self.assertEqual(len(cmdset_f.commands), 8) - def test_reverse_sameprio_duplicate(self): + def test_reverse_sameprio_duplicate__explicit(self): """ Test of `duplicates` transfer, which does not propagate. C.duplication=True @@ -982,6 +989,7 @@ class TestOptionTransferReplace(TestCase): import sys + from evennia.commands import cmdhandler from twisted.trial.unittest import TestCase as TwistedTestCase @@ -1085,6 +1093,11 @@ class TestGetAndMergeCmdSets(TwistedTestCase, BaseEvenniaTest): deferred.addCallback(_callback) return deferred + def test_command_replace_different_aliases(self): + cmdset_ee = _CmdSetEe_Ef() + self.assertEqual(len(cmdset_ee.commands), 1) + self.assertEqual(cmdset_ee.commands[0].key, "e") + class AccessableCommand(Command): def access(*args, **kwargs):