- Changed the output of report_start() and report_unexpected_exception()

to be more consistent with report_failure()
- If `want` or `got` is empty, then print "Expected nothing\n" or
  "Got nothing\n" rather than "Expected:\n" or "Got:\n"
- Got rid of _tag_msg
This commit is contained in:
Edward Loper 2004-08-26 01:19:50 +00:00
parent 0e448073d6
commit aacf083388
2 changed files with 64 additions and 58 deletions

View file

@ -343,25 +343,13 @@ def _normalize_module(module, depth=2):
else: else:
raise TypeError("Expected a module, string, or None") raise TypeError("Expected a module, string, or None")
def _tag_msg(tag, msg, indent=' '): def _indent(s, indent=4):
""" """
Return a string that displays a tag-and-message pair nicely, Add the given number of space characters to the beginning every
keeping the tag and its message on the same line when that non-blank line in `s`, and return the result.
makes sense. If the message is displayed on separate lines,
then `indent` is added to the beginning of each line.
""" """
# If the message doesn't end in a newline, then add one. # This regexp matches the start of non-blank lines:
if msg[-1:] != '\n': return re.sub('(?m)^(?!$)', indent*' ', s)
msg += '\n'
# If the message is short enough, and contains no internal
# newlines, then display it on the same line as the tag.
# Otherwise, display the tag on its own line.
if (len(tag) + len(msg) < 75 and
msg.find('\n', 0, len(msg)-1) == -1):
return '%s: %s' % (tag, msg)
else:
msg = '\n'.join([indent+l for l in msg[:-1].split('\n')])
return '%s:\n%s\n' % (tag, msg)
def _exception_traceback(exc_info): def _exception_traceback(exc_info):
""" """
@ -1273,8 +1261,12 @@ def report_start(self, out, test, example):
example. (Only displays a message if verbose=True) example. (Only displays a message if verbose=True)
""" """
if self._verbose: if self._verbose:
out(_tag_msg("Trying", example.source) + if example.want:
_tag_msg("Expecting", example.want or "nothing")) out('Trying:\n' + _indent(example.source) +
'Expecting:\n' + _indent(example.want))
else:
out('Trying:\n' + _indent(example.source) +
'Expecting nothing\n')
def report_success(self, out, test, example, got): def report_success(self, out, test, example, got):
""" """
@ -1298,7 +1290,7 @@ def report_unexpected_exception(self, out, test, example, exc_info):
Report that the given example raised an unexpected exception. Report that the given example raised an unexpected exception.
""" """
out(self._failure_header(test, example) + out(self._failure_header(test, example) +
_tag_msg("Exception raised", _exception_traceback(exc_info))) 'Exception raised:\n' + _indent(_exception_traceback(exc_info)))
def _failure_header(self, test, example): def _failure_header(self, test, example):
out = [self.DIVIDER] out = [self.DIVIDER]
@ -1313,10 +1305,8 @@ def _failure_header(self, test, example):
out.append('Line %s, in %s' % (example.lineno+1, test.name)) out.append('Line %s, in %s' % (example.lineno+1, test.name))
out.append('Failed example:') out.append('Failed example:')
source = example.source source = example.source
if source.endswith('\n'): out.append(_indent(source))
source = source[:-1] return '\n'.join(out)
out.append(' ' + '\n '.join(source.split('\n')))
return '\n'.join(out)+'\n'
#///////////////////////////////////////////////////////////////// #/////////////////////////////////////////////////////////////////
# DocTest Running # DocTest Running
@ -1612,10 +1602,8 @@ def output_difference(self, want, got, optionflags):
Return a string describing the differences between the Return a string describing the differences between the
expected output for an example (`want`) and the actual output expected output for an example (`want`) and the actual output
(`got`). `optionflags` is the set of option flags used to (`got`). `optionflags` is the set of option flags used to
compare `want` and `got`. `indent` is the indentation of the compare `want` and `got`.
original example.
""" """
# If <BLANKLINE>s are being used, then replace blank lines # If <BLANKLINE>s are being used, then replace blank lines
# with <BLANKLINE> in the actual output string. # with <BLANKLINE> in the actual output string.
if not (optionflags & DONT_ACCEPT_BLANKLINE): if not (optionflags & DONT_ACCEPT_BLANKLINE):
@ -1645,18 +1633,18 @@ def output_difference(self, want, got, optionflags):
assert 0, 'Bad diff option' assert 0, 'Bad diff option'
# Remove trailing whitespace on diff output. # Remove trailing whitespace on diff output.
diff = [line.rstrip() + '\n' for line in diff] diff = [line.rstrip() + '\n' for line in diff]
return _tag_msg("Differences (" + kind + ")", return 'Differences (%s):\n' % kind + _indent(''.join(diff))
''.join(diff))
# If we're not using diff, then simply list the expected # If we're not using diff, then simply list the expected
# output followed by the actual output. # output followed by the actual output.
if want.endswith('\n'): if want and got:
want = want[:-1] return 'Expected:\n%sGot:\n%s' % (_indent(want), _indent(got))
want = ' ' + '\n '.join(want.split('\n')) elif want:
if got.endswith('\n'): return 'Expected:\n%sGot nothing\n' % _indent(want)
got = got[:-1] elif got:
got = ' ' + '\n '.join(got.split('\n')) return 'Expected nothing\nGot:\n%s' % _indent(got)
return "Expected:\n%s\nGot:\n%s\n" % (want, got) else:
return 'Expected nothing\nGot nothing\n'
class DocTestFailure(Exception): class DocTestFailure(Exception):
"""A DocTest example has failed in debugging mode. """A DocTest example has failed in debugging mode.

