Use Mocks to handle test cases.

This commit is contained in:
Jonathan Piacenti 2015-02-22 16:22:29 -06:00 committed by Griatch
parent 2edb82095a
commit 584d94b32b
3 changed files with 47 additions and 68 deletions

View file

@ -14,10 +14,9 @@ main test suite started with
import re import re
from django.conf import settings from django.conf import settings
from mock import Mock
from evennia.commands.default.cmdset_character import CharacterCmdSet from evennia.commands.default.cmdset_character import CharacterCmdSet
import evennia
evennia.init()
from evennia.tests.resources import EvenniaTest from evennia.tests.resources import EvenniaTest
from evennia.commands.default import help, general, system, admin, player, building, batchprocess, comms from evennia.commands.default import help, general, system, admin, player, building, batchprocess, comms
from evennia.utils import ansi from evennia.utils import ansi
@ -37,7 +36,7 @@ class CommandTest(EvenniaTest):
Tests a command Tests a command
""" """
def call(self, cmdobj, args, msg=None, cmdset=None, noansi=True, caller=None): def call(self, cmdobj, args, msg=None, cmdset=None, noansi=True, caller=None, receiver=None):
""" """
Test a command by assigning all the needed Test a command by assigning all the needed
properties to cmdobj and running properties to cmdobj and running
@ -48,7 +47,9 @@ class CommandTest(EvenniaTest):
The msgreturn value is compared to eventual The msgreturn value is compared to eventual
output sent to caller.msg in the game output sent to caller.msg in the game
""" """
cmdobj.caller = caller if caller else self.char1 caller = caller if caller else self.char1
receiver = receiver if receiver else caller
cmdobj.caller = caller
cmdobj.cmdstring = cmdobj.key cmdobj.cmdstring = cmdobj.key
cmdobj.args = args cmdobj.args = args
cmdobj.cmdset = cmdset cmdobj.cmdset = cmdset
@ -58,22 +59,26 @@ class CommandTest(EvenniaTest):
cmdobj.raw_string = cmdobj.key + " " + args cmdobj.raw_string = cmdobj.key + " " + args
cmdobj.obj = caller if caller else self.char1 cmdobj.obj = caller if caller else self.char1
# test # test
self.char1.player.ndb.stored_msg = [] old_msg = receiver.msg
cmdobj.at_pre_cmd() try:
cmdobj.parse() receiver.msg = Mock()
cmdobj.func() cmdobj.at_pre_cmd()
cmdobj.at_post_cmd() cmdobj.parse()
# clean out prettytable sugar cmdobj.func()
stored_msg = self.char1.player.ndb.stored_msg if self.char1.player else self.char1.ndb.stored_msg cmdobj.at_post_cmd()
returned_msg = "|".join(_RE.sub("", mess) for mess in stored_msg) # clean out prettytable sugar
returned_msg = ansi.parse_ansi(returned_msg, strip_ansi=noansi).strip() stored_msg = [args[0] for name, args, kwargs in receiver.msg.mock_calls]
if msg != None: returned_msg = "|".join(_RE.sub("", mess) for mess in stored_msg)
if msg == "" and returned_msg or not returned_msg.startswith(msg.strip()): returned_msg = ansi.parse_ansi(returned_msg, strip_ansi=noansi).strip()
sep1 = "\n" + "="*30 + "Wanted message" + "="*34 + "\n" if msg is not None:
sep2 = "\n" + "="*30 + "Returned message" + "="*32 + "\n" if msg == "" and returned_msg or not returned_msg.startswith(msg.strip()):
sep3 = "\n" + "="*78 sep1 = "\n" + "="*30 + "Wanted message" + "="*34 + "\n"
retval = sep1 + msg.strip() + sep2 + returned_msg + sep3 sep2 = "\n" + "="*30 + "Returned message" + "="*32 + "\n"
raise AssertionError(retval) sep3 = "\n" + "="*78
retval = sep1 + msg.strip() + sep2 + returned_msg + sep3
raise AssertionError(retval)
finally:
receiver.msg = old_msg
# ------------------------------------------------------------ # ------------------------------------------------------------
# Individual module Tests # Individual module Tests
@ -167,7 +172,7 @@ class TestPlayer(CommandTest):
self.call(player.CmdOOC(), "", "You are already", caller=self.player) self.call(player.CmdOOC(), "", "You are already", caller=self.player)
def test_ic(self): def test_ic(self):
self.call(player.CmdIC(), "Char", "You become Char.", caller=self.player) self.call(player.CmdIC(), "Char", "You become Char.", caller=self.player, receiver=self.char1)
def test_password(self): def test_password(self):
self.call(player.CmdPassword(), "testpassword = testpassword", "Password changed.", caller=self.player) self.call(player.CmdPassword(), "testpassword = testpassword", "Password changed.", caller=self.player)
@ -191,7 +196,7 @@ class TestPlayer(CommandTest):
self.call(player.CmdCharCreate(), "Test1=Test char", "Created new character Test1. Use @ic Test1 to enter the game", caller=self.player) self.call(player.CmdCharCreate(), "Test1=Test char", "Created new character Test1. Use @ic Test1 to enter the game", caller=self.player)
def test_quell(self): def test_quell(self):
self.call(player.CmdIC(), "Char", "You become Char.", caller=self.player) self.call(player.CmdIC(), "Char", "You become Char.", caller=self.player, receiver=self.char1)
self.call(player.CmdQuell(), "", "Quelling to current puppet's permissions (immortals).", caller=self.player) self.call(player.CmdQuell(), "", "Quelling to current puppet's permissions (immortals).", caller=self.player)
@ -244,7 +249,7 @@ class TestBuilding(CommandTest):
def test_typeclass(self): def test_typeclass(self):
self.call(building.CmdTypeclass(), "Obj = evennia.objects.objects.DefaultExit", self.call(building.CmdTypeclass(), "Obj = evennia.objects.objects.DefaultExit",
"Obj changed typeclass from evennia.tests.resources.TestObjectClass to evennia.objects.objects.DefaultExit.") "Obj changed typeclass from evennia.objects.objects.DefaultObject to evennia.objects.objects.DefaultExit.")
def test_lock(self): def test_lock(self):
self.call(building.CmdLock(), "Obj = test:perm(Immortals)", "Added lock 'test:perm(Immortals)' to Obj.") self.call(building.CmdLock(), "Obj = test:perm(Immortals)", "Added lock 'test:perm(Immortals)' to Obj.")
@ -263,39 +268,39 @@ class TestComms(CommandTest):
def setUp(self): def setUp(self):
super(CommandTest, self).setUp() super(CommandTest, self).setUp()
self.call(comms.CmdChannelCreate(), "testchan;test=Test Channel", "Created channel testchan and connected to it.") self.call(comms.CmdChannelCreate(), "testchan;test=Test Channel", "Created channel testchan and connected to it.", receiver=self.player)
def test_toggle_com(self): def test_toggle_com(self):
self.call(comms.CmdAddCom(), "tc = testchan", "You are already connected to channel testchan. You can now") self.call(comms.CmdAddCom(), "tc = testchan", "You are already connected to channel testchan. You can now", receiver=self.player)
self.call(comms.CmdDelCom(), "tc", "Your alias 'tc' for channel testchan was cleared.") self.call(comms.CmdDelCom(), "tc", "Your alias 'tc' for channel testchan was cleared.", receiver=self.player)
def test_channels(self): def test_channels(self):
self.call(comms.CmdChannels(), "" ,"Available channels (use comlist,addcom and delcom to manage") self.call(comms.CmdChannels(), "" ,"Available channels (use comlist,addcom and delcom to manage", receiver=self.player)
def test_all_com(self): def test_all_com(self):
self.call(comms.CmdAllCom(), "", "Available channels (use comlist,addcom and delcom to manage") self.call(comms.CmdAllCom(), "", "Available channels (use comlist,addcom and delcom to manage", receiver=self.player)
def test_clock(self): def test_clock(self):
self.call(comms.CmdClock(), "testchan=send:all()", "Lock(s) applied. Current locks on testchan:") self.call(comms.CmdClock(), "testchan=send:all()", "Lock(s) applied. Current locks on testchan:", receiver=self.player)
def test_cdesc(self): def test_cdesc(self):
self.call(comms.CmdCdesc(), "testchan = Test Channel", "Description of channel 'testchan' set to 'Test Channel'.") self.call(comms.CmdCdesc(), "testchan = Test Channel", "Description of channel 'testchan' set to 'Test Channel'.", receiver=self.player)
def test_cemit(self): def test_cemit(self):
self.call(comms.CmdCemit(), "testchan = Test Message", "[testchan] Test Message|Sent to channel testchan: Test Message") self.call(comms.CmdCemit(), "testchan = Test Message", "[testchan] Test Message|Sent to channel testchan: Test Message", receiver=self.player)
def test_cwho(self): def test_cwho(self):
self.call(comms.CmdCWho(), "testchan", "Channel subscriptions\ntestchan:\n TestPlayer") self.call(comms.CmdCWho(), "testchan", "Channel subscriptions\ntestchan:\n TestPlayer", receiver=self.player)
def test_page(self): def test_page(self):
self.call(comms.CmdPage(), "TestPlayer2 = Test", "TestPlayer2 is offline. They will see your message if they list their pages later.|You paged TestPlayer2 with: 'Test'.") self.call(comms.CmdPage(), "TestPlayer2 = Test", "TestPlayer2 is offline. They will see your message if they list their pages later.|You paged TestPlayer2 with: 'Test'.", receiver=self.player)
def test_cboot(self): def test_cboot(self):
# No one else connected to boot # No one else connected to boot
self.call(comms.CmdCBoot(), "", "Usage: @cboot[/quiet] <channel> = <player> [:reason]") self.call(comms.CmdCBoot(), "", "Usage: @cboot[/quiet] <channel> = <player> [:reason]", receiver=self.player)
def test_cdestroy(self): def test_cdestroy(self):
self.call(comms.CmdCdestroy(), "testchan" ,"[testchan] TestPlayer: testchan is being destroyed. Make sure to change your aliases.|Channel 'testchan' was destroyed.") self.call(comms.CmdCdestroy(), "testchan" ,"[testchan] TestPlayer: testchan is being destroyed. Make sure to change your aliases.|Channel 'testchan' was destroyed.", receiver=self.player)
class TestBatchProcess(CommandTest): class TestBatchProcess(CommandTest):

