Add unit tests for mapbuilder, menu_login and multidescer contribs, as per #1105.

This commit is contained in:
Griatch 2017-02-19 20:21:07 +01:00
parent 108dd35ee7
commit 42c13ee870
2 changed files with 134 additions and 69 deletions

View file

@ -287,6 +287,87 @@ def _map_to_list(game_map):
else character for character in list_map]
def build_map(caller, game_map, legend, iterations=1, build_exits=True):
"""
Receives the fetched map and legend vars provided by the player.
Args:
caller (Object): The creator of the map.
game_map (str): An ASCII map string.
legend (dict): Mapping of map symbols to object types.
iterations (int): The number of iteration passes.
build_exits (bool): Create exits between new rooms.
Notes:
The map
is iterated over character by character, comparing it to the trigger
characters in the legend var and executing the build instructions on
finding a match. The map is iterated over according to the `iterations`
value and exits are optionally generated between adjacent rooms according
to the `build_exits` value.
"""
# Split map string to list of rows and create reference list.
caller.msg("Creating Map...")
caller.msg(game_map)
game_map = _map_to_list(game_map)
# Create a reference dictionary which be passed to build functions and
# will store obj returned by build functions so objs can be referenced.
room_dict = {}
caller.msg("Creating Landmass...")
for iteration in xrange(iterations):
for y in xrange(len(game_map)):
for x in xrange(len(game_map[y])):
for key in legend:
if game_map[y][x] in key:
room = legend[key](x, y, iteration=iteration,
room_dict=room_dict,
caller=caller)
if iteration == 0:
room_dict[(x, y)] = room
if build_exits:
# Creating exits. Assumes single room object in dict entry
caller.msg("Connecting Areas...")
for loc_key, location in room_dict.iteritems():
x = loc_key[0]
y = loc_key[1]
# north
if (x, y-1) in room_dict:
if room_dict[(x, y-1)]:
create_object(exits.Exit, key="north",
aliases=["n"], location=location,
destination=room_dict[(x, y-1)])
# east
if (x+1, y) in room_dict:
if room_dict[(x+1, y)]:
create_object(exits.Exit, key="east",
aliases=["e"], location=location,
destination=room_dict[(x+1, y)])
# south
if (x, y+1) in room_dict:
if room_dict[(x, y+1)]:
create_object(exits.Exit, key="south",
aliases=["s"], location=location,
destination=room_dict[(x, y+1)])
# west
if (x-1, y) in room_dict:
if room_dict[(x-1, y)]:
create_object(exits.Exit, key="west",
aliases=["w"], location=location,
destination=room_dict[(x-1, y)])
caller.msg("Map Created.")
# access command
class CmdMapBuilder(COMMAND_DEFAULT_CLASS):
"""
Build a map from a 2D ASCII map.
@ -396,72 +477,3 @@ class CmdMapBuilder(COMMAND_DEFAULT_CLASS):
# Pass map and legend to the build function.
build_map(caller, game_map, legend, iterations, build_exits)
def build_map(caller, game_map, legend, iterations=1, build_exits=True):
"""
Receives the fetched map and legend vars provided by the player. The map
is iterated over character by character, comparing it to the trigger
characters in the legend var and executing the build instructions on
finding a match. The map is iterated over according to the `iterations`
value and exits are optionally generated between adjacent rooms according
to the `build_exits` value.
"""
# Split map string to list of rows and create reference list.
caller.msg("Creating Map...")
caller.msg(game_map)
game_map = _map_to_list(game_map)
# Create a reference dictionary which be passed to build functions and
# will store obj returned by build functions so objs can be referenced.
room_dict = {}
caller.msg("Creating Landmass...")
for iteration in xrange(iterations):
for y in xrange(len(game_map)):
for x in xrange(len(game_map[y])):
for key in legend:
if game_map[y][x] in key:
room = legend[key](x, y, iteration=iteration,
room_dict=room_dict,
caller=caller)
if iteration == 0:
room_dict[(x, y)] = room
if build_exits:
# Creating exits. Assumes single room object in dict entry
caller.msg("Connecting Areas...")
for loc_key, location in room_dict.iteritems():
x = loc_key[0]
y = loc_key[1]
# north
if (x, y-1) in room_dict:
if room_dict[(x, y-1)]:
create_object(exits.Exit, key="north",
aliases=["n"], location=location,
destination=room_dict[(x, y-1)])
# east
if (x+1, y) in room_dict:
if room_dict[(x+1, y)]:
create_object(exits.Exit, key="east",
aliases=["e"], location=location,
destination=room_dict[(x+1, y)])
# south
if (x, y+1) in room_dict:
if room_dict[(x, y+1)]:
create_object(exits.Exit, key="south",
aliases=["s"], location=location,
destination=room_dict[(x, y+1)])
# west
if (x-1, y) in room_dict:
if room_dict[(x-1, y)]:
create_object(exits.Exit, key="west",
aliases=["w"], location=location,
destination=room_dict[(x-1, y)])
caller.msg("Map Created.")

View file

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
Testing suite for contrib folder
@ -538,3 +539,55 @@ class TestMail(CommandTest):
self.call(mail.CmdMail(), "/forward TestPlayer2 = 1/Forward message", "You sent your message.|Message forwarded.", caller=self.player)
self.call(mail.CmdMail(), "/reply 2=Reply Message2", "You sent your message.", caller=self.player)
self.call(mail.CmdMail(), "/delete 2", "Message 2 deleted", caller=self.player)
# test map builder contrib
from evennia.contrib 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...|""")
# test menu_login
from evennia.contrib import menu_login
class TestMenuLogin(CommandTest):
def test_cmdunloggedlook(self):
self.call(menu_login.CmdUnloggedinLook(), "", "======")
# test multidescer contrib
from evennia.contrib import multidescer
class TestMultidescer(CommandTest):
def test_cmdmultidesc(self):
self.call(multidescer.CmdMultiDesc(),"/list", "Stored descs:\ncaller:")
self.call(multidescer.CmdMultiDesc(),"test = Desc 1", "Stored description 'test': \"Desc 1\"")
self.call(multidescer.CmdMultiDesc(),"test2 = Desc 2", "Stored description 'test2': \"Desc 2\"")
self.call(multidescer.CmdMultiDesc(),"/swap test-test2", "Swapped descs 'test' and 'test2'.")
self.call(multidescer.CmdMultiDesc(),"test3 = Desc 3init", "Stored description 'test3': \"Desc 3init\"")
self.call(multidescer.CmdMultiDesc(),"/list", "Stored descs:\ntest3: Desc 3init\ntest: Desc 1\ntest2: Desc 2\ncaller:")
self.call(multidescer.CmdMultiDesc(),"test3 = Desc 3", "Stored description 'test3': \"Desc 3\"")
self.call(multidescer.CmdMultiDesc(),"/set test1 + test2 + + test3", "test1 Desc 2 Desc 3\n\n"
"The above was set as the current description.")
self.assertEqual(self.char1.db.desc, "test1 Desc 2 Desc 3")