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_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_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_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_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_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 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_can_validate_the_a_move(self): test_validator = Validator() move = "8" assert test_validator.is_valid_move(move) == True
def test_that_it_can_validate_the_user_turn_order(self): test_validator = Validator() turn_order = "1" assert test_validator.is_valid_turn_order(turn_order) == True
def test_that_it_can_validate_the_user_icon(self): test_validator = Validator() icon = "x" assert test_validator.is_valid_icon(icon) == True
def test_game_params(self): return { 'messages': CliMessages(), 'game_input': Input(), 'validator': Validator(), }
def test_that_it_can_validate_the_user_name(self): test_validator = Validator() name = "foo" assert test_validator.is_valid_name(name) == True