Example #1
0
def test_king_takes_rook():
    """test_of_a_bug_it_was_possible_to_put_a_king_that_takes_a_rook"""
    king = King()
    board = Board(3, 3)
    board.put(king, 0, 0)
    positions_to_take = king.positions_to_take(board, 0, 0)
    assert (0, 1) in positions_to_take
Example #2
0
def test_king_invalid_move():
    """test_king_cant_move_more_than_one_step"""
    king_piece = King()
    small_board = Board(3, 3)
    small_board.put(king_piece, 0, 0)
    assert king_piece.takes(small_board, 0, 0, 2, 2) is False
    assert king_piece.takes(small_board, 0, 0, 0, 2) is False
Example #3
0
def test_complete_returns_true():
    """ test_complete_returns_true_when_all_pieces_are_in_the_board """

    king = King()
    board = Board(3, 3)
    board.put(king, 0, 0)

    assert board.complete([King()]) is True
Example #4
0
def test_used_king_ok():
    """test_positions_used_from_for_king_in_the_upper_corner_are_valid"""
    king_piece = King()
    small_board = Board(3, 3)
    assert (0, 0) in king_piece.positions_to_take(small_board, 0, 0)
    assert (1, 0) in king_piece.positions_to_take(small_board, 0, 0)
    assert (1, 1) in king_piece.positions_to_take(small_board, 0, 0)
    assert (0, 1) in king_piece.positions_to_take(small_board, 0, 0)
    assert (0, 2) not in king_piece.positions_to_take(small_board, 0, 0)
Example #5
0
def test_equal_order():
    """ test_equal_with_the_same_pieces_at_same_positions_added_in_different_order"""
    king = King()
    king_2 = King()
    board = Board(3, 3)
    board.put(king, 0, 0)
    board.put(king_2, 2, 2)
    another_board = Board(3, 3)
    another_board.put(king_2, 2, 2)
    another_board.put(king, 0, 0)
    assert another_board == board
    assert hash(another_board) == hash(board)
Example #6
0
def test_copy_is_ok():
    """ test_copy_method_copies_every_piece_and_dimension_are_correct """
    king = King()
    king_2 = King()
    board = Board(3, 3)
    board.put(king, 0, 0)
    board.put(king_2, 2, 2)

    another_board = board.copy()

    assert another_board == board
    assert set(another_board.pieces.keys()) == set(board.pieces.keys())
    assert set(another_board.pieces.values()) == set(board.pieces.values())
    assert another_board.n == another_board.n
    assert another_board.m == another_board.m
Example #7
0
def test_example_test_case_given():
    """
        This test case was given as an example.
        The assert were done manually before the to_json method was done.
        To make checks easily see: test_with_data which uses a "fuzzer"
        case generator to verify results.
    """
    expected = [{
        'pieces': {
            '(2, 0)': 'K',
            '(1, 2)': 'R',
            '(0, 0)': 'K'
        },
        'm': 3,
        'n': 3
    }, {
        'pieces': {
            '(0, 2)': 'K',
            '(2, 1)': 'R',
            '(0, 0)': 'K'
        },
        'm': 3,
        'n': 3
    }, {
        'pieces': {
            '(0, 1)': 'R',
            '(2, 0)': 'K',
            '(2, 2)': 'K'
        },
        'm': 3,
        'n': 3
    }, {
        'pieces': {
            '(0, 2)': 'K',
            '(1, 0)': 'R',
            '(2, 2)': 'K'
        },
        'm': 3,
        'n': 3
    }]
    pieces = [King(), King(), Rook()]
    board = Board(3, 3)
    res = []
    for board in backtracking(board, pieces.copy(), pieces, set()):
        res.append(json.loads(board.to_json()))
    assert len(expected) == len(res)
    for expected_res in expected:
        assert expected_res in res
Example #8
0
def test_clean_is_ok():
    """ test_clean_when_there_is_a_piece_in_the_position """
    king = King()
    board = Board(3, 3)
    board.put(king, 0, 0)
    board.clean(0, 0)
    assert board.pieces == {}
Example #9
0
def test_free_with_king():
    """test_free_with_a_king_in_the_board_happy_path """
    king = King()
    board = Board(3, 3)
    board.put(king, 0, 0)
    assert (0, 0) not in board.free_positions()
    assert (0, 1) not in board.free_positions()
    assert (2, 2) in board.free_positions()
Example #10
0
def test_equal_same_positions():
    """ test_equal_with_the_same_pieces_at_same_positions """
    king = King()
    board = Board(3, 3)
    board.put(king, 0, 0)
    another_board = Board(3, 3)
    another_board.put(king, 0, 0)
    assert another_board == board
    assert another_board == board
    assert hash(another_board) == hash(board)
Example #11
0
def test_from_json_two_pieces():
    """test_from_json_with_a_board_with_two_pieces"""
    king = King()
    rook = Rook()
    expected_board = Board(3, 4)
    expected_board.put(king, 1, 1)
    expected_board.put(rook, 2, 2)
    res = Board.from_json(
        '{"m": 4, "pieces": {"(2, 2)": "R", "(1, 1)": "K"}, "n": 3}')
    assert res == expected_board
Example #12
0
def test_pieces_positions_check():
    """
        docstrings should comply to pep257 for every public class
        and method and module function (except from tests)
    """
    king = King()
    board = Board(3, 3)
    board.put(king, 1, 1)

    assert [position for position in board.piece_positions()] == [(1, 1)]
