[Lxml-checkins] r47440 - in lxml/trunk: . src/lxml

ianb at codespeak.net ianb at codespeak.net
Sun Oct 14 02:12:32 CEST 2007


Author: ianb
Date: Sun Oct 14 02:12:31 2007
New Revision: 47440

Modified:
   lxml/trunk/CHANGES.txt
   lxml/trunk/src/lxml/doctestcompare.py
Log:
Improve doctestcompare a little: NOPARSE_MARKUP option to suppress its behavior, and xmlns=... now works

Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt	(original)
+++ lxml/trunk/CHANGES.txt	Sun Oct 14 02:12:31 2007
@@ -8,12 +8,20 @@
 Features added
 --------------
 
+* When using ``lxml.doctestcompare`` you can give the doctest option
+  ``NOPARSE_MARKUP`` (like ``# doctest: +NOPARSE_MARKUP``) to suppress
+  the special checking for one test.
+
 Bugs fixed
 ----------
 
 * lxml.etree could crash when adding more than 10000 namespaces to a
   document
 
+* With ``lxml.doctestcompare`` if you do ``<tag xmlns="...">`` in your
+  output, it will then be namespace-neutral (before the ellipsis was
+  treated as a real namespace).
+
 Other changes
 -------------
 

Modified: lxml/trunk/src/lxml/doctestcompare.py
==============================================================================
--- lxml/trunk/src/lxml/doctestcompare.py	(original)
+++ lxml/trunk/src/lxml/doctestcompare.py	Sun Oct 14 02:12:31 2007
@@ -23,6 +23,8 @@
 displayed (indented), and a rough diff-like output is given.  Anything
 marked with ``-`` is in the output but wasn't supposed to be, and
 similarly ``+`` means its in the example but wasn't in the output.
+
+You can disable parsing on one line with ``# doctest:+NOPARSE_MARKUP``
 """
 
 from lxml import etree
@@ -36,6 +38,7 @@
 
 PARSE_HTML = doctest.register_optionflag('PARSE_HTML')
 PARSE_XML = doctest.register_optionflag('PARSE_XML')
+NOPARSE_MARKUP = doctest.register_optionflag('NOPARSE_MARKUP')
 
 OutputChecker = doctest.OutputChecker
 
@@ -84,6 +87,8 @@
 
     def get_parser(self, want, got, optionflags):
         parser = None
+        if NOPARSE_MARKUP & optionflags:
+            return None
         if PARSE_HTML & optionflags:
             parser = document_fromstring
         elif PARSE_XML & optionflags:
@@ -102,7 +107,7 @@
                 and not _repr_re.search(s))
 
     def compare_docs(self, want, got):
-        if want.tag != got.tag and want.tag != 'any':
+        if not self.tag_compare(want.tag, got.tag):
             return False
         if not self.text_compare(want.text, got.text, True):
             return False
@@ -143,6 +148,17 @@
         else:
             return False
 
+    def tag_compare(self, want, got):
+        if want == 'any':
+            return True
+        want = want or ''
+        got = got or ''
+        if want.startswith('{...}'):
+            # Ellipsis on the namespace
+            return want.split('}')[-1] == got.split('}')[-1]
+        else:
+            return want == got
+
     def output_difference(self, example, got, optionflags):
         want = example.want
         parser = self.get_parser(want, got, optionflags)
@@ -282,7 +298,7 @@
         return ''.join(parts)
 
     def collect_diff_tag(self, want, got):
-        if want.tag != got.tag and want.tag != 'any':
+        if not self.tag_compare(want.tag, got.tag):
             tag = '%s (got: %s)' % (want.tag, got.tag)
         else:
             tag = got.tag
@@ -431,3 +447,21 @@
     raise LookupError(
         "Could not find doctest (only use this function *inside* a doctest)")
     
+__test__ = {
+    'basic': '''
+    >>> temp_install()
+    >>> print """<xml a="1" b="2">stuff</xml>"""
+    <xml b="2" a="1">...</xml>
+    >>> print """<xml xmlns="http://example.com"><tag   attr="bar"   /></xml>"""
+    <xml xmlns="...">
+      <tag attr="..." />
+    </xml>
+    >>> print """<xml>blahblahblah<foo /></xml>""" # doctest: +NOPARSE_MARKUP, +ELLIPSIS
+    <xml>...foo /></xml>
+    '''}
+
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()
+    
+    


More information about the lxml-checkins mailing list