Exemple #1
0
    def test_initializing_components_at_production_time(self):
        cat_factory = Assemblage(components=[Alive, Portable])
        zombie_cat = cat_factory.make(alive=False)

        self.assertTrue(isinstance(zombie_cat, Entity))
        self.assertFalse(zombie_cat.alive)
        self.assertTrue(zombie_cat.is_portable)
Exemple #2
0
    def test_unknown_initial_properties_at_production_time(self):
        cat_factory = Assemblage(components=[Alive, Portable])

        with self.assertRaises(ValueError) as e:
            cat_factory.make(live=True, aliv=True)  # misspell some properties

        self.assertEqual(e.exception.message, "Unknown initial properties: live, aliv")
Exemple #3
0
    def test_initializing_components_at_production_time(self):
        cat_factory = Assemblage(components=[Alive, Portable])
        zombie_cat = cat_factory.make(alive=False)

        self.assertTrue(isinstance(zombie_cat, Entity))
        self.assertFalse(zombie_cat.alive)
        self.assertTrue(zombie_cat.is_portable)
Exemple #4
0
    def test_assemblage_makes_entity(self):
        cat_factory = Assemblage([Alive, Portable])
        cat = cat_factory.make()

        self.assertTrue(isinstance(cat, Entity))
        self.assertTrue(cat.alive)
        self.assertTrue(cat.is_portable)
Exemple #5
0
    def setUp(self):
        self.world = World()
        self.player = self.world.make_entity(
            Assemblage([duel.EquipmentBearing]))

        self.wand_factory = Assemblage([duel.Equipment], equipment_type='wand')
        self.wand = self.world.make_entity(self.wand_factory)
Exemple #6
0
    def test_assemblage_makes_entity(self):
        cat_factory = Assemblage([Alive, Portable])
        cat = cat_factory.make()

        self.assertTrue(isinstance(cat, Entity))
        self.assertTrue(cat.alive)
        self.assertTrue(cat.is_portable)
def make_toy_world():
    world = World()
    name_system = NameSystem(world)
    player = world.make_entity(Assemblage(components=[Location]))
    for direction_name, direction_shorthand in [('north', 'n'), ('south', 's'),
                                                ('east', 'e'), ('west', 'w'),
                                                ('up', 'u'), ('down', 'd')]:
        direction_entity = world.make_entity(
            Assemblage(components=[Direction, Name]), name=direction_name)
        name_system.add_name(direction_name, direction_entity)
        name_system.add_name(direction_shorthand, direction_entity)

        if direction_name == 'north':
            north = direction_entity
        if direction_name == 'south':
            south = direction_entity

    room_one = world.make_entity(room_factory,
                                 description="You are in room one.")
    room_two = world.make_entity(room_factory,
                                 description="You are in room two.")
    room_one.paths = {north: room_two}
    room_two.paths = {south: room_one}

    player.location = room_one

    return world, name_system, player, room_one, room_two
Exemple #8
0
    def test_adding_component_with_initial_components(self):
        cat_factory = Assemblage(components=[Portable])
        cat_factory.add_component(Alive)
        cat = cat_factory.make()

        self.assertTrue(isinstance(cat, Entity))
        self.assertTrue(cat.alive)
        self.assertTrue(cat.is_portable)
Exemple #9
0
    def test_adding_component_with_initial_components(self):
        cat_factory = Assemblage(components=[Portable])
        cat_factory.add_component(Alive)
        cat = cat_factory.make()

        self.assertTrue(isinstance(cat, Entity))
        self.assertTrue(cat.alive)
        self.assertTrue(cat.is_portable)
Exemple #10
0
    def setUp(self):
        self.world = World()
        bucket_factory = Assemblage(components=[Container])
        self.bucket_one = self.world.make_entity(bucket_factory)
        self.bucket_two = self.world.make_entity(bucket_factory)

        self.thing_factory = Assemblage(components=[Moveable])
        self.thing = self.world.make_entity(self.thing_factory, location=self.bucket_one)

        self.container_system = ContainerSystem(world=self.world)
