Esempio n. 1
0
 def setUp(self):
     self.normal_world = loc.Location("Normal World",
                                      "You see less magic people.")
     self.wizard_world = loc.Location("Wizard World",
                                      "You see magic people.")
     self.magic_portal = loc.Exit(self.wizard_world,
                                  "portal",
                                  other_names=["magic portal", "magic"])
     self.magic_door = loc.Exit(self.wizard_world,
                                "door",
                                other_names=["magic door"])
     self.redundant_portal = loc.Exit(self.wizard_world, "redundant portal",
                                      ["portal"])
Esempio n. 2
0
 def setUp(self):
     self.room = loc.Location("Room", "This is just a room for testing.")
     self.bill = Human("Bill")
     self.bill.set_location(TEST_ROOM)
     self.phil = char.Character("Phil")
     self.phil.set_location(TEST_ROOM)
     self.dana = char.Character("Dana")
     self.dana.set_location(TEST_OUT)
Esempio n. 3
0
 def setUp(self):
     self.wizard_world = loc.Location("Wizard World",
                                      "You see magic people.")
     self.magic_portal = loc.Exit(self.wizard_world,
                                  "portal",
                                  other_names=["magic portal", "magic"])
     self.magic_door = loc.Exit(self.wizard_world,
                                "door",
                                other_names=["magic door"],
                                hide_des=True)
Esempio n. 4
0
    def test_error_handling(self):
        """Test that util.find properly checks its arguments"""
        return
        obj = location.Location("test", "")
        msg = ("util.find() names argument should be a str or "
               "iterable of strings, (received type '{}')")

        # this should work
        find(obj, name="foo")
        # this should fail since the provided name is an int
        with self.assertRaises(TypeError, msg=msg.format("<class 'int'>")):
            find(obj, name=3)

        # multiple strings
        find(obj, name=["foo", "bar"])
        find(obj, name={"foo", "bar"})
        with self.assertRaises(TypeError, msg=msg.format("<class 'float'>")):
            find(obj, name=["foo", 3.4])

        msg = ("util.find() types argument should be a type or "
               "iterable of types, (received value '{}')")
        find(obj, name="foo", type=str)
        find(obj, name="foo", type=(str, float))
        find(obj, name="foo", type={str, float})
        with self.assertRaises(TypeError, msg=msg.format("<class 'int'>")):
            find(obj, type=3)
        with self.assertRaises(TypeError, msg=msg.format("<class 'str'>")):
            find(obj, type=(str, "float"))

        find(obj, maxdepth=3)
        msg = ("util.find() maxdepth argument must be int or float, received "
               "type '{}'")
        with self.assertRaises(TypeError, msg=msg.format("<class 'str'>")):
            find(obj, type=(str, "float"))

        # can provide must_have arguments
        find(obj, must_have={"foo": "bar"})
        msg = ("util.find() optional argument must be dict, received "
               "type '{}'")
        with self.assertRaises(TypeError, msg=msg.format("<class 'int'>")):
            find(obj, must_have=3)
Esempio n. 5
0
        self.assertEqual(blacklist._classes, set())
        self.assertEqual(set(blacklist._include_chars), {self.bloog})
        self.assertEqual(set(blacklist._exclude_chars), {self.vloobuk})
        # now include vloobuk and exclude bloog
        blacklist.include(self.vloobuk)
        blacklist.exclude(self.bloog)
        expected[self.vloobuk] = True
        expected[self.bloog] = False
        self.assertEqual(self.filter_test(blacklist), expected)
        self.assertEqual(blacklist._classes, set())
        self.assertEqual(set(blacklist._include_chars), {self.vloobuk})
        self.assertEqual(set(blacklist._exclude_chars), {self.bloog})


# some test locations
TEST_ROOM = loc.Location("Room", "This is just a room for testing.")
TEST_OUT = loc.Location("Outside", "This room is outside.")
TEST_EXIT = loc.Exit(TEST_OUT, "out", ["outside", "test out"])
TEST_ROOM.add_exit(TEST_EXIT)

# some test items
class Coin(item.Item):
    """a simple coin"""


class HealthPotion(item.Item):
    """a health potion with vary strength"""
    def __init__(self, hp):
        self.hp = 5

    @classmethod
