Ejemplo n.º 1
0
def test_creates_a_player_when_provided_a_valid_type():
    console = Console(MockConsoleIO())
    factory = PlayerFactory()

    player = factory.create('HumanPlayer', 'Player 1', 'X', console)

    assert player is not None
Ejemplo n.º 2
0
def test_user_can_reverse_the_order_of_turns():
    menu = Menu(
        Console(MockConsoleIO(['1', '1', '1', 'X', '', '2', 'O', '', '2'])))

    config = menu.start()

    assert config.first_player.get_name() == 'Player 2'
Ejemplo n.º 3
0
def test_on_first_turn_outputs_a_message_with_no_previous_move(game):
    console_io = MockConsoleIO()
    console = Console(console_io)

    game.play_turn(console)

    assert console_io.last_output == "\nplayer 1's turn."
Ejemplo n.º 4
0
def test_can_play_a_full_game():
    app.consoleio = MockConsoleIO(['1', '1', '1', '', '', '1', '', '', '1', '1', '5', '2', '3', '7', '4', '6', '8', '9', 'n'])
    app.console = Console(app.consoleio)
    app.menu = Menu(app.console)

    app.main()

    assert app.consoleio.last_output == end_game_message()
Ejemplo n.º 5
0
def test_exits_the_game():
    io = MockConsoleIO([])
    console = Console(io)
    menu = Menu(console)

    menu.exit()

    assert io.last_output == end_game_message()
Ejemplo n.º 6
0
def test_displays_the_board_and_a_message_on_game_over(players):
    game_over_board = Board(['X', 'X', 'X', ' ', ' ', ' ', ' ', ' ', ' '])
    game = Game(players[0], players[1], game_over_board)
    console_io = MockConsoleIO()
    console = Console(console_io)
    game.game_over(console)

    assert console_io.print_output_call_count == 2
Ejemplo n.º 7
0
def test_a_completed_game_is_no_longer_in_progress(players):
    game_over_board = Board(['X', 'X', 'X', ' ', ' ', ' ', ' ', ' ', ' '])
    game = Game(players[0], players[1], game_over_board)
    console_io = MockConsoleIO()
    console = Console(console_io)
    game.game_over(console)

    assert not game.in_progress()
Ejemplo n.º 8
0
def test_current_player_reverts_to_player_one_after_player_two_turn(
        game, players):
    current_player_on_turn_three = players[0].get_name()
    console = Console(MockConsoleIO(['1', '0']))
    game.play_turn(console)
    game.play_turn(console)

    assert game.get_current_player_name() == current_player_on_turn_three
Ejemplo n.º 9
0
def test_on_subsequent_turns_outputs_a_message_with_previous_move(game):
    console_io = MockConsoleIO()
    console = Console(console_io)

    game.play_turn(console)
    game.play_turn(console)

    assert console_io.last_output == "\nplayer 2's turn. player 1 chose space 1"
Ejemplo n.º 10
0
def test_user_cannot_select_the_same_marker_as_their_opponent():
    menu = Menu(
        Console(MockConsoleIO(['1', '1', '1', 'X', '', '2', 'X', 'O', '',
                               '1'])))

    config = menu.start()

    assert config.second_player.get_marker() == 'O'
Ejemplo n.º 11
0
def test_a_player_will_retain_their_default_marker_if_they_provide_an_empty_string(
):
    menu = Menu(
        Console(MockConsoleIO(['1', '1', '1', '', '', '1', '', '', '1'])))

    config = menu.start()

    assert config.first_player.get_marker() == 'O'
Ejemplo n.º 12
0
def test_throws_an_error_if_provided_a_non_valid_type():
    with pytest.raises(ValueError) as err:
        console = Console(MockConsoleIO())
        factory = PlayerFactory()

        factory.create('invalidType', 'Player 1', 'X', console)

    assert "invalidType is not a valid player type" in str(err.value)
Ejemplo n.º 13
0
def test_if_player_one_selects_x_as_marker_player_two_default_marker_changes_to_o(
):
    menu = Menu(
        Console(MockConsoleIO(['1', '1', '1', 'X', '', '1', '', '', '1',
                               'n'])))

    config = menu.start()

    assert config.second_player.get_marker() == 'O'
Ejemplo n.º 14
0
def test_user_has_the_option_not_to_play_again():
    io = MockConsoleIO(['n'])
    console = Console(io)
    menu = Menu(console)

    play_again = menu.play_again()

    assert io.last_output == end_game_message()
    assert not play_again
Ejemplo n.º 15
0
def test_user_has_the_option_to_play_again():
    io = MockConsoleIO(['y'])
    console = Console(io)
    menu = Menu(console)

    play_again = menu.play_again()

    assert io.last_output == play_again_message()
    assert play_again