View file

@ -1,5 +1,6 @@
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase
from mock import Mock
from evennia.objects import DefaultObject, DefaultCharacter, DefaultRoom from evennia.objects import DefaultObject, DefaultCharacter, DefaultRoom
from evennia.players import DefaultPlayer from evennia.players import DefaultPlayer
from evennia.scripts import DefaultScript from evennia.scripts import DefaultScript
@ -9,45 +10,17 @@ from evennia.utils import create
from evennia.utils.idmapper.base import flush_cache from evennia.utils.idmapper.base import flush_cache
def dummy(self, *args, **kwargs): SESSIONS.data_out = Mock()
pass SESSIONS.disconnect = Mock()
SESSIONS.data_out = dummy
SESSIONS.disconnect = dummy
class TestObjectClass(DefaultObject):
def msg(self, text="", **kwargs):
"test message"
pass
class TestCharacterClass(DefaultCharacter):
def msg(self, text="", **kwargs):
"test message"
if self.player:
self.player.msg(text=text, **kwargs)
else:
if not self.ndb.stored_msg:
self.ndb.stored_msg = []
self.ndb.stored_msg.append(text)
class TestPlayerClass(DefaultPlayer):
def msg(self, text="", **kwargs):
"test message"
if not self.ndb.stored_msg:
self.ndb.stored_msg = []
self.ndb.stored_msg.append(text)
class EvenniaTest(TestCase): class EvenniaTest(TestCase):
""" """
Base test for Evennia, sets up a basic environment. Base test for Evennia, sets up a basic environment.
""" """
player_typeclass = TestPlayerClass player_typeclass = DefaultPlayer
object_typeclass = TestObjectClass object_typeclass = DefaultObject
character_typeclass = TestCharacterClass character_typeclass = DefaultCharacter
room_typeclass = DefaultRoom room_typeclass = DefaultRoom
script_typeclass = DefaultScript script_typeclass = DefaultScript

View file

@ -3,4 +3,5 @@
django >= 1.7 django >= 1.7
twisted >= 12.0 twisted >= 12.0
mock >= 1.0.1
pillow pillow