Exemplo n.º 1
0
def test_gamer_wins(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO happy Pascal 3x3".encode("utf-8"))
    game = init_game(sock, "boring Hawking")

    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "MOVE A 2".encode("utf-8"))
    set_stdin_value(mocker, "A 1")
    make_step(sock, game)

    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "MOVE B 1".encode("utf-8"))
    set_stdin_value(mocker, "B 2")
    make_step(sock, game)

    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "MOVE C 2".encode("utf-8"))
    set_stdin_value(mocker, "C 3")
    make_step(sock, game)

    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "STOP O".encode("utf-8"))
    game_over = make_step(sock, game)

    assert game_over
    assert game.check_winner() == "O"
Exemplo n.º 2
0
def test_enemy_send_wrong_command(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO happy Pascal 1x1".encode("utf-8"))
    game = init_game(sock, "boring Hawking")

    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "OMG A 1".encode("utf-8"))

    with pytest.raises(WrongCommand):
        make_step(sock, game)
Exemplo n.º 3
0
def test_enemy_send_wrong_command(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO boring Hawking".encode("utf-8"))
    game = init_game(sock, 3, "happy Pascal")

    set_stdin_value(mocker, "A 2")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "WRONG_MOVE A 1".encode("utf-8"))

    with pytest.raises(WrongCommand):
        make_step(sock, game)
Exemplo n.º 4
0
def test_game_correctly_ends(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO happy Pascal 1x1".encode("utf-8"))
    game = init_game(sock, "boring Hawking")

    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "MOVE A 1 STOP X".encode("utf-8"))
    game_over = make_step(sock, game)
    sock.close()

    assert game_over
    assert game.check_winner() == "X"
Exemplo n.º 5
0
def test_game_starts_incorrectly(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "OMG happy Pascal 3x3".encode("utf-8"))

    with pytest.raises(WrongCommand):
        init_game(sock, "boring Hawking")

    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO happy Pascal ??".encode("utf-8"))

    with pytest.raises(ValueError):
        init_game(sock, "boring Hawking")
Exemplo n.º 6
0
def test_game_starts_incorrectly(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HEY! boring Hawking".encode("utf-8"))

    with pytest.raises(WrongCommand):
        init_game(sock, 3, "happy Pascal")
        sock.close()
Exemplo n.º 7
0
def test_game_initializes(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO boring Hawking".encode("utf-8"))

    try:
        init_game(sock, 3, "happy Pascal")
    except WrongCommand:
        pytest.fail()
    finally:
        sock.close()
Exemplo n.º 8
0
def test_game_initializes(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO happy Pascal 3x3".encode("utf-8"))

    try:
        game = init_game(sock, "boring Hawking")
    except WrongCommand:
        pytest.fail()
        return

    assert game.field_size == 3
    assert game.enemy_name == "happy Pascal"
Exemplo n.º 9
0
def test_game_correctly_ends(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO boring Hawking".encode("utf-8"))
    game = init_game(sock, 1, "happy Pascal")

    sendall_spy = mocker.spy(sock, "sendall")
    set_stdin_value(mocker, "A 1")
    make_step(sock, game)
    sock.close()

    assert game.check_winner() == "X"
    sendall_spy.assert_called_once_with("MOVE A 1 STOP X".encode("utf-8"))
Exemplo n.º 10
0
def test_enemy_wins(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO boring Hawking".encode("utf-8"))
    game = init_game(sock, 3, "happy Pascal")

    set_stdin_value(mocker, "A 2")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "MOVE A 1".encode("utf-8"))
    make_step(sock, game)

    set_stdin_value(mocker, "B 1")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "MOVE B 2".encode("utf-8"))
    make_step(sock, game)

    set_stdin_value(mocker, "C 2")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "MOVE C 3".encode("utf-8"))
    sendall_spy = mocker.spy(sock, "sendall")
    make_step(sock, game)

    sendall_spy.assert_called_with("STOP O".encode("utf-8"))
Exemplo n.º 11
0
def test_game_continues_after_step(mocker: MockFixture):
    mock_socket_cls = mocker.patch("socket.socket")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "HELO boring Hawking".encode("utf-8"))
    game = init_game(sock, 2, "happy Pascal")
    sock.close()

    set_stdin_value(mocker, "A 1")
    sock = get_socket_with_mocked_recv(mock_socket_cls,
                                       "MOVE B 1".encode("utf-8"))
    game_over = make_step(sock, game)
    sock.close()

    assert not game_over

    set_stdin_value(mocker, "A 2")
    sock = mock_socket_cls()
    sendall_spy = mocker.spy(sock, "sendall")
    game_over = make_step(sock, game)
    sock.close()

    assert game_over
    sendall_spy.assert_called_once_with("MOVE A 2 STOP X".encode("utf-8"))