Exemple #11
0
    def test_assembled_cats_are_independent(self):
        cat_factory = Assemblage([Alive, Portable])

        my_cat = cat_factory.make()
        stray_cat = cat_factory.make()
        self.human.pick_up(my_cat)

        self.assertNotEqual(my_cat.uuid, stray_cat.uuid)
        self.assertIn(my_cat, self.human.inventory)
        self.assertNotIn(stray_cat, self.human.inventory)
Exemple #12
0
    def test_assembling_entity_with_initial_conditions(self):
        zombie_cat_factory = Assemblage([Alive], alive=False)
        zombie_cat = zombie_cat_factory.make()
        self.assertFalse(zombie_cat.alive)
        zombie_cat.resurrect()
        self.assertTrue(zombie_cat.alive)

        fed_cat_factory = Assemblage([Container], inventory=set([self.food]))
        fed_cat = fed_cat_factory.make()
        self.assertEqual(self.food.uuid, fed_cat.inventory.pop().uuid)
Exemple #13
0
    def setUp(self):
        self.world = World()
        bucket_factory = Assemblage(components=[duel.Container])
        self.bucket_one = self.world.make_entity(bucket_factory)
        self.bucket_two = self.world.make_entity(bucket_factory)

        self.thing_factory = Assemblage(components=[duel.Moveable])
        self.thing = self.world.make_entity(self.thing_factory,
                                            location=self.bucket_one)
        self.bucket_one.inventory.add(self.thing)
Exemple #14
0
    def test_assembled_cats_are_independent(self):
        cat_factory = Assemblage([Alive, Portable])

        my_cat = cat_factory.make()
        stray_cat = cat_factory.make()
        self.human.pick_up(my_cat)

        self.assertNotEqual(my_cat.uuid, stray_cat.uuid)
        self.assertIn(my_cat, self.human.inventory)
        self.assertNotIn(stray_cat, self.human.inventory)
Exemple #15
0
    def test_register_stores_entity_by_component(self):
        manager = Manager(ComponentWithState)
        entity = Assemblage(components=[ComponentWithState]).make(number=1,
                                                                  letter='b')
        specific_component = entity.get_component(ComponentWithState)

        manager.register(entity)

        self.assertEqual(manager.entities_by_component[specific_component],
                         entity)
Exemple #16
0
    def test_assembled_entity_interacts_normally(self):
        cat_factory = Assemblage([Alive, Portable, Container])
        cat = cat_factory.make()

        # pick up cat
        self.human.pick_up(cat)
        self.assertIn(cat, self.human.inventory)

        # feed cat
        cat.pick_up(self.food)
        self.assertIn(self.food, cat.inventory)
Exemple #17
0
    def test_assembled_entity_interacts_normally(self):
        cat_factory = Assemblage([Alive, Portable, Container])
        cat = cat_factory.make()

        # pick up cat
        self.human.pick_up(cat)
        self.assertIn(cat, self.human.inventory)

        # feed cat
        cat.pick_up(self.food)
        self.assertIn(self.food, cat.inventory)
Exemple #18
0
    def test_unknown_initial_properties_at_production_time(self):
        cat_factory = Assemblage(components=[Alive, Portable])

        with self.assertRaises(ValueError) as e:
            cat_factory.make(live=True, aliv=True)  # misspell some properties

        if six.PY2:
            exception_message = e.exception.message
        if six.PY3:
            exception_message = str(e.exception)

        self.assertEqual(exception_message,
                         "Unknown initial properties: live, aliv")
Exemple #19
0
    def test_make_entity_with_assemblage_makes_entity(self):
        new_entity = self.world.make_entity(Assemblage([Moveable, Location]))

        self.assertTrue(isinstance(new_entity, Entity))
        self.assertIn(new_entity, self.world.entities)
        self.assertTrue(new_entity.has_component(Moveable))
        self.assertTrue(new_entity.has_component(Location))
