import re


def parse_test_interactions(test_file):
    """ Method to parse a test file, and return the necessary
        interaction information.

    :param: str test_file: Path to the file to parse.

    Test interactions are annotated as comments in the test file.
    Interaction markups should be placed one line above the expected
    interaction, and must consist of only a single line. The current
    available markups are as follows:
        - `#$ input=`: Denotes that the next line requires an input
                       to the REPL.
        - `#$ output=`: Denotes what the next line's output in the
                        REPL should be.
        - `#$ verify=`: Denotes a function that exists inside RosiePi
                        to use for verification. The function should
                        be prefixed with the module that contains it.

    For example:
    ... code: python

        #$ input=4
        result = input()

        #$ output=4
        print(result)

        with digitalio.DigitalInOut(board.D0) as mypin:
            #$ verify=pin_tests.assert_pin_high
            mypin.switch_to_output(value=True)
            #$ input=\r\n
            input() # only proceed if previous verify passed
    """
    has_action = re.compile(r"^\#\$\s(input|output|verify)\=(.+$)")
    interactions = {}
    with open(test_file, 'r') as file:
        for line_no, line in enumerate(file.readlines(), start=1):
            check_line = has_action.match(line)
            if check_line:
                # interaction key should be the line after the marker
                # so add 1 to the current line number
                interactions[(line_no + 1)] = {"action": check_line.group(1),
                                               "value": check_line.group(2)}
            else:
                if line.startswith("#$"):
                    exc_msg = [
                        "Improper interaction syntax on",
                        f"line {line_no} in '{test_file}'",
                    ]
                    raise SyntaxWarning(" ".join(exc_msg))
    #print(interactions)
    return interactions