From 057bed7010d7598cdfab512e5d173a78745b8de9 Mon Sep 17 00:00:00 2001 From: Griatch Date: Thu, 12 Nov 2020 20:46:53 +0100 Subject: [PATCH] Fix an inconsistency in category-refresh for empty string, related to #2236 --- evennia/typeclasses/attributes.py | 9 +++-- evennia/utils/create.py | 7 ++-- evennia/utils/tests/test_create_functions.py | 39 ++++++++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/evennia/typeclasses/attributes.py b/evennia/typeclasses/attributes.py index 99a897f275..26bbeff04a 100644 --- a/evennia/typeclasses/attributes.py +++ b/evennia/typeclasses/attributes.py @@ -181,10 +181,10 @@ class Attribute(SharedMemoryModel): # def __str__(self): - return smart_str("%s(%s)" % (self.db_key, self.id)) + return smart_str("%s[category=%s](#%s)" % (self.db_key, self.db_category, self.id)) def __repr__(self): - return "%s(%s)" % (self.db_key, self.id) + return "%s[category=%s](#%s)" % (self.db_key, self.db_category, self.id) def access(self, accessing_obj, access_type="attrread", default=False, **kwargs): """ @@ -257,7 +257,7 @@ class AttributeHandler(object): "%s-%s" % ( to_str(attr.db_key).lower(), - attr.db_category.lower() if attr.db_category else None, + attr.db_category.lower() if attr.db_category is not None else None, ), attr, ) @@ -289,7 +289,7 @@ class AttributeHandler(object): """ key = key.strip().lower() if key else None - category = category.strip().lower() if category else None + category = category.strip().lower() if category is not None else None if key: cachekey = "%s-%s" % (key, category) cachefound = False @@ -558,6 +558,7 @@ class AttributeHandler(object): return category = category.strip().lower() if category is not None else None + keystr = key.strip().lower() attr_obj = self._getcache(key, category) diff --git a/evennia/utils/create.py b/evennia/utils/create.py index 9adce45289..e6066d1191 100644 --- a/evennia/utils/create.py +++ b/evennia/utils/create.py @@ -96,7 +96,7 @@ def create_object( location itself or during unittests. attributes (list): Tuples on the form (key, value) or (key, value, category), (key, value, lockstring) or (key, value, lockstring, default_access). - to set as Attributes on the new object. + to set as Attributes on the new object. nattributes (list): Non-persistent tuples on the form (key, value). Note that adding this rarely makes sense since this data will not survive a reload. @@ -229,8 +229,9 @@ def create_script( report_to (Object): The object to return error messages to. desc (str): Optional description of script tags (list): List of tags or tuples (tag, category). - attributes (list): List if tuples (key, value) or (key, value, category) - (key, value, lockstring) or (key, value, lockstring, default_access). + attributes (list): List of tuples `(key, value)`, `(key, value, category)`, + `(key, value, category, lockstring)` or + `(key, value, category, lockstring, default_access)`. Returns: script (obj): An instance of the script created diff --git a/evennia/utils/tests/test_create_functions.py b/evennia/utils/tests/test_create_functions.py index c9ae5b2cb7..10f9892219 100644 --- a/evennia/utils/tests/test_create_functions.py +++ b/evennia/utils/tests/test_create_functions.py @@ -78,6 +78,45 @@ class TestCreateScript(EvenniaTest): assert script.key == "test_script" script.stop() + def test_attr_creation_func(self): + """ + Test of assigning attributes during creation + + """ + attrvalue = {'test1': 1, 'test2': 'boo'} + + # creation-function direct call + script = create.create_script( + key='script_broken', + attributes=[ + ('testname', attrvalue, '') + ] + ) + self.assertTrue(script) + self.assertEqual(script.db.testname, None) # since the category is '' and not None + self.assertEqual(script.attributes.get("testname", category=''), attrvalue) + script.stop() + + def test_attr_method_creation_malformed(self): + """ + Adding the wrong type for one attribute-tuple element + + """ + attrvalue = {'test1': 1, 'test2': 'boo'} + + # method-based creation + script, err = DefaultScript.create( + 'scripttest2', + attributes=[ + # test of wrong syntax - last element should be bool + ('testname', attrvalue, None, '', '') + ] + ) + self.assertFalse(err) + self.assertTrue(script) + self.assertEqual(script.db.testname, attrvalue) + script.stop() + class TestCreateHelpEntry(TestCase):