[Checkins] SVN: z3c.pt.compat/trunk/ Added option to specify which doctest module to use with output checker.

Malthe Borch mborch at gmail.com
Tue Nov 25 09:07:18 EST 2008


Log message for revision 93339:
  Added option to specify which doctest module to use with output checker.

Changed:
  U   z3c.pt.compat/trunk/CHANGES.txt
  U   z3c.pt.compat/trunk/src/z3c/pt/compat/testing.py

-=-
Modified: z3c.pt.compat/trunk/CHANGES.txt
===================================================================
--- z3c.pt.compat/trunk/CHANGES.txt	2008-11-25 13:45:26 UTC (rev 93338)
+++ z3c.pt.compat/trunk/CHANGES.txt	2008-11-25 14:07:18 UTC (rev 93339)
@@ -4,6 +4,10 @@
 HEAD
 ~~~~
 
+- Added optional option ``doctest`` for output checker to allow usage
+  with alternative implementations, e.g. the Zope doctest
+  module. [malthe]
+
 - Added tests for meta-directives and fixed some minor errors. [malthe]
 
 - Added update-tool to walk a source tree and automatically rewrite

Modified: z3c.pt.compat/trunk/src/z3c/pt/compat/testing.py
===================================================================
--- z3c.pt.compat/trunk/src/z3c/pt/compat/testing.py	2008-11-25 13:45:26 UTC (rev 93338)
+++ z3c.pt.compat/trunk/src/z3c/pt/compat/testing.py	2008-11-25 14:07:18 UTC (rev 93339)
@@ -1,4 +1,4 @@
-import doctest
+import doctest as pythondoctest
 
 import lxml.etree
 import lxml.doctestcompare
@@ -12,7 +12,15 @@
     built-in ``doctest`` module to allow the use of ellipsis."""
     
     _repr_re = re.compile(r'^<([A-Z]|[^>]+ (at|object) |[a-z]+ \'[A-Za-z0-9_.]+\'>)')
-    
+
+    def __init__(self, doctest=pythondoctest):
+        self.doctest = doctest
+
+        # make sure these optionflags are registered
+        doctest.register_optionflag('PARSE_HTML')
+        doctest.register_optionflag('PARSE_XML')
+        doctest.register_optionflag('NOPARSE_MARKUP')
+        
     def _looks_like_markup(self, s):
         s = s.replace('<BLANKLINE>', '\n').strip()
         return (s.startswith('<')
@@ -21,10 +29,33 @@
     def text_compare(self, want, got, strip):
         if want is None: want = ""
         if got is None: got = ""
-        checker = doctest.OutputChecker()
+        checker = self.doctest.OutputChecker()
         return checker.check_output(
-            want, got, doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE)
+            want, got, self.doctest.ELLIPSIS|self.doctest.NORMALIZE_WHITESPACE)
 
+    def get_parser(self, want, got, optionflags):
+        NOPARSE_MARKUP = self.doctest.OPTIONFLAGS_BY_NAME.get(
+            "NOPARSE_MARKUP", 0)
+        PARSE_HTML = self.doctest.OPTIONFLAGS_BY_NAME.get(
+            "PARSE_HTML", 0)
+        PARSE_XML = self.doctest.OPTIONFLAGS_BY_NAME.get(
+            "PARSE_XML", 0)
+        
+        parser = None
+        if NOPARSE_MARKUP & optionflags:
+            return None
+        if PARSE_HTML & optionflags:
+            parser = lxml.doctestcompare.html_fromstring
+        elif PARSE_XML & optionflags:
+            parser = lxml.etree.XML
+        elif (want.strip().lower().startswith('<html')
+              and got.strip().startswith('<html')):
+            parser = html_fromstring
+        elif (self._looks_like_markup(want)
+              and self._looks_like_markup(got)):
+            parser = self.get_default_parser()
+        return parser
+
 def render(view, xpath='.'):
     method = getattr(view, 'render', None)
     if method is None:



More information about the Checkins mailing list