Add a @spawn unit test

Tests the @spawn command and its variations.  The test is designed to
aslo check the properties of the spawned objects for some common/obvious
settings.

Test also adds a new prototypes file to be used with the Evennia unit
tester for any tests that require named prototypes.  The spawner test
requires testing named prototypes, so it is added in this change.
This commit is contained in:
Scyfris Talivinsky 2017-10-06 01:34:10 -07:00
parent a994fd3c63
commit b6649bcef8
3 changed files with 91 additions and 0 deletions

View file

@ -24,6 +24,8 @@ from evennia.commands.default import help, general, system, admin, account, buil
from evennia.commands.command import Command, InterruptCommand
from evennia.utils import ansi, utils
from evennia.server.sessionhandler import SESSIONS
from evennia import search_object
from evennia import DefaultObject, DefaultCharacter
# set up signal here since we are not starting the server
@ -296,6 +298,73 @@ class TestBuilding(CommandTest):
def test_teleport(self):
self.call(building.CmdTeleport(), "Room2", "Room2(#2)\n|Teleported to Room2.")
def test_spawn(self):
def getObject(commandTest, objKeyStr):
# A helper function to get a spawned object and
# check that it exists in the process.
query = search_object(objKeyStr)
commandTest.assertIsNotNone(query)
obj = query[0]
commandTest.assertIsNotNone(obj)
return obj
# Tests "@spawn" without any arguments.
self.call(building.CmdSpawn(), " ", "Usage: @spawn")
# Tests "@spawn <prototype_dictionary>" without specifying location.
self.call(building.CmdSpawn(), \
"{'key':'goblin', 'typeclass':'evennia.DefaultCharacter'}", "Spawned goblin")
goblin = getObject(self, "goblin")
# Tests that the spawned object's type is a DefaultCharacter.
self.assertIsInstance(goblin, DefaultCharacter)
# Tests that the spawned object's location is the same as the caharacter's location, since
# we did not specify it.
self.assertEqual(goblin.location, self.char1.location)
goblin.delete()
# Test "@spawn <prototype_dictionary>" with a location other than the character's.
spawnLoc = self.room2
if spawnLoc == self.char1.location:
# Just to make sure we use a different location, in case someone changes
# char1's default location in the future...
spawnLoc = self.room1
self.call(building.CmdSpawn(), \
"{'prototype':'GOBLIN', 'key':'goblin', 'location':'%s'}" \
% spawnLoc.dbref, "Spawned goblin")
goblin = getObject(self, "goblin")
self.assertEqual(goblin.location, spawnLoc)
goblin.delete()
# Tests "@spawn <prototype_name>"
self.call(building.CmdSpawn(), "'BALL'", "Spawned Ball")
ball = getObject(self, "Ball")
self.assertEqual(ball.location, self.char1.location)
self.assertIsInstance(ball, DefaultObject)
ball.delete()
# Tests "@spawn/noloc ..." without specifying a location.
# Location should be "None".
self.call(building.CmdSpawn(), "/noloc 'BALL'", "Spawned Ball")
ball = getObject(self, "Ball")
self.assertIsNone(ball.location)
ball.delete()
# Tests "@spawn/noloc ...", but DO specify a location.
# Location should be the specified location.
self.call(building.CmdSpawn(), \
"/noloc {'prototype':'BALL', 'location':'%s'}" \
% spawnLoc.dbref, "Spawned Ball")
ball = getObject(self, "Ball")
self.assertEqual(ball.location, spawnLoc)
ball.delete()
# test calling spawn with an invalid prototype.
self.call(building.CmdSpawn(), \
"'NO_EXIST'", "No prototype named 'NO_EXIST'")
class TestComms(CommandTest):

View file

@ -0,0 +1,20 @@
"""
test_prototypes
This is meant to be used with Evennia unittest framework
to provide some named prototypes for use with various tests
(for example commands that accept named prototypes, not just
prototype dictionaries)
"""
GOBLIN = {
"key" : "Goblin", \
"typeclass" : "DefaultCharacter", \
"desc" : "A goblin."
}
BALL = {
"key" : "Ball", \
"typeclass" : "DefaultObject", \
"desc" : "A ball."
}

View file

@ -34,6 +34,8 @@ class EvenniaTest(TestCase):
self.room1 = create.create_object(self.room_typeclass, key="Room", nohome=True)
self.room1.db.desc = "room_desc"
settings.DEFAULT_HOME = "#%i" % self.room1.id # we must have a default home
# Set up fake prototype module for allowing tests to use named prototypes.
settings.PROTOTYPE_MODULES = "evennia.utils.test_prototypes"
self.room2 = create.create_object(self.room_typeclass, key="Room2")
self.exit = create.create_object(self.exit_typeclass, key='out', location=self.room1, destination=self.room2)
self.obj1 = create.create_object(self.object_typeclass, key="Obj", location=self.room1, home=self.room1)