Upgrade test runner to derive from DiscoveryRunner.

This commit is contained in:
Jonathan Piacenti 2015-02-22 17:48:24 -06:00 committed by Griatch
parent f075bcf297
commit c03bac5efd
10 changed files with 67 additions and 97 deletions

View file

@ -0,0 +1,58 @@
from django.conf import settings
from django.test import TestCase
from mock import Mock
from evennia.objects import DefaultObject, DefaultCharacter, DefaultRoom
from evennia.players import DefaultPlayer
from evennia.scripts import DefaultScript
from evennia.server.serversession import ServerSession
from evennia.server.sessionhandler import SESSIONS
from evennia.utils import create
from evennia.utils.idmapper.base import flush_cache
SESSIONS.data_out = Mock()
SESSIONS.disconnect = Mock()
class EvenniaTest(TestCase):
"""
Base test for Evennia, sets up a basic environment.
"""
player_typeclass = DefaultPlayer
object_typeclass = DefaultObject
character_typeclass = DefaultCharacter
room_typeclass = DefaultRoom
script_typeclass = DefaultScript
def setUp(self):
"""
Sets up testing environment
"""
self.player = create.create_player("TestPlayer", email="test@test.com", password="testpassword", typeclass=self.player_typeclass)
self.player2 = create.create_player("TestPlayer2", email="test@test.com", password="testpassword", typeclass=self.player_typeclass)
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
self.room2 = create.create_object(self.room_typeclass, key="Room2")
self.obj1 = create.create_object(self.object_typeclass, key="Obj", location=self.room1, home=self.room1)
self.obj2 = create.create_object(self.object_typeclass, key="Obj2", location=self.room1, home=self.room1)
self.char1 = create.create_object(self.character_typeclass, key="Char", location=self.room1, home=self.room1)
self.char1.permissions.add("Immortals")
self.char2 = create.create_object(self.character_typeclass, key="Char2", location=self.room1, home=self.room1)
self.char1.player = self.player
self.char2.player = self.player2
self.script = create.create_script(self.script_typeclass, key="Script")
self.player.permissions.add("Immortals")
# set up a fake session
session = ServerSession()
session.init_session("telnet", ("localhost", "testmode"), SESSIONS)
session.sessid = 1
SESSIONS.portal_connect(session.get_sync_data())
SESSIONS.login(SESSIONS.session_from_sessid(1), self.player, testmode=True)
self.session = session
def tearDown(self):
flush_cache()
del SESSIONS.sessions[self.session.sessid]

View file

@ -6,6 +6,7 @@ except ImportError:
from django.test import TestCase
from ansi import ANSIString
import utils
class ANSIStringTestCase(TestCase):
@ -119,3 +120,60 @@ class ANSIStringTestCase(TestCase):
target = ANSIString('{gtest{n')
result = u'\x1b[1m\x1b[32mTest\x1b[0m'
self.checker(target.capitalize(), result, u'Test')
class TestIsIter(TestCase):
def test_is_iter(self):
self.assertEqual(True, utils.is_iter([1,2,3,4]))
self.assertEqual(False, utils.is_iter("This is not an iterable"))
class TestCrop(TestCase):
def test_crop(self):
# No text, return no text
self.assertEqual("", utils.crop("", width=10, suffix="[...]"))
# Input length equal to max width, no crop
self.assertEqual("0123456789", utils.crop("0123456789", width=10, suffix="[...]"))
# Input length greater than max width, crop (suffix included in width)
self.assertEqual("0123[...]", utils.crop("0123456789", width=9, suffix="[...]"))
# Input length less than desired width, no crop
self.assertEqual("0123", utils.crop("0123", width=9, suffix="[...]"))
# Width too small or equal to width of suffix
self.assertEqual("012", utils.crop("0123", width=3, suffix="[...]"))
self.assertEqual("01234", utils.crop("0123456", width=5, suffix="[...]"))
class TestDedent(TestCase):
def test_dedent(self):
#print "Did TestDedent run?"
# Empty string, return empty string
self.assertEqual("", utils.dedent(""))
# No leading whitespace
self.assertEqual("TestDedent", utils.dedent("TestDedent"))
# Leading whitespace, single line
self.assertEqual("TestDedent", utils.dedent(" TestDedent"))
# Leading whitespace, multi line
input_string = " hello\n world"
expected_string = "hello\nworld"
self.assertEqual(expected_string, utils.dedent(input_string))
class TestListToString(TestCase):
"""
Default function header from utils.py:
list_to_string(inlist, endsep="and", addquote=False)
Examples:
no endsep:
[1,2,3] -> '1, 2, 3'
with endsep=='and':
[1,2,3] -> '1, 2 and 3'
with addquote and endsep
[1,2,3] -> '"1", "2" and "3"'
"""
def test_list_to_string(self):
self.assertEqual('1, 2, 3', utils.list_to_string([1,2,3], endsep=""))
self.assertEqual('"1", "2", "3"', utils.list_to_string([1,2,3], endsep="", addquote=True))
self.assertEqual('1, 2 and 3', utils.list_to_string([1,2,3]))
self.assertEqual('"1", "2" and "3"', utils.list_to_string([1,2,3], endsep="and", addquote=True))