Example #13
0
def test_to_json_two_pieces():
    """test_to_json_with_a_board_with_two_pieces"""
    king = King()
    rook = Rook()
    board = Board(3, 4)
    board.put(king, 1, 1)
    board.put(rook, 2, 2)
    res = json.loads(board.to_json())
    expected = json.loads(
        '{"m": 4, "pieces": {"(2, 2)": "R", "(1, 1)": "K"}, "n": 3}')
    assert res == expected
Example #14
0
def test_equals_different_board_2():
    """ test_equals_on_different_board_different_piece_in_same_position """
    king = King()
    bishop = Bishop()
    board = Board(3, 3)
    board.put(king, 0, 0)

    another_board = Board(3, 3)
    another_board.put(bishop, 0, 0)

    assert another_board != board
    assert hash(another_board) != hash(board)
Example #15
0
def test_equal_on_different_board():
    """
        docstrings should comply to pep257 for every public class
        and method and module function (except from tests)
    """
    king = King()
    board = Board(3, 3)
    board.put(king, 0, 0)

    another_board = Board(3, 3)
    another_board.put(king, 2, 2)

    assert another_board != board
    assert hash(another_board) != hash(board)
Example #16
0
def test_free_with_pieces():
    """test_free_with_a_king_in_the_board_happy_path """
    king = King()
    queen = Queen()
    board = Board(7, 7)
    assert len(board.free_positions()) == 49
    board.put(king, 5, 5)
    assert len(board.free_positions()) == 40
    board.clean(5, 5)
    assert len(board.free_positions()) == 49
    board.put(queen, 4, 3)
    assert len(board.free_positions()) == 26
    board.clean(4, 3)
    assert len(board.free_positions()) == 49
Example #17
0
def test_very_simple():
    """
        A board with one position available.
        test_very_simple_1x1_board_with_one_piece
    """
    expected = [{'pieces': {'(0, 0)': 'K'}, 'n': 1, 'm': 1}]
    board = Board(1, 1)
    pieces = [King()]
    board.put(pieces[0], 0, 0)
    res = []
    for board in backtracking(board, pieces, pieces, set()):
        res.append(json.loads(board.to_json()))

    assert res == expected
Example #18
0
def test_draw_a_board_with_one_king():
    """
        test draw with one king.
        checks that the king is drawn in the correct pos
    """
    expected = "┌──────┐\n"
    expected += "│K _ _ │\n"
    expected += "│_ _ _ │\n"
    expected += "│_ _ _ │\n"
    expected += "└──────┘\n\n"
    board = Board(3, 3)
    king = King()
    board.put(king, 0, 0)
    res = draw_board(board)
    assert res == expected
Example #19
0
def test_king_right_corner():
    """
        Checks that the king is drawn in the corner.
        Edge case test.
    """
    expected = "┌──────┐\n"
    expected += "│_ _ _ │\n"
    expected += "│_ _ _ │\n"
    expected += "│_ _ K │\n"
    expected += "└──────┘\n\n"
    board = Board(3, 3)
    king = King()
    board.put(king, 2, 2)
    res = draw_board(board)
    assert res == expected
Example #20
0
def test_example_one():
    """
        Case used in one example.
    """
    expected = "┌──────┐\n"
    expected += "│K _ K │\n"
    expected += "│_ _ _ │\n"
    expected += "│_ R _ │\n"
    expected += "└──────┘\n\n"
    board = Board(3, 3)
    king = King()
    rook = Rook()
    board.put(king, 0, 0)
    board.put(king, 0, 2)
    board.put(rook, 2, 1)
    res = draw_board(board)
    assert res == expected
Example #21
0
def test_king_takes_happy_cases():
    """
        The King can move anywhere but only by one step.
        This test asserts that the function returns True for all valid cases
    """

    king_piece = King()
    small_board = Board(3, 3)
    small_board.put(king_piece, 1, 1)
    assert king_piece.takes(small_board, 1, 1, 0, 0)
    assert king_piece.takes(small_board, 1, 1, 0, 1)
    assert king_piece.takes(small_board, 1, 1, 0, 2)
    assert king_piece.takes(small_board, 1, 1, 1, 0)
    assert king_piece.takes(small_board, 1, 1, 1, 1)
    assert king_piece.takes(small_board, 1, 1, 1, 2)
    assert king_piece.takes(small_board, 1, 1, 2, 0)
    assert king_piece.takes(small_board, 1, 1, 2, 0)
    assert king_piece.takes(small_board, 1, 1, 2, 1)
    assert king_piece.takes(small_board, 1, 1, 2, 2)
Example #22
0
def test_complete_returns_false():
    """ test_complete_returns_false_when_pieces_are_missing"""
    board = Board(3, 3)
    assert board.complete([King()]) is False
Example #23
0
def test_pieces__indistingueable():
    """test_pieces_of_the_same_type_are_indistingueable"""
    king = King()
    king_2 = King()
    assert king == king_2
Example #24
0
def test_pieces_distingueable():
    """test_pieces_of_different_type_are_distingueable"""
    king = King()
    rook = Rook()
    assert king != rook
Example #25
0
def test_identification_k():
    """test_king_identification_is_K"""
    piece = King()
    assert piece.piece_identification == 'K'