- 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:
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,
keeping the tag and its message on the same line when that
makes sense. If the message is displayed on separate lines,
then `indent` is added to the beginning of each line.
Add the given number of space characters to the beginning every
non-blank line in `s`, and return the result.
"""
# If the message doesn't end in a newline, then add one.
if msg[-1:] != '\n':
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)
# This regexp matches the start of non-blank lines:
return re.sub('(?m)^(?!$)', indent*' ', s)
def _exception_traceback(exc_info):
"""
@ -1273,8 +1261,12 @@ def report_start(self, out, test, example):
example. (Only displays a message if verbose=True)
"""
if self._verbose:
out(_tag_msg("Trying", example.source) +
_tag_msg("Expecting", example.want or "nothing"))
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_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.
"""
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):
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('Failed example:')
source = example.source
if source.endswith('\n'):
source = source[:-1]
out.append(' ' + '\n '.join(source.split('\n')))
return '\n'.join(out)+'\n'
out.append(_indent(source))
return '\n'.join(out)
#/////////////////////////////////////////////////////////////////
# DocTest Running
@ -1612,10 +1602,8 @@ def output_difference(self, want, got, optionflags):
Return a string describing the differences between the
expected output for an example (`want`) and the actual output
(`got`). `optionflags` is the set of option flags used to
compare `want` and `got`. `indent` is the indentation of the
original example.
compare `want` and `got`.
"""
# If <BLANKLINE>s are being used, then replace blank lines
# with <BLANKLINE> in the actual output string.
if not (optionflags & DONT_ACCEPT_BLANKLINE):
@ -1645,18 +1633,18 @@ def output_difference(self, want, got, optionflags):
assert 0, 'Bad diff option'
# Remove trailing whitespace on diff output.
diff = [line.rstrip() + '\n' for line in diff]
return _tag_msg("Differences (" + kind + ")",
''.join(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.endswith('\n'):
want = want[:-1]
want = ' ' + '\n '.join(want.split('\n'))
if got.endswith('\n'):
got = got[:-1]
got = ' ' + '\n '.join(got.split('\n'))
return "Expected:\n%s\nGot:\n%s\n" % (want, got)
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'
class DocTestFailure(Exception):
"""A DocTest example has failed in debugging mode.

View file

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