Exemple #20
0
class TestContainerSystem(unittest.TestCase):

    def setUp(self):
        self.world = World()
        bucket_factory = Assemblage(components=[Container])
        self.bucket_one = self.world.make_entity(bucket_factory)
        self.bucket_two = self.world.make_entity(bucket_factory)

        self.thing_factory = Assemblage(components=[Moveable])
        self.thing = self.world.make_entity(self.thing_factory, location=self.bucket_one)

        self.container_system = ContainerSystem(world=self.world)

    def test_move_item_to_new_inventory(self):
        self.container_system.move(self.thing, self.bucket_two)

        self.assertEqual(self.thing.location, self.bucket_two)
        self.assertEqual(self.bucket_two.inventory, set([self.thing]))

    def test_cannot_move_immoveable_item(self):
        bookcase = self.world.make_entity()

        with self.assertRaises(ValueError) as e:
            self.container_system.move(bookcase, self.bucket_two)

        self.assertEqual(e.exception.message, "You cannot move this item")
        self.assertEqual(self.bucket_two.inventory, set([]))

    def test_cannot_move_item_to_non_container(self):
        new_thing = self.thing_factory.make()
        with self.assertRaises(ValueError) as e:
            self.container_system.move(self.thing, new_thing)

        self.assertEqual(e.exception.message, "Invalid destination")
        self.assertEqual(self.thing.location, self.bucket_one)
Exemple #21
0
class TestContainerSystem(unittest.TestCase):

    def setUp(self):
        self.world = World()
        bucket_factory = Assemblage(components=[duel.Container])
        self.bucket_one = self.world.make_entity(bucket_factory)
        self.bucket_two = self.world.make_entity(bucket_factory)

        self.thing_factory = Assemblage(components=[duel.Moveable])
        self.thing = self.world.make_entity(self.thing_factory, location=self.bucket_one)

        self.container_system = duel.ContainerSystem(world=self.world, auto_update=True)

    def test_move_item_to_new_inventory(self):
        self.container_system.move(self.thing, self.bucket_two)

        self.assertEqual(self.thing.location, self.bucket_two)
        self.assertEqual(self.bucket_two.inventory, set([self.thing]))

    def test_cannot_move_immoveable_item(self):
        bookcase = self.world.make_entity()

        with self.assertRaises(ValueError) as e:
            self.container_system.move(bookcase, self.bucket_two)

        self.assertEqual(e.exception.message, "You cannot move this item")
        self.assertEqual(self.bucket_two.inventory, set([]))

    def test_cannot_move_item_to_non_container(self):
        new_thing = self.thing_factory.make()
        with self.assertRaises(ValueError) as e:
            self.container_system.move(self.thing, new_thing)

        self.assertEqual(e.exception.message, "Invalid destination")
        self.assertEqual(self.thing.location, self.bucket_one)
Exemple #22
0
    def setUp(self):
        self.world = World()
        bucket_factory = Assemblage(components=[duel.Container])
        self.bucket_one = self.world.make_entity(bucket_factory)
        self.bucket_two = self.world.make_entity(bucket_factory)

        self.thing_factory = Assemblage(components=[duel.Moveable])
        self.thing = self.world.make_entity(self.thing_factory, location=self.bucket_one)

        self.container_system = duel.ContainerSystem(world=self.world, auto_update=True)
Exemple #23
0
    def test_register_puts_entity_in_appropriate_registry(self):
        manager = Manager(ComponentWithState)
        entity = Assemblage(components=[ComponentWithState]).make(number=1,
                                                                  letter='a')

        manager.register(entity)

        self.assertIn(1, six.iterkeys(manager.entities_by_number))
        self.assertIn(entity, manager.entities_by_number[1])
        self.assertIn('a', six.iterkeys(manager.entities_by_letter))
        self.assertIn(entity, manager.entities_by_letter['a'])
