def test_with(index, context_vals=False, # whether to check number of context vals context_tests=None, # check on context expressions body=None, undefined_msg=None, context_vals_len_msg=None, context_vals_msg=None, expand_message=True, state=None): """Test a with statement. with open_file('...') as bla: [ open_file('...').__enter__() ] with open_file('...') as file: [ ] """ rep = Reporter.active_reporter rep.set_tag("fun", "test_with") check_with = partial(check_node, 'withs', index-1, "`with` statement", MSG_MISSING, state=state) child = check_with(MSG_PREPEND if expand_message else "") child2 = check_with(MSG_PREPEND2 if expand_message else "") quiet_child = quiet(1, child) if context_vals: # test num context vars ---- too_many = len(child.student_parts['context']) > len(child.solution_parts['context']) if too_many: _msg = child.build_message(MSG_NUM_CTXT) rep.do_test(Test(Feedback(_msg, child.student_tree))) # test context var names ---- for i in range(len(child.solution_parts['context'])): ctxt_state = check_part_index('context', i, "", state=child) has_equal_part('target_vars', MSG_CTXT_NAMES, state=ctxt_state) # Context sub tests ---- if context_tests and not isinstance(context_tests, list): context_tests = [context_tests] for i, context_test in enumerate(context_tests or []): # partial the substate check, because the function uses two prepended messages check_context = partial(check_part_index, 'context', i, "%s context"%utils.get_ord(i+1), MSG_NUM_CTXT2) check_context(state=child) # test exist ctxt_state = check_context(state=child2) # sub tests multi(context_test, state=ctxt_state) # Body sub tests ---- if body is not None: body_state = check_part('body', 'body', state=child2) with_context(body, state=body_state)
def test_args(arg_names, arg_defaults, nb_args_msg, arg_names_msg, arg_defaults_msg, child, quiet_child): if arg_names or arg_defaults: # test number of args has_equal_part_len('_spec1_args', nb_args_msg or MSG_NUM_ARGS, state=quiet_child) # iterate over each arg, testing name and default for ii in range(len(child.solution_parts['_spec1_args'])): # get argument state arg_state = check_part_index('_spec1_args', ii, 'argument', "NO MISSING MSG", state=child) # test exact name has_equal_part('name', arg_names_msg or MSG_BAD_ARG_NAME, arg_state) if arg_defaults: # test whether is default has_equal_part('is_default', arg_defaults_msg or MSG_BAD_DEFAULT, arg_state) # test default value, use if to prevent running a process no default if arg_state.solution_parts['is_default']: has_equal_value(arg_defaults_msg or MSG_INC_DEFAULT, "error message", state = arg_state) # test *args and **kwargs if child.solution_parts['*args']: vararg = check_part('*args', "", missing_msg = MSG_NO_VARARG, state = child) has_equal_part('name', MSG_VARARG_NAME, vararg) if child.solution_parts['**kwargs']: kwarg = check_part('**kwargs', "", missing_msg = MSG_NO_KWARGS, state = child) has_equal_part('name', MSG_KWARG_NAME, kwarg)
def test_args(arg_names, arg_defaults, nb_args_msg, arg_names_msg, arg_defaults_msg, child, quiet_child): if arg_names or arg_defaults: # test number of args has_equal_part_len('_spec1_args', nb_args_msg or MSG_NUM_ARGS, state=quiet_child) # iterate over each arg, testing name and default for ii in range(len(child.solution_parts['_spec1_args'])): # get argument state arg_state = check_part_index('_spec1_args', ii, 'argument', "NO MISSING MSG", expand_msg="", state=child) # test exact name has_equal_part('name', arg_names_msg or MSG_BAD_ARG_NAME, arg_state) if arg_defaults: # test whether is default has_equal_part('is_default', arg_defaults_msg or MSG_BAD_DEFAULT, arg_state) # test default value, use if to prevent running a process no default if arg_state.solution_parts['is_default']: has_equal_value(incorrect_msg = arg_defaults_msg or MSG_INC_DEFAULT, error_msg="error message", append=True, state=arg_state) # test *args and **kwargs if child.solution_parts['*args']: vararg = check_part('*args', "", missing_msg=MSG_NO_VARARG, expand_msg="", state=child) has_equal_part('name', MSG_VARARG_NAME, state=vararg) if child.solution_parts['**kwargs']: kwarg = check_part('**kwargs', "", missing_msg=MSG_NO_KWARGS, expand_msg="", state=child) has_equal_part('name', MSG_KWARG_NAME, state=kwarg)
def test_with( index, context_vals=False, # whether to check number of context vals context_tests=None, # check on context expressions body=None, undefined_msg=None, context_vals_len_msg=None, context_vals_msg=None, expand_message=True, state=None): """Test a with statement. with open_file('...') as bla: [ open_file('...').__enter__() ] with open_file('...') as file: [ ] """ rep = Reporter.active_reporter rep.set_tag("fun", "test_with") check_with = partial(check_node, 'withs', index - 1, "`with` statement", MSG_MISSING, state=state) child = check_with(MSG_PREPEND if expand_message else "") child2 = check_with(MSG_PREPEND2 if expand_message else "") quiet_child = quiet(1, child) if context_vals: # test num context vars ---- too_many = len(child.student_parts['context']) > len( child.solution_parts['context']) if too_many: _msg = child.build_message(MSG_NUM_CTXT) rep.do_test(Test(Feedback(_msg, child.student_tree))) # test context var names ---- for i in range(len(child.solution_parts['context'])): ctxt_state = check_part_index('context', i, "", state=child) has_equal_part('target_vars', MSG_CTXT_NAMES, state=ctxt_state) # Context sub tests ---- if context_tests and not isinstance(context_tests, list): context_tests = [context_tests] for i, context_test in enumerate(context_tests or []): # partial the substate check, because the function uses two prepended messages check_context = partial(check_part_index, 'context', i, "%s context" % utils.get_ord(i + 1), MSG_NUM_CTXT2) check_context(state=child) # test exist ctxt_state = check_context(state=child2) # sub tests multi(context_test, state=ctxt_state) # Body sub tests ---- if body is not None: body_state = check_part('body', 'body', state=child2) with_context(body, state=body_state)