def report_start(self, out, test, example): """ Report that the test runner is about to process the given example. (Only displays a message if verbose=True) """ if self._verbose: if example.want: out("Trying:\n" + _indent(example.source) + "Expecting:\n" + _indent(example.want)) else: out("Trying:\n" + _indent(example.source) + "Expecting nothing\n")
def report_start(self, out, test, example): source = example.source[self._BEFORE:-(self._AFTER + 1)] + "\n" if self._verbose_level > 1: out('Label:%s\n' % example.label) if self._verbose: if example.want: out('Trying:\n' + doctest._indent(source) + 'Expecting:\n' + doctest._indent(example.want)) else: out('Trying:\n' + doctest._indent(source) + 'Expecting nothing\n')
def report_start(self, out, test, example): source = example.source[self._BEFORE:-(self._AFTER+1)] + "\n" if self._verbose_level > 1: out('Label:%s\n' % example.label) if self._verbose: if example.want: out('Trying:\n' + doctest._indent(source) + 'Expecting:\n' + doctest._indent(example.want)) else: out('Trying:\n' + doctest._indent(source) + 'Expecting nothing\n')
def report_start(self, out, test, example): """ Report that the test runner is about to process the given example. (Only displays a message if verbose=True) """ if self._verbose: if example.want: out('Trying:\n' + _indent(example.source) + 'Expecting:\n' + _indent(example.want)) else: out('Trying:\n' + _indent(example.source) + 'Expecting nothing\n')
def report_unexpected_exception(self, out, test, example, exc_info): """ Report that the given example raised an unexpected exception. """ out( self._failure_header(test, example) + "Exception raised:\n" + _indent(_exception_traceback(exc_info)))
def report_start(self, out, test, example): if 1 <= self._verbosity <= 2: src = example.source.split('\n')[0] if len(src) > 60: src = src[:57]+'...' if isinstance(src, unicode): src = src.encode('utf8') lineno = test.lineno + example.lineno + 1 if self._verbosity == 1: if self._stderr_term.CLEAR_LINE: sys.__stderr__.write(self._stderr_term.CLEAR_LINE) else: sys.__stderr__.write('\n') sys.__stderr__.write('%s [Line %s] %s%s' % (self._stderr_term.BOLD, lineno, self._stderr_term.NORMAL, src)) if self._verbosity == 2: sys.__stderr__.write('\n') else: DocTestRunner.report_start(self, out, test, example) sys.__stdout__.flush() self._current_test = (test, example) # Total hack warning: This munges the original source to # catch any keyboard interrupts, and turn them into special # ValueError interrupts. example.original_source = example.source if self._kbinterrupt_continue: example.source = ('try:\n%sexcept KeyboardInterrupt:\n ' 'raise ValueError("KEYBOARD-INTERRUPT")\n' % doctest._indent(example.source))
def report_start(self, out, test, example): if 1 <= self._verbosity <= 2: src = example.source.split('\n')[0] if len(src) > 60: src = src[:57]+'...' lineno = test.lineno + example.lineno + 1 if self._verbosity == 1: if self._stderr_term.CLEAR_LINE: sys.__stderr__.write(self._stderr_term.CLEAR_LINE) else: sys.__stderr__.write('\n') sys.__stderr__.write('%s [Line %s] %s%s' % (self._stderr_term.BOLD, lineno, self._stderr_term.NORMAL, src)) if self._verbosity == 2: sys.__stderr__.write('\n') else: DocTestRunner.report_start(self, out, test, example) sys.__stdout__.flush() self._current_test = (test, example) # Total hack warning: This munges the original source to # catch any keyboard interrupts, and turn them into special # ValueError interrupts. example.original_source = example.source if self._kbinterrupt_continue: example.source = ('try:\n%sexcept KeyboardInterrupt:\n ' 'raise ValueError("KEYBOARD-INTERRUPT")\n' % doctest._indent(example.source))
def output_difference(self, example, got, optionflags): """ Return a string describing the differences between the expected output for a given example (`example`) and the actual output (`got`). `optionflags` is the set of option flags used to compare `want` and `got`. """ want = example.want # If <BLANKLINE>s are being used, then replace blank lines # with <BLANKLINE> in the actual output string. if not (optionflags & DONT_ACCEPT_BLANKLINE): got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got) # Check if we should use diff. if self._do_a_fancy_diff(want, got, optionflags): # Split want & got into lines. want_lines = want.splitlines(True) # True == keep line ends got_lines = got.splitlines(True) # Use difflib to find their differences. if optionflags & REPORT_UDIFF: diff = difflib.unified_diff(want_lines, got_lines, n=2) diff = list(diff)[2:] # strip the diff header kind = 'unified diff with -expected +actual' elif optionflags & REPORT_CDIFF: diff = difflib.context_diff(want_lines, got_lines, n=2) diff = list(diff)[2:] # strip the diff header kind = 'context diff with expected followed by actual' elif optionflags & REPORT_NDIFF: engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK) diff = list(engine.compare(want_lines, got_lines)) kind = 'ndiff with -expected +actual' else: assert 0, 'Bad diff option' # Remove trailing whitespace on diff output. diff = [line.rstrip() + '\n' for line in diff] return 'Differences (%s):\n' % kind + _indent(''.join(diff)) # If we're not using diff, then simply list the expected # output followed by the actual output. if want and got: return 'Expected:\n%sGot:\n%s' % (_indent(want), _indent(got)) elif want: return 'Expected:\n%sGot nothing\n' % _indent(want) elif got: return 'Expected nothing\nGot:\n%s' % _indent(got) else: return 'Expected nothing\nGot nothing\n'
def output_difference(self, example, got, optionflags): if optionflags & XMLDATA or optionflags & XMLDATA_IGNOREORDER: want = example.want if want and got: # it only makes sense to compare actual data error = 'Expected:\n%sGot:\n%s' %(doctest._indent(want), doctest._indent(got)) result, errmsg = _assertXMLElement(want, got, optionflags) assert not result, "assertXMLEqual didn't fail." if errmsg: error += "XML differences:\n" + \ doctest._indent(errmsg) + "\n" else: # XXX - not tested error += "No known XML difference." return error # XXX - not tested return doctest.OutputChecker.output_difference( self, example, got, optionflags)
def _print_error_header(self, example): filepath = example.filepath lineno = example.start_lineno self._write("*" * 70) msg = '\nFile "%s", line %i\n' % (filepath, lineno) self._write(msg) self._write("Failed example:\n") self._write(_indent(highlight_syntax(example, self.use_colors)))
def _failure_header(self, test, example): out = [self.DIVIDER] if test.filename: if test.lineno is not None and example.lineno is not None: lineno = test.lineno + example.lineno + 1 else: lineno = "?" out.append('File "%s", line %s, in %s' % (test.filename, lineno, test.name)) else: out.append("Line %s, in %s" % (example.lineno + 1, test.name)) out.append("Failed example:") source = example.source out.append(_indent(source)) return "\n".join(out)
def _failure_header(self, test, example): out = [self.DIVIDER] if test.filename: if test.lineno is not None and example.lineno is not None: lineno = test.lineno + example.lineno + 1 else: lineno = '?' out.append('File "%s", line %s, in %s' % (test.filename, lineno, test.name)) else: out.append('Line %s, in %s' % (example.lineno + 1, test.name)) out.append('Failed example:') source = example.source[self._BEFORE:-(self._AFTER + 1)] + "\n" out.append(doctest._indent(source)) return '\n'.join(out)
def _failure_header(self, test, example): out = [self.DIVIDER] if test.filename: if test.lineno is not None and example.lineno is not None: lineno = test.lineno + example.lineno + 1 else: lineno = '?' out.append('File "%s", line %s, in %s' % (test.filename, lineno, test.name)) else: out.append('Line %s, in %s' % (example.lineno+1, test.name)) out.append('Failed example:') source = example.source[self._BEFORE:-(self._AFTER+1)] + "\n" out.append(doctest._indent(source)) return '\n'.join(out)
def build_where_msg(where, owner, msg=None, use_colors=False): tmp = [] try: tmp.append('File "%s", line %i' % (where.filepath, where.start_lineno)) except: if where: tmp.append(str(where)) if owner: owner = "[%s]" % str(owner) tmp.append((', ' + owner) if tmp else owner) try: tmp.append('\n' + _indent(highlight_syntax(where, use_colors))) except Exception as e: pass if msg: tmp.append('\n' + msg) return ''.join(tmp)
def report_unexpected_exception(self, out, test, example, exc_info): """Report that the given example raised an unexpected exception.""" header = 'Exception raised in {}' backtrace = doctest._indent(doctest._exception_traceback(exc_info)) err = CheckstyleRunner.error_node(test, example, header, backtrace) self.checkstyle_errors.append(err)
def report_unexpected_exception(self, out, test, example, exc_info): """ Report that the given example raised an unexpected exception. """ out(self._failure_header(test, example) + 'Exception raised:\n' + _indent(_exception_traceback(exc_info)))
def update_event(self, inp=-1): self.set_output_val(0, doctest._indent(self.input(0), self.input(1)))