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,177 @@
"""
Building menu tests.
"""
from evennia.commands.default.tests import CommandTest
from . building_menu import BuildingMenu, CmdNoMatch
class Submenu(BuildingMenu):
def init(self, exit):
self.add_choice("title", key="t", attr="key")
class TestBuildingMenu(CommandTest):
def setUp(self):
super(TestBuildingMenu, self).setUp()
self.menu = BuildingMenu(caller=self.char1, obj=self.room1, title="test")
self.menu.add_choice("title", key="t", attr="key")
def test_quit(self):
"""Try to quit the building menu."""
self.assertFalse(self.char1.cmdset.has("building_menu"))
self.menu.open()
self.assertTrue(self.char1.cmdset.has("building_menu"))
self.call(CmdNoMatch(building_menu=self.menu), "q")
# char1 tries to quit the editor
self.assertFalse(self.char1.cmdset.has("building_menu"))
def test_setattr(self):
"""Test the simple setattr provided by building menus."""
self.menu.open()
self.call(CmdNoMatch(building_menu=self.menu), "t")
self.assertIsNotNone(self.menu.current_choice)
self.call(CmdNoMatch(building_menu=self.menu), "some new title")
self.call(CmdNoMatch(building_menu=self.menu), "@")
self.assertIsNone(self.menu.current_choice)
self.assertEqual(self.room1.key, "some new title")
self.call(CmdNoMatch(building_menu=self.menu), "q")
def test_add_choice_without_key(self):
"""Try to add choices without keys."""
choices = []
for i in range(20):
choices.append(self.menu.add_choice("choice", attr="test"))
self.menu._add_keys_choice()
keys = [
"c",
"h",
"o",
"i",
"e",
"ch",
"ho",
"oi",
"ic",
"ce",
"cho",
"hoi",
"oic",
"ice",
"choi",
"hoic",
"oice",
"choic",
"hoice",
"choice",
]
for i in range(20):
self.assertEqual(choices[i].key, keys[i])
# Adding another key of the same title would break, no more available shortcut
self.menu.add_choice("choice", attr="test")
with self.assertRaises(ValueError):
self.menu._add_keys_choice()
def test_callbacks(self):
"""Test callbacks in menus."""
self.room1.key = "room1"
def on_enter(caller, menu):
caller.msg("on_enter:{}".format(menu.title))
def on_nomatch(caller, string, choice):
caller.msg("on_nomatch:{},{}".format(string, choice.key))
def on_leave(caller, obj):
caller.msg("on_leave:{}".format(obj.key))
self.menu.add_choice(
"test", key="e", on_enter=on_enter, on_nomatch=on_nomatch, on_leave=on_leave
)
self.call(CmdNoMatch(building_menu=self.menu), "e", "on_enter:test")
self.call(CmdNoMatch(building_menu=self.menu), "ok", "on_nomatch:ok,e")
self.call(CmdNoMatch(building_menu=self.menu), "@", "on_leave:room1")
self.call(CmdNoMatch(building_menu=self.menu), "q")
def test_multi_level(self):
"""Test multi-level choices."""
# Creaste three succeeding menu (t2 is contained in t1, t3 is contained in t2)
def on_nomatch_t1(caller, menu):
menu.move("whatever") # this will be valid since after t1 is a joker
def on_nomatch_t2(caller, menu):
menu.move("t3") # this time the key matters
t1 = self.menu.add_choice("what", key="t1", on_nomatch=on_nomatch_t1)
t2 = self.menu.add_choice("and", key="t1.*", on_nomatch=on_nomatch_t2)
t3 = self.menu.add_choice("why", key="t1.*.t3")
self.menu.open()
# Move into t1
self.assertIn(t1, self.menu.relevant_choices)
self.assertNotIn(t2, self.menu.relevant_choices)
self.assertNotIn(t3, self.menu.relevant_choices)
self.assertIsNone(self.menu.current_choice)
self.call(CmdNoMatch(building_menu=self.menu), "t1")
self.assertEqual(self.menu.current_choice, t1)
self.assertNotIn(t1, self.menu.relevant_choices)
self.assertIn(t2, self.menu.relevant_choices)
self.assertNotIn(t3, self.menu.relevant_choices)
# Move into t2
self.call(CmdNoMatch(building_menu=self.menu), "t2")
self.assertEqual(self.menu.current_choice, t2)
self.assertNotIn(t1, self.menu.relevant_choices)
self.assertNotIn(t2, self.menu.relevant_choices)
self.assertIn(t3, self.menu.relevant_choices)
# Move into t3
self.call(CmdNoMatch(building_menu=self.menu), "t3")
self.assertEqual(self.menu.current_choice, t3)
self.assertNotIn(t1, self.menu.relevant_choices)
self.assertNotIn(t2, self.menu.relevant_choices)
self.assertNotIn(t3, self.menu.relevant_choices)
# Move back to t2
self.call(CmdNoMatch(building_menu=self.menu), "@")
self.assertEqual(self.menu.current_choice, t2)
self.assertNotIn(t1, self.menu.relevant_choices)
self.assertNotIn(t2, self.menu.relevant_choices)
self.assertIn(t3, self.menu.relevant_choices)
# Move back into t1
self.call(CmdNoMatch(building_menu=self.menu), "@")
self.assertEqual(self.menu.current_choice, t1)
self.assertNotIn(t1, self.menu.relevant_choices)
self.assertIn(t2, self.menu.relevant_choices)
self.assertNotIn(t3, self.menu.relevant_choices)
# Moves back to the main menu
self.call(CmdNoMatch(building_menu=self.menu), "@")
self.assertIn(t1, self.menu.relevant_choices)
self.assertNotIn(t2, self.menu.relevant_choices)
self.assertNotIn(t3, self.menu.relevant_choices)
self.assertIsNone(self.menu.current_choice)
self.call(CmdNoMatch(building_menu=self.menu), "q")
def test_submenu(self):
"""Test to add sub-menus."""
def open_exit(menu):
menu.open_submenu("evennia.contrib.tests.Submenu", self.exit)
return False
self.menu.add_choice("exit", key="x", on_enter=open_exit)
self.menu.open()
self.call(CmdNoMatch(building_menu=self.menu), "x")
self.menu = self.char1.ndb._building_menu
self.call(CmdNoMatch(building_menu=self.menu), "t")
self.call(CmdNoMatch(building_menu=self.menu), "in")
self.call(CmdNoMatch(building_menu=self.menu), "@")
self.call(CmdNoMatch(building_menu=self.menu), "@")
self.menu = self.char1.ndb._building_menu
self.assertEqual(self.char1.ndb._building_menu.obj, self.room1)
self.call(CmdNoMatch(building_menu=self.menu), "q")
self.assertEqual(self.exit.key, "in")

