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)
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() self.world.add_system(duel.ContainerSystem) self.world.add_system(duel.EquipmentSystem) room = self.world.make_entity(duel.room_factory, name="duel room", description="test room") self.player = self.world.make_entity( duel.player_factory, name="you", description="You are a test", location=room) self.wand = self.world.make_entity( duel.wand_factory, description="Surprisingly swishy.", location=self.player, name="your wand", owner=self.player) self.other_wand = self.world.make_entity( duel.wand_factory, description="Yep, that Elder Wand", location=room, name="elder wand") self.world.systems[duel.EquipmentSystem].auto_update = True self.world.systems[duel.EquipmentSystem].equip(self.player, self.wand) self.world.refresh()
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
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)
def test_child_methods_look_for_before_hooks(self, callback_mock): world = World() system = GoodSystem(world) world.subscribe(system, 'child_method', before_child_method_runs, before=True) system.child_method('first_arg', kwarg_two='keyword_arg') callback_mock.assert_called_once_with(system, 'first_arg', kwarg_two='keyword_arg')
def setUp(): # Make World duel_world = World() duel_world.add_system(duel.ContainerSystem) duel_world.add_system(duel.EquipmentSystem) duel_world.add_system(NameSystem) # Make room duel_room = duel_world.make_entity( duel.room_factory, description="You are in a large, dusty room, standing at one end of a long wooden table. Someone has placed a sign on an easel that says 'Duelling club'. There is a door in the southwest corner.", name='Duel Room' ) # Make duellers player = duel_world.make_entity( duel.player_factory, description="You stare trepidously down the table at Justin Finch-Fletchley.", location=duel_room, name="you") justin = duel_world.make_entity( duel.player_factory, description="Justin Finch-Fletchley stares at you bullishly from the other end of the table.", location=duel_room, name="justin finch-fletchley") # Make wands player_wand = duel_world.make_entity( duel.wand_factory, description="Surprisingly swishy.", location=player, name="your wand", owner=player) justin_wand = duel_world.make_entity( duel.wand_factory, description="Heavy but brittle.", location=justin, name="justin's wand", owner=justin) duel_world.systems[duel.EquipmentSystem].equip(player, player_wand) duel_world.systems[duel.EquipmentSystem].equip(justin, justin_wand) duel_world.systems[duel.EquipmentSystem].update() duel_world.systems[NameSystem].update() duel_world.systems[duel.ContainerSystem].update() parser = Parser(duel_world, duel_world.systems[NameSystem], player, commands) parser.name_system.add_name('you', player) parser.name_system.add_name("my wand", player_wand) parser.name_system.add_name("his wand", justin_wand) parser.name_system.add_name("justin's wand", justin_wand) parser.name_system.add_name("justin", justin) return parser
class TestEquipmentSystem(unittest.TestCase): 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 test_player_has_no_equipment(self): self.assertFalse(hasattr(self.player, 'wand')) def test_nonbearer_item_cannot_equip_equipment(self): second_wand = self.world.make_entity(self.wand_factory) with self.assertRaises(ValueError) as e: duel.equipment_system.equip(self.wand, second_wand) self.assertEqual(get_exception_message(e.exception), "That cannot equip other items") def test_player_equips_an_item(self): duel.equipment_system.equip(self.player, self.wand) self.assertEqual(self.player.wand, self.wand) def test_player_cannot_equip_two_items(self): """ In other minigames, you will be allowed to equip an arbitrary number of items, but that is not necessary for the duel simulator. """ duel.equipment_system.equip(self.player, self.wand) second_wand = self.world.make_entity(self.wand_factory) with self.assertRaises(ValueError) as e: duel.equipment_system.equip(self.player, second_wand) self.assertEqual(get_exception_message(e.exception), "You are already equipping an item of this type") self.assertEqual(self.player.wand, self.wand) def test_unequipping_an_item(self): duel.equipment_system.equip(self.player, self.wand) duel.equipment_system.unequip(self.player, self.wand) self.assertFalse(hasattr(self.player, 'wand')) def test_unequipping_and_reequipping_an_item(self): duel.equipment_system.equip(self.player, self.wand) duel.equipment_system.unequip(self.player, self.wand) second_wand = self.world.make_entity(self.wand_factory) duel.equipment_system.equip(self.player, second_wand) self.assertEqual(self.player.wand, second_wand)
class TestEquipmentSystem(unittest.TestCase): 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) self.equipment_system = duel.EquipmentSystem(world=self.world, auto_update=True) def test_player_has_no_equipment(self): self.assertFalse(hasattr(self.player, 'wand')) def test_nonbearer_item_cannot_equip_equipment(self): second_wand = self.world.make_entity(self.wand_factory) with self.assertRaises(ValueError) as e: self.equipment_system.equip(self.wand, second_wand) self.assertEqual(e.exception.message, "That cannot equip other items") def test_player_equips_an_item(self): self.equipment_system.equip(self.player, self.wand) self.assertEqual(self.player.wand, self.wand) def test_player_cannot_equip_two_items(self): """ In other minigames, you will be allowed to equip an arbitrary number of items, but that is not necessary for the duel simulator. """ self.equipment_system.equip(self.player, self.wand) second_wand = self.world.make_entity(self.wand_factory) with self.assertRaises(ValueError) as e: self.equipment_system.equip(self.player, second_wand) self.assertEqual(e.exception.message, "You cannot equip that at this time") self.assertEqual(self.player.wand, self.wand) def test_unequipping_an_item(self): self.equipment_system.equip(self.player, self.wand) self.equipment_system.unequip(self.player, self.wand) self.assertFalse(hasattr(self.player, 'wand')) def test_unequipping_and_reequipping_an_item(self): self.equipment_system.equip(self.player, self.wand) self.equipment_system.unequip(self.player, self.wand) second_wand = self.world.make_entity(self.wand_factory) self.equipment_system.equip(self.player, second_wand) self.assertEqual(self.player.wand, second_wand)
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 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_child_methods_look_for_after_hooks(self, callback_mock): world = World() system = System(world) @system def child_method(arg_one, kwarg_two=False): pass world.subscribe(system, 'child_method', after_child_method_runs, after=True) system.child_method('first_arg', kwarg_two='keyword_arg') callback_mock.assert_called_once_with('first_arg', kwarg_two='keyword_arg')
def setUp(self): self.world = World() self.human = Entity() self.human.components.add(Container()) self.food = Entity() self.food.components.add(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) self.equipment_system = duel.EquipmentSystem(world=self.world, auto_update=True)
class TestBasicNameSystem(unittest.TestCase): 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) def test_entity_retrievable_from_name(self): entity = self.name_system.get_token_from_name('item one') self.assertEqual(entity, self.item) def test_unknown_name_raises(self): with self.assertRaises(ValueError) as e: self.assertIsNone(self.name_system.get_token_from_name('asdfdsa')) self.assertEqual(e.exception.message, "I don't know what you're talking about") def test_aliases_can_be_created(self): self.name_system.add_name('cool item', self.item) self.assertIn('cool item', self.name_system.names.keys()) self.assertEqual(self.name_system.names['cool item'], [self.item]) def test_no_duplicate_name_entity_pairs_can_be_added(self): with self.assertRaises(ValueError) as e: self.name_system.add_name('item one', self.item) self.assertEqual(e.exception.message, 'Duplicate entity names') def test_names_in_tokens_property(self): self.assertEqual(self.name_system.tokens, self.name_system.names.keys()) def test_retrieving_unspecific_name_without_context_raises(self): other_item = self.world.make_entity(self.item_factory, name='other item') self.name_system.add_name('item one', other_item) with self.assertRaises(ValueError) as e: self.name_system.get_token_from_name('item one') self.assertEqual(e.exception.message, "For now I can't handle confusion")
def test_child_methods_are_decorated(self): world = World() system = System(world) @system def child_method(arg_one, kwarg_two=False): pass self.assertIn('run_hooks', system.child_method.__code__.co_names)
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)
class TestExpelliarmusHelperCommands(unittest.TestCase): def setUp(self): self.world = World() self.world.add_system(duel.ContainerSystem) self.world.add_system(duel.EquipmentSystem) self.world.add_system(NameSystem) room = self.world.make_entity(duel.room_factory, name="duel room", description="test room") self.player = self.world.make_entity( duel.player_factory, name="you", description="You are a test", location=room) self.wand = self.world.make_entity( duel.wand_factory, description="Surprisingly swishy.", location=self.player, name="your wand", owner=self.player) self.world.systems[duel.EquipmentSystem].equip(self.player, self.wand) self.world.refresh() def test_set(self): try: set_expelliarmus_skill(self.world, self.player, 10) except (StateError, LogicError, ParserError): self.fail("correct usage of `set` should not raise an error") self.assertEqual(self.player.skill, 10) def test_set_non_integer_skill(self): initial_skill = self.player.skill with self.assertRaises(LogicError): set_expelliarmus_skill(self.world, self.player, self.wand) self.assertEqual(self.player.skill, initial_skill) def test_set_out_of_range_skill(self): initial_skill = self.player.skill with self.assertRaises(LogicError): set_expelliarmus_skill(self.world, self.player, 1000000) self.assertEqual(self.player.skill, initial_skill) def test_get(self): try: get_expelliarmus_skill(self.world, self.player) except (StateError, LogicError, ParserError): self.fail("correct usage of `get` should not raise an error")
class TestEquipCommand(unittest.TestCase): def setUp(self): self.world = World() self.world.add_system(duel.ContainerSystem) self.world.add_system(duel.EquipmentSystem) room = self.world.make_entity(duel.room_factory, name="duel room", description="test room") self.player = self.world.make_entity( duel.player_factory, name="you", description="You are a test", location=room) self.wand = self.world.make_entity( duel.wand_factory, description="Surprisingly swishy.", location=self.player, name="your wand", owner=self.player) self.other_wand = self.world.make_entity( duel.wand_factory, description="Yep, that Elder Wand", location=room, name="elder wand") self.world.systems[duel.EquipmentSystem].auto_update = True self.world.systems[duel.EquipmentSystem].equip(self.player, self.wand) self.world.refresh() def test_can_equip_wand_in_inventory(self): self.assertEqual(self.player.wand, self.wand) self.world.systems[duel.ContainerSystem].move(self.other_wand, self.player, True) try: equip(self.world, self.player, self.other_wand) except LogicError: self.fail("correct syntax for `equip` should not raise an error") self.assertEqual(self.player.wand, self.other_wand) # self.assertIsNone(self.wand.bearer) self.assertEqual(self.player, self.wand.owner) # self.assertEqual(self.player, self.other_wand.bearer) self.assertIsNone(self.other_wand.owner) def test_cannot_equip_wand_not_in_inventory(self): self.assertEqual(self.player.wand, self.wand) with self.assertRaises(LogicError) as e: equip(self.world, self.player, self.other_wand) self.assertEqual(e.exception.message, "You are not carrying that.") self.assertEqual(self.player.wand, self.wand)
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 setUp(self): world = World() self.player = world.make_entity(Assemblage(components=[Name, Description]), name='you') self.wand = world.make_entity(Assemblage(components=[Name, Description]), name='wand') self.fluff = world.make_entity(Assemblage(components=[Name, Description]), name='fluff') self.cotton_candy = world.make_entity(Assemblage(components=[Name, Description]), name='cotton candy') world.add_system(NameSystem) self.command = Command( name='take', syntax=[lambda x, y: True], response=lambda x, y: 'Congratulations you took your wand') commands = {'take': self.command, 'get': self.command} self.parser = Parser(world, world.systems[NameSystem], self.player, commands)
def setUp(self): self.world = World()
class TestWorld(unittest.TestCase): def setUp(self): self.world = World() def test_make_entity_without_assemblage_makes_entity(self): new_entity = self.world.make_entity() self.assertTrue(isinstance(new_entity, Entity)) self.assertIn(new_entity, self.world.entities) self.assertEqual(new_entity.components, set()) 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)) 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_destroy_entity_removes_entity_from_world(self): new_entity = self.world.make_entity() self.assertIn(new_entity, self.world.entities) self.world.destroy_entity(new_entity) self.assertNotIn(new_entity, self.world.entities) 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))) def test_add_system_registers_system(self): self.skipTest('Functionality soon to be removed') new_system = self.world.add_system(SomeKindOfSystem) self.assertTrue(isinstance(new_system, System)) self.assertEqual(new_system, self.world.systems[SomeKindOfSystem]) self.assertEqual(new_system.world, self.world) def test_add_system_rejects_non_systems(self): self.skipTest('Functionality soon to be removed') with self.assertRaises(ValueError) as e: self.world.add_system(Assemblage) self.assertEqual( e.exception.message, "{} is not a type of System".format(Assemblage.__name__)) def test_add_system_rejects_duplicate_systems(self): self.skipTest('Functionality soon to be removed') self.world.add_system(SomeKindOfSystem) with self.assertRaises(ValueError) as e: self.world.add_system(SomeKindOfSystem) self.assertEqual( e.exception.message, "World already contains a System of type {}".format( repr(SomeKindOfSystem))) def test_can_subscribe_functions_to_systems(self): def check_to_run_before_method(system, thing): pass def check_to_run_after_method(system, thing): pass some_kind_of_system = System(self.world) self.world.subscribe(some_kind_of_system, 'do_something', check_to_run_before_method, before=True) self.world.subscribe(some_kind_of_system, 'do_something', check_to_run_after_method, after=True) self.assertIn( check_to_run_before_method, self.world.subscriptions[some_kind_of_system]['do_something'] ['before']) self.assertIn( check_to_run_after_method, self.world.subscriptions[some_kind_of_system]['do_something'] ['after']) def test_cannot_subscribe_non_callables_to_systems(self): class Foo(object): pass foo = Foo() some_kind_of_system = System(self.world) for thing in ['string', [], {}, foo]: with self.assertRaises(TypeError): self.world.subscribe(some_kind_of_system, 'do_something', thing, after=True) def test_must_choose_a_time_for_callback_to_be_called(self): def callback_method(*args): pass some_kind_of_system = System(self.world) with self.assertRaises(ValueError): self.world.subscribe(some_kind_of_system, 'do_something', callback_method)
def setUp(): # Make World duel_world = World() duel_world.add_system(duel.ContainerSystem) duel_world.add_system(duel.EquipmentSystem) duel_world.add_system(NameSystem) # Make room duel_room = duel_world.make_entity( duel.room_factory, description= "You are in a large, dusty room, standing at one end of a long wooden table. Someone has placed a sign on an easel that says 'Duelling club'. There is a door in the southwest corner.", name='Duel Room') # Make duellers player = duel_world.make_entity( duel.player_factory, description= "You stare trepidously down the table at Justin Finch-Fletchley.", location=duel_room, name="you") justin = duel_world.make_entity( duel.player_factory, description= "Justin Finch-Fletchley stares at you bullishly from the other end of the table.", location=duel_room, name="justin finch-fletchley") # Make wands player_wand = duel_world.make_entity(duel.wand_factory, description="Surprisingly swishy.", location=player, name="your wand", owner=player) justin_wand = duel_world.make_entity(duel.wand_factory, description="Heavy but brittle.", location=justin, name="justin's wand", owner=justin) duel_world.systems[duel.EquipmentSystem].equip(player, player_wand) duel_world.systems[duel.EquipmentSystem].equip(justin, justin_wand) duel_world.systems[duel.EquipmentSystem].update() duel_world.systems[NameSystem].update() duel_world.systems[duel.ContainerSystem].update() parser = Parser(duel_world, duel_world.systems[NameSystem], player, commands) parser.name_system.add_name('you', player) parser.name_system.add_name("my wand", player_wand) parser.name_system.add_name("his wand", justin_wand) parser.name_system.add_name("justin's wand", justin_wand) parser.name_system.add_name("justin", justin) return parser
class TestWorld(unittest.TestCase): def setUp(self): self.world = World() def test_make_entity_without_assemblage_makes_entity(self): new_entity = self.world.make_entity() self.assertTrue(isinstance(new_entity, Entity)) self.assertIn(new_entity, self.world.entities) self.assertEqual(new_entity.components, set()) 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)) 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_destroy_entity_removes_entity_from_world(self): new_entity = self.world.make_entity() self.assertIn(new_entity, self.world.entities) self.world.destroy_entity(new_entity) self.assertNotIn(new_entity, self.world.entities) 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) self.assertEqual(e.exception.message, "{0} does not contain {1}".format(repr(self.world), repr(unrelated_entity))) def test_add_system_registers_system(self): new_system = self.world.add_system(SomeKindOfSystem) self.assertTrue(isinstance(new_system, System)) self.assertEqual(new_system, self.world.systems[SomeKindOfSystem]) self.assertEqual(new_system.world, self.world) def test_add_system_rejects_non_systems(self): with self.assertRaises(ValueError) as e: self.world.add_system(Assemblage) self.assertEqual(e.exception.message, "{} is not a type of System".format(Assemblage.__name__)) def test_add_system_rejects_duplicate_systems(self): self.world.add_system(SomeKindOfSystem) with self.assertRaises(ValueError) as e: self.world.add_system(SomeKindOfSystem) self.assertEqual(e.exception.message, "World already contains a System of type {}".format(repr(SomeKindOfSystem)))
class TestSignals(unittest.TestCase): def setUp(self): self.world = World() self.door = self.world.make_entity() self.oiled_door = self.world.make_entity() self.room = self.world.make_entity() def creaking_door(self, door_uuid): if door_uuid == self.oiled_door.uuid: return "The door silently glides open" else: return "The door's rusty hinges make a horrible screeching noise." def test_subscribe_adds_callback(self): self.world.subscribe("door opens", self.creaking_door) self.assertEqual(len(self.world.subscriptions["door opens"]), 1) def test_unsubscribe_removes_callback(self): self.world.subscribe("door opens", self.creaking_door) self.world.unsubscribe("door opens", self.creaking_door) self.assertEqual(len(self.world.subscriptions["door opens"]), 0) def test_publish_calls_callback(self): self.world.subscribe("door opens", self.creaking_door) extra_output = self.world.publish("door opens", self.door.uuid) self.assertIn("horrible screeching noise", extra_output) extra_output = self.world.publish("door opens", self.oiled_door.uuid) self.assertIn("silently glides", extra_output)
def setUp(self): self.world = World() self.door = self.world.make_entity() self.oiled_door = self.world.make_entity() self.room = self.world.make_entity()
class TestTradeCommand(unittest.TestCase): def setUp(self): self.world = World() self.world.add_system(duel.ContainerSystem) self.world.add_system(duel.EquipmentSystem) self.world.add_system(NameSystem) room = self.world.make_entity(duel.room_factory, name="duel room", description="test room") self.player = self.world.make_entity(duel.player_factory, name="you", description="You are a test", location=room) self.opponent = self.world.make_entity(duel.player_factory, name="them", description="They are a test opponent", location=room) self.wand = self.world.make_entity( duel.wand_factory, description="Surprisingly swishy.", location=self.player, name="your wand", owner=self.player) self.other_wand = self.world.make_entity( duel.wand_factory, description="Yep, that Elder Wand", location=room, name="elder wand") self.world.systems[duel.ContainerSystem].auto_update = True self.world.systems[duel.EquipmentSystem].auto_update = True self.world.systems[duel.ContainerSystem].move(self.wand, self.player) self.world.systems[duel.EquipmentSystem].equip(self.player, self.wand) self.world.systems[duel.ContainerSystem].move(self.other_wand, self.opponent) self.world.systems[duel.EquipmentSystem].equip(self.opponent, self.other_wand) self.world.refresh() def test_command_execute_with_correct_syntax(self): try: trade_wands(self.world, self.player, self.opponent) except LogicError: self.fail("That is correct syntax") self.assertIn(self.other_wand, self.player.inventory) self.assertEqual(self.wand.bearer, self.opponent) self.assertNotIn(self.wand, self.player.inventory) self.assertNotIn(self.other_wand, self.opponent.inventory) self.assertEqual(self.other_wand.bearer, self.player) self.assertIn(self.wand, self.opponent.inventory)
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)
class TestExpelliarmusCommand(unittest.TestCase): def setUp(self): self.world = World() self.world.add_system(duel.ContainerSystem) self.world.add_system(duel.EquipmentSystem) self.world.add_system(NameSystem) room = self.world.make_entity(duel.room_factory, name="duel room", description="test room") self.player = self.world.make_entity(duel.player_factory, name="you", description="You are a test", location=room) self.opponent = self.world.make_entity(duel.player_factory, name="them", description="They are a test opponent", location=room) self.wand = self.world.make_entity( duel.wand_factory, description="Surprisingly swishy.", location=self.player, name="your wand", owner=self.player) self.other_wand = self.world.make_entity( duel.wand_factory, description="Yep, that Elder Wand", location=room, name="elder wand") self.world.systems[duel.ContainerSystem].auto_update = True self.world.systems[duel.EquipmentSystem].auto_update = True self.world.systems[duel.ContainerSystem].move(self.wand, self.player) self.world.systems[duel.EquipmentSystem].equip(self.player, self.wand) self.world.systems[duel.ContainerSystem].move(self.other_wand, self.opponent) self.world.refresh() def test_bad_syntax(self): with self.assertRaises(LogicError) as e: expelliarmus(self.world, self.player, self.wand) self.assertEqual(e.exception.message, "You can only perform that action on other people!") def test_cannot_cast_expelliarmus_on_yourself(self): try: self.player.skill = 100 expelliarmus(self.world, self.player, self.player) except LogicError: self.fail("You can cast on yourself") def test_cannot_cast_on_wandless_player(self): with self.assertRaises(AttributeError): self.opponent.wand self.player.skill = 100 with self.assertRaises(LogicError) as e: expelliarmus(self.world, self.player, self.opponent) self.assertEqual(e.exception.message, "Nothing happens. Your opponent is not carrying their wand!") def test_wandless_player_cannot_cast_expelliarmus(self): self.world.systems[duel.EquipmentSystem].unequip(self.player, self.wand) self.world.systems[duel.EquipmentSystem].equip(self.opponent, self.other_wand) self.world.refresh() with self.assertRaises(AttributeError): self.player.wand self.assertTrue(self.opponent.wand) with self.assertRaises(LogicError) as e: expelliarmus(self.world, self.player, self.opponent) self.assertEqual(e.exception.message, "Nothing happens.") def test_setup_of_wand(self): self.assertEqual(self.player.wand.owner, self.player) self.assertEqual(self.player.wand.bearer, self.player)
class TestWorld(unittest.TestCase): def setUp(self): self.world = World() def test_make_entity_without_assemblage_makes_entity(self): new_entity = self.world.make_entity() self.assertTrue(isinstance(new_entity, Entity)) self.assertIn(new_entity, self.world.entities) self.assertEqual(new_entity.components, set()) 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)) 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_destroy_entity_removes_entity_from_world(self): new_entity = self.world.make_entity() self.assertIn(new_entity, self.world.entities) self.world.destroy_entity(new_entity) self.assertNotIn(new_entity, self.world.entities) 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) self.assertEqual(e.exception.message, "{0} does not contain {1}".format(repr(self.world), repr(unrelated_entity))) def test_add_system_registers_system(self): new_system = self.world.add_system(SomeKindOfSystem) self.assertTrue(isinstance(new_system, System)) self.assertEqual(new_system, self.world.systems[SomeKindOfSystem]) self.assertEqual(new_system.world, self.world) def test_add_system_rejects_non_systems(self): with self.assertRaises(ValueError) as e: self.world.add_system(Assemblage) self.assertEqual(e.exception.message, "{} is not a type of System".format(Assemblage.__name__)) def test_add_system_rejects_duplicate_systems(self): self.world.add_system(SomeKindOfSystem) with self.assertRaises(ValueError) as e: self.world.add_system(SomeKindOfSystem) self.assertEqual(e.exception.message, "World already contains a System of type {}".format(repr(SomeKindOfSystem))) def test_can_subscribe_functions_to_systems(self): def check_to_run_before_method(system, thing): pass def check_to_run_after_method(system, thing): pass system = self.world.add_system(SomeKindOfSystem) self.world.subscribe(system, 'do_something', check_to_run_before_method, before=True) self.world.subscribe(system, 'do_something', check_to_run_after_method, after=True) self.assertIn(check_to_run_before_method, self.world.subscriptions[system]['do_something']['before']) self.assertIn(check_to_run_after_method, self.world.subscriptions[system]['do_something']['after']) def test_cannot_subscribe_non_functions_to_systems(self): system = self.world.add_system(SomeKindOfSystem) for thing in ['string', [], {}, system]: with self.assertRaises(TypeError): self.world.subscribe(system, 'do_something', thing)