Testing for command duplication bug]

This commit is contained in:
Griatch 2022-11-07 23:13:49 +01:00
parent 176cf1133e
commit 789b8784d5
3 changed files with 56 additions and 38 deletions

View file

@ -515,6 +515,11 @@ class CmdSet(object, metaclass=_CmdSetMeta):
existing ones to make a unique set. 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"): if inherits_from(cmd, "evennia.commands.cmdset.CmdSet"):
# cmd is a command set so merge all commands in that set # 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] cmds = [self._instantiate(c) for c in cmd]
else: else:
cmds = [self._instantiate(cmd)] cmds = [self._instantiate(cmd)]
commands = self.commands commands = self.commands
system_commands = self.system_commands system_commands = self.system_commands
for cmd in cmds: for cmd in cmds:
# add all commands # add all commands
if not hasattr(cmd, "obj") or cmd.obj is None: if not hasattr(cmd, "obj") or cmd.obj is None:
cmd.obj = self.cmdsetobj cmd.obj = self.cmdsetobj
try:
ic = commands.index(cmd) # remove duplicates and add new
commands[ic] = cmd # replace for _dum in range(commands.count(cmd)):
except ValueError: commands.remove(cmd)
commands.append(cmd) commands.append(cmd)
# add system_command to separate list as well, # add system_command to separate list as well,
# for quick look-up # for quick look-up. These have no
if cmd.key.startswith("__"): if cmd.key.startswith("__"):
try: # remove same-matches and add new
ic = system_commands.index(cmd) for _dum in range(system_commands.count(cmd)):
system_commands[ic] = cmd # replace system_commands.remove(cmd)
except ValueError: system_commands.append(cmd)
system_commands.append(cmd)
self.commands = commands
if not allow_duplicates: if not allow_duplicates:
# extra run to make sure to avoid doublets # 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): def remove(self, cmd):
""" """

View file

@ -262,7 +262,7 @@ class Command(metaclass=CommandMeta):
str, too. str, too.
""" """
return hash("\n".join(self._matchset)) return hash("command")
def __ne__(self, cmd): def __ne__(self, cmd):
""" """

View file

@ -4,45 +4,44 @@ Unit testing for the Command system itself.
""" """
from django.test import override_settings 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.cmdset import CmdSet
from evennia.commands.command import Command from evennia.commands.command import Command
from evennia.commands import cmdparser from evennia.utils.test_resources import BaseEvenniaTest, TestCase
# Testing-command sets # 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" key = "A"
def __init__(self, cmdset, *args, **kwargs):
super().__init__(*args, **kwargs)
self.from_cmdset = cmdset
class _CmdB(_BaseCmd):
class _CmdB(Command):
key = "B" key = "B"
def __init__(self, cmdset, *args, **kwargs):
super().__init__(*args, **kwargs)
self.from_cmdset = cmdset
class _CmdC(_BaseCmd):
class _CmdC(Command):
key = "C" key = "C"
def __init__(self, cmdset, *args, **kwargs):
super().__init__(*args, **kwargs)
self.from_cmdset = cmdset
class _CmdD(_BaseCmd):
class _CmdD(Command):
key = "D" key = "D"
def __init__(self, cmdset, *args, **kwargs):
super().__init__(*args, **kwargs) class _CmdEe(_BaseCmd):
self.from_cmdset = cmdset key = "E"
aliases = ["ee"]
class _CmdEf(_BaseCmd):
key = "E"
aliases = ["ff"]
class _CmdSetA(CmdSet): class _CmdSetA(CmdSet):
@ -82,6 +81,14 @@ class _CmdSetD(CmdSet):
self.add(_CmdD("D")) 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 # testing Command Sets
@ -816,7 +823,7 @@ class TestDuplicateBehavior(TestCase):
self.cmdset_d.priority = 0 self.cmdset_d.priority = 0
self.cmdset_a.duplicates = True 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 Test of `duplicates` transfer which does not propagate. Only
A has duplicates=True. A has duplicates=True.
@ -831,7 +838,7 @@ class TestDuplicateBehavior(TestCase):
self.assertIsNone(cmdset_f.duplicates) self.assertIsNone(cmdset_f.duplicates)
self.assertEqual(len(cmdset_f.commands), 8) 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. Test of `duplicates` transfer, which does not propagate.
C.duplication=True C.duplication=True
@ -982,6 +989,7 @@ class TestOptionTransferReplace(TestCase):
import sys import sys
from evennia.commands import cmdhandler from evennia.commands import cmdhandler
from twisted.trial.unittest import TestCase as TwistedTestCase from twisted.trial.unittest import TestCase as TwistedTestCase
@ -1085,6 +1093,11 @@ class TestGetAndMergeCmdSets(TwistedTestCase, BaseEvenniaTest):
deferred.addCallback(_callback) deferred.addCallback(_callback)
return deferred 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): class AccessableCommand(Command):
def access(*args, **kwargs): def access(*args, **kwargs):