Exemple #1
0
 def test_search_prototype(self):
     protlib.save_prototype(self.prot)
     match = protlib.search_prototype("NotFound")
     self.assertFalse(match)
     match = protlib.search_prototype()
     self.assertTrue(match)
     match = protlib.search_prototype(self.prot["prototype_key"])
     self.assertEqual(match, [self.prot])
Exemple #2
0
 def test_spawn_from_str(self):
     protlib.save_prototype(self.prot1)
     obj1 = spawner.spawn(self.prot1['prototype_key'])
     self.assertEqual(
         list(protlib.search_objects_with_prototype("testprototype")), obj1)
     self.assertEqual([
         o.key for o in spawner.spawn(_PROTPARENTS["GOBLIN"],
                                      _PROTPARENTS["GOBLIN_ARCHWIZARD"],
                                      prototype_parents=_PROTPARENTS)
     ], ['goblin grunt', 'goblin archwizard'])
Exemple #3
0
 def create(self, num=None):
     if not num:
         num = self.num_prototypes
     # print(f"Creating {num} additional prototypes...")
     for x in range(num):
         prot = {
             'prototype_key': str(uuid.uuid4()),
             'some_attributes': [str(uuid.uuid4()) for x in range(10)],
             'prototype_tags': list(sample(['demo', 'test', 'stuff'], 2)),
         }
         protlib.save_prototype(prot)
Exemple #4
0
 def create(self, num=None):
     if not num:
         num = self.num_prototypes
     # print(f"Creating {num} additional prototypes...")
     for x in range(num):
         prot = {
             "prototype_key": str(uuid.uuid4()),
             "some_attributes": [str(uuid.uuid4()) for x in range(10)],
             "prototype_tags": list(sample(["demo", "test", "stuff"], 2)),
         }
         protlib.save_prototype(prot)
Exemple #5
0
 def test_save_prototype(self):
     result = protlib.save_prototype(self.prot)
     self.assertEqual(result, self.prot)
     # faulty
     self.prot["prototype_key"] = None
     self.assertRaises(protlib.ValidationError, protlib.save_prototype,
                       self.prot)