Esempio n. 6
0
    def test_find_location(self):
        """Test that util.find works with locations.
        (Implictly tests that Location.find_child works.)
        """
        shopkeep = char.Character("Bill")
        # the drunkard is named "outside" for some reason
        drunkard = char.Character("Outside")
        guest = char.Character("Matt")

        tavern = location.Location("Tavern", "A cool tavern")
        outside = location.Location("Outside", "It's muddy.")
        basement = location.Location("Tavern Basement",
                                     "This is where he stores the ale.")
        upstairs = location.Location("Tavern Upstairs",
                                     "There is a comfy bed for guests.")
        safe_room = location.Location("Tavern Safe Room",
                                      "Room only shopkeep knows about.")
        exit_list = [
            location.Exit(outside, "out", ["outside"]),
            # only shopkeeper allowed downstairs
            location.Exit(basement,
                          "basement", ["downstairs"],
                          interact=char.Filter("whitelist",
                                               include_chars=[shopkeep])),
            # only the guest and the shopkeeper allowed upstairs
            location.Exit(upstairs,
                          "upstairs", ["up"],
                          interact=char.Filter("blacklist",
                                               exclude_chars=[drunkard])),
            # only the shopkeeper can see the safe_room, but others
            # can interact if they know about it
            location.Exit(safe_room,
                          "safe room", ["safe"],
                          perceive=char.Filter("whitelist",
                                               include_chars=[shopkeep]))
        ]
        for ex in exit_list:
            tavern.add_exit(ex)
        tavern.add_char(shopkeep)
        tavern.add_char(drunkard)
        tavern.add_char(guest)

        self.maxDiff = 3000
        # just doing a generic find should yield everything in the location
        self.assertCountEqual(find(tavern),
                              exit_list + [shopkeep, drunkard, guest])
        # providing a depth less than 0 should give us no items
        self.assertCountEqual(find(tavern, maxdepth=-1), [])

        # can we filter by type?
        self.assertCountEqual(find(tavern, type=location.Exit), exit_list)
        self.assertCountEqual(find(tavern, type=char.Character),
                              [shopkeep, guest, drunkard])
        self.assertCountEqual(
            find(tavern, type=(char.Character, location.Exit)),
            exit_list + [shopkeep, drunkard, guest])

        # can we look by name?
        self.assertCountEqual(find(tavern, name="outside"),
                              [exit_list[0], drunkard])
        self.assertCountEqual(find(tavern, name="outside", type=location.Exit),
                              [exit_list[0]])
        # name and type
        self.assertCountEqual(find(tavern, name="outside", type=location.Exit),
                              [exit_list[0]])
        self.assertCountEqual(find(tavern, name="up"), [exit_list[2]])
        self.assertCountEqual(find(tavern, name="safe"), [exit_list[3]])
        self.assertCountEqual(find(tavern, name="bill"), [shopkeep])
        # testing that all the permissions checking works as expected
        self.assertCountEqual(find(tavern, name="bill", pov=shopkeep),
                              [shopkeep])
        # shopkeeper can see all the exits
        self.assertCountEqual(find(tavern, type=location.Exit, pov=shopkeep),
                              exit_list)
        self.assertCountEqual(find(tavern, type=location.Exit, pov=drunkard),
                              [exit_list[0], exit_list[3]])
        self.assertCountEqual(find(tavern, type=location.Exit, pov=guest),
                              [exit_list[0], exit_list[2], exit_list[3]])

        # now adding some items
        tavern.add_item(HealthPotion(1))
        tavern.add_item(HealthPotion(1))
        tavern.add_item(HealthPotion(5))
        tavern.add_item(SilverCoin())
        tavern.add_item(SilverCoin())
        tavern.add_item(SilverCoin())
        items = [(HealthPotion(1), 2), (HealthPotion(5), 1), (SilverCoin(), 3)]

        # directly comparing items is hard, so we will serialize them first
        self.assertEqual(10, len(find(tavern)))

        # helper function to make comparing items easier
        def item_list(i_list):
            return [(item.save(), c) for (item, c) in i_list]

        self.assertCountEqual(item_list(find(tavern, type=Item)),
                              item_list(items))
        self.assertCountEqual(
            item_list(find(tavern, name="Health Potion")),
            item_list([(HealthPotion(1), 2), (HealthPotion(5), 1)]))
        self.assertCountEqual(item_list(find(tavern, hp=1)),
                              item_list([(HealthPotion(1), 2)]))
Esempio n. 7
0
"""testcases for interaction between characters, items, and entities"""
import unittest
import swampymud.location as location
import swampymud.character as character
import swampymud.item as item
import swampymud.inventory as inventory

# first, some locations
tavern = location.Location("Tavern", "A scuzzy tavern.")
plaza = location.Location("Plaza", "A few shops can be seen.")
# now connect the two
plaza.add_exit(location.Exit(tavern, "tavern"))
tavern.add_exit(location.Exit(plaza, "outside", ["back", "plaza"]))

# now create some CharacterClasses
class Humanoid(character.Character):
    """A class representing most humanoids"""
    equip_slots = ["Right hand", "head", "cHEST"]

    starting_location = tavern


class Brute(Humanoid):
    """A simple character, known to resolve problems with his fists"""

    @character.Command
    def smash(self, args):
        self.message("RARG BIG SMASH")


class Thief(Humanoid):