First version of moved contrib tests

This commit is contained in:
Griatch 2021-12-18 23:42:14 +01:00
parent a6cb94056c
commit 04a95297b5
37 changed files with 4135 additions and 3589 deletions

View file

@ -0,0 +1,103 @@
"""
Testing of ExtendedRoom contrib
"""
import datetime
from mock import patch, Mock
from django.conf import settings
from evennia.commands.default.tests import CommandTest
from evennia.objects.objects import DefaultRoom
from . import extended_room
class ForceUTCDatetime(datetime.datetime):
"""Force UTC datetime."""
@classmethod
def fromtimestamp(cls, timestamp):
"""Force fromtimestamp to run with naive datetimes."""
return datetime.datetime.utcfromtimestamp(timestamp)
@patch("evennia.contrib.extended_room.datetime.datetime", ForceUTCDatetime)
# mock gametime to return April 9, 2064, at 21:06 (spring evening)
@patch("evennia.utils.gametime.gametime", new=Mock(return_value=2975000766))
class TestExtendedRoom(CommandTest):
room_typeclass = extended_room.ExtendedRoom
DETAIL_DESC = "A test detail."
SPRING_DESC = "A spring description."
OLD_DESC = "Old description."
settings.TIME_ZONE = "UTC"
def setUp(self):
super().setUp()
self.room1.ndb.last_timeslot = "afternoon"
self.room1.ndb.last_season = "winter"
self.room1.db.details = {"testdetail": self.DETAIL_DESC}
self.room1.db.spring_desc = self.SPRING_DESC
self.room1.db.desc = self.OLD_DESC
def test_return_appearance(self):
# get the appearance of a non-extended room for contrast purposes
old_desc = DefaultRoom.return_appearance(self.room1, self.char1)
# the new appearance should be the old one, but with the desc switched
self.assertEqual(
old_desc.replace(self.OLD_DESC, self.SPRING_DESC),
self.room1.return_appearance(self.char1),
)
self.assertEqual("spring", self.room1.ndb.last_season)
self.assertEqual("evening", self.room1.ndb.last_timeslot)
def test_return_detail(self):
self.assertEqual(self.DETAIL_DESC, self.room1.return_detail("testdetail"))
def test_cmdextendedlook(self):
rid = self.room1.id
self.call(
extended_room.CmdExtendedRoomLook(),
"here",
"Room(#{})\n{}".format(rid, self.SPRING_DESC),
)
self.call(extended_room.CmdExtendedRoomLook(), "testdetail", self.DETAIL_DESC)
self.call(
extended_room.CmdExtendedRoomLook(), "nonexistent", "Could not find 'nonexistent'."
)
def test_cmdsetdetail(self):
self.call(extended_room.CmdExtendedRoomDetail(), "", "Details on Room")
self.call(
extended_room.CmdExtendedRoomDetail(),
"thingie = newdetail with spaces",
"Detail set 'thingie': 'newdetail with spaces'",
)
self.call(extended_room.CmdExtendedRoomDetail(), "thingie", "Detail 'thingie' on Room:\n")
self.call(
extended_room.CmdExtendedRoomDetail(),
"/del thingie",
"Detail thingie deleted, if it existed.",
cmdstring="detail",
)
self.call(extended_room.CmdExtendedRoomDetail(), "thingie", "Detail 'thingie' not found.")
# Test with aliases
self.call(extended_room.CmdExtendedRoomDetail(), "", "Details on Room")
self.call(
extended_room.CmdExtendedRoomDetail(),
"thingie;other;stuff = newdetail with spaces",
"Detail set 'thingie;other;stuff': 'newdetail with spaces'",
)
self.call(extended_room.CmdExtendedRoomDetail(), "thingie", "Detail 'thingie' on Room:\n")
self.call(extended_room.CmdExtendedRoomDetail(), "other", "Detail 'other' on Room:\n")
self.call(extended_room.CmdExtendedRoomDetail(), "stuff", "Detail 'stuff' on Room:\n")
self.call(
extended_room.CmdExtendedRoomDetail(),
"/del other;stuff",
"Detail other;stuff deleted, if it existed.",
)
self.call(extended_room.CmdExtendedRoomDetail(), "other", "Detail 'other' not found.")
self.call(extended_room.CmdExtendedRoomDetail(), "stuff", "Detail 'stuff' not found.")
def test_cmdgametime(self):
self.call(extended_room.CmdExtendedRoomGameTime(), "", "It's a spring day, in the evening.")