Ejemplo n.º 16
0
def test_a_player_will_continue_to_be_prompted_if_they_provide_whitespace_as_a_marker(
):
    menu = Menu(
        Console(
            MockConsoleIO(
                ['1', '1', '1', ' ', ' ', '   ', '!', '', '1', '', '', '1'])))

    config = menu.start()

    assert config.first_player.get_marker() == '!'
Ejemplo n.º 17
0
def test_can_play_a_full_game():
    io = MockConsoleIO([])
    game_runner = GameRunner(Console(io))
    player_1 = MockPlayer('player 1', 'O', [0, 1, 6, 5, 8])
    player_2 = MockPlayer('player 2', 'X', [4, 2, 3, 7])
    config = Config(player_1, player_2, Board())

    game_runner.run(config)

    assert io.last_output == game_tied_message()
Ejemplo n.º 18
0
def test_game_ends_if_a_player_wins():
    io = MockConsoleIO([])
    game_runner = GameRunner(Console(io))
    player_1 = MockPlayer('player 1', 'O', [0, 2, 4, 6])
    player_2 = MockPlayer('player 2', 'X', [1, 3, 5])
    config = Config(player_1, player_2, Board())

    game_runner.run(config)

    assert "player 1 won!" in io.last_output
Ejemplo n.º 19
0
def test_send_an_encoded_string_to_the_socket():
    console_io = MockConsoleIO(['1'])
    console = Console(console_io)
    client = TTTClient(console)

    client.start('127.0.0.1', MockSocket)
    client._socket.received_data = [b'1\n\nMessage', b'Board\n\nMessage']

    client.play()

    assert client._socket.sent_data == b'1'
Ejemplo n.º 20
0
def test_will_continue_to_prompt_if_provided_non_integer_input():
    console_io = MockConsoleIO(['not an integer', '!', '1'])
    console = Console(console_io)
    client = TTTClient(console)

    client.start('127.0.0.1', MockSocket)
    client._socket.received_data = [b'1\n\nMessage', b'Board\n\nMessage']

    client.play()

    assert client._socket.sent_data == b'1'
Ejemplo n.º 21
0
def test_prints_received_data_while_game_is_in_progress():
    console_io = MockConsoleIO(['1'])
    console = Console(console_io)
    client = TTTClient(console)

    client.start('127.0.0.1', MockSocket)
    client._socket.received_data = [b'Board\n\nMessage']

    client.play()

    assert console_io.last_output == "Board\n\nMessage"
Ejemplo n.º 22
0
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
Ejemplo n.º 23
0
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
Ejemplo n.º 24
0
def test_creates_and_sends_a_string_representing_current_game_state_on_each_turn(
):
    expected_data_string = " X\x1b[0m  | X\x1b[0m  | 3\x1b[0m  \n--------------\n 4\x1b[0m  | 5\x1b[0m  | 6\x1b[0m  \n--------------\n 7\x1b[0m  | 8\x1b[0m  | 9\x1b[0m  \n\n\nPlayer 1's turn."
    console_io = MockConsoleIO()
    console = Console(console_io)
    runner = NetworkedGameRunner(console)

    player_1 = MockPlayer('Player 1', 'X', inputs=[2])
    player_2 = MockPlayer('Player 2', 'O')
    board = Board(['X', 'X', '3', '4', '5', '6', '7', '8', '9'])
    server = MockServer()

    config = Config(first_player=player_1,
                    second_player=player_2,
                    board=board,
                    server=server)

    runner.run(config)

    assert server.sent_data == expected_data_string
Ejemplo n.º 25
0
 def __init__(self, io=PersisterIO(), game_config=GameConfig(Console())):
     self._io = io
     self._game_config = game_config
Ejemplo n.º 26
0
 def __init__(self, console=Console()):
     self._socket = None
     self._console = console
     self._turns = 0
     self._last_board_state = None
 def __init__(self, console=Console(), game_class=Game):
     self._console = console
     self._game_class = game_class
     self._game = None
     self._server = None
Ejemplo n.º 28
0
def test_current_player_switches_to_player_two_after_player_one_turn(
        game, players):
    current_player_after_switch = players[1].get_name()
    game.play_turn(Console(MockConsoleIO(['1'])))

    assert game.get_current_player_name() == current_player_after_switch
Ejemplo n.º 29
0
def test_play_turn_occupies_a_space_on_the_board(game):
    available_space_count = len(game.available_spaces())
    game.play_turn(Console(MockConsoleIO(['1'])))

    assert len(game.available_spaces()) == available_space_count - 1
Ejemplo n.º 30
0
def test_playing_a_turn_prompts_player_for_a_move(game, players):
    game.play_turn(Console(MockConsoleIO()))

    assert players[0].get_move_call_count == 1