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.
"""
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):
"""

View file

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

View file

@ -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):