from pathlib import Path


def get_cases(f, sep="---"): 
    """
    Extracts inputs and outputs for each test/verification case within f, where f is a folder.

    Params
    ======
    f: str
        The folder containing the cases to be extracted.
    sep: str
        The substring separating comments from the input from the output in each case file. 

    Returns 
    =======
    cases: []
        Array of dictionaries containing each case

        Each case is a dictionary with the following fields:
         - "filename": The name of the file
         - "comments": Any comments in the folder
         - "inputs": The inputs
         - "outputs": The expected outputs

    Raises
    ======
    AssertionError:
        If the given path is not a folder.
    """
    # Initialise path
    p = Path(f)

    # Assert that target folder is a folder
    assert p.is_dir()

    # List of cases in the folder
    cases = []

    # Loop through all cases within the folder
    for f in p.iterdir():

        # Open each case file
        with open(f) as fr:

            # Obtain the contents of the case file
            contents = fr.read()

            # The case files are structured such that it has COMMENTS, followed by the separator substring, followed by the INPUTS, followed by the separator substring, and finally followed by the OUTPUTS

            # Instantiate case dictionary
            c = {}

            # Separate the contents by the separator, and then clean each individual element of newline/whitespace
            contents = contents.split(sep)
            contents = [c.strip() for c in contents]

            # Populate dictionary
            c["filename"] = f.with_suffix("").name
            c["inputs"] = contents[0]
            c["outputs"] = contents[1]

            if len(contents) == 3:
                c["comments"] = contents[2]

            # Add dictionary to list of cases
            cases.append(c)

    # After all cases have been looped through, return cases
    return cases