コード例 #1
0
ファイル: test_assemblage.py プロジェクト: pathunstrom/braga
    def setUp(self):
        self.world = World()

        self.human = Entity()
        self.human.components.add(Container())

        self.food = Entity()
        self.food.components.add(Portable())
コード例 #2
0
    def make(self, **kwargs):
        """ Makes an Entity with the Assemblage's combination of Components.

            Can initialize the Entity's components with particular values.
            If the Assemblage has been created with initial values for the parameters of its Components,
                those initial values are applied.
            If this method is called with kwargs, it will pass that initial value to the appropriate Component.
            Calling `make` with kwargs will override the initial values set on the Assemblage.

            Returns an Entity.
        """

        entity = Entity()

        for component_type, init_kwargs in six.iteritems(self.component_types):
            instance_kwargs = init_kwargs
            instance_kwargs.update({k:v for k,v in six.iteritems(kwargs) if k in component_type.__slots__})
            kwargs = {k:v for k,v in six.iteritems(kwargs) if k not in component_type.__slots__}

            component = component_type(**instance_kwargs)
            entity.components.add(component)

        if kwargs:
            raise ValueError("Unknown initial properties: {}".format(', '.join(six.iterkeys(kwargs))))

        return entity
コード例 #3
0
ファイル: test_assemblage.py プロジェクト: EricSchles/braga
    def setUp(self):
        self.world = World()

        self.human = Entity()
        self.human.components.add(Container())

        self.food = Entity()
        self.food.components.add(Portable())
コード例 #4
0
ファイル: test_entity.py プロジェクト: EricSchles/braga
    def test_entity_attributes(self):
        cat = Entity()
        with self.assertRaises(AttributeError):
            cat.alive

        catalive = Alive()
        cat.components.add(catalive)

        self.assertTrue(cat.alive)

        cat.die()
        self.assertFalse(cat.alive)
        self.assertFalse(catalive.alive)

        cat.components.remove(catalive)

        with self.assertRaises(AttributeError):
            cat.resurrect()
コード例 #5
0
    def setUp(self):
        self.cat = Entity()
        self.cat.components |= set([Alive(), Portable(), Container()])

        self.plant = Entity()
        self.plant.components.add(Alive())

        self.bathtub = Entity()
        self.bathtub.components.add(Container())

        self.brains = Entity()
        self.brains.components |= set([Portable(), Location()])

        self.zombie = Entity()
        self.zombie.components |= set([Moveable(), Location(), Container()])

        self.entities = set(
            [self.cat, self.plant, self.bathtub, self.brains, self.zombie])
コード例 #6
0
ファイル: test_world.py プロジェクト: pathunstrom/braga
    def test_destroy_entity_raises_if_not_entity(self):
        unrelated_entity = Entity()
        self.assertNotIn(unrelated_entity, self.world.entities)

        with self.assertRaises(ValueError) as e:
            self.world.destroy_entity(unrelated_entity)

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

        self.assertEqual(
            exception_message,
            "{0} does not contain {1}".format(repr(self.world),
                                              repr(unrelated_entity)))
コード例 #7
0
ファイル: test_assemblage.py プロジェクト: pathunstrom/braga
class TestAssemblage(unittest.TestCase):
    def setUp(self):
        self.world = World()

        self.human = Entity()
        self.human.components.add(Container())

        self.food = Entity()
        self.food.components.add(Portable())

    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 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)

    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)

    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)

    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)

    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)

    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")
コード例 #8
0
ファイル: test_entity.py プロジェクト: EricSchles/braga
    def test_get_component(self):
        cat = Entity()
        catalive = Alive()
        cat.components.add(catalive)

        self.assertEqual(cat.get_component(Alive), catalive)
コード例 #9
0
ファイル: test_entity.py プロジェクト: EricSchles/braga
    def test_has_component(self):
        cat = Entity()

        catalive = Alive()
        cat.components.add(catalive)
        self.assertTrue(cat.has_component(Alive))
コード例 #10
0
ファイル: test_assemblage.py プロジェクト: EricSchles/braga
class TestAssemblage(unittest.TestCase):

    def setUp(self):
        self.world = World()

        self.human = Entity()
        self.human.components.add(Container())

        self.food = Entity()
        self.food.components.add(Portable())

    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 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)

    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)

    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)

    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)

    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)

    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")