def test_change_map_to_invalid_map_throws_exception(): base_map = generate_valid_map() game = BaseGame(initial_map=base_map) with pytest.raises(ValueError): game.change_map("Invalid map name.")
def test_open_menu_with_invalid_menu_name_raises_value_error(): base_map = generate_valid_map() game = BaseGame(initial_map=base_map) with pytest.raises(ValueError): game.open_menu("test")
def test_shutdown_stops_the_game(event_post_mock): base_map = generate_valid_map() quit_event = pygame.event.Event(pygame.QUIT, dict=None) game = BaseGame(initial_map=base_map) game.shutdown() event_post_mock.assert_called_with(quit_event)
def test_register_menu_adds_new_menu_to_menus(): base_menu = get_base_menu() test_menu = get_base_menu() game = BaseGame(main_menu=base_menu) game.register_menu("test menu", test_menu) assert game.menus["test menu"] is test_menu assert len(game.menus) == 2
def test_register_map_adds_new_map_to_maps(): base_map = generate_valid_map() test_map = generate_valid_map() game = BaseGame(initial_map=base_map, initial_map_name="base map") game.register_map("test map", test_map) assert game.maps["test map"] is test_map assert len(game.maps) == 2
def test_register_player_controlled_object_correctly_registers(): pco = PlayerControlledObject("sprite.txt") game = BaseGame(initial_map=generate_valid_map()) game.register_player_controlled_object(pco, 1, 2) assert game.player_controlled_objects[pco] == (1, 2) assert len(game.player_controlled_objects) == 1
def test_load_player_controlled_objects_loads_them(): game = BaseGame(initial_map=generate_valid_map()) pco1 = PlayerControlledObject("sprite.png") pco2 = PlayerControlledObject("sprite.png") load_map = Map([[pco1, pco2]], [[0, 0]], [[0, 0]], [[0, 0]], validate=False) game.load_player_controlled_objects(load_map) assert pco1 in game.player_controlled_objects assert pco2 in game.player_controlled_objects assert game.player_controlled_objects[pco1] == (0, 0) assert game.player_controlled_objects[pco2] == (1, 0)
def test_initializing_engine_with_game_loads_player_controlled_objects( event_queue): pco1 = PlayerControlledObject("sprite.png") pco2 = PlayerControlledObject("sprite.png") test_map = Map( [[pco1, pco2]], [[Mock(), Mock()]], [[Mock(), Mock()]], [[Mock(), Mock()]], validate=False, ) game = BaseGame(initial_map=test_map, camera_width=3, camera_height=1, camera_x=1, camera_y=1) engine = Engine(game=game) event_queue.return_value = [DummyEvent(pygame.QUIT)] try: engine.start() except StopIteration: # When keypresses have run out, a StopIteration exception will be thrown by mock. # This exception will be used to tell when the necessary testing is done, and to # stop the game, hence, pass. pass assert pco1 in engine.context.player_controlled_objects assert pco2 in engine.context.player_controlled_objects assert engine.context.player_controlled_objects[pco1] == (0, 0) assert engine.context.player_controlled_objects[pco2] == (1, 0)
def test_arrow_keys_can_be_used_to_interact_with_a_menu( keypress_mock, keystroke_pattern, interaction_index): keypress_mock.side_effect = generate_keypress_pattern(keystroke_pattern) mock_interaction = Mock() menu = BaseMenu(MagicMock()) menu.register_interactive_zone( 0, 0, 0, 0, Mock() if interaction_index == 1 else mock_interaction) menu.register_interactive_zone( 1, 1, 1, 1, Mock() if interaction_index == 0 else mock_interaction) game = BaseGame(menu) engine = Engine(game) try: engine.start() except StopIteration: # When keypresses have run out, a StopIteration exception will be thrown by mock. # This exception will be used to tell when the necessary testing is done, and to # stop the game, hence, pass. pass assert game.active_menu.focused_zone == interaction_index assert mock_interaction.called
def test_handle_keystrokes_calls_each_player_controlled_objects_interaction( keypress_mock): pco1 = Mock(spec=PlayerControlledObject) pco2 = Mock(spec=PlayerControlledObject) pco1.sprite_location = "sprite.png" pco2.sprite_location = "sprite.png" keypress_mock.side_effect = generate_keypress_pattern( ["w"], convert_to_keystroke=True) test_map = Map( [[pco1, pco2]], [[Mock(), Mock()]], [[Mock(), Mock()]], [[Mock(), Mock()]], validate=False, ) game = BaseGame(initial_map=test_map, camera_width=3, camera_height=1, camera_x=1, camera_y=1) engine = Engine(game=game) try: engine.start() except StopIteration: # When keypresses have run out, a StopIteration exception will be thrown by mock. # This exception will be used to tell when the necessary testing is done, and to # stop the game, hence, pass. pass assert pco1.player_interaction.called assert pco2.player_interaction.called
def test_player_character_facing_south_can_interact_with_igos(): igo = Mock(spec=InteractiveGameObject) pc = PlayerCharacter(sprite_location="sprite.png", facing_direction=PlayerCharacter.SOUTH) # Create a map of 1x2 sheets as we only need to look south. character_sheet = [[pc], [igo]] sheet = [[None], [None]] keystrokes = ["\r"] test_map = Map(sheet, character_sheet, sheet, sheet) game = BaseGame(initial_map=test_map) game.register_player_controlled_object(pc, 0, 0) pc.player_interaction(keystrokes, game) assert igo.interact.called
def test_creating_game_with_main_menu_sets_active_screen_and_menu(): menu = get_base_menu() game = BaseGame(main_menu=menu) assert game.active_screen is menu assert game.active_menu is menu assert game.maps == {}
def test_unload_active_map_sets_game_object_sprites_to_none(): go = GameObject("sprite.png") base_sheet = [[go.clone(), go.clone()], [go.clone(), go.clone()]] base_map = Map( list(base_sheet), list(base_sheet), list(base_sheet), list(base_sheet) ) game = BaseGame(initial_map=base_map) game.unload_active_map() assert base_sheet[0][0]._loaded_sprite is None assert base_sheet[0][1]._loaded_sprite is None assert base_sheet[1][0]._loaded_sprite is None assert base_sheet[1][1]._loaded_sprite is None
def test_creating_game_with_initial_map_sets_active_screen_and_maps(): test_map = generate_valid_map() game = BaseGame(initial_map=test_map, initial_map_name="test map") assert game.active_screen is test_map assert game.active_menu is None assert game.maps["test map"] is test_map
def test_load_active_maps_sets_game_object_sprites(): go = GameObject("sprite.png") base_sheet = [[go.clone(), go.clone()], [go.clone(), go.clone()]] base_map = Map( list(base_sheet), list(base_sheet), list(base_sheet), list(base_sheet) ) sprite_mock = Mock() game = BaseGame(initial_map=base_map) game.object_images["sprite.png"] = sprite_mock # Usually the engine handles calling this, but there is no # engine running so we need to call this manually. game.load_active_map() assert base_sheet[0][0].get_sprite().image is sprite_mock assert base_sheet[0][1].get_sprite().image is sprite_mock assert base_sheet[1][0].get_sprite().image is sprite_mock assert base_sheet[1][1].get_sprite().image is sprite_mock
def test_player_character_move_moves_as_documented(up, down, left, right, initial_pos, final_pos): """ Documentation from the class: "Going up always takes priority, going up or down takes priority over going left and right, and going left takes priority over going right. This means that if up and down, then the PC will move up; if up and left, then the PC will move up; if down and right, then the PC will move down; and if left and right, the PC will move left." """ pc = PlayerCharacter("sprite.png") game = BaseGame(MagicMock()) game.register_player_controlled_object(pc, *initial_pos) pc.move(game, up, down, left, right) assert game.player_controlled_objects[pc] == final_pos
def test_loading_a_game_with_no_menu_loads_the_active_map( load_map_mock, event_get_mock): event_get_mock.return_value = [DummyEvent(pygame.QUIT)] game_map = generate_valid_map() game = BaseGame(initial_map=game_map) engine = Engine(game) engine.start() assert load_map_mock.called
def test_clear_player_controlled_objects_clears_them(): game = BaseGame(initial_map=generate_valid_map()) game.register_player_controlled_object(PlayerControlledObject("sprite.png"), 0, 0) game.register_player_controlled_object(PlayerControlledObject("sprite.png"), 0, 1) game.clear_player_controlled_objects() assert game.player_controlled_objects == {}
def test_change_map_sets_active_screen(): base_map = generate_valid_map() test_map = generate_valid_map() game = BaseGame(initial_map=base_map, initial_map_name="bae map") game.register_map("test map", test_map) game.change_map("test map") assert game.active_screen is test_map
def test_open_menu_unloads_current_map(unload_map_mock): base_map = generate_valid_map() base_menu = get_base_menu() game = BaseGame(initial_map=base_map) game.register_menu("menu", base_menu) game.open_menu("menu") assert unload_map_mock.called
def test_open_menu_sets_the_menu(): base_menu = get_base_menu() game = BaseGame(main_menu=base_menu) game.register_menu("menu", base_menu) game.open_menu("menu") assert game.active_menu is base_menu assert game.active_screen is base_menu
def test_mouse_button_up_calls_interaction_in_menu(event_get_mock): event_get_mock.return_value = [ DummyEvent(pygame.MOUSEBUTTONDOWN), DummyEvent(pygame.MOUSEBUTTONUP), DummyEvent(pygame.QUIT), ] mock_interaction = Mock() menu = BaseMenu(MagicMock()) menu.register_interactive_zone(0, 0, 0, 0, mock_interaction) game = BaseGame(menu) engine = Engine(game) engine.start() assert mock_interaction.called
def test_onscreen_sprites_are_correctly_added(sprite_group_mock, event_get_mock, image_load_mock): event_get_mock.return_value = [DummyEvent(pygame.QUIT)] sprite_mock = Mock(name="sprite_mock") convert_alpha_mock = Mock() convert_alpha_mock.convert_alpha.return_value = sprite_mock image_load_mock.return_value = convert_alpha_mock go1 = GameObject("sprite.png") go2 = GameObject("sprite2.png") go3 = GameObject("sprite3.png") sheet = [ [go1.clone(), go2.clone(), go3.clone()], [go3.clone(), go1.clone(), go2.clone()], [go2.clone(), go3.clone(), go1.clone()], ] game_map = Map(list(sheet), list(sheet), list(sheet), list(sheet), validate=False) game = BaseGame(initial_map=game_map, camera_width=3, camera_height=3, camera_x=1, camera_y=1) engine = Engine(game) engine.start() call_args = sprite_group_mock.call_args # Ignore empty calls. assert all(call_arg[0].image is sprite_mock for call_arg in call_args if len(call_arg) >= 1)
def test_change_map_loads_new_player_controlled_objects_and_unloads_previous_ones(): pco1 = PlayerControlledObject("sprite.png") pco2 = PlayerControlledObject("sprite.png") pco3 = PlayerControlledObject("sprite.png") pco4 = PlayerControlledObject("sprite.png") go_mock = Mock() go_mock.sprite_location = "sprite.png" test_map = Map( [[pco1, pco2]], [[go_mock, go_mock]], [[go_mock, go_mock]], [[go_mock, go_mock]], validate=False, ) alt_map = Map( [[pco3, pco4]], [[go_mock, go_mock]], [[go_mock, go_mock]], [[go_mock, go_mock]], validate=False, ) game = BaseGame(initial_map=test_map) game.object_images["sprite.png"] = Mock() # Setup a mock to load the image as. game.register_map("alt", alt_map) game.change_map("alt") assert pco1 not in game.player_controlled_objects assert pco2 not in game.player_controlled_objects assert game.player_controlled_objects[pco3] == (0, 0) assert game.player_controlled_objects[pco4] == (1, 0) assert len(game.player_controlled_objects) == 2
def test_creating_game_without_main_screen_or_menu_throws_error(): with pytest.raises(SyntaxError): BaseGame()