Exemple #6
0
    def test_node_helpers(self):

        caller = self.caller

        with mock.patch(
                "evennia.prototypes.menus.protlib.search_prototype",
                new=mock.MagicMock(return_value=[self.test_prot]),
        ):
            # prototype_key helpers
            self.assertEqual(
                olc_menus._check_prototype_key(caller, "test_prot"), None)
            caller.ndb._menutree.olc_new = True
            self.assertEqual(
                olc_menus._check_prototype_key(caller, "test_prot"),
                "node_index")

            # prototype_parent helpers
            self.assertEqual(olc_menus._all_prototype_parents(caller),
                             ["test_prot"])
            # self.assertEqual(olc_menus._prototype_parent_parse(
            #     caller, 'test_prot'),
            #     "|cprototype key:|n test_prot, |ctags:|n None, |clocks:|n edit:all();spawn:all() "
            #     "\n|cdesc:|n None \n|cprototype:|n "
            #     "{\n  'typeclass': 'evennia.objects.objects.DefaultObject', \n}")

        with mock.patch(
                "evennia.prototypes.menus.protlib.search_prototype",
                new=mock.MagicMock(return_value=[_PROTPARENTS["GOBLIN"]]),
        ):
            self.assertEqual(
                olc_menus._prototype_parent_select(caller, "goblin"),
                "node_prototype_parent")

        self.assertEqual(
            olc_menus._get_menu_prototype(caller),
            {
                "prototype_key": "test_prot",
                "prototype_locks": "edit:all();spawn:all()",
                "prototype_parent": "goblin",
                "typeclass": "evennia.objects.objects.DefaultObject",
            },
        )

        # typeclass helpers
        with mock.patch(
                "evennia.utils.utils.get_all_typeclasses",
                new=mock.MagicMock(return_value={
                    "foo": None,
                    "bar": None
                }),
        ):
            self.assertEqual(olc_menus._all_typeclasses(caller),
                             ["bar", "foo"])

        self.assertEqual(
            olc_menus._typeclass_select(
                caller, "evennia.objects.objects.DefaultObject"), None)
        # prototype_parent should be popped off here
        self.assertEqual(
            olc_menus._get_menu_prototype(caller),
            {
                "prototype_key": "test_prot",
                "prototype_locks": "edit:all();spawn:all()",
                "prototype_parent": "goblin",
                "typeclass": "evennia.objects.objects.DefaultObject",
            },
        )

        # attr helpers
        self.assertEqual(olc_menus._caller_attrs(caller), [])
        self.assertEqual(olc_menus._add_attr(caller, "test1=foo1"), Something)
        self.assertEqual(olc_menus._add_attr(caller, "test2;cat1=foo2"),
                         Something)
        self.assertEqual(
            olc_menus._add_attr(caller, "test3;cat2;edit:false()=foo3"),
            Something)
        self.assertEqual(
            olc_menus._add_attr(caller,
                                "test4;cat3;set:true();edit:false()=foo4"),
            Something)
        self.assertEqual(
            olc_menus._add_attr(caller,
                                "test5;cat4;set:true();edit:false()=123"),
            Something)
        self.assertEqual(olc_menus._add_attr(caller, "test1=foo1_changed"),
                         Something)
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)["attrs"],
            [
                ("test1", "foo1_changed", None, ""),
                ("test2", "foo2", "cat1", ""),
                ("test3", "foo3", "cat2", "edit:false()"),
                ("test4", "foo4", "cat3", "set:true();edit:false()"),
                ("test5", "123", "cat4", "set:true();edit:false()"),
            ],
        )

        # tag helpers
        self.assertEqual(olc_menus._caller_tags(caller), [])
        self.assertEqual(olc_menus._add_tag(caller, "foo1"), Something)
        self.assertEqual(olc_menus._add_tag(caller, "foo2;cat1"), Something)
        self.assertEqual(olc_menus._add_tag(caller, "foo3;cat2;dat1"),
                         Something)
        self.assertEqual(olc_menus._caller_tags(caller),
                         ["foo1", "foo2", "foo3"])
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)["tags"],
            [("foo1", None, ""), ("foo2", "cat1", ""),
             ("foo3", "cat2", "dat1")],
        )
        self.assertEqual(olc_menus._add_tag(caller, "foo1", delete=True),
                         "Removed Tag 'foo1'.")
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)["tags"],
            [("foo2", "cat1", ""), ("foo3", "cat2", "dat1")],
        )

        self.assertEqual(
            olc_menus._display_tag(
                olc_menus._get_menu_prototype(caller)["tags"][0]), Something)
        self.assertEqual(olc_menus._caller_tags(caller), ["foo2", "foo3"])

        protlib.save_prototype(self.test_prot)

        # locks helpers
        self.assertEqual(olc_menus._lock_add(caller, "foo:false()"),
                         "Added lock 'foo:false()'.")
        self.assertEqual(olc_menus._lock_add(caller, "foo2:false()"),
                         "Added lock 'foo2:false()'.")
        self.assertEqual(olc_menus._lock_add(caller, "foo2:true()"),
                         "Lock with locktype 'foo2' updated.")
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)["locks"],
            "foo:false();foo2:true()")

        # perm helpers
        self.assertEqual(olc_menus._add_perm(caller, "foo"),
                         "Added Permission 'foo'")
        self.assertEqual(olc_menus._add_perm(caller, "foo2"),
                         "Added Permission 'foo2'")
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)["permissions"],
            ["foo", "foo2"])

        # prototype_tags helpers
        self.assertEqual(olc_menus._add_prototype_tag(caller, "foo"),
                         "Added Prototype-Tag 'foo'.")
        self.assertEqual(olc_menus._add_prototype_tag(caller, "foo2"),
                         "Added Prototype-Tag 'foo2'.")
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)["prototype_tags"],
            ["foo", "foo2"])

        # spawn helpers
        with mock.patch(
                "evennia.prototypes.menus.protlib.search_prototype",
                new=mock.MagicMock(return_value=[_PROTPARENTS["GOBLIN"]]),
        ):
            self.assertEqual(
                olc_menus._spawn(caller, prototype=self.test_prot), Something)
        obj = caller.contents[0]

        self.assertEqual(obj.typeclass_path,
                         "evennia.objects.objects.DefaultObject")
        self.assertEqual(obj.tags.get(category=spawner.PROTOTYPE_TAG_CATEGORY),
                         self.test_prot["prototype_key"])

        # update helpers
        self.assertEqual(
            olc_menus._apply_diff(caller,
                                  prototype=self.test_prot,
                                  back_node="foo",
                                  objects=[obj]),
            "foo",
        )  # no changes to apply
        self.test_prot["key"] = "updated key"  # change prototype
        self.assertEqual(
            olc_menus._apply_diff(caller,
                                  prototype=self.test_prot,
                                  objects=[obj],
                                  back_node="foo"),
            "foo",
        )  # apply change to the one obj

        # load helpers
        self.assertEqual(
            olc_menus._prototype_load_select(caller,
                                             self.test_prot["prototype_key"]),
            ("node_examine_entity", {
                "text": "|gLoaded prototype test_prot.|n",
                "back": "index"
            }),
        )

        # diff helpers
        obj_diff = {
            "attrs": {
                "desc": (
                    ("desc", "This is User #1.", None, ""),
                    ("desc", "This is User #1.", None, ""),
                    "KEEP",
                ),
                "foo": (None, ("foo", "bar", None, ""), "ADD"),
                "prelogout_location": (
                    ("prelogout_location", "#2", None, ""),
                    ("prelogout_location", "#2", None, ""),
                    "KEEP",
                ),
            },
            "home": ("#2", "#2", "KEEP"),
            "key": ("TestChar", "TestChar", "KEEP"),
            "locks": (
                "boot:false();call:false();control:perm(Developer);delete:false();"
                "edit:false();examine:perm(Developer);get:false();msg:all();"
                "puppet:false();tell:perm(Admin);view:all()",
                "boot:false();call:false();control:perm(Developer);delete:false();"
                "edit:false();examine:perm(Developer);get:false();msg:all();"
                "puppet:false();tell:perm(Admin);view:all()",
                "KEEP",
            ),
            "permissions": {
                "developer": ("developer", "developer", "KEEP")
            },
            "prototype_desc": ("Testobject build", None, "REMOVE"),
            "prototype_key": ("TestDiffKey", "TestDiffKey", "KEEP"),
            "prototype_locks":
            ("spawn:all();edit:all()", "spawn:all();edit:all()", "KEEP"),
            "prototype_tags": {},
            "tags": {
                "foo": (None, ("foo", None, ""), "ADD")
            },
            "typeclass": (
                "typeclasses.characters.Character",
                "typeclasses.characters.Character",
                "KEEP",
            ),
        }

        texts, options = olc_menus._format_diff_text_and_options(obj_diff)
        self.assertEqual(
            "\n".join(txt.strip() for txt in texts),
            "- |wattrs:|n    |c[1] |yADD|n: foo |W=|n bar |W(category:|n None|W, locks:|n |W)|n"
            "\n- |whome:|n"
            "\n- |wkey:|n"
            "\n- |wlocks:|n"
            "\n- |wpermissions:|n"
            "\n- |wprototype_desc:|n    |c[2] |rREMOVE|n: Testobject build"
            "\n- |wprototype_key:|n"
            "\n- |wprototype_locks:|n"
            "\n- |wprototype_tags:|n"
            "\n- |wtags:|n    |c[3] |yADD|n: foo |W(category:|n None|W)|n"
            "\n- |wtypeclass:|n",
        )

        self.assertEqual(
            options,
            [
                {
                    "goto": (Something, Something),
                    "key": "1",
                    "desc": "|gKEEP|n (attrs) None"
                },
                {
                    "goto": (Something, Something),
                    "key": "2",
                    "desc": "|gKEEP|n (prototype_desc) Testobject build",
                },
                {
                    "goto": (Something, Something),
                    "key": "3",
                    "desc": "|gKEEP|n (tags) None"
                },
            ],
        )
