Source code for evennia.contrib.base_systems.building_menu.tests

Building menu tests.


from evennia.commands.default.tests import BaseEvenniaCommandTest

from .building_menu import BuildingMenu, CmdNoMatch

[docs]class TestBuildingMenu(BaseEvenniaCommandTest):
[docs] def setUp(self): super().setUp() = BuildingMenu(caller=self.char1, obj=self.room1, title="test")"title", key="t", attr="key")
[docs] def test_quit(self): """Try to quit the building menu.""" self.assertFalse(self.char1.cmdset.has("building_menu")) self.assertTrue(self.char1.cmdset.has("building_menu")), "q") # char1 tries to quit the editor self.assertFalse(self.char1.cmdset.has("building_menu"))
[docs] def test_setattr(self): """Test the simple setattr provided by building menus.""", "t") self.assertIsNotNone(, "some new title"), "@") self.assertIsNone( self.assertEqual(self.room1.key, "some new title"), "q")
[docs] def test_add_choice_without_key(self): """Try to add choices without keys.""" choices = [] for i in range(20): choices.append("choice", attr="test")) 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"choice", attr="test") with self.assertRaises(ValueError):
[docs] 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)) "test", key="e", on_enter=on_enter, on_nomatch=on_nomatch, on_leave=on_leave ), "e", "on_enter:test"), "ok", "on_nomatch:ok,e"), "@", "on_leave:room1"), "q")
[docs] 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 ="what", key="t1", on_nomatch=on_nomatch_t1) t2 ="and", key="t1.*", on_nomatch=on_nomatch_t2) t3 ="why", key="t1.*.t3") # Move into t1 self.assertIn(t1, self.assertNotIn(t2, self.assertNotIn(t3, self.assertIsNone(, "t1") self.assertEqual(, t1) self.assertNotIn(t1, self.assertIn(t2, self.assertNotIn(t3, # Move into t2, "t2") self.assertEqual(, t2) self.assertNotIn(t1, self.assertNotIn(t2, self.assertIn(t3, # Move into t3, "t3") self.assertEqual(, t3) self.assertNotIn(t1, self.assertNotIn(t2, self.assertNotIn(t3, # Move back to t2, "@") self.assertEqual(, t2) self.assertNotIn(t1, self.assertNotIn(t2, self.assertIn(t3, # Move back into t1, "@") self.assertEqual(, t1) self.assertNotIn(t1, self.assertIn(t2, self.assertNotIn(t3, # Moves back to the main menu, "@") self.assertIn(t1, self.assertNotIn(t2, self.assertNotIn(t3, self.assertIsNone(, "q")
[docs] def test_submenu(self): """Test to add sub-menus.""" def open_exit(menu): menu.open_submenu("evennia.contrib.base_systems.building_menu.tests.Submenu", self.exit) return False"exit", key="x", on_enter=open_exit), "x") = self.char1.ndb._building_menu, "t"), "in"), "@"), "@") = self.char1.ndb._building_menu self.assertEqual(self.char1.ndb._building_menu.obj, self.room1), "q") self.assertEqual(self.exit.key, "in")