From b6649bcef8a87c470e033bc34dfde74a65866b6d Mon Sep 17 00:00:00 2001 From: Scyfris Talivinsky Date: Fri, 6 Oct 2017 01:34:10 -0700 Subject: [PATCH] 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. --- evennia/commands/default/tests.py | 69 +++++++++++++++++++++++++++++++ evennia/utils/test_prototypes.py | 20 +++++++++ evennia/utils/test_resources.py | 2 + 3 files changed, 91 insertions(+) create mode 100644 evennia/utils/test_prototypes.py diff --git a/evennia/commands/default/tests.py b/evennia/commands/default/tests.py index 20eb3bacae..ffb63ef723 100644 --- a/evennia/commands/default/tests.py +++ b/evennia/commands/default/tests.py @@ -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 " 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 " 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 " + 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): diff --git a/evennia/utils/test_prototypes.py b/evennia/utils/test_prototypes.py new file mode 100644 index 0000000000..5c79bad462 --- /dev/null +++ b/evennia/utils/test_prototypes.py @@ -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." +} diff --git a/evennia/utils/test_resources.py b/evennia/utils/test_resources.py index 47716e979e..f6785a2f1f 100644 --- a/evennia/utils/test_resources.py +++ b/evennia/utils/test_resources.py @@ -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)