Пример #1
0
def test_pec_parsing_error():
    with pytest.raises(InstructorError):
        State(
            student_code='parses',
            solution_code='parses',
            pre_exercise_code='does not parse',
        )
Пример #2
0
def test_exercise(
    sct,
    student_code,
    solution_code,
    pre_exercise_code,
    student_process,
    solution_process,
    raw_student_output,
    ex_type,
    error,
    force_diagnose=False,
):
    """
    Point of interaction with the Python backend.
    Args:
            sct (str): The solution corectness test as a string of code.
            student_code (str): The code which is entered by the student.
            solution_code (str): The code which is in the solution.
            pre_exercise_code (str): The code which is executed pre exercise.
            student_process (Process): Process in which the student code was executed.
            solution_process (Process): Process in which the solution code was executed.
            raw_student_output (str): The output which is given by executing the student's program.
            ex_type (str): The type of the exercise.
            error (tuple): A tuple with some information on possible errors.
    Returns:
            dict: Returns dict with correct - whether the SCT passed, message - the feedback message and
              tags - the tags belonging to the SCT execution.
    """

    try:
        state = State(
            student_code=check_str(student_code),
            solution_code=check_str(solution_code),
            pre_exercise_code=check_str(pre_exercise_code),
            student_process=check_process(student_process),
            solution_process=check_process(solution_process),
            raw_student_output=check_str(raw_student_output),
            force_diagnose=force_diagnose,
            reporter=Reporter([error] if error else [])
        )

        State.root_state = state

        tree, sct_cntxt = prep_context()

        # Actually execute SCTs
        exec(sct, sct_cntxt)

        # Run remaining nodes on tree (v1 only)
        if tree:
            for test in tree.crnt_node:
                test(state)

    except TestFail as e:
        return e.payload

    return state.reporter.build_final_payload()
Пример #3
0
def test_pec_parsing_error():
    with pytest.raises(InstructorError):
        State(student_code="parses",
              solution_code="parses",
              pre_exercise_code="does not parse",
              student_process=None,
              solution_process=None,
              reporter=None,
              raw_student_output=None)
Пример #4
0
def test_exercise(sct,
                  student_code,
                  solution_code,
                  pre_exercise_code,
                  student_process,
                  solution_process,
                  raw_student_output,
                  ex_type,
                  error):
    """
    Point of interaction with the Python backend.
    Args:
            sct (str): The solution corectness test as a string of code.
            student_code (str): The code which is entered by the student.
            solution_code (str): The code which is in the solution.
            pre_exercise_code (str): The code which is executed pre exercise.
            student_process (Process): Process in which the student code was executed.
            solution_process (Process): Process in which the solution code was executed.
            raw_student_output (str): The output which is given by executing the student's program.
            ex_type (str): The type of the exercise.
            error (tuple): A tuple with some information on possible errors.
    Returns:
            dict: Returns dict with correct - whether the SCT passed, message - the feedback message and
              tags - the tags belonging to the SCT execution.
    """

    rep = Reporter(error)
    Reporter.active_reporter = rep

    try:
        state = State(
            student_code = check_str(student_code),
            solution_code = check_str(solution_code),
            pre_exercise_code = check_str(pre_exercise_code),
            student_process = check_process(student_process),
            solution_process = check_process(solution_process),
            raw_student_output = check_str(raw_student_output)
        )

        State.root_state = state

        # Populate sct context with 'old' functions (in terms of probes) and check functions.
        tree, sct_cntxt = create_test_probes(cntxt)
        sct_cntxt.update(spec_2_context)

        # Actually execute SCTs
        exec(sct, sct_cntxt)         # Spec v2 tests run immediately
        for test in tree.crnt_node:  # Spec v1 tests run after
            test(state)

    except TestFail as e:
        return e.payload

    return rep.build_final_payload()
Пример #5
0
def setup_state(stu_code="", sol_code="", pec="", pid=None):
    sol_process, stu_process, raw_stu_output, _ = run_exercise(pec,
                                                               sol_code,
                                                               stu_code,
                                                               pid=pid)

    state = State(student_code=stu_code,
                  solution_code=sol_code,
                  pre_exercise_code=pec,
                  student_process=stu_process,
                  solution_process=sol_process,
                  raw_student_output=raw_stu_output,
                  reporter=Reporter())

    State.root_state = state
    return Ex(state)
Пример #6
0
def setup_state(stu_code="", sol_code="", pec="", **kwargs):
    sol_process, stu_process, raw_stu_output, error = run_exercise(
        pec, sol_code, stu_code, **kwargs)

    state = State(
        student_code=stu_code,
        solution_code=sol_code,
        pre_exercise_code=pec,
        student_process=stu_process,
        solution_process=sol_process,
        raw_student_output=raw_stu_output,
        reporter=Reporter(errors=[error] if error else []),
    )

    State.root_state = state
    return Ex(state)
Пример #7
0
def create_state(solution_code="",
                 student_code="",
                 pre_exercise_code="",
                 student_process=None,
                 solution_process=None,
                 raw_student_output="",
                 reporter=None):
    """Create a state for testing SCTs. 
    
    Custom processes are required for SCTs that execute code (e.g. has_equal_value).
    See MockProcess above.
    """
    if reporter is None: reporter = Reporter()
    Reporter.active_reporter = reporter
    state = State(full_student_code=student_code,
                  full_solution_code=solution_code,
                  **locals())

    # only used to get converters :/
    State.root_state = state

    return state
Пример #8
0
def setup_state(stu_code = "", sol_code = "", pec = "", pid = None):

    stu_output = io.StringIO()
    with redirect_stdout(stu_output):
        stu_process = StubProcess("%s\n%s" % (pec, stu_code), pid)

    sol_output = io.StringIO()
    with redirect_stdout(sol_output):
        sol_process = StubProcess("%s\n%s" % (pec, sol_code), pid)

    rep = Reporter()
    Reporter.active_reporter = rep

    state = State(
        student_code = stu_code,
        solution_code = sol_code,
        pre_exercise_code = pec,
        student_process = stu_process,
        solution_process = sol_process,
        raw_student_output = stu_output.getvalue())

    State.root_state = state
    return(Ex(state))
Пример #9
0
def parsesWithoutError(s):
    try:
        State.parse_internal(s)
    except:
        pytest.fail("Parsing failed")
Пример #10
0
def test_parses_without_error(script):
    State.parse_internal(script)