Unittests pass for all protfuncs

This commit is contained in:
Griatch 2018-06-21 22:42:50 +02:00
parent ec52ca1d55
commit e601e03884
4 changed files with 52 additions and 28 deletions

View file

@ -263,21 +263,21 @@ def _obj_search(*args, **kwargs):
query = "".join(args)
session = kwargs.get("session", None)
return_list = kwargs.pop("return_list", False)
account = None
if session:
account = session.account
if not session:
raise ValueError("$obj called by Evennia without Session. This is not supported.")
account = session.account
if not account:
raise ValueError("$obj requires a logged-in account session.")
targets = search.search_object(query)
print("targets: {}".format(targets))
if return_list:
retlist = []
for target in targets:
if target.access(account, target, 'control'):
retlist.append(target)
if account:
for target in targets:
if target.access(account, target, 'control'):
retlist.append(target)
else:
retlist = targets
return retlist
else:
# single-match
@ -288,11 +288,12 @@ def _obj_search(*args, **kwargs):
"query or use $objlist instead.".format(
query=query, nmatches=len(targets)))
target = targets[0]
if not target.access(account, target, 'control'):
raise ValueError("$obj: Obj {target}(#{dbref} cannot be added - "
"Account {account} does not have 'control' access.".format(
target=target.key, dbref=target.id, account=account))
return target
if account:
if not target.access(account, target, 'control'):
raise ValueError("$obj: Obj {target}(#{dbref} cannot be added - "
"Account {account} does not have 'control' access.".format(
target=target.key, dbref=target.id, account=account))
return target
def obj(*args, **kwargs):

View file

@ -27,7 +27,7 @@ _PROTOTYPE_TAG_META_CATEGORY = "db_prototype"
_PROT_FUNCS = {}
_RE_DBREF = re.compile(r"(?<!\$obj\()#[0-9]+")
_RE_DBREF = re.compile(r"(?<!\$obj\()(#[0-9]+)")
class PermissionError(RuntimeError):
@ -52,7 +52,7 @@ for mod in settings.PROT_FUNC_MODULES:
raise
def protfunc_parser(value, available_functions=None, testing=False, **kwargs):
def protfunc_parser(value, available_functions=None, testing=False, stacktrace=False, **kwargs):
"""
Parse a prototype value string for a protfunc and process it.
@ -65,6 +65,7 @@ def protfunc_parser(value, available_functions=None, testing=False, **kwargs):
available_functions (dict, optional): Mapping of name:protfunction to use for this parsing.
testing (bool, optional): Passed to protfunc. If in a testing mode, some protfuncs may
behave differently.
stacktrace (bool, optional): If set, print the stack parsing process of the protfunc-parser.
Kwargs:
session (Session): Passed to protfunc. Session of the entity spawning the prototype.
@ -91,11 +92,9 @@ def protfunc_parser(value, available_functions=None, testing=False, **kwargs):
value = _RE_DBREF.sub("$obj(\\1)", value)
result = inlinefuncs.parse_inlinefunc(
value, available_funcs=available_functions, testing=testing, **kwargs)
value, available_funcs=available_functions,
stacktrace=stacktrace, testing=testing, **kwargs)
# at this point we have a string where all procfuncs were parsed
# print("parse_inlinefuncs(\"{}\", available_funcs={}) => {}".format(value, available_functions, result))
result = value_to_obj_or_any(result)
err = None
try:
result = literal_eval(result)

View file

@ -5,10 +5,10 @@ Unit tests for the prototypes and spawner
from random import randint
import mock
from anything import Anything, Something
from anything import Something
from django.test.utils import override_settings
from evennia.utils.test_resources import EvenniaTest
from evennia.prototypes import spawner, prototypes as protlib, protfuncs
from evennia.prototypes import spawner, prototypes as protlib
from evennia.prototypes.prototypes import _PROTOTYPE_TAG_META_CATEGORY
@ -227,6 +227,18 @@ class TestProtFuncs(EvenniaTest):
"$eval({'test': '1', 2:3, 3: $toint(3.5)})"), {'test': '1', 2: 3, 3: 3})
self.assertEqual(protlib.protfunc_parser("$obj(#1)", session=self.session), '#1')
self.assertEqual(protlib.protfunc_parser("#1", session=self.session), '#1')
self.assertEqual(protlib.protfunc_parser("$obj(Char)", session=self.session), '#6')
self.assertEqual(protlib.protfunc_parser("$obj(Char)", session=self.session), '#6')
self.assertEqual(protlib.protfunc_parser("$objlist(#1)", session=self.session), ['#1'])
self.assertEqual(protlib.value_to_obj(
protlib.protfunc_parser("#6", session=self.session)), self.char1)
self.assertEqual(protlib.value_to_obj_or_any(
protlib.protfunc_parser("#6", session=self.session)), self.char1)
self.assertEqual(protlib.value_to_obj_or_any(
protlib.protfunc_parser("[1,2,3,'#6',5]", session=self.session)),
[1, 2, 3, self.char1, 5])
class TestPrototypeStorage(EvenniaTest):