Esempio n. 1
0
def test_saved_cases():
    """
        docstrings should comply to pep257 for every public class and method
        and module function (except from tests)
    """
    mapping = {'K': King, 'Q': Queen, 'R': Rook, 'B': Bishop, 'N': Knight}
    for parameter_filename in iglob(os.path.join(TEST_DATA_PATH, 'params_*')):
        case_nro = parameter_filename.split('/')[-1].split('_')[1]
        with open(parameter_filename, 'r') as parameter_file:
            input_parameters = json.loads(parameter_file.read())
            board = Board(int(input_parameters['n']),
                          int(input_parameters['m']))
            pieces = []
            for piece_type in input_parameters['pieces']:
                pieces.append(mapping[piece_type]())
            res = []
            for board in backtracking(board, pieces.copy(), pieces, set()):
                res.append(board)

        solution_filename = 'solution_{0}'.format(case_nro)
        expected = []
        with open(os.path.join(TEST_DATA_PATH, solution_filename),
                  'r') as solution_file:
            for solution in solution_file:
                expected.append(Board.from_json(solution))

        # just in case we test that all solutions are unique.
        assert len(expected) == len(res)
        for expected_res in expected:
            assert expected_res in res
Esempio n. 2
0
def load_test_case_solution(solution_filename):
    res = []
    with open(solution_filename, 'r') as solution_file:
        for solution in solution_file:
            if solution.strip('\n'):
                res.append(Board.from_json(solution))

    return res
Esempio n. 3
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