def test_sends_a_json_representation_of_the_game_to_be_saved(save_game_json_data): persister_io = MockPersisterIO() persister = Persister(persister_io) board = Board(['X', 'O', 'X', 'O', '5', '6', '7', '8', '9']) player_1 = SimpleComputerPlayer('Player 1', 'O') player_2 = SimpleComputerPlayer('Player 2', 'X') game = Game(player_1, player_2, board) persister.save('myfile.txt', game) assert persister_io.saved_data == save_game_json_data
def test_if_save_data_exists_new_save_is_added_with_an_incremented_save_id(save_game_json_data): persister_io = MockPersisterIO(save_game_json_data) persister = Persister(persister_io) board = Board(['X', 'O', 'X', 'O', '5', '6', '7', '8', '9']) player_1 = SimpleComputerPlayer('Player 1', 'O') player_2 = SimpleComputerPlayer('Player 2', 'X') game = Game(player_1, player_2, board) persister.save('myfile.txt', game) updated_data = json.loads(persister_io.saved_data) assert len(updated_data) == 2 assert '2' in list(updated_data.keys())
def test_converts_a_json_representation_of_the_game_with_the_passed_id_into_a_config_object(): board_state = ['X', 'O', 'X', 'O', '5', '6', '7', '8', '9'] current_player = SimpleComputerPlayer('Player 1', 'O') opponent = SimpleComputerPlayer('Player 2', 'X') save_id = '1' saved_game_json_data = create_json_object_for_save_data(board_state, current_player, opponent, save_id) persister_io = MockPersisterIO(saved_game_json_data) persister = Persister(persister_io) config = persister.load('myfile.txt', save_id) assert config.board.get_spaces() == ['X', 'O', 'X', 'O', '5', '6', '7', '8', '9'] assert config.first_player.get_name() == "Player 1" assert config.second_player.get_name() == "Player 2"
def test_a_game_which_is_stopped_while_in_progress_gets_saved(): player_1 = MockPlayer('player 1', 'O', []) player_2 = MockPlayer('player 2', 'X', []) io = MockConsoleIO([]) persister_io = MockPersisterIO() persister = Persister(persister_io) game_runner = GameRunner(Console(io), persister) game_runner._game = Game(player_1, player_2) game_runner.stop() assert persister_io.saved_data is not None
def test_user_can_choose_to_load_an_existing_game(save_game_json_data): inputs = ['1', '2', '1'] persister_io = MockPersisterIO(save_game_json_data) persister = Persister(persister_io) console = MockConsole(inputs) menu = Menu(console, persister=persister) config = menu.start() assert config.first_player.get_name() == "Player 1" assert config.second_player.get_name() == "Player 2" assert config.board.get_spaces() == [ 'X', 'O', 'X', 'O', '5', '6', '7', '8', '9' ]
def test_a_game_which_is_stopped_but_not_in_progress_does_not_get_saved(): player_1 = MockPlayer('player 1', 'O', [0, 1, 2]) player_2 = MockPlayer('player 2', 'X', [3, 4]) io = MockConsoleIO([]) persister_io = MockPersisterIO() persister = Persister(persister_io) game_runner = GameRunner(Console(io), persister) config = Config(player_1, player_2, Board()) game_runner.run(config) game_runner.stop() assert persister_io.saved_data is None
def test_user_will_continue_to_be_prompted_if_they_provide_an_invalid_save_id( save_game_json_data): inputs = ['1', '2', '2', '5', '1'] persister_io = MockPersisterIO(save_game_json_data) persister = Persister(persister_io) console = MockConsole(inputs) menu = Menu(console, persister=persister) config = menu.start() assert config.first_player.get_name() == "Player 1" assert config.second_player.get_name() == "Player 2" assert config.board.get_spaces() == [ 'X', 'O', 'X', 'O', '5', '6', '7', '8', '9' ]
def __init__(self, console, game_config=GameConfig, persister=Persister()): self._console = console self._game_config = game_config(console) self._persister = persister self._server = None