class TestEventDispatching(object): """ Tests for event dispatching relating to moving """ def __init__(self): """ Default constructor """ super(TestEventDispatching, self).__init__() self.model = None self.character = None self.level = None self.listener = None def setup(self): """ Setup test case """ self.model = Model() action_factory = (ActionFactoryBuilder() .with_move_factory() .build()) self.character = (CharacterBuilder() .with_action_factory(action_factory) .with_model(self.model) .with_location((10, 10)) .build()) self.level = (LevelBuilder() .with_character(self.character) .build()) self.listener = EventListener() self.model.register_event_listener(self.listener) def test_event_is_relayed(self): """ Test that moving will create an event and send it forward """ self.character.move(3) assert_that(len(self.listener.events), is_(equal_to(1))) def test_affected_tiles_are_marked(self): """ Test that moving marks tiles for redrawing """ expected_redraws = [(10, 10), (10, 11)] self.character.move(5) event = self.listener.events[0] assert_that(event, has_marked_for_redrawing(expected_redraws))
def test_player_moving_updates_whole_screen(self): """ Test that moving player character reports whole screen updated """ model = Model() surface = mock() player = (CharacterBuilder() .with_model(model) .as_player_character() .with_action_factory(ActionFactoryBuilder() .with_model(model) .with_move_factory()) .with_name('player') .with_location((11, 11)) .build()) monster = (CharacterBuilder() .with_model(model) .with_action_factory(ActionFactoryBuilder() .with_model(model) .with_move_factory()) .with_name('rat') .with_location((5, 5)) .build()) level = (LevelBuilder() .with_character(player) .with_character(monster) .build()) application = Application() application.world = model game_gui = GameArea(application = application, surface_manager = mock(), decorate = False, width = 800, height = 600) game_gui.old_location = player.location model.register_event_listener(game_gui) player.move(7) rects = game_gui.update(surface) assert_that(rects, has_items(Rect(0, 0, 800, 600)))
def test_updating_screen_while_monster_moves(self): """ Test that only needed spots are reported as updated as a monster moves """ model = Model() surface = mock() player = (CharacterBuilder() .with_model(model) .as_player_character() .with_name('player') .with_location((11, 11)) .build()) monster = (CharacterBuilder() .with_model(model) .with_action_factory(ActionFactoryBuilder() .with_model(model) .with_move_factory()) .with_name('rat') .with_location((5, 5)) .build()) level = (LevelBuilder() .with_character(player) .with_character(monster) .build()) application = Application() application.world = model game_gui = GameArea(application = application, surface_manager = mock(), decorate = False, width = 800, height = 600) game_gui.old_location = player.location model.register_event_listener(game_gui) game_gui.paint(surface) monster.move(7) rects = game_gui.update(surface) assert_that(rects, has_items(Rect(160, 88, 192, 120), Rect(192, 88, 224, 120)))
class TestEventDispatching(): """ Tests for event dispatching relating to moving """ def __init__(self): """ Default constructor """ super().__init__() self.model = None self.character = None self.level = None self.listener = None self.actions = None def setup(self): """ Setup test case """ self.model = Model() self.character = (CharacterBuilder() .with_model(self.model) .with_location((1, 1)) .build()) self.model.dungeon = (LevelBuilder() .with_character(self.character) .build()) self.listener = mock() self.model.register_event_listener(self.listener) set_action_factory(ActionFactoryBuilder() .build()) def test_event_is_relayed(self): """ Test that moving will create an event and send it forward """ pyherc.vtable['\ufdd0:move'](character=self.character, direction=Direction.east) verify(self.listener).receive_event(EventType('move'))
class TestModel(): """ Class for testing Model """ def __init__(self): """ Default constructor """ super(TestModel, self).__init__() self.model = None self.listener = None self.level = None def setup(self): """ Setup test cases """ self.model = Model() self.listener = mock() self.level = LevelBuilder().build() self.model.register_event_listener(self.listener) def test_registering_event_listener(self): """ Test that event listener can be registered on the model """ assert_that(self.model, has_event_listener(self.listener)) def test_dispatching_event_to_listeners(self): """ Test that events are dispatched to listeners """ event = new_move_event(character = (CharacterBuilder() .with_model(self.model) .build()), old_location = (5, 5), old_level = self.level, direction = 1) self.model.raise_event(event) verify(self.listener).receive_event(event)
class TestModel(): """ Class for testing Model """ def __init__(self): """ Default constructor """ super(TestModel, self).__init__() self.model = None self.listener = None self.level = None def setup(self): """ Setup test cases """ self.model = Model() self.listener = mock() self.level = LevelBuilder().build() self.model.register_event_listener(self.listener) def test_registering_event_listener(self): """ Test that event listener can be registered on the model """ assert_that(self.model, has_event_listener(self.listener)) def test_dispatching_event_to_listeners(self): """ Test that events are dispatched to listeners """ event = new_move_event(character=(CharacterBuilder().with_model( self.model).build()), old_location=(5, 5), old_level=self.level, direction=1) self.model.raise_event(event) verify(self.listener).receive_event(event)
class TestEventDispatching(): """ Tests for event dispatching relating to moving """ def __init__(self): """ Default constructor """ super().__init__() self.model = None self.character = None self.level = None self.listener = None self.actions = None def setup(self): """ Setup test case """ self.model = Model() self.character = (CharacterBuilder().with_model( self.model).with_location((1, 1)).build()) self.model.dungeon = (LevelBuilder().with_character( self.character).build()) self.listener = mock() self.model.register_event_listener(self.listener) set_action_factory(ActionFactoryBuilder().build()) def test_event_is_relayed(self): """ Test that moving will create an event and send it forward """ pyherc.vtable['\ufdd0:move'](character=self.character, direction=Direction.east) verify(self.listener).receive_event(EventType('move'))