Start protfunc tests (unworking)

This commit is contained in:
Griatch 2018-06-12 20:10:20 +02:00
parent e26ea4f386
commit 8d7a7490a9
3 changed files with 35 additions and 16 deletions

View file

@ -15,7 +15,7 @@ In the prototype dict, the protfunc is specified as a string inside the prototyp
and multiple functions can be nested (no keyword args are supported). The result will be used as the and multiple functions can be nested (no keyword args are supported). The result will be used as the
value for that prototype key for that individual spawn. value for that prototype key for that individual spawn.
Available protfuncs are callables in one of the modules of `settings.PROTOTYPEFUNC_MODULES`. They Available protfuncs are callables in one of the modules of `settings.PROT_FUNC_MODULES`. They
are specified as functions are specified as functions
def funcname (*args, **kwargs) def funcname (*args, **kwargs)
@ -42,17 +42,16 @@ from django.conf import settings
from evennia.utils import inlinefuncs from evennia.utils import inlinefuncs
from evennia.utils.utils import callables_from_module from evennia.utils.utils import callables_from_module
from evennia.utils.utils import justify as base_justify, is_iter from evennia.utils.utils import justify as base_justify, is_iter
from evennia.prototypes.prototypes import value_to_obj_or_any
_PROTLIB = None
_PROT_FUNCS = {}
_PROTOTYPEFUNCS = {} for mod in settings.PROT_FUNC_MODULES:
for mod in settings.PROTOTYPEFUNC_MODULES:
try: try:
callables = callables_from_module(mod) callables = callables_from_module(mod)
if mod == __name__: if mod == __name__:
callables.pop("protfunc_parser") callables.pop("protfunc_parser", None)
_PROTOTYPEFUNCS.update(callables) _PROT_FUNCS.update(callables)
except ImportError: except ImportError:
pass pass
@ -62,7 +61,7 @@ def protfunc_parser(value, available_functions=None, **kwargs):
Parse a prototype value string for a protfunc and process it. Parse a prototype value string for a protfunc and process it.
Available protfuncs are specified as callables in one of the modules of Available protfuncs are specified as callables in one of the modules of
`settings.PROTOTYPEFUNC_MODULES`, or specified on the command line. `settings.PROTFUNC_MODULES`, or specified on the command line.
Args: Args:
value (any): The value to test for a parseable protfunc. Only strings will be parsed for value (any): The value to test for a parseable protfunc. Only strings will be parsed for
@ -81,18 +80,21 @@ def protfunc_parser(value, available_functions=None, **kwargs):
""" """
global _PROTLIB
if not _PROTLIB:
from evennia.prototypes import prototypes as _PROTLIB
if not isinstance(value, basestring): if not isinstance(value, basestring):
return value return value
available_functions = _PROTOTYPEFUNCS if available_functions is None else available_functions available_functions = _PROT_FUNCS if available_functions is None else available_functions
result = inlinefuncs.parse_inlinefunc(value, _available_funcs=available_functions, **kwargs) result = inlinefuncs.parse_inlinefunc(value, _available_funcs=available_functions, **kwargs)
result = value_to_obj_or_any(result) result = _PROTLIB.value_to_obj_or_any(result)
try: try:
return literal_eval(result) return literal_eval(result)
except ValueError: except ValueError:
return result return result
# default protfuncs # default protfuncs
def random(*args, **kwargs): def random(*args, **kwargs):

View file

@ -6,8 +6,9 @@ Unit tests for the prototypes and spawner
from random import randint from random import randint
import mock import mock
from anything import Anything, Something from anything import Anything, Something
from django.test.utils import override_settings
from evennia.utils.test_resources import EvenniaTest from evennia.utils.test_resources import EvenniaTest
from evennia.prototypes import spawner, prototypes as protlib from evennia.prototypes import spawner, prototypes as protlib, protfuncs
from evennia.prototypes.prototypes import _PROTOTYPE_TAG_META_CATEGORY from evennia.prototypes.prototypes import _PROTOTYPE_TAG_META_CATEGORY
@ -40,10 +41,6 @@ _PROTPARENTS = {
} }
class TestPrototypes(EvenniaTest):
pass
class TestSpawner(EvenniaTest): class TestSpawner(EvenniaTest):
def setUp(self): def setUp(self):
@ -169,6 +166,23 @@ class TestProtLib(EvenniaTest):
def test_check_permission(self): def test_check_permission(self):
pass pass
@override_settings(PROT_FUNC_MODULES=['evennia.prototypes.protfuncs'])
class TestProtFuncs(EvenniaTest):
def setUp(self):
super(TestProtFuncs, self).setUp()
self.prot = {"prototype_key": "test_prototype",
"prototype_desc": "testing prot",
"key": "ExampleObj"}
@mock.patch("random.random", new=mock.MagicMock(return_value=0.5))
@mock.patch("random.randint", new=mock.MagicMock(return_value=5))
def test_protfuncs(self):
self.assertEqual(protfuncs.protfunc_parser("$random()", 0.5))
self.assertEqual(protfuncs.protfunc_parser("$randint(1, 10)", 5))
class TestPrototypeStorage(EvenniaTest): class TestPrototypeStorage(EvenniaTest):
def setUp(self): def setUp(self):

View file

@ -354,6 +354,9 @@ LOCK_FUNC_MODULES = ("evennia.locks.lockfuncs", "server.conf.lockfuncs",)
INPUT_FUNC_MODULES = ["evennia.server.inputfuncs", "server.conf.inputfuncs"] INPUT_FUNC_MODULES = ["evennia.server.inputfuncs", "server.conf.inputfuncs"]
# Modules that contain prototypes for use with the spawner mechanism. # Modules that contain prototypes for use with the spawner mechanism.
PROTOTYPE_MODULES = ["world.prototypes"] PROTOTYPE_MODULES = ["world.prototypes"]
# Modules containining Prototype functions able to be embedded in prototype
# definitions from in-game.
PROT_FUNC_MODULES = ["evennia.prototypes.protfuncs"]
# Module holding settings/actions for the dummyrunner program (see the # Module holding settings/actions for the dummyrunner program (see the
# dummyrunner for more information) # dummyrunner for more information)
DUMMYRUNNER_SETTINGS_MODULE = "evennia.server.profiling.dummyrunner_settings" DUMMYRUNNER_SETTINGS_MODULE = "evennia.server.profiling.dummyrunner_settings"