def test_state_dec_instant_eval(): state = State("student_code", "", "", None, None, {}, {}, Reporter()) @state_dec def stu_code(state, x='x'): return state.student_code + x assert stu_code(state) == "student_codex"
def test_initial_state(): State(student_code={'script.py': '1'}, solution_code={'script.py': '1'}, reporter=Reporter(), pre_exercise_code="", student_result="", solution_result="", student_conn=None, solution_conn=None, ast_dispatcher=Dispatcher(DUMMY_NODES, ParseHey()))
def has_dir(state: State, path, msg="Did you create a directory `{}`?"): """Test whether a directory exists. Args: state: State instance describing student and solution code. Can be omitted if used with Ex(). path: expected location of the directory msg: feedback message if no directory is found in the expected location :Example: To check if a user created the subdirectory ``resources`` in the directory where the exercise is run, use this SCT:: Ex().has_dir("resources") """ if not Path(path).is_dir(): state.report(msg.format(path)) return state
def state(): return State( student_code="", solution_code="", reporter=Reporter(), # args below should be ignored pre_exercise_code="NA", student_result={'a': [1]}, solution_result={'b': [2]}, student_conn=None, solution_conn=None)
def check_file( state: State, path, missing_msg="Did you create the file `{}`?", is_dir_msg="Want to check the file `{}`, but found a directory.", parse=True, solution_code=None, ): """Test whether file exists, and make its contents the student code. Args: state: State instance describing student and solution code. Can be omitted if used with Ex(). path: expected location of the file missing_msg: feedback message if no file is found in the expected location is_dir_msg: feedback message if the path is a directory instead of a file parse: If ``True`` (the default) the content of the file is interpreted as code in the main exercise technology. This enables more checks on the content of the file. solution_code: this argument can be used to pass the expected code for the file so it can be used by subsequent checks. Note: This SCT fails if the file is a directory. :Example: To check if a user created the file ``my_output.txt`` in the subdirectory ``resources`` of the directory where the exercise is run, use this SCT:: Ex().check_file("resources/my_output.txt", parse=False) """ path_obj = Path(path) if not path_obj.exists(): state.report(missing_msg.format(path)) # test file exists if path_obj.is_dir(): state.report(is_dir_msg.format(path)) # test its not a dir code = get_file_content(path_obj) sol_kwargs = {"solution_code": solution_code, "solution_ast": None} if solution_code: solution_ast = False if parse: with debugger(state): solution_ast = state.parse(solution_code) sol_kwargs["solution_ast"] = solution_ast child_state = state.to_child( append_message="We checked the file `{}`. ".format(path), student_code=code, student_ast=state.parse(code) if parse else False, **sol_kwargs) child_state.path = path_obj # .parent + .name return child_state
def state(): return State( student_code="", solution_code="", reporter=Reporter(), # args below should be ignored pre_exercise_code="NA", student_result="", solution_result="", student_conn=None, solution_conn=None, ast_dispatcher=Dispatcher(DUMMY_NODES, ParseHey()))
def state(): return State( # only Reporter and Dispatcher are used student_code="", solution_code="", reporter=Reporter(), pre_exercise_code="", student_result="", solution_result="", student_conn=None, solution_conn=None, )
def state(): return State( # only Reporter and Dispatcher are used student_code="", solution_code="", reporter=Reporter(), pre_exercise_code="", student_result="", solution_result="", student_conn=None, solution_conn=None, ast_dispatcher=Dispatcher(ast.AST, DUMMY_NODES, ParseHey()), )
def prepare_state(student_code): return State( student_code=student_code, reporter=Reporter(), # args below should be ignored solution_code="NA", pre_exercise_code="NA", solution_ast="NA", student_ast="NA", student_result=[], solution_result=[], student_conn=None, solution_conn=None)
def code_state(): return State( student_code={ 'script1.py': '1 + 1', 'script2.py': '2 + 2' }, solution_code={ 'script1.py': '3 + 3', 'script2.py': '4 + 4' }, reporter=Reporter(), # args below should be ignored pre_exercise_code="NA", student_result="", solution_result="", student_conn=None, solution_conn=None, ast_dispatcher=Dispatcher(DUMMY_NODES, ParseHey()))
def state(): return State("student_code", "", "", None, None, {}, {}, Reporter())