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
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
def funcname (*args, **kwargs)
@ -42,17 +42,16 @@ from django.conf import settings
from evennia.utils import inlinefuncs
from evennia.utils.utils import callables_from_module
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.PROTOTYPEFUNC_MODULES:
for mod in settings.PROT_FUNC_MODULES:
try:
callables = callables_from_module(mod)
if mod == __name__:
callables.pop("protfunc_parser")
_PROTOTYPEFUNCS.update(callables)
callables.pop("protfunc_parser", None)
_PROT_FUNCS.update(callables)
except ImportError:
pass
@ -62,7 +61,7 @@ def protfunc_parser(value, available_functions=None, **kwargs):
Parse a prototype value string for a protfunc and process it.
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:
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):
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 = value_to_obj_or_any(result)
result = _PROTLIB.value_to_obj_or_any(result)
try:
return literal_eval(result)
except ValueError:
return result
# default protfuncs
def random(*args, **kwargs):

View file

@ -6,8 +6,9 @@ Unit tests for the prototypes and spawner
from random import randint
import mock
from anything import Anything, Something
from django.test.utils import override_settings
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
@ -40,10 +41,6 @@ _PROTPARENTS = {
}
class TestPrototypes(EvenniaTest):
pass
class TestSpawner(EvenniaTest):
def setUp(self):
@ -169,6 +166,23 @@ class TestProtLib(EvenniaTest):
def test_check_permission(self):
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):
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"]
# Modules that contain prototypes for use with the spawner mechanism.
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
# dummyrunner for more information)
DUMMYRUNNER_SETTINGS_MODULE = "evennia.server.profiling.dummyrunner_settings"