Add more unit tests for content types

This commit is contained in:
Griatch 2020-04-13 11:47:24 +02:00
parent 556091c395
commit 7ac53b576f
2 changed files with 36 additions and 3 deletions

View file

@ -203,6 +203,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
without `obj.save()` having to be called explicitly.
"""
# Used for sorting / filtering in inventories / room contents.
_content_types = ("object",)
@ -1659,6 +1660,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
**kwargs (dict): Arbitrary, optional arguments for users
overriding the call (unused by default).
"""
def filter_visible(obj_list):
# Helper method to determine if objects are visible to the looker.
return [obj for obj in obj_list if obj != looker and obj.access(looker, "view")]
@ -1667,8 +1669,8 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
return ""
# get and identify all objects
exits_list = filter_visible(self.contents_get(content_type='exit'))
users_list = filter_visible(self.contents_get(content_type='character'))
exits_list = filter_visible(self.contents_get(content_type="exit"))
users_list = filter_visible(self.contents_get(content_type="character"))
things_list = filter_visible(self.contents_get(content_type="object"))
things = defaultdict(list)
@ -2034,6 +2036,7 @@ class DefaultCharacter(DefaultObject):
a character avatar controlled by an account.
"""
# Tuple of types used for indexing inventory contents. Characters generally wouldn't be in
# anyone's inventory, but this also governs displays in room contents.
_content_types = ("character",)
@ -2288,6 +2291,7 @@ class DefaultRoom(DefaultObject):
This is the base room object. It's just like any Object except its
location is always `None`.
"""
# A tuple of strings used for indexing this object inside an inventory.
# Generally, a room isn't expected to HAVE a location, but maybe in some games?
_content_types = ("room",)
@ -2441,6 +2445,7 @@ class DefaultExit(DefaultObject):
exits simply by giving the exit-object's name on its own.
"""
_content_types = ("exit",)
exit_command = ExitCommand
priority = 101

View file

@ -147,10 +147,12 @@ class TestObjectManager(EvenniaTest):
self.assertEqual(self.obj1.attributes.get(key="phrase", category="adventure"), "plugh")
self.assertEqual(obj2.attributes.get(key="phrase", category="adventure"), "plugh")
class TestContentHandler(EvenniaTest):
"Test the ContentHandler (obj.contents)"
def test_cache_clearing(self):
def test_object_create_remove(self):
"""Create/destroy object"""
self.assertTrue(self.obj1 in self.room1.contents)
self.assertTrue(self.obj2 in self.room1.contents)
@ -159,3 +161,29 @@ class TestContentHandler(EvenniaTest):
obj3.delete()
self.assertFalse(obj3 in self.room1.contents)
def test_object_move(self):
"""Move object from room to room in various ways"""
self.assertTrue(self.obj1 in self.room1.contents)
# use move_to hook
self.obj1.move_to(self.room2)
self.assertFalse(self.obj1 in self.room1.contents)
self.assertTrue(self.obj1 in self.room2.contents)
# move back via direct setting of .location
self.obj1.location = self.room1
self.assertTrue(self.obj1 in self.room1.contents)
self.assertFalse(self.obj1 in self.room2.contents)
def test_content_type(self):
self.assertEqual(
set(self.room1.contents_get()),
set([self.char1, self.char2, self.obj1, self.obj2, self.exit]),
)
self.assertEqual(
set(self.room1.contents_get(content_type="object")), set([self.obj1, self.obj2])
)
self.assertEqual(
set(self.room1.contents_get(content_type="character")), set([self.char1, self.char2])
)
self.assertEqual(set(self.room1.contents_get(content_type="exit")), set([self.exit]))