clean-up and test updates
This commit is contained in:
parent
0b1790ecfe
commit
2e9971dc82
2 changed files with 47 additions and 60 deletions
|
|
@ -19,8 +19,6 @@ from evennia.utils import logger
|
||||||
from evennia.utils.utils import callables_from_module, class_from_module
|
from evennia.utils.utils import callables_from_module, class_from_module
|
||||||
|
|
||||||
SCRIPTDB = None
|
SCRIPTDB = None
|
||||||
_BASE_SCRIPT_TYPECLASS = None
|
|
||||||
|
|
||||||
|
|
||||||
class Container:
|
class Container:
|
||||||
"""
|
"""
|
||||||
|
|
@ -201,10 +199,6 @@ class GlobalScriptContainer(Container):
|
||||||
initialized.
|
initialized.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
global _BASE_SCRIPT_TYPECLASS
|
|
||||||
if not _BASE_SCRIPT_TYPECLASS:
|
|
||||||
_BASE_SCRIPT_TYPECLASS = class_from_module(settings.BASE_SCRIPT_TYPECLASS)
|
|
||||||
|
|
||||||
if self.typeclass_storage is None:
|
if self.typeclass_storage is None:
|
||||||
self.typeclass_storage = {}
|
self.typeclass_storage = {}
|
||||||
for key, data in list(self.loaded_data.items()):
|
for key, data in list(self.loaded_data.items()):
|
||||||
|
|
|
||||||
|
|
@ -4,78 +4,71 @@ from evennia.utils import containers
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.test import override_settings
|
from django.test import override_settings
|
||||||
from evennia.utils.utils import class_from_module
|
from evennia.utils.utils import class_from_module
|
||||||
|
from evennia import DefaultScript
|
||||||
|
|
||||||
_BASE_SCRIPT_TYPECLASS = class_from_module(settings.BASE_SCRIPT_TYPECLASS)
|
_BASE_TYPECLASS = class_from_module(settings.BASE_SCRIPT_TYPECLASS)
|
||||||
|
|
||||||
class GoodScript(_BASE_SCRIPT_TYPECLASS):
|
class GoodScript(DefaultScript):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class BadScript:
|
class BrokenScript(DefaultScript):
|
||||||
"""Not subclass of _BASE_SCRIPT_TYPECLASS,"""
|
"""objects will fail upon call"""
|
||||||
pass
|
@property
|
||||||
|
def objects(self):
|
||||||
class WorseScript(_BASE_SCRIPT_TYPECLASS):
|
from evennia import module_that_doesnt_exist
|
||||||
"""objects will fail upon call"""
|
|
||||||
@property
|
|
||||||
def objects(self):
|
|
||||||
from evennia import module_that_doesnt_exist
|
|
||||||
|
|
||||||
class TestGlobalScriptContainer(unittest.TestCase):
|
class TestGlobalScriptContainer(unittest.TestCase):
|
||||||
|
|
||||||
def test_init_with_no_scripts(self):
|
def test_init_with_no_scripts(self):
|
||||||
gsc = containers.GlobalScriptContainer()
|
gsc = containers.GlobalScriptContainer()
|
||||||
|
|
||||||
self.assertEqual(len(gsc.loaded_data), 0)
|
self.assertEqual(len(gsc.loaded_data), 0)
|
||||||
|
|
||||||
@override_settings(GLOBAL_SCRIPTS={'script_name': {}})
|
@override_settings(GLOBAL_SCRIPTS={})
|
||||||
def test_init_with_typeclassless_script(self):
|
def test_start_with_no_scripts(self):
|
||||||
|
gsc = containers.GlobalScriptContainer()
|
||||||
|
|
||||||
gsc = containers.GlobalScriptContainer()
|
gsc.start()
|
||||||
|
|
||||||
self.assertEqual(len(gsc.loaded_data), 1)
|
self.assertEqual(len(gsc.typeclass_storage), 0)
|
||||||
self.assertIn('script_name', gsc.loaded_data)
|
|
||||||
|
|
||||||
def test_start_with_no_scripts(self):
|
@override_settings(GLOBAL_SCRIPTS={'script_name': {}})
|
||||||
gsc = containers.GlobalScriptContainer()
|
def test_start_with_typeclassless_script(self):
|
||||||
|
"""No specified typeclass should fallback to base"""
|
||||||
|
gsc = containers.GlobalScriptContainer()
|
||||||
|
|
||||||
gsc.start()
|
gsc.start()
|
||||||
|
|
||||||
self.assertEqual(len(gsc.typeclass_storage), 0)
|
self.assertEqual(len(gsc.typeclass_storage), 1)
|
||||||
|
self.assertIn('script_name', gsc.typeclass_storage)
|
||||||
|
self.assertEqual(gsc.typeclass_storage['script_name'], _BASE_TYPECLASS)
|
||||||
|
|
||||||
@override_settings(GLOBAL_SCRIPTS={'script_name': {}})
|
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.test_containers.NoScript'}})
|
||||||
def test_start_with_typeclassless_script_defaults_to_base(self):
|
def test_start_with_nonexistent_script(self):
|
||||||
gsc = containers.GlobalScriptContainer()
|
"""Missing script class should fall back to base"""
|
||||||
|
gsc = containers.GlobalScriptContainer()
|
||||||
|
|
||||||
gsc.start()
|
gsc.start()
|
||||||
|
|
||||||
self.assertEqual(len(gsc.typeclass_storage), 1)
|
self.assertEqual(len(gsc.typeclass_storage), 1)
|
||||||
self.assertIn('script_name', gsc.typeclass_storage)
|
self.assertIn('script_name', gsc.typeclass_storage)
|
||||||
self.assertEqual(gsc.typeclass_storage['script_name'], _BASE_SCRIPT_TYPECLASS)
|
self.assertEqual(gsc.typeclass_storage['script_name'], _BASE_TYPECLASS)
|
||||||
|
|
||||||
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.test_containers.GoodScript'}})
|
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.test_containers.GoodScript'}})
|
||||||
def test_start_with_typeclassed_script_loads_it(self):
|
def test_start_with_valid_script(self):
|
||||||
gsc = containers.GlobalScriptContainer()
|
gsc = containers.GlobalScriptContainer()
|
||||||
|
|
||||||
gsc.start()
|
gsc.start()
|
||||||
|
|
||||||
self.assertEqual(len(gsc.typeclass_storage), 1)
|
self.assertEqual(len(gsc.typeclass_storage), 1)
|
||||||
self.assertIn('script_name', gsc.typeclass_storage)
|
self.assertIn('script_name', gsc.typeclass_storage)
|
||||||
self.assertEqual(gsc.typeclass_storage['script_name'], GoodScript)
|
self.assertEqual(gsc.typeclass_storage['script_name'], GoodScript)
|
||||||
|
|
||||||
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.test_containers.BadScript'}})
|
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.test_containers.BrokenScript'}})
|
||||||
def test_start_with_bad_typeclassed_script_skips_it(self):
|
def test_start_with_broken_script(self):
|
||||||
gsc = containers.GlobalScriptContainer()
|
"""Broken script module should traceback"""
|
||||||
|
gsc = containers.GlobalScriptContainer()
|
||||||
|
|
||||||
gsc.start()
|
with self.assertRaises(Exception) as cm:
|
||||||
|
gsc.start()
|
||||||
self.assertEqual(len(gsc.typeclass_storage), 0)
|
self.fail("An exception was expected but it didn't occur.")
|
||||||
self.assertNotIn('script_name', gsc.typeclass_storage)
|
|
||||||
|
|
||||||
@override_settings(GLOBAL_SCRIPTS={'script_name': {'typeclass': 'evennia.utils.tests.test_containers.WorstScript'}})
|
|
||||||
def test_start_with_worst_typeclassed_script_skips_it(self):
|
|
||||||
gsc = containers.GlobalScriptContainer()
|
|
||||||
|
|
||||||
gsc.start()
|
|
||||||
|
|
||||||
self.assertEqual(len(gsc.typeclass_storage), 0)
|
|
||||||
self.assertNotIn('script_name', gsc.typeclass_storage)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue