예제 #1
0
    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
예제 #2
0
 def testError_skipLocked(self):
     key = "testKey"
     hashedAnswer = locking.lock(key, "4")
     self.calls_interpret(
         False, """
     >>> 2 + 2
     %s
     # locked
     """ % hashedAnswer)
예제 #3
0
 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)
예제 #4
0
    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()
예제 #5
0
    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
예제 #6
0
    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
예제 #7
0
 def _verify(self, guess, locked):
     return locking.lock(self.hash_key, guess) == locked
예제 #8
0
파일: lock.py 프로젝트: Kelel1/CS61A
 def _hash_fn(self, text):
     text = format.normalize(text)
     return locking.lock(self.assignment.name, text)
예제 #9
0
 def _verify(self, guess, locked):
     return locking.lock(self.hash_key, guess) == locked