diff options
author | Edward Loper <edloper@gradient.cis.upenn.edu> | 2004-08-26 01:19:50 (GMT) |
---|---|---|
committer | Edward Loper <edloper@gradient.cis.upenn.edu> | 2004-08-26 01:19:50 (GMT) |
commit | aacf0833886c30f900fe18db1367d887cc0d1172 (patch) | |
tree | 70d0b7a8b49702b132a93b4d6e8cc8078d1ab2fc | |
parent | 0e448073d6afed4806a8dbd3261057a681023993 (diff) | |
download | cpython-aacf0833886c30f900fe18db1367d887cc0d1172.zip cpython-aacf0833886c30f900fe18db1367d887cc0d1172.tar.gz cpython-aacf0833886c30f900fe18db1367d887cc0d1172.tar.bz2 |
- 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
-rw-r--r-- | Lib/doctest.py | 60 | ||||
-rw-r--r-- | Lib/test/test_doctest.py | 62 |
2 files changed, 64 insertions, 58 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index 2cd96ba..dfa1848 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -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 @@ class DocTestRunner: 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 @@ class DocTestRunner: 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 @@ class DocTestRunner: 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 @@ class OutputChecker: 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 @@ class OutputChecker: 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. diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index a9076a6..7884c04 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -591,11 +591,14 @@ the failure and proceeds to the next example: ... ''' >>> 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 @@ the failure and proceeds to the next example: 14 Got: 12 - Trying: x/2 - Expecting: 6 + Trying: + x/2 + Expecting: + 6 ok (1, 3) """ @@ -624,14 +629,19 @@ output: >>> 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 @@ iff `-v` appears in sys.argv: >>> # 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) |