Exemple #7
0
    def test_node_helpers(self):

        caller = self.caller

        with mock.patch("evennia.prototypes.menus.protlib.search_prototype",
                        new=mock.MagicMock(return_value=[self.test_prot])):
            # prototype_key helpers
            self.assertEqual(
                olc_menus._check_prototype_key(caller, "test_prot"), None)
            caller.ndb._menutree.olc_new = True
            self.assertEqual(
                olc_menus._check_prototype_key(caller, "test_prot"),
                "node_index")

            # prototype_parent helpers
            self.assertEqual(olc_menus._all_prototype_parents(caller),
                             ['test_prot'])
            # self.assertEqual(olc_menus._prototype_parent_parse(
            #     caller, 'test_prot'),
            #     "|cprototype key:|n test_prot, |ctags:|n None, |clocks:|n edit:all();spawn:all() "
            #     "\n|cdesc:|n None \n|cprototype:|n "
            #     "{\n  'typeclass': 'evennia.objects.objects.DefaultObject', \n}")

        with mock.patch(
                "evennia.prototypes.menus.protlib.search_prototype",
                new=mock.MagicMock(return_value=[_PROTPARENTS['GOBLIN']])):
            self.assertEqual(
                olc_menus._prototype_parent_select(caller, "goblin"),
                "node_prototype_parent")

        self.assertEqual(
            olc_menus._get_menu_prototype(caller), {
                'prototype_key': 'test_prot',
                'prototype_locks': 'edit:all();spawn:all()',
                'prototype_parent': 'goblin',
                'typeclass': 'evennia.objects.objects.DefaultObject'
            })

        # typeclass helpers
        with mock.patch("evennia.utils.utils.get_all_typeclasses",
                        new=mock.MagicMock(return_value={
                            "foo": None,
                            "bar": None
                        })):
            self.assertEqual(olc_menus._all_typeclasses(caller),
                             ["bar", "foo"])

        self.assertEqual(
            olc_menus._typeclass_select(
                caller, "evennia.objects.objects.DefaultObject"), None)
        # prototype_parent should be popped off here
        self.assertEqual(
            olc_menus._get_menu_prototype(caller), {
                'prototype_key': 'test_prot',
                'prototype_locks': 'edit:all();spawn:all()',
                'prototype_parent': 'goblin',
                'typeclass': 'evennia.objects.objects.DefaultObject'
            })

        # attr helpers
        self.assertEqual(olc_menus._caller_attrs(caller), [])
        self.assertEqual(olc_menus._add_attr(caller, "test1=foo1"), Something)
        self.assertEqual(olc_menus._add_attr(caller, "test2;cat1=foo2"),
                         Something)
        self.assertEqual(
            olc_menus._add_attr(caller, "test3;cat2;edit:false()=foo3"),
            Something)
        self.assertEqual(
            olc_menus._add_attr(caller,
                                "test4;cat3;set:true();edit:false()=foo4"),
            Something)
        self.assertEqual(
            olc_menus._add_attr(caller,
                                "test5;cat4;set:true();edit:false()=123"),
            Something)
        self.assertEqual(olc_menus._add_attr(caller, "test1=foo1_changed"),
                         Something)
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)['attrs'],
            [("test1", "foo1_changed", None, ''),
             ("test2", "foo2", "cat1", ''),
             ("test3", "foo3", "cat2", "edit:false()"),
             ("test4", "foo4", "cat3", "set:true();edit:false()"),
             ("test5", '123', "cat4", "set:true();edit:false()")])

        # tag helpers
        self.assertEqual(olc_menus._caller_tags(caller), [])
        self.assertEqual(olc_menus._add_tag(caller, "foo1"), Something)
        self.assertEqual(olc_menus._add_tag(caller, "foo2;cat1"), Something)
        self.assertEqual(olc_menus._add_tag(caller, "foo3;cat2;dat1"),
                         Something)
        self.assertEqual(olc_menus._caller_tags(caller),
                         ['foo1', 'foo2', 'foo3'])
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)['tags'],
            [('foo1', None, ""), ('foo2', 'cat1', ""),
             ('foo3', 'cat2', "dat1")])
        self.assertEqual(olc_menus._add_tag(caller, "foo1", delete=True),
                         "Removed Tag 'foo1'.")
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)['tags'],
            [('foo2', 'cat1', ""), ('foo3', 'cat2', "dat1")])

        self.assertEqual(
            olc_menus._display_tag(
                olc_menus._get_menu_prototype(caller)['tags'][0]), Something)
        self.assertEqual(olc_menus._caller_tags(caller), ["foo2", "foo3"])

        protlib.save_prototype(**self.test_prot)

        # locks helpers
        self.assertEqual(olc_menus._lock_add(caller, "foo:false()"),
                         "Added lock 'foo:false()'.")
        self.assertEqual(olc_menus._lock_add(caller, "foo2:false()"),
                         "Added lock 'foo2:false()'.")
        self.assertEqual(olc_menus._lock_add(caller, "foo2:true()"),
                         "Lock with locktype 'foo2' updated.")
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)["locks"],
            "foo:false();foo2:true()")

        # perm helpers
        self.assertEqual(olc_menus._add_perm(caller, "foo"),
                         "Added Permission 'foo'")
        self.assertEqual(olc_menus._add_perm(caller, "foo2"),
                         "Added Permission 'foo2'")
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)["permissions"],
            ["foo", "foo2"])

        # prototype_tags helpers
        self.assertEqual(olc_menus._add_prototype_tag(caller, "foo"),
                         "Added Prototype-Tag 'foo'.")
        self.assertEqual(olc_menus._add_prototype_tag(caller, "foo2"),
                         "Added Prototype-Tag 'foo2'.")
        self.assertEqual(
            olc_menus._get_menu_prototype(caller)["prototype_tags"],
            ["foo", "foo2"])

        # spawn helpers
        with mock.patch(
                "evennia.prototypes.menus.protlib.search_prototype",
                new=mock.MagicMock(return_value=[_PROTPARENTS['GOBLIN']])):
            self.assertEqual(
                olc_menus._spawn(caller, prototype=self.test_prot), Something)
        obj = caller.contents[0]

        self.assertEqual(obj.typeclass_path,
                         "evennia.objects.objects.DefaultObject")
        self.assertEqual(
            obj.tags.get(category=spawner._PROTOTYPE_TAG_CATEGORY),
            self.test_prot['prototype_key'])

        # update helpers
        self.assertEqual(
            olc_menus._apply_diff(caller,
                                  prototype=self.test_prot,
                                  back_node="foo",
                                  objects=[obj]), 'foo')  # no changes to apply
        self.test_prot['key'] = "updated key"  # change prototype
        self.assertEqual(
            olc_menus._apply_diff(caller,
                                  prototype=self.test_prot,
                                  objects=[obj],
                                  back_node='foo'),
            'foo')  # apply change to the one obj

        # load helpers
        self.assertEqual(
            olc_menus._prototype_load_select(caller,
                                             self.test_prot['prototype_key']),
            ('node_examine_entity', {
                'text': '|gLoaded prototype test_prot.|n',
                'back': 'index'
            }))

        # diff helpers
        obj_diff = {
            'attrs': {
                u'desc': ((u'desc', u'This is User #1.', None, ''),
                          (u'desc', u'This is User #1.', None, ''), 'KEEP'),
                u'foo': (None, (u'foo', u'bar', None, ''), 'ADD'),
                u'prelogout_location':
                ((u'prelogout_location', "#2", None, ''),
                 (u'prelogout_location', "#2", None, ''), 'KEEP')
            },
            'home': ('#2', '#2', 'KEEP'),
            'key': (u'TestChar', u'TestChar', 'KEEP'),
            'locks':
            ('boot:false();call:false();control:perm(Developer);delete:false();'
             'edit:false();examine:perm(Developer);get:false();msg:all();'
             'puppet:false();tell:perm(Admin);view:all()',
             'boot:false();call:false();control:perm(Developer);delete:false();'
             'edit:false();examine:perm(Developer);get:false();msg:all();'
             'puppet:false();tell:perm(Admin);view:all()', 'KEEP'),
            'permissions': {
                'developer': ('developer', 'developer', 'KEEP')
            },
            'prototype_desc': ('Testobject build', None, 'REMOVE'),
            'prototype_key': ('TestDiffKey', 'TestDiffKey', 'KEEP'),
            'prototype_locks':
            ('spawn:all();edit:all()', 'spawn:all();edit:all()', 'KEEP'),
            'prototype_tags': {},
            'tags': {
                'foo': (None, ('foo', None, ''), 'ADD')
            },
            'typeclass': (u'typeclasses.characters.Character',
                          u'typeclasses.characters.Character', 'KEEP')
        }

        texts, options = olc_menus._format_diff_text_and_options(obj_diff)
        self.assertEqual(
            "\n".join(texts), '- |wattrs:|n \n'
            '   |c[1] |yADD|n|W:|n None |W->|n foo |W=|n bar |W(category:|n None|W, locks:|n |W)|n\n'
            '   |gKEEP|W:|n prelogout_location |W=|n #2 |W(category:|n None|W, locks:|n |W)|n\n'
            '   |gKEEP|W:|n desc |W=|n This is User #1. |W(category:|n None|W, locks:|n |W)|n\n'
            '- |whome:|n    |gKEEP|W:|n #2\n'
            '- |wkey:|n    |gKEEP|W:|n TestChar\n'
            '- |wlocks:|n    |gKEEP|W:|n boot:false();call:false();control:perm(Developer);delete:false();edit:false();examine:perm(Developer);get:false();msg:all();puppet:false();tell:perm(Admin);view:all()\n'
            '- |wpermissions:|n \n'
            '   |gKEEP|W:|n developer\n'
            '- |wprototype_desc:|n    |c[2] |rREMOVE|n|W:|n Testobject build |W->|n None\n'
            '- |wprototype_key:|n    |gKEEP|W:|n TestDiffKey\n'
            '- |wprototype_locks:|n    |gKEEP|W:|n spawn:all();edit:all()\n'
            '- |wprototype_tags:|n \n'
            '- |wtags:|n \n'
            '   |c[3] |yADD|n|W:|n None |W->|n foo |W(category:|n None|W)|n\n'
            '- |wtypeclass:|n    |gKEEP|W:|n typeclasses.characters.Character')
        self.assertEqual(
            options, [{
                'goto': (Something, Something),
                'key': '1',
                'desc': '|gKEEP|n (attrs) None'
            }, {
                'goto': (Something, Something),
                'key': '2',
                'desc': '|gKEEP|n (prototype_desc) Testobject build'
            }, {
                'goto': (Something, Something),
                'key': '3',
                'desc': '|gKEEP|n (tags) None'
            }])