def section(*args): ''' TODO: Deprecate? ''' _setup_assertions(MAIN_REPORT) def wrap(f): """ Args: f: Returns: """ _add_phase(phase_name, _handle_entry) MAIN_REPORT['assertions']['phases'].append((section_number, f)) return f section_number = -1 if len(args) >= 1 and callable(args[0]): if len(args) >= 2: section_number = args[1] return wrap(args[0]) elif len(args) >= 1: section_number = args[0] return wrap
def stop_on_failure(f): """ Args: f: Returns: """ _setup_assertions(MAIN_REPORT) @wraps(f) def wrapped(*args, **kwargs): """ Args: *args: **kwargs: Returns: """ old_exception_state = MAIN_REPORT['assertions']['exceptions'] MAIN_REPORT['assertions']['exceptions'] = True value = None try: value = f(*args, **kwargs) except AssertionException: pass MAIN_REPORT['assertions']['exceptions'] = old_exception_state return value return wrapped
def try_all(f): """ Args: f: Returns: """ _setup_assertions(MAIN_REPORT) @wraps(f) def wrapped(*args, **kwargs): """ Args: *args: **kwargs: Returns: """ old_exception_state = MAIN_REPORT['assertions']['exceptions'] MAIN_REPORT['assertions']['exceptions'] = False value = f(*args, **kwargs) MAIN_REPORT['assertions']['exceptions'] = old_exception_state return value return wrapped
def _basic_assertion(left, right, operator, code_comparison_message, hc_message, hc_message_past, message, report, contextualize, show_expected_value=True, modify_right=None): if report is None: report = MAIN_REPORT _setup_assertions(report) context = "" # TODO: Handle right-side sandbox result #if is_sandbox_result(right): # right = right._actual_value if isinstance(left, Exception): return False if isinstance(right, Exception): return False if not operator(left, right): failure = _fail(code_comparison_message, hc_message, hc_message_past, show_expected_value, modify_right, left, right) report['assertions']['collected'].append(failure) report.attach('Instructor Test', category='student', tool='Assertions', mistake={'message': "Student code failed instructor test.<br>\n"+ context+str(failure)}) report['assertions']['failures'] += 1 if report['assertions']['exceptions']: raise failure else: return False return True
def phase(phase_name, before=None, after=None): ''' Args: phase_name (str): The name of the phase this function will belong to. before (list[str] or str): the name(s) of any phases that this phase should be before. after (list[str] or str): the name(s) of any phases that this phase should be after. ''' _setup_assertions(MAIN_REPORT) def wrap(f): """ Args: f: Returns: """ @wraps(f) def _handle_entry(*args, **kwargs): old_exception_state = MAIN_REPORT['assertions']['exceptions'] MAIN_REPORT['assertions']['exceptions'] = True value = f(*args, **kwargs) MAIN_REPORT['assertions']['exceptions'] = old_exception_state return value _add_phase(phase_name, _handle_entry) _add_relationships(phase_name, before) _add_relationships(after, phase_name) return _handle_entry return wrap
def try_all(): _setup_assertions(MAIN_REPORT) @wraps(f) def wrapped(*args, **kwargs): old_exception_state = MAIN_REPORT['assertions']['exceptions'] MAIN_REPORT['assertions']['exceptions'] = False value = f(*args, **kwargs) MAIN_REPORT['assertions']['exceptions'] = old_exception_state return value return wrapped
def assertPrints(result, expected_output, args=None, returns=None, score=None, message=None, report=None, contextualize=True, exact=False): if not isinstance(result, SandboxResult): return False raise TypeError( "You must pass in a SandboxResult (e.g., using `call`) to assertPrints" ) if report is None: report = MAIN_REPORT _setup_assertions(report) call_id = result._actual_call_id sandbox = result._actual_sandbox calls = sandbox.call_contexts[call_id] inputs = sandbox.input_contexts[call_id] actual_output = sandbox.output_contexts[call_id] if not output_test(actual_output, expected_output, exact): context = [] if calls: context.append("I ran:\n<pre>" + "\n".join(map(str, calls)) + "</pre>") if inputs: context.append("I entered as input:\n<pre>" + "\n".join(map(str, inputs)) + "</pre>") if actual_output: context.append("The function printed:\n<pre>" + "\n".join(map(str, actual_output)) + "</pre>") else: context.append("The function printed nothing.") context.append("But I expected the output:\n<pre>" + "\n".join(map(str, expected_output)) + "</pre>") failure = AssertionException("\n".join(context)) report['assertions']['collected'].append(failure) report.attach('Instructor Test', category='student', tool='Assertions', mistake={ 'message': "Student code failed instructor test.<br>\n" + str(failure) }) report['assertions']['failures'] += 1 if report['assertions']['exceptions']: raise failure else: return False report.give_partial(score) return True
def phase(name): _setup_assertions(MAIN_REPORT) def wrap(f): @wraps(f) def _handle_entry(*args, **kwargs): old_exception_state = MAIN_REPORT['assertions']['exceptions'] MAIN_REPORT['assertions']['exceptions'] = True value = f(*args, **kwargs) MAIN_REPORT['assertions']['exceptions'] = old_exception_state return value MAIN_REPORT['assertions']['functions'].append(_handle_entry) return _handle_entry return wrap
def stop_on_failure(f): _setup_assertions(MAIN_REPORT) @wraps(f) def wrapped(*args, **kwargs): old_exception_state = MAIN_REPORT['assertions']['exceptions'] MAIN_REPORT['assertions']['exceptions'] = True value = None try: value = f(*args, **kwargs) except AssertionException: pass MAIN_REPORT['assertions']['exceptions'] = old_exception_state return value return wrapped
def assertGenerally(expression, score=None, message=None, report=None, contextualize=True): if report is None: report = MAIN_REPORT _setup_assertions(report) if expression: report.give_partial(score) return True else: report['assertions']['failures'] += 1 if report['assertions']['exceptions']: raise AssertionException("General assertion") else: return False
def section(*args): ''' ''' _setup_assertions(MAIN_REPORT) def wrap(f): MAIN_REPORT['assertions']['functions'].append(f) return f section_number = -1 if len(args) >= 1 and callable(args[0]): if len(args) >= 2: section_number = args[1] return wrap(args[0]) elif len(args) >= 1: section_number = args[0] return wrap
def assertPrints(result, expected_output, args=None, returns=None, score=None, message=None, report=None, contextualize=True, exact=False): if not isinstance(result, SandboxResult): return False raise TypeError("You must pass in a SandboxResult (e.g., using `call`) to assertPrints") if report is None: report = MAIN_REPORT _setup_assertions(report) call_id = result._actual_call_id sandbox = result._actual_sandbox calls = sandbox.call_contexts[call_id] inputs = sandbox.input_contexts[call_id] actual_output = sandbox.output_contexts[call_id] if not equality_test(actual_output, expected_output, exact, DELTA, True): context= [] if calls: context.append("I ran:<pre>"+ "\n".join(map(str, calls))+ "</pre>") if inputs: context.append("I entered as input:<pre>"+ "\n".join(map(str, inputs))+ "</pre>") if actual_output: context.append("The function printed:<pre>"+ "\n".join(map(str, actual_output))+ "</pre>") else: context.append("The function printed nothing.") context.append("But I expected the output:<pre>"+ "\n".join(map(str, expected_output))+ "</pre>") failure = AssertionException("\n".join(context)) report['assertions']['collected'].append(failure) report.attach('Instructor Test', category='student', tool='Assertions', mistake={'message': "Student code failed instructor test.<br>\n"+ str(failure)}) report['assertions']['failures'] += 1 if report['assertions']['exceptions']: raise failure else: return False report.give_partial(score) return True
def set_assertion_mode(exceptions=True, report=None): if report is None: report = MAIN_REPORT _setup_assertions(report) report['assertions']['exceptions'] = exceptions