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_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 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 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 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)
class ExpelliarmusSkill(Component): """Ability to cast expelliarmus, stores skill at casting expelliarmus. For players.""" __slots__ = ['skill'] def __init__(self): self.skill = 0 ##################################### # Define systems to manage components ##################################### container_system = System() equipment_system = System() @container_system def move(thing, new_container): if not thing.has_component(Moveable): raise ValueError("You cannot move this item") if not new_container.has_component(Container): raise ValueError("Invalid destination") old_container = thing.location thing.location = new_container if old_container: old_container.inventory.remove(thing) new_container.inventory.add(thing)