diff options
author | Jim Fulton <jim@zope.com> | 2004-08-22 14:10:00 (GMT) |
---|---|---|
committer | Jim Fulton <jim@zope.com> | 2004-08-22 14:10:00 (GMT) |
commit | 07a349c4eeda0a937367ea9a2eff9958c46b11e1 (patch) | |
tree | 4b563ee7403637345e86211fc288da9d0c919011 /Lib | |
parent | 8ffe9abd092794369a76330c6347387d0d8bf6ab (diff) | |
download | cpython-07a349c4eeda0a937367ea9a2eff9958c46b11e1.zip cpython-07a349c4eeda0a937367ea9a2eff9958c46b11e1.tar.gz cpython-07a349c4eeda0a937367ea9a2eff9958c46b11e1.tar.bz2 |
Bugs fixed:
- Test filenames sometimes had trailing .pyc or .pyo sufixes
(when module __file__ did).
- Trailing spaces spaces in expected output were dropped.
New default failure format:
- Separation of examples from file info makes examples easier to see
- More vertical separation, improving readability
- Emacs-recognized file info (also closer to Python exception format)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/doctest.py | 69 | ||||
-rw-r--r-- | Lib/test/test_doctest.py | 171 |
2 files changed, 160 insertions, 80 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index 25b14ca..ccf976c 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -740,7 +740,15 @@ class DocTestParser: # Divide want into lines; check that it's properly # indented; and then strip the indentation. - want_lines = m.group('want').rstrip().split('\n') + want = m.group('want') + + # Strip trailing newline and following spaces + l = len(want.rstrip()) + l = want.find('\n', l) + if l >= 0: + want = want[:l] + + want_lines = want.split('\n') self._check_prefix(want_lines, ' '*indent, name, lineno+len(source_lines)) want = '\n'.join([wl[indent:] for wl in want_lines]) @@ -1063,6 +1071,8 @@ class DocTestFinder: filename = None else: filename = getattr(module, '__file__', module.__name__) + if filename[-4:] in (".pyc", ".pyo"): + filename = filename[:-1] return self._parser.get_doctest(docstring, globs, name, filename, lineno) @@ -1199,6 +1209,7 @@ class DocTestRunner: verbose = '-v' in sys.argv self._verbose = verbose self.optionflags = optionflags + self.original_optionflags = optionflags # Keep track of the examples we've run. self.tries = 0 @@ -1246,20 +1257,22 @@ class DocTestRunner: _tag_msg("Exception raised", _exception_traceback(exc_info))) def _failure_header(self, test, example): - s = (self.DIVIDER + "\n" + - _tag_msg("Failure in example", example.source)) - if test.filename is None: - # [XX] I'm not putting +1 here, to give the same output - # as the old version. But I think it *should* go here. - return s + ("from line #%s of %s\n" % - (example.lineno, test.name)) - elif test.lineno is None: - return s + ("from line #%s of %s in %s\n" % - (example.lineno+1, test.name, test.filename)) + 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: - lineno = test.lineno+example.lineno+1 - return s + ("from line #%s of %s (%s)\n" % - (lineno, test.filename, test.name)) + 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' #///////////////////////////////////////////////////////////////// # DocTest Running @@ -1568,6 +1581,7 @@ class OutputChecker: compare `want` and `got`. `indent` is the indentation of the original example. """ + # If <BLANKLINE>s are being used, then replace blank lines # with <BLANKLINE> in the actual output string. if not (optionflags & DONT_ACCEPT_BLANKLINE): @@ -1600,8 +1614,13 @@ class OutputChecker: # If we're not using diff, then simply list the expected # output followed by the actual output. - return (_tag_msg("Expected", want or "Nothing") + - _tag_msg("Got", got)) + 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) class DocTestFailure(Exception): """A DocTest example has failed in debugging mode. @@ -1989,6 +2008,7 @@ class DocTestCase(unittest.TestCase): def __init__(self, test, optionflags=0, setUp=None, tearDown=None, checker=None): + unittest.TestCase.__init__(self) self._dt_optionflags = optionflags self._dt_checker = checker @@ -2025,7 +2045,7 @@ class DocTestCase(unittest.TestCase): if test.lineno is None: lineno = 'unknown line number' else: - lineno = 'line %s' % test.lineno + lineno = '%s' % test.lineno lname = '.'.join(test.name.split('.')[-1:]) return ('Failed doctest test for %s\n' ' File "%s", line %s, in %s\n\n%s' @@ -2150,9 +2170,7 @@ def DocTestSuite(module=None, globs=None, extraglobs=None, continue if not test.filename: filename = module.__file__ - if filename.endswith(".pyc"): - filename = filename[:-1] - elif filename.endswith(".pyo"): + if filename[-4:] in (".pyc", ".pyo"): filename = filename[:-1] test.filename = filename suite.addTest(DocTestCase(test, optionflags, setUp, tearDown, @@ -2469,10 +2487,13 @@ def test1(): r""" ... 42 ... ''', 'XYZ') ********************************************************************** -Failure in example: print x -from line #2 of XYZ -Expected: 42 -Got: 84 +Line 3, in XYZ +Failed example: + print x +Expected: + 42 +Got: + 84 (1, 2) >>> t.runstring(">>> x = x * 2\n>>> print x\n84\n", 'example2') (0, 2) diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index 3c5fa08..6d9d745 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -267,11 +267,24 @@ For a function whose docstring contains examples, DocTestFinder.find() will return a single test (for that function's docstring): >>> finder = doctest.DocTestFinder() + +We'll simulate a __file__ attr that ends in pyc: + + >>> import test.test_doctest + >>> old = test.test_doctest.__file__ + >>> test.test_doctest.__file__ = 'test_doctest.pyc' + >>> tests = finder.find(sample_func) >>> print tests # doctest: +ELLIPSIS [<DocTest sample_func from ...:12 (1 example)>] + >>> tests[0].filename + 'test_doctest.py' + + >>> test.test_doctest.__file__ = old + + >>> e = tests[0].examples[0] >>> (e.source, e.want, e.lineno) ('print sample_func(22)\n', '44\n', 3) @@ -519,10 +532,13 @@ the failure and proceeds to the next example: Trying: print x Expecting: 14 ********************************************************************** - Failure in example: print x - from line #2 of f - Expected: 14 - Got: 12 + Line 3, in f + Failed example: + print x + Expected: + 14 + Got: + 12 Trying: x/2 Expecting: 6 ok @@ -645,8 +661,9 @@ message is raised, then it is reported as a failure: >>> doctest.DocTestRunner(verbose=False).run(test) ... # doctest: +ELLIPSIS ********************************************************************** - Failure in example: raise ValueError, 'message' - from line #1 of f + Line 2, in f + Failed example: + raise ValueError, 'message' Expected: Traceback (most recent call last): ValueError: wrong message @@ -668,11 +685,12 @@ unexpected exception: >>> doctest.DocTestRunner(verbose=False).run(test) ... # doctest: +ELLIPSIS ********************************************************************** - Failure in example: 1/0 - from line #1 of f + Line 2, in f + Failed example: + 1/0 Exception raised: Traceback (most recent call last): - ... + ... ZeroDivisionError: integer division or modulo by zero (1, 1) """ @@ -700,10 +718,13 @@ and 1/0: >>> flags = doctest.DONT_ACCEPT_TRUE_FOR_1 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) ********************************************************************** - Failure in example: True - from line #0 of f - Expected: 1 - Got: True + Line 1, in f + Failed example: + True + Expected: + 1 + Got: + True (1, 1) The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines @@ -722,8 +743,9 @@ and the '<BLANKLINE>' marker: >>> flags = doctest.DONT_ACCEPT_BLANKLINE >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) ********************************************************************** - Failure in example: print "a\n\nb" - from line #0 of f + Line 1, in f + Failed example: + print "a\n\nb" Expected: a <BLANKLINE> @@ -744,12 +766,14 @@ treated as equal: >>> test = doctest.DocTestFinder().find(f)[0] >>> doctest.DocTestRunner(verbose=False).run(test) ********************************************************************** - Failure in example: print 1, 2, 3 - from line #0 of f + Line 1, in f + Failed example: + print 1, 2, 3 Expected: 1 2 3 - Got: 1 2 3 + Got: + 1 2 3 (1, 1) >>> # With the flag: @@ -773,10 +797,13 @@ output to match any substring in the actual output: >>> test = doctest.DocTestFinder().find(f)[0] >>> doctest.DocTestRunner(verbose=False).run(test) ********************************************************************** - Failure in example: print range(15) - from line #0 of f - Expected: [0, 1, 2, ..., 14] - Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] + Line 1, in f + Failed example: + print range(15) + Expected: + [0, 1, 2, ..., 14] + Got: + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] (1, 1) >>> # With the flag: @@ -825,8 +852,9 @@ and actual outputs to be displayed using a unified diff: >>> test = doctest.DocTestFinder().find(f)[0] >>> doctest.DocTestRunner(verbose=False).run(test) ********************************************************************** - Failure in example: print '\n'.join('abcdefg') - from line #1 of f + Line 2, in f + Failed example: + print '\n'.join('abcdefg') Expected: a B @@ -850,8 +878,9 @@ and actual outputs to be displayed using a unified diff: >>> flags = doctest.UNIFIED_DIFF >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) ********************************************************************** - Failure in example: print '\n'.join('abcdefg') - from line #1 of f + Line 2, in f + Failed example: + print '\n'.join('abcdefg') Differences (unified diff): --- Expected +++ Got @@ -876,8 +905,9 @@ and actual outputs to be displayed using a context diff: >>> flags = doctest.CONTEXT_DIFF >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) ********************************************************************** - Failure in example: print '\n'.join('abcdefg') - from line #1 of f + Line 2, in f + Failed example: + print '\n'.join('abcdefg') Differences (context diff): *** Expected --- Got @@ -919,10 +949,13 @@ example with a comment of the form ``# doctest: +OPTION``: >>> test = doctest.DocTestFinder().find(f)[0] >>> doctest.DocTestRunner(verbose=False).run(test) ********************************************************************** - Failure in example: print range(10) # should fail: no ellipsis - from line #1 of f - Expected: [0, 1, ..., 9] - Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + Line 2, in f + Failed example: + print range(10) # should fail: no ellipsis + Expected: + [0, 1, ..., 9] + Got: + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (1, 2) To turn an option off for an example, follow that example with a @@ -940,10 +973,13 @@ comment of the form ``# doctest: -OPTION``: >>> doctest.DocTestRunner(verbose=False, ... optionflags=doctest.ELLIPSIS).run(test) ********************************************************************** - Failure in example: print range(10) # doctest: -ELLIPSIS - from line #5 of f - Expected: [0, 1, ..., 9] - Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + Line 6, in f + Failed example: + print range(10) # doctest: -ELLIPSIS + Expected: + [0, 1, ..., 9] + Got: + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (1, 2) Option directives affect only the example that they appear with; they @@ -962,15 +998,21 @@ do not change the options for surrounding examples: >>> test = doctest.DocTestFinder().find(f)[0] >>> doctest.DocTestRunner(verbose=False).run(test) ********************************************************************** - Failure in example: print range(10) # Should fail: no ellipsis - from line #1 of f - Expected: [0, 1, ..., 9] - Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + Line 2, in f + Failed example: + print range(10) # Should fail: no ellipsis + Expected: + [0, 1, ..., 9] + Got: + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ********************************************************************** - Failure in example: print range(10) # Should fail: no ellipsis - from line #7 of f - Expected: [0, 1, ..., 9] - Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + Line 8, in f + Failed example: + print range(10) # Should fail: no ellipsis + Expected: + [0, 1, ..., 9] + Got: + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (2, 3) Multiple options may be modified by a single option directive. They @@ -986,10 +1028,13 @@ may be separated by whitespace, commas, or both: >>> test = doctest.DocTestFinder().find(f)[0] >>> doctest.DocTestRunner(verbose=False).run(test) ********************************************************************** - Failure in example: print range(10) # Should fail - from line #1 of f - Expected: [0, 1, ..., 9] - Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + Line 2, in f + Failed example: + print range(10) # Should fail + Expected: + [0, 1, ..., 9] + Got: + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (1, 2) >>> def f(x): r''' @@ -1002,10 +1047,13 @@ may be separated by whitespace, commas, or both: >>> test = doctest.DocTestFinder().find(f)[0] >>> doctest.DocTestRunner(verbose=False).run(test) ********************************************************************** - Failure in example: print range(10) # Should fail - from line #1 of f - Expected: [0, 1, ..., 9] - Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + Line 2, in f + Failed example: + print range(10) # Should fail + Expected: + [0, 1, ..., 9] + Got: + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (1, 2) >>> def f(x): r''' @@ -1018,10 +1066,13 @@ may be separated by whitespace, commas, or both: >>> test = doctest.DocTestFinder().find(f)[0] >>> doctest.DocTestRunner(verbose=False).run(test) ********************************************************************** - Failure in example: print range(10) # Should fail - from line #1 of f - Expected: [0, 1, ..., 9] - Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + Line 2, in f + Failed example: + print range(10) # Should fail + Expected: + [0, 1, ..., 9] + Got: + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] (1, 2) The option directive may be put on the line following the source, as @@ -1414,6 +1465,14 @@ def test_DocFileSuite(): """ +def test_trailing_space_in_test(): + """ + Trailing spaces in expcted output are significant: + + >>> x, y = 'foo', '' + >>> print x, y + foo \n + """ ###################################################################### ## Main |