diff --git a/evennia/prototypes/protfuncs.py b/evennia/prototypes/protfuncs.py index a13aa7e532..e222876221 100644 --- a/evennia/prototypes/protfuncs.py +++ b/evennia/prototypes/protfuncs.py @@ -37,12 +37,15 @@ prototype key (this value must be possible to serialize in an Attribute). from ast import literal_eval from random import randint as base_randint, random as base_random, choice as base_choice +import re from evennia.utils import search from evennia.utils.utils import justify as base_justify, is_iter, to_str _PROTLIB = None +_RE_DBREF = re.compile(r"\#[0-9]+") + # default protfuncs @@ -325,3 +328,14 @@ def objlist(*args, **kwargs): """ return ["#{}".format(obj.id) for obj in _obj_search(return_list=True, *args, **kwargs)] + + +def dbref(*args, **kwargs): + """ + Usage $dbref(<#dbref>) + Returns one Object searched globally by #dbref. Error if #dbref is invalid. + """ + if not args or len(args) < 1 or _RE_DBREF.match(args[0]) is None: + raise ValueError('$dbref requires a valid #dbref argument.') + + return obj(args[0]) diff --git a/evennia/prototypes/prototypes.py b/evennia/prototypes/prototypes.py index 833316cf59..a3281777e0 100644 --- a/evennia/prototypes/prototypes.py +++ b/evennia/prototypes/prototypes.py @@ -5,7 +5,6 @@ Handling storage of prototypes, both database-based ones (DBPrototypes) and thos """ -import re import hashlib import time from ast import literal_eval @@ -33,8 +32,6 @@ _PROTOTYPE_TAG_CATEGORY = "from_prototype" _PROTOTYPE_TAG_META_CATEGORY = "db_prototype" PROT_FUNCS = {} -_RE_DBREF = re.compile(r"(?') + mocked__obj_search.assert_not_called() + + with mock.patch("evennia.prototypes.protfuncs._obj_search", wraps=protofuncs._obj_search) as mocked__obj_search: + self.assertRaises(ValueError, protlib.protfunc_parser, "$dbref(Char)") + mocked__obj_search.assert_not_called() + self.assertEqual(protlib.value_to_obj( protlib.protfunc_parser("#6", session=self.session)), self.char1)