View file

@ -0,0 +1,33 @@
"""
Test map builder.
"""
from evennia.commands.default.tests import CommandTest
from . import mapbuilder
class TestMapBuilder(CommandTest):
def test_cmdmapbuilder(self):
self.call(
mapbuilder.CmdMapBuilder(),
"evennia.contrib.mapbuilder.EXAMPLE1_MAP evennia.contrib.mapbuilder.EXAMPLE1_LEGEND",
"""Creating Map...|≈≈≈≈≈
n
n
|Creating Landmass...|""",
)
self.call(
mapbuilder.CmdMapBuilder(),
"evennia.contrib.mapbuilder.EXAMPLE2_MAP evennia.contrib.mapbuilder.EXAMPLE2_LEGEND",
"""Creating Map...|≈ ≈ ≈ ≈ ≈
--
--
|Creating Landmass...|""",
)

View file

@ -0,0 +1,29 @@
"""
Tests of simpledoor.
"""
from evennia.commands.default.tests import CommandTest
from . import simpledoor
class TestSimpleDoor(CommandTest):
def test_cmdopen(self):
self.call(
simpledoor.CmdOpen(),
"newdoor;door:contrib.simpledoor.SimpleDoor,backdoor;door = Room2",
"Created new Exit 'newdoor' from Room to Room2 (aliases: door).|Note: A door-type exit was "
"created - ignored eventual custom return-exit type.|Created new Exit 'newdoor' from Room2 to Room (aliases: door).",
)
self.call(simpledoor.CmdOpenCloseDoor(), "newdoor", "You close newdoor.", cmdstring="close")
self.call(
simpledoor.CmdOpenCloseDoor(),
"newdoor",
"newdoor is already closed.",
cmdstring="close",
)
self.call(simpledoor.CmdOpenCloseDoor(), "newdoor", "You open newdoor.", cmdstring="open")
self.call(
simpledoor.CmdOpenCloseDoor(), "newdoor", "newdoor is already open.", cmdstring="open"
)

View file

@ -0,0 +1,27 @@
"""
Slow exit tests.
"""
from mock import Mock, patch
from evennia.commands.default.tests import CommandTest
from evennia.utils.create import create_object
from . import slow_exit
slow_exit.MOVE_DELAY = {"stroll": 0, "walk": 0, "run": 0, "sprint": 0}
def _cancellable_mockdelay(time, callback, *args, **kwargs):
callback(*args, **kwargs)
return Mock()
class TestSlowExit(CommandTest):
@patch("evennia.utils.delay", _cancellable_mockdelay)
def test_exit(self):
exi = create_object(
slow_exit.SlowExit, key="slowexit", location=self.room1, destination=self.room2
)
exi.at_traverse(self.char1, self.room2)
self.call(slow_exit.CmdSetSpeed(), "walk", "You are now walking.")
self.call(slow_exit.CmdStop(), "", "You stop moving.")

View file

