def test_that_run_returns_a_game_over_message(self, capsys): board = [ ["@", "x", "x"], ["x", "@", "x"], ["@", "x", "@"], ] test_board = Board(board) test_rules = Rules(test_board) test_game = Game(test_board, test_rules) test_game_over = GameOver() game_params = { 'messages': FakeCliMessages(), 'game_input': Input(), 'cli_output': Output(), 'validator': Validator() } human_player = Player("Player 1", "x", "foo") computer_player = Player("Computer", "@", "foo") test_game.add_player(human_player) test_game.add_player(computer_player) test_game.set_current_player(computer_player) test_game_over.run(test_game, game_params) captured = capsys.readouterr() assert captured.out.strip().split("\n")[-1] == "game has ended"
def test_valid_game_config(self): game_params = { 'messages': CliMessages(), 'cli_input': Input(), 'cli_output': Output(), 'validator': Validator() } return game_params
def test_game_params(self): game_params = { 'messages': CliMessages(), 'cli_input': FakeInput("7", "5"), 'cli_output': Output(), 'validator': Validator() } return game_params
def test_that_it_can_check_if_the_user_move_is_valid(self, monkeypatch, capsys): game_params = { 'messages': CliMessages(), 'cli_input': FakeInput("363", "7"), 'cli_output': Output(), 'validator': Validator() } name = "foo" test_input_strategy = InputStrategy() result = test_input_strategy.get_move(name, game_params) assert result == "7"
def test_that_it_can_return_a_user_move(self, monkeypatch): game_params = { 'messages': CliMessages(), 'cli_input': Input(), 'cli_output': Output(), 'validator': Validator() } test_input_strategy = InputStrategy() monkeypatch.setattr('builtins.input', lambda x: " 5 ") i = test_input_strategy.get_move("player name", game_params) assert i == "5"
def test_that_it_can_check_if_the_user_icon_is_invalid( self, test_game_config, monkeypatch, capsys): game_params = { 'messages': CliMessages(), 'cli_input': FakeInput("test", "x"), 'cli_output': Output(), 'validator': Validator() } game_params['cli_input'].reset_count() test_game_config.get_icon(game_params) captured = capsys.readouterr() assert captured.out == "\nThat was an invalid selection. Icon should be one alpha character. Please try again.\n\nYour selected icon is x\n"
def test_that_it_can_check_if_the_user_name_is_invalid( self, test_game_config, monkeypatch, capsys): game_params = { 'messages': CliMessages(), 'cli_input': FakeInput("p", "Player 1"), 'cli_output': Output(), 'validator': Validator() } game_params['cli_input'].reset_count() test_game_config.get_name(game_params) captured = capsys.readouterr() assert captured.out == "\nName should not be empty, please try again\n\nYour selected player name is Player 1\n"
def test_that_it_can_check_if_the_user_turn_is_invalid( self, test_game_config, monkeypatch, capsys): game_params = { 'messages': CliMessages(), 'cli_input': FakeInput("345", "2"), 'cli_output': Output(), 'validator': Validator() } game_params['cli_input'].reset_count() test_game_config.get_turn_order(game_params) captured = capsys.readouterr() assert captured.out == "\nThat was an invalid selection. Please enter 1 if you would like to go first or 2 if second.\n\nYour selected turn order is 2\n"
def test_that_it_can_check_if_the_user_move_is_invalid(self, monkeypatch, capsys): game_params = { 'messages': CliMessages(), 'cli_input': FakeInput("568", "4"), 'cli_output': Output(), 'validator': Validator() } game_params['cli_input'].reset_count() name = "foo" test_input_strategy = InputStrategy() test_input_strategy.get_move(name, game_params) captured = capsys.readouterr() assert captured.out == "That was an invalid move, please try again. Please select a move between 1 - 9:\nfoo selects square 4. Placing foo's move.\n"
def test_input_move(self): game_params = { 'messages': CliMessages(), 'cli_input': Input(), 'cli_output': Output(), 'validator': Validator() } board = Board() rules = Rules(board) game = Game(board, rules) strategy = FakeInputStrategy() human_player = Player("player 1", "x", strategy) assert human_player.select_move(game, game_params) == 5
def test_minimax_move(self): game_params = { 'messages': CliMessages(), 'cli_input': Input(), 'cli_output': Output(), 'validator': Validator() } board = Board() rules = Rules(board) game = Game(board, rules) strategy = FakeMinimaxStrategy() computer_player = Player("computer", "o", strategy) assert computer_player.select_move(game, game_params) == "5"
def test_that_it_returns_a_user_config_hash(self, test_game_config): game_params = { 'messages': FakeCliMessages(), 'cli_input': FakePlayerConfigInput(), 'cli_output': Output(), 'validator': FakeValidator() } expected_result = { 'name': 'foo', 'icon': 'foo', 'turn_order': 'foo', 'computer_name': 'foo', 'computer_icon': 'foo' } assert test_game_config.player_config(game_params) == expected_result
def test_that_run_returns_a_game_object_with_an_empty_board( self, test_game_config): game_params = { 'messages': FakeCliMessages(), 'cli_input': FakePlayerConfigInput(), 'cli_output': Output(), 'validator': FakeValidator() } board = Board() rules = Rules(board) game = Game(board, rules) result = test_game_config.run(game, game_params) assert result.get_board().board() == [[None, None, None], [None, None, None], [None, None, None]]
def run(self): messages = CliMessages() game_params = { 'messages': messages, 'cli_input': Input(), 'cli_output': Output(), 'validator': Validator() } board = Board() rules = Rules(board) game = Game(board, rules) game = GameConfig().run(game, game_params) game = GameLoop().run(game, game_params) GameOver().run(game, game_params)
def test_that_best_move_returns_a_move(self): game_params = { 'messages': CliMessages(), 'cli_input': FakeInput("363", "7"), 'cli_output': Output(), 'validator': Validator() } board = Board() rules = Rules(board) game = Game(board, rules) human_player = Player("Player 1", "x", "foo") computer_player = Player("Computer", "o", "foo") game.add_player(human_player) game.add_player(computer_player) game.set_current_player(computer_player) test_input_strategy = InputStrategy() assert test_input_strategy.best_move(game, game_params) == "7"
def test_that_it_prints_the_output(self, capsys): output = Output() output.display("test") captured = capsys.readouterr() assert captured.out == "test\n"