View file

@ -0,0 +1,66 @@
"""
Test Color markup.
"""
import re
from evennia.utils.test_resources import EvenniaTest
from . import color_markups
class TestColorMarkup(EvenniaTest):
"""
Note: Normally this would be tested by importing the ansi parser and run
the mappings through it. This is not possible since the ansi module creates
its mapping at the module/class level; since the ansi module is used by so
many other modules it appears that trying to overload
settings to test it causes issues with unrelated tests.
"""
def test_curly_markup(self):
ansi_map = color_markups.CURLY_COLOR_ANSI_EXTRA_MAP
self.assertIsNotNone(re.match(re.escape(ansi_map[7][0]), "{r"))
self.assertIsNotNone(re.match(re.escape(ansi_map[-1][0]), "{[X"))
xterm_fg = color_markups.CURLY_COLOR_XTERM256_EXTRA_FG
self.assertIsNotNone(re.match(xterm_fg[0], "{001"))
self.assertIsNotNone(re.match(xterm_fg[0], "{123"))
self.assertIsNotNone(re.match(xterm_fg[0], "{455"))
xterm_bg = color_markups.CURLY_COLOR_XTERM256_EXTRA_BG
self.assertIsNotNone(re.match(xterm_bg[0], "{[001"))
self.assertIsNotNone(re.match(xterm_bg[0], "{[123"))
self.assertIsNotNone(re.match(xterm_bg[0], "{[455"))
xterm_gfg = color_markups.CURLY_COLOR_XTERM256_EXTRA_GFG
self.assertIsNotNone(re.match(xterm_gfg[0], "{=h"))
self.assertIsNotNone(re.match(xterm_gfg[0], "{=e"))
self.assertIsNotNone(re.match(xterm_gfg[0], "{=w"))
xterm_gbg = color_markups.CURLY_COLOR_XTERM256_EXTRA_GBG
self.assertIsNotNone(re.match(xterm_gbg[0], "{[=a"))
self.assertIsNotNone(re.match(xterm_gbg[0], "{[=k"))
self.assertIsNotNone(re.match(xterm_gbg[0], "{[=z"))
bright_map = color_markups.CURLY_COLOR_ANSI_XTERM256_BRIGHT_BG_EXTRA_MAP
self.assertEqual(bright_map[0][1], "{[500")
self.assertEqual(bright_map[-1][1], "{[222")
def test_mux_markup(self):
ansi_map = color_markups.MUX_COLOR_ANSI_EXTRA_MAP
self.assertIsNotNone(re.match(re.escape(ansi_map[10][0]), "%cr"))
self.assertIsNotNone(re.match(re.escape(ansi_map[-1][0]), "%cX"))
xterm_fg = color_markups.MUX_COLOR_XTERM256_EXTRA_FG
self.assertIsNotNone(re.match(xterm_fg[0], "%c001"))
self.assertIsNotNone(re.match(xterm_fg[0], "%c123"))
self.assertIsNotNone(re.match(xterm_fg[0], "%c455"))
xterm_bg = color_markups.MUX_COLOR_XTERM256_EXTRA_BG
self.assertIsNotNone(re.match(xterm_bg[0], "%c[001"))
self.assertIsNotNone(re.match(xterm_bg[0], "%c[123"))
self.assertIsNotNone(re.match(xterm_bg[0], "%c[455"))
xterm_gfg = color_markups.MUX_COLOR_XTERM256_EXTRA_GFG
self.assertIsNotNone(re.match(xterm_gfg[0], "%c=h"))
self.assertIsNotNone(re.match(xterm_gfg[0], "%c=e"))
self.assertIsNotNone(re.match(xterm_gfg[0], "%c=w"))
xterm_gbg = color_markups.MUX_COLOR_XTERM256_EXTRA_GBG
self.assertIsNotNone(re.match(xterm_gbg[0], "%c[=a"))
self.assertIsNotNone(re.match(xterm_gbg[0], "%c[=k"))
self.assertIsNotNone(re.match(xterm_gbg[0], "%c[=z"))
bright_map = color_markups.MUX_COLOR_ANSI_XTERM256_BRIGHT_BG_EXTRA_MAP
self.assertEqual(bright_map[0][1], "%c[500")
self.assertEqual(bright_map[-1][1], "%c[222")

