import json


def parse_expected(answer_file):
    """
    Parse parameters and answers from the expected file

    :param answer_file: file path of the expected file
    :type answer_file: string or path-like
    :return: test information for each of the tests in the answer file
    :rtype: list of dictionaries
    """
    line_types = [
        'description',
        'thread_mapping',
        'cpu_mapping',
        'gpu_mapping'
    ]
    test_info = []
    cur_answer = dict()
    type_idx = 0
    with open(answer_file, 'r') as f:
        for line in f.readlines():
            line = line.strip() 
            if not line:
                continue
            if line[0] == '#' and 'params' not in line:
                continue
            
            if 'params' in line:
                json_string = line.replace('# ', '')
                cur_answer['params'] = json.loads(json_string)['params']
            else:
                cur_answer[line_types[type_idx]] = line.replace('"', '')
                type_idx += 1

            if type_idx == 4:
                test_info.append(cur_answer)
                type_idx = 0
                cur_answer = dict()

    return test_info