]> granicus.if.org Git - python/commitdiff
Fixed bug in line-number finding for examples (DocTestParser wasn't
authorEdward Loper <edloper@gradient.cis.upenn.edu>
Tue, 17 Aug 2004 16:37:12 +0000 (16:37 +0000)
committerEdward Loper <edloper@gradient.cis.upenn.edu>
Tue, 17 Aug 2004 16:37:12 +0000 (16:37 +0000)
updating line numbers correctly for bare prompts & examples containing
only comments).

Lib/doctest.py
Lib/test/test_doctest.py

index aa523a64d6e1671634dcb45af387a9c85ae31924..c033b0d8df8f80252b7081d02a1c0f87c6c52f46 100644 (file)
@@ -552,12 +552,10 @@ class DocTestParser:
             (source, want) = self._parse_example(m, name, lineno)
             # Extract extra options from the source.
             options = self._find_options(source, name, lineno)
-            # If it contains no real source, then ignore it.
-            if self._IS_BLANK_OR_COMMENT(source):
-                continue
             # Create an Example, and add it to the list.
-            examples.append( Example(source, want, lineno,
-                                     len(m.group('indent')), options) )
+            if not self._IS_BLANK_OR_COMMENT(source):
+                examples.append( Example(source, want, lineno,
+                                         len(m.group('indent')), options) )
             # Update lineno (lines inside this example)
             lineno += string.count('\n', m.start(), m.end())
             # Update charno.
index 8e651d05a91a647a4883af9c444286479ff97b02..dec9110f3fdba03fd85e092d2dab677549c89155 100644 (file)
@@ -253,7 +253,6 @@ will raise a ValueError:
 
 """
 
-# [XX] test that it's getting line numbers right.
 def test_DocTestFinder(): r"""
 Unit tests for the `DocTestFinder` class.
 
@@ -450,6 +449,30 @@ using the `recurse` flag:
     >>> for t in tests:
     ...     print '%2s  %s' % (len(t.examples), t.name)
      1  SampleClass
+
+Line numbers
+~~~~~~~~~~~~
+DocTestFinder finds the line number of each example:
+
+    >>> def f(x):
+    ...     '''
+    ...     >>> x = 12
+    ...
+    ...     some text
+    ...
+    ...     >>> # examples are not created for comments & bare prompts.
+    ...     >>>
+    ...     ...
+    ...
+    ...     >>> for x in range(10):
+    ...     ...     print x,
+    ...     0 1 2 3 4 5 6 7 8 9
+    ...     >>> x/2
+    ...     6
+    ...     '''
+    >>> test = doctest.DocTestFinder().find(f)[0]
+    >>> [e.lineno for e in test.examples]
+    [1, 9, 12]
 """
 
 class test_DocTestRunner:
@@ -892,7 +915,7 @@ comment of the form ``# doctest: -OPTION``:
     ...                       optionflags=doctest.ELLIPSIS).run(test)
     **********************************************************************
     Failure in example: print range(10)       # doctest: -ELLIPSIS
-    from line #6 of f
+    from line #5 of f
     Expected: [0, 1, ..., 9]
     Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     (1, 2)