@ -0,0 +1,137 @@
"""
Test wilderness
"""
from evennia.utils.test_resources import EvenniaTest
from evennia import DefaultCharacter
from evennia.utils.create import create_object
from . import wilderness
class TestWilderness(EvenniaTest):
def setUp(self):
super().setUp()
self.char1 = create_object(DefaultCharacter, key="char1")
self.char2 = create_object(DefaultCharacter, key="char2")
def get_wilderness_script(self, name="default"):
w = wilderness.WildernessScript.objects.get("default")
return w
def test_create_wilderness_default_name(self):
wilderness.create_wilderness()
w = self.get_wilderness_script()
self.assertIsNotNone(w)
def test_create_wilderness_custom_name(self):
name = "customname"
wilderness.create_wilderness(name)
w = self.get_wilderness_script(name)
self.assertIsNotNone(w)
def test_enter_wilderness(self):
wilderness.create_wilderness()
wilderness.enter_wilderness(self.char1)
self.assertIsInstance(self.char1.location, wilderness.WildernessRoom)
w = self.get_wilderness_script()
self.assertEqual(w.db.itemcoordinates[self.char1], (0, 0))
def test_enter_wilderness_custom_coordinates(self):
wilderness.create_wilderness()
wilderness.enter_wilderness(self.char1, coordinates=(1, 2))
self.assertIsInstance(self.char1.location, wilderness.WildernessRoom)
w = self.get_wilderness_script()
self.assertEqual(w.db.itemcoordinates[self.char1], (1, 2))
def test_enter_wilderness_custom_name(self):
name = "customnname"
wilderness.create_wilderness(name)
wilderness.enter_wilderness(self.char1, name=name)
self.assertIsInstance(self.char1.location, wilderness.WildernessRoom)
def test_wilderness_correct_exits(self):
wilderness.create_wilderness()
wilderness.enter_wilderness(self.char1)
# By default we enter at a corner (0, 0), so only a few exits should
# be visible / traversable
exits = [
i
for i in self.char1.location.contents
if i.destination and (i.access(self.char1, "view") or i.access(self.char1, "traverse"))
]
self.assertEqual(len(exits), 3)
exitsok = ["north", "northeast", "east"]
for each_exit in exitsok:
self.assertTrue(any([e for e in exits if e.key == each_exit]))
# If we move to another location not on an edge, then all directions
# should be visible / traversable
wilderness.enter_wilderness(self.char1, coordinates=(1, 1))
exits = [
i
for i in self.char1.location.contents
if i.destination and (i.access(self.char1, "view") or i.access(self.char1, "traverse"))
]
self.assertEqual(len(exits), 8)
exitsok = [
"north",
"northeast",
"east",
"southeast",
"south",
"southwest",
"west",
"northwest",
]
for each_exit in exitsok:
self.assertTrue(any([e for e in exits if e.key == each_exit]))
def test_room_creation(self):
# Pretend that both char1 and char2 are connected...
self.char1.sessions.add(1)
self.char2.sessions.add(1)
self.assertTrue(self.char1.has_account)
self.assertTrue(self.char2.has_account)
wilderness.create_wilderness()
w = self.get_wilderness_script()
# We should have no unused room after moving the first account in.
self.assertEqual(len(w.db.unused_rooms), 0)
w.move_obj(self.char1, (0, 0))
self.assertEqual(len(w.db.unused_rooms), 0)
# And also no unused room after moving the second one in.
w.move_obj(self.char2, (1, 1))
self.assertEqual(len(w.db.unused_rooms), 0)
# But if char2 moves into char1's room, we should have one unused room
# Which should be char2's old room that got created.
w.move_obj(self.char2, (0, 0))
self.assertEqual(len(w.db.unused_rooms), 1)
self.assertEqual(self.char1.location, self.char2.location)
# And if char2 moves back out, that unused room should be put back to
# use again.
w.move_obj(self.char2, (1, 1))
self.assertNotEqual(self.char1.location, self.char2.location)
self.assertEqual(len(w.db.unused_rooms), 0)
def test_get_new_coordinates(self):
loc = (1, 1)
directions = {
"north": (1, 2),
"northeast": (2, 2),
"east": (2, 1),
"southeast": (2, 0),
"south": (1, 0),
"southwest": (0, 0),
"west": (0, 1),
"northwest": (0, 2),
}
for (direction, correct_loc) in directions.items(): # Not compatible with Python 3
new_loc = wilderness.get_new_coordinates(loc, direction)
self.assertEqual(new_loc, correct_loc, direction)