def _compare(self, expected, code): try: value, printed = self.evaluate(code) except ConsoleException as e: actual = e.exception_type else: if value is not None: print(self._output_fn(value)) actual = (printed + self._output_fn(value)).strip() else: actual = printed.strip() expected = expected.strip() if not self.skip_locked_cases and expected != actual: actual = locking.lock(self.hash_key, actual) if expected != actual: print() print("# Error: expected and actual results do not match") raise ConsoleException elif expected != actual: print() print('# Error: expected') print('\n'.join('# {}'.format(line) for line in expected.split('\n'))) print('# but got') print('\n'.join('# {}'.format(line) for line in actual.split('\n'))) raise ConsoleException
def testError_skipLocked(self): key = "testKey" hashedAnswer = locking.lock(key, "4") self.calls_interpret( False, """ >>> 2 + 2 %s # locked """ % hashedAnswer)
def testError_locked(self): key = "testKey" hashedAnswer = locking.lock(key, "5") self.calls_interpret(False, """ >>> 2 + 2 %s # locked """ % hashedAnswer, skip_locked_cases=False, hash_key=key)
def _compare(self, expected, code): try: value, printed = self.evaluate(code) except ConsoleException as e: detail = "{}: {}".format(e.exception_type, str(e.exception)) actual = CodeAnswer(exception=True, exception_type=e.exception_type, exception_detail=detail.splitlines()) else: if value is not None: print(self._output_fn(value)) printed += self._output_fn(value) output = printed.splitlines() actual = CodeAnswer(output=output) self.cases_total += 1 if not self.skip_locked_cases and expected.locked: if '\n'.join(expected.output) != locking.lock( self.hash_key, actual.dump()): print() print("# Error: expected and actual results do not match") raise ConsoleException else: return correct = (expected.exception == actual.exception and expected.output_lines() == actual.output_lines()) correct_legacy_exception = (actual.exception and [actual.exception_type ] == expected.output_lines()) if not correct and not correct_legacy_exception: print() print('# Error: expected') print('\n'.join('# {}'.format(line) for line in expected.output_lines())) print('# but got') print('\n'.join('# {}'.format(line) for line in actual.output_lines())) # Bail out on first failed test, or if we're showing all test results, bail on infinite loop timeout if not self.show_all_cases or (actual.exception and actual.exception_type == exceptions.Timeout.__name__): raise ConsoleException elif self.CASE_PREFIX in code: print(":(", f"Test Case {self.cases_total} failed") format.print_line('-') print() elif correct: self.cases_passed += 1 if self.CASE_PREFIX in code: print(":D", f"Test Case {self.cases_total} passed") format.print_line('-') print()
def _compare(self, expected, code): try: value, printed = self.evaluate(code) except ConsoleException as e: detail = "{}: {}".format(e.exception_type, str(e.exception)) actual = CodeAnswer(exception=True, exception_type=e.exception_type, exception_detail=detail.splitlines()) else: if value is not None: print(self._output_fn(value)) printed += self._output_fn(value) output = printed.splitlines() actual = CodeAnswer(output=output) if not self.skip_locked_cases and expected.locked: if '\n'.join(expected.output) != locking.lock( self.hash_key, actual.dump()): print() print("# Error: expected and actual results do not match") raise ConsoleException else: return correct = (expected.exception == actual.exception and expected.output_lines() == actual.output_lines()) correct_legacy_exception = (actual.exception and [actual.exception_type ] == expected.output_lines()) if not correct and not correct_legacy_exception: print() print('# Error: expected') print('\n'.join('# {}'.format(line) for line in expected.output_lines())) print('# but got') print('\n'.join('# {}'.format(line) for line in actual.output_lines())) raise ConsoleException
def _compare(self, expected, code): try: value, printed = self.evaluate(code) except ConsoleException as e: detail = "{}: {}".format(e.exception_type, str(e.exception)) actual = CodeAnswer( exception=True, exception_type=e.exception_type, exception_detail=detail.splitlines()) else: if value is not None: print(self._output_fn(value)) printed += self._output_fn(value) output = printed.splitlines() actual = CodeAnswer(output=output) if not self.skip_locked_cases and expected.locked: if '\n'.join(expected.output) != locking.lock(self.hash_key, actual.dump()): print() print("# Error: expected and actual results do not match") raise ConsoleException else: return correct = (expected.exception == actual.exception and expected.output_lines() == actual.output_lines()) correct_legacy_exception = (actual.exception and [actual.exception_type] == expected.output_lines()) if not correct and not correct_legacy_exception: print() print('# Error: expected') print('\n'.join('# {}'.format(line) for line in expected.output_lines())) print('# but got') print('\n'.join('# {}'.format(line) for line in actual.output_lines())) raise ConsoleException
def _verify(self, guess, locked): return locking.lock(self.hash_key, guess) == locked
def _hash_fn(self, text): text = format.normalize(text) return locking.lock(self.assignment.name, text)
def _verify(self, guess, locked): return locking.lock(self.hash_key, guess) == locked