Author: russellm
Date: 2008-07-20 00:46:41 -0500 (Sun, 20 Jul 2008)
New Revision: 8003

Modified:
   django/trunk/django/test/testcases.py
   django/trunk/tests/regressiontests/test_utils/tests.py
Log:
Fixed #7441 -- Removed some of the shortcuts in the doctest output comparators, 
and added a wrapper to allow comparison of xml fragments. Thanks to Leo Soto 
for the report and fix.

Modified: django/trunk/django/test/testcases.py
===================================================================
--- django/trunk/django/test/testcases.py       2008-07-20 01:48:23 UTC (rev 
8002)
+++ django/trunk/django/test/testcases.py       2008-07-20 05:46:41 UTC (rev 
8003)
@@ -58,19 +58,10 @@
         
         Based on http://codespeak.net/svn/lxml/trunk/src/lxml/doctestcompare.py
         """
-        
-        # We use this to distinguish the output of repr() from an XML element:
-        _repr_re = re.compile(r'^<[^>]+ (at|object) ')
-
         _norm_whitespace_re = re.compile(r'[ \t\n][ \t\n]+')
         def norm_whitespace(v):
             return _norm_whitespace_re.sub(' ', v)
 
-        def looks_like_xml(s):
-            s = s.strip()
-            return (s.startswith('<')
-                    and not _repr_re.search(s))
-
         def child_text(element):
             return ''.join([c.data for c in element.childNodes
                             if c.nodeType == Node.TEXT_NODE])
@@ -104,12 +95,14 @@
         want, got = self._strip_quotes(want, got)
         want = want.replace('\\n','\n')
         got = got.replace('\\n','\n')
-        
-        # If what we want doesn't look like markup, don't bother trying
-        # to parse it.
-        if not looks_like_xml(want):
-            return False
 
+        # If the string is not a complete xml document, we may need to add a
+        # root element. This allow us to compare fragments, like "<foo/><bar/>"
+        if not want.startswith('<?xml'):
+            wrapper = '<root>%s</root>'
+            want = wrapper % want
+            got = wrapper % got
+            
         # Parse the want and got strings, and compare the parsings.
         try:
             want_root = parseString(want).firstChild

Modified: django/trunk/tests/regressiontests/test_utils/tests.py
===================================================================
--- django/trunk/tests/regressiontests/test_utils/tests.py      2008-07-20 
01:48:23 UTC (rev 8002)
+++ django/trunk/tests/regressiontests/test_utils/tests.py      2008-07-20 
05:46:41 UTC (rev 8003)
@@ -29,6 +29,16 @@
 ...     xml.endDocument()
 ...     return stream.getvalue()
 
+>>> def produce_xml_fragment():
+...     stream = StringIO()
+...     xml = SimplerXMLGenerator(stream, encoding='utf-8')
+...     xml.startElement("foo", {"aaa" : "1.0", "bbb": "2.0"})
+...     xml.characters("Hello")
+...     xml.endElement("foo")
+...     xml.startElement("bar", {})
+...     xml.endElement("bar")
+...     return stream.getvalue()
+
 # Long values are normalized and are comparable to normal integers ...
 >>> produce_long()
 42
@@ -53,5 +63,10 @@
 >>> produce_xml()
 '<?xml version="1.0" encoding="UTF-8"?>\n<foo bbb="2.0" aaa="1.0"><bar 
ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
 
+>>> produce_xml_fragment()
+'<foo aaa="1.0" bbb="2.0">Hello</foo><bar></bar>'
 
+>>> produce_xml_fragment()
+'<foo bbb="2.0" aaa="1.0">Hello</foo><bar></bar>'
+
 """
\ No newline at end of file


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to