[Checkins] SVN: manuel/branches/elro-test-count/src/manuel/ Group contiguous doctest sections as a single test case

Laurence Rowe l at lrowe.co.uk
Fri Aug 13 14:34:41 EDT 2010


Log message for revision 115673:
  Group contiguous doctest sections as a single test case

Changed:
  U   manuel/branches/elro-test-count/src/manuel/README.txt
  U   manuel/branches/elro-test-count/src/manuel/doctest.py

-=-
Modified: manuel/branches/elro-test-count/src/manuel/README.txt
===================================================================
--- manuel/branches/elro-test-count/src/manuel/README.txt	2010-08-13 18:33:43 UTC (rev 115672)
+++ manuel/branches/elro-test-count/src/manuel/README.txt	2010-08-13 18:34:41 UTC (rev 115673)
@@ -223,7 +223,7 @@
     >>> for region in document:
     ...     print (region.lineno, region.parsed or region.source)
     (1, 'This is my\ndoctest.\n\n')
-    (4, <doctest.Example instance at 0x...>)
+    (4, [<doctest.Example instance at 0x...>])
 
 Now we can evaluate the examples.
 
@@ -336,7 +336,7 @@
     (1, '\nWe can have a list of numbers...\n\n')
     (4, <NumbersTest object at 0x...>)
     (5, '\n... and we can test Python.\n\n')
-    (8, <doctest.Example instance at 0x...>)
+    (8, [<doctest.Example instance at 0x...>])
     (10, '\n')
 
 We can look at the formatted output to see that each of the two tests failed.
@@ -431,12 +431,12 @@
     import doctest
 
     def informative_evaluater(region, document, globs):
-        if not isinstance(region.parsed, doctest.Example):
+        if not isinstance(region.parsed, manuel.doctest.DocTestExamples):
             return
         if region.evaluated.getvalue():
             info = ''
             for name in sorted(globs):
-                if name in region.parsed.source:
+                if name in region.source:
                     info += '\n    ' + name + ' = ' + repr(globs[name])
 
             if info:
@@ -520,7 +520,7 @@
     >>> import tokenize
 
     >>> def informative_evaluater_2(region, document, globs):
-    ...     if not isinstance(region.parsed, doctest.Example):
+    ...     if not isinstance(region.parsed, manuel.doctest.DocTestExamples):
     ...         return
     ...
     ...     if region.evaluated.getvalue():

Modified: manuel/branches/elro-test-count/src/manuel/doctest.py
===================================================================
--- manuel/branches/elro-test-count/src/manuel/doctest.py	2010-08-13 18:33:43 UTC (rev 115672)
+++ manuel/branches/elro-test-count/src/manuel/doctest.py	2010-08-13 18:34:41 UTC (rev 115673)
@@ -9,20 +9,35 @@
     pass
 
 
+class DocTestExamples(list):
+    pass
+
+
+def group_examples(chunks):
+    # Group contiguous doctest examples
+    group = DocTestExamples()
+    for chunk in chunks:
+        if chunk == '':
+            continue
+        if isinstance(chunk, doctest.Example):
+            group.append(chunk)
+        elif group:
+            yield group
+            group = DocTestExamples()
+    if group:
+        yield group
+
 def parse(document):
     for region in list(document):
         if region.parsed:
             continue
         region_start = region.lineno
         region_end = region.lineno + region.source.count('\n')
-        for chunk in doctest.DocTestParser().parse(region.source):
-            # If the chunk contains prose (as opposed to and example), skip it.
-            if isinstance(chunk, basestring):
-                continue
-            chunk_line_count = (chunk.source.count('\n')
-                + chunk.want.count('\n'))
-
-            split_line_1 = region_start + chunk.lineno
+        for group in group_examples(doctest.DocTestParser().parse(region.source)):
+            chunk_line_count = sum(chunk.source.count('\n')
+                + chunk.want.count('\n') for chunk in group)
+            chunk_0_lineno = group[0].lineno
+            split_line_1 = region_start + chunk_0_lineno
             split_line_2 = split_line_1 + chunk_line_count
 
             # if there is some source we need to trim off the front...
@@ -38,8 +53,9 @@
 
             # Since we're treating each example as a stand-alone thing, we need
             # to reset its line number to zero.
-            chunk.lineno = 0
-            found.parsed = chunk
+            for chunk in group:
+                chunk.lineno -= chunk_0_lineno
+            found.parsed = group
 
             assert region in document
 
@@ -55,7 +71,7 @@
 def evaluate(m, region, document, globs):
     # If the parsed object is not a doctest Example then we don't need to
     # handle it.
-    if not isinstance(region.parsed, doctest.Example):
+    if not isinstance(region.parsed, DocTestExamples):
         return
 
     result = DocTestResult()
@@ -67,7 +83,7 @@
 
     runner.DIVIDER = '' # disable unwanted result formatting
     runner.run(
-        DocTest([region.parsed], globs, test_name,
+        DocTest(region.parsed, globs, test_name,
             document.location, region.lineno-1, None),
         out=result.write, clear_globs=False)
     region.evaluated = result



More information about the checkins mailing list