Exemple #24
0
    def test_make_entity_with_initial_properties(self):
        new_entity = self.world.make_entity(Assemblage([Moveable, Location]),
                                            x=1,
                                            v_y=2)

        self.assertTrue(isinstance(new_entity, Entity))
        self.assertIn(new_entity, self.world.entities)
        self.assertTrue(new_entity.has_component(Moveable))
        self.assertTrue(new_entity.has_component(Location))

        self.assertEqual(new_entity.x, 1)
        self.assertEqual(new_entity.v_y, 2)
Exemple #25
0
    def test_make_aspect_from_assemblage(self):
        factory = Assemblage(components=[Portable])

        aspect = Aspect.make_from(factory)

        self.assertTrue(aspect.is_interested_in(self.cat))
        self.assertFalse(aspect.is_interested_in(self.plant))
        self.assertFalse(aspect.is_interested_in(self.bathtub))
        self.assertTrue(aspect.is_interested_in(self.brains))
        self.assertFalse(aspect.is_interested_in(self.zombie))

        self.assertEqual(aspect.select_entities(self.entities),
                         set([self.cat, self.brains]))
Exemple #26
0
    def test_assembling_entity_with_initial_conditions(self):
        zombie_cat_factory = Assemblage([Alive], alive=False)
        zombie_cat = zombie_cat_factory.make()
        self.assertFalse(zombie_cat.alive)
        zombie_cat.resurrect()
        self.assertTrue(zombie_cat.alive)

        fed_cat_factory = Assemblage([Container], inventory=set([self.food]))
        fed_cat = fed_cat_factory.make()
        self.assertEqual(self.food.uuid, fed_cat.inventory.pop().uuid)
    player.location = new_location


def _look(world, player):
    return player.location.description


look = Command(name='look', response=_look)
go = ChangefulCommand(name='go',
                      syntax=[_is_a_direction],
                      rules=[_path_exists],
                      state_changes=[_move_player],
                      response=_look)
commands = {'look': look, 'go': go}

room_factory = Assemblage(components=[Description, Mapping])


def make_toy_world():
    world = World()
    name_system = NameSystem(world)
    player = world.make_entity(Assemblage(components=[Location]))
    for direction_name, direction_shorthand in [('north', 'n'), ('south', 's'),
                                                ('east', 'e'), ('west', 'w'),
                                                ('up', 'u'), ('down', 'd')]:
        direction_entity = world.make_entity(
            Assemblage(components=[Direction, Name]), name=direction_name)
        name_system.add_name(direction_name, direction_entity)
        name_system.add_name(direction_shorthand, direction_entity)

        if direction_name == 'north':
Exemple #28
0
    def populate_tags_for_entity(self, entity):
        if not self.description_values[entity][tag]:
            self.populate_tag_for_entity(self, tag, entity)

    def update(self):
        for entity in self.entities:
            self.populate_tags_for_entity(entity)


#########################
# Define what a player is
#########################

player_factory = Assemblage(components=[
    Name, Description, Container, Moveable, EquipmentBearing, ExpelliarmusSkill
])

########################
# Define what a room is
########################

room_factory = Assemblage(components=[Description, Container, Mappable, Name])

#######################
# Define what a wand is
#######################

wand_factory = Assemblage(
    components=[Name, Description, Equipment, Moveable, Loyalty],
    equipment_type='wand')
Exemple #29
0
 def setUp(self):
     self.world, self.name_system, self.player, _, __ = make_toy_world()
     self.there = self.world.make_entity(Assemblage(components=[Name]), name='there')
Exemple #30
0
 def setUp(self):
     self.world, self.name_system, self.player, self.room_one, _ = make_toy_world()
     self.north = self.name_system.get_token_from_name('north')
     self.south = self.name_system.get_token_from_name('south')
     self.there = self.world.make_entity(Assemblage(components=[Name]), name='there')
    def setUp(self):
        self.world = World()
        self.item_factory = Assemblage(components=[Name])
        self.item = self.world.make_entity(self.item_factory, name='item one')

        self.name_system = NameSystem(world=self.world)