View file

@ -0,0 +1,54 @@
"""
Testing custom game time
"""
# Testing custom_gametime
from mock import Mock, patch
from evennia.utils.test_resources import EvenniaTest
from . import custom_gametime
def _testcallback():
pass
@patch("evennia.utils.gametime.gametime", new=Mock(return_value=2975000898.46))
class TestCustomGameTime(EvenniaTest):
def tearDown(self):
if hasattr(self, "timescript"):
self.timescript.stop()
def test_time_to_tuple(self):
self.assertEqual(custom_gametime.time_to_tuple(10000, 34, 2, 4, 6, 1), (294, 2, 0, 0, 0, 0))
self.assertEqual(custom_gametime.time_to_tuple(10000, 3, 3, 4), (3333, 0, 0, 1))
self.assertEqual(custom_gametime.time_to_tuple(100000, 239, 24, 3), (418, 4, 0, 2))
def test_gametime_to_realtime(self):
self.assertEqual(custom_gametime.gametime_to_realtime(days=2, mins=4), 86520.0)
self.assertEqual(
custom_gametime.gametime_to_realtime(format=True, days=2), (0, 0, 0, 1, 0, 0, 0)
)
def test_realtime_to_gametime(self):
self.assertEqual(custom_gametime.realtime_to_gametime(days=3, mins=34), 349680.0)
self.assertEqual(
custom_gametime.realtime_to_gametime(days=3, mins=34, format=True),
(0, 0, 0, 4, 1, 8, 0),
)
self.assertEqual(
custom_gametime.realtime_to_gametime(format=True, days=3, mins=4), (0, 0, 0, 4, 0, 8, 0)
)
def test_custom_gametime(self):
self.assertEqual(custom_gametime.custom_gametime(), (102, 5, 2, 6, 21, 8, 18))
self.assertEqual(custom_gametime.custom_gametime(absolute=True), (102, 5, 2, 6, 21, 8, 18))
def test_real_seconds_until(self):
self.assertEqual(
custom_gametime.real_seconds_until(year=2300, month=12, day=7), 31911667199.77
)
def test_schedule(self):
self.timescript = custom_gametime.schedule(_testcallback, repeat=True, min=5, sec=0)
self.assertEqual(self.timescript.interval, 1700.7699999809265)

View file

@ -0,0 +1,36 @@
"""
Test email login.
"""
from evennia.commands.default.tests import CommandTest
from . import email_login
class TestEmailLogin(CommandTest):
def test_connect(self):
self.call(
email_login.CmdUnconnectedConnect(),
"mytest@test.com test",
"The email 'mytest@test.com' does not match any accounts.",
)
self.call(
email_login.CmdUnconnectedCreate(),
'"mytest" mytest@test.com test11111',
"A new account 'mytest' was created. Welcome!",
)
self.call(
email_login.CmdUnconnectedConnect(),
"mytest@test.com test11111",
"",
caller=self.account.sessions.get()[0],
)
def test_quit(self):
self.call(email_login.CmdUnconnectedQuit(), "", "", caller=self.account.sessions.get()[0])
def test_unconnectedlook(self):
self.call(email_login.CmdUnconnectedLook(), "", "==========")
def test_unconnectedhelp(self):
self.call(email_login.CmdUnconnectedHelp(), "", "You are not yet logged into the game.")

