Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
    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)
Esempio n. 6
0
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")
Esempio n. 7
0
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")
Esempio n. 8
0
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)
Esempio n. 9
0
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)
Esempio n. 10
0
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)
Esempio n. 11
0
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)
Esempio n. 12
0
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)
Esempio n. 13
0
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)
Esempio n. 14
0
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)))