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")
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 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)
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
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 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_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_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 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)
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)
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_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")
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))
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)
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)
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_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'])
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)
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]))
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':
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')
def setUp(self): self.world, self.name_system, self.player, _, __ = make_toy_world() self.there = self.world.make_entity(Assemblage(components=[Name]), name='there')
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)