View file

@ -12,8 +12,8 @@ from evennia.objects.objects import ExitCommand
from evennia.utils import ansi, utils
from evennia.utils.create import create_object, create_script
from evennia.utils.test_resources import EvenniaTest
from evennia.contrib.base_systems.ingame_python.commands import CmdCallback
from evennia.contrib.base_systems.ingame_python.callbackhandler import CallbackHandler
from .commands import CmdCallback
from .callbackhandler import CallbackHandler
# Force settings
settings.EVENTS_CALENDAR = "standard"

View file

@ -0,0 +1,12 @@
"""
Test menu_login
"""
from evennia.commands.default.tests import CommandTest
from . import menu_login
class TestMenuLogin(CommandTest):
def test_cmdunloggedlook(self):
self.call(menu_login.CmdUnloggedinLook(), "", "======")

View file

@ -0,0 +1,86 @@
"""
Legacy Mux comms tests (extracted from 0.9.5)
"""
from evennia.commands.default.tests import CommandTest
from . import mux_comms_cmds as comms
class TestLegacyMuxComms(CommandTest):
"""
Test the legacy comms contrib.
"""
def setUp(self):
super(CommandTest, self).setUp()
self.call(
comms.CmdChannelCreate(),
"testchan;test=Test Channel",
"Created channel testchan and connected to it.",
receiver=self.account,
)
def test_toggle_com(self):
self.call(
comms.CmdAddCom(),
"tc = testchan",
"You are already connected to channel testchan.| You can now",
receiver=self.account,
)
self.call(
comms.CmdDelCom(),
"tc",
"Any alias 'tc' for channel testchan was cleared.",
receiver=self.account,
)
def test_all_com(self):
self.call(
comms.CmdAllCom(),
"",
"Available channels:",
receiver=self.account,
)
def test_clock(self):
self.call(
comms.CmdClock(),
"testchan=send:all()",
"Lock(s) applied. Current locks on testchan:",
receiver=self.account,
)
def test_cdesc(self):
self.call(
comms.CmdCdesc(),
"testchan = Test Channel",
"Description of channel 'testchan' set to 'Test Channel'.",
receiver=self.account,
)
def test_cwho(self):
self.call(
comms.CmdCWho(),
"testchan",
"Channel subscriptions\ntestchan:\n TestAccount",
receiver=self.account,
)
def test_cboot(self):
# No one else connected to boot
self.call(
comms.CmdCBoot(),
"",
"Usage: cboot[/quiet] <channel> = <account> [:reason]",
receiver=self.account,
)
def test_cdestroy(self):
self.call(
comms.CmdCdestroy(),
"testchan",
"[testchan] TestAccount: testchan is being destroyed. Make sure to change your aliases."
"|Channel 'testchan' was destroyed.",
receiver=self.account,
)

View file

@ -0,0 +1,50 @@
"""
Test of the Unixcommand.
"""
from evennia.commands.default.tests import CommandTest
from .unixcommand import UnixCommand
class CmdDummy(UnixCommand):
"""A dummy UnixCommand."""
key = "dummy"
def init_parser(self):
"""Fill out options."""
self.parser.add_argument("nb1", type=int, help="the first number")
self.parser.add_argument("nb2", type=int, help="the second number")
self.parser.add_argument("-v", "--verbose", action="store_true")
def func(self):
nb1 = self.opts.nb1
nb2 = self.opts.nb2
result = nb1 * nb2
verbose = self.opts.verbose
if verbose:
self.msg("{} times {} is {}".format(nb1, nb2, result))
else:
self.msg("{} * {} = {}".format(nb1, nb2, result))
class TestUnixCommand(CommandTest):
def test_success(self):
"""See the command parsing succeed."""
self.call(CmdDummy(), "5 10", "5 * 10 = 50")
self.call(CmdDummy(), "5 10 -v", "5 times 10 is 50")
def test_failure(self):
"""If not provided with the right info, should fail."""
ret = self.call(CmdDummy(), "5")
lines = ret.splitlines()
self.assertTrue(any(lin.startswith("usage:") for lin in lines))
self.assertTrue(any(lin.startswith("dummy: error:") for lin in lines))
# If we specify an incorrect number as parameter
ret = self.call(CmdDummy(), "five ten")
lines = ret.splitlines()
self.assertTrue(any(lin.startswith("usage:") for lin in lines))
self.assertTrue(any(lin.startswith("dummy: error:") for lin in lines))