View file

@ -591,11 +591,14 @@ def basics(): r"""
... ''' ... '''
>>> test = doctest.DocTestFinder().find(f)[0] >>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=True).run(test) >>> doctest.DocTestRunner(verbose=True).run(test)
Trying: x = 12 Trying:
Expecting: nothing x = 12
Expecting nothing
ok ok
Trying: print x Trying:
Expecting: 14 print x
Expecting:
14
********************************************************************** **********************************************************************
Line 3, in f Line 3, in f
Failed example: Failed example:
@ -604,8 +607,10 @@ def basics(): r"""
14 14
Got: Got:
12 12
Trying: x/2 Trying:
Expecting: 6 x/2
Expecting:
6
ok ok
(1, 3) (1, 3)
""" """
@ -624,14 +629,19 @@ def verbose_flag(): r"""
>>> test = doctest.DocTestFinder().find(f)[0] >>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=True).run(test) >>> doctest.DocTestRunner(verbose=True).run(test)
Trying: x = 12 Trying:
Expecting: nothing x = 12
Expecting nothing
ok ok
Trying: print x Trying:
Expecting: 12 print x
Expecting:
12
ok ok
Trying: x/2 Trying:
Expecting: 6 x/2
Expecting:
6
ok ok
(0, 3) (0, 3)
@ -649,14 +659,19 @@ def verbose_flag(): r"""
>>> # If -v does appear in sys.argv, then output is verbose. >>> # If -v does appear in sys.argv, then output is verbose.
>>> sys.argv = ['test', '-v'] >>> sys.argv = ['test', '-v']
>>> doctest.DocTestRunner().run(test) >>> doctest.DocTestRunner().run(test)
Trying: x = 12 Trying:
Expecting: nothing x = 12
Expecting nothing
ok ok
Trying: print x Trying:
Expecting: 12 print x
Expecting:
12
ok ok
Trying: x/2 Trying:
Expecting: 6 x/2
Expecting:
6
ok ok
(0, 3) (0, 3)
@ -1633,11 +1648,14 @@ def old_test2(): r"""
... ''' ... '''
>>> t.runstring(test, "Example") >>> t.runstring(test, "Example")
Running string Example Running string Example
Trying: x = 1 + 2 Trying:
Expecting: nothing x = 1 + 2
Expecting nothing
ok ok
Trying: x Trying:
Expecting: 3 x
Expecting:
3
ok ok
0 of 2 examples failed in string Example 0 of 2 examples failed in string Example
(0, 2) (0, 2)