Django

Code

Changeset 8003

Show
Ignore:
Timestamp:
07/20/08 00:46:41 (4 months ago)
Author:
russellm
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/testcases.py

    r7981 r8003  
    5959        Based on http://codespeak.net/svn/lxml/trunk/src/lxml/doctestcompare.py 
    6060        """ 
    61          
    62         # We use this to distinguish the output of repr() from an XML element: 
    63         _repr_re = re.compile(r'^<[^>]+ (at|object) ') 
    64  
    6561        _norm_whitespace_re = re.compile(r'[ \t\n][ \t\n]+') 
    6662        def norm_whitespace(v): 
    6763            return _norm_whitespace_re.sub(' ', v) 
    68  
    69         def looks_like_xml(s): 
    70             s = s.strip() 
    71             return (s.startswith('<') 
    72                     and not _repr_re.search(s)) 
    7364 
    7465        def child_text(element): 
     
    10596        want = want.replace('\\n','\n') 
    10697        got = got.replace('\\n','\n') 
    107          
    108         # If what we want doesn't look like markup, don't bother trying 
    109         # to parse it. 
    110         if not looks_like_xml(want): 
    111             return False 
    112  
     98 
     99        # If the string is not a complete xml document, we may need to add a 
     100        # root element. This allow us to compare fragments, like "<foo/><bar/>" 
     101        if not want.startswith('<?xml'): 
     102            wrapper = '<root>%s</root>' 
     103            want = wrapper % want 
     104            got = wrapper % got 
     105             
    113106        # Parse the want and got strings, and compare the parsings. 
    114107        try: 
  • django/trunk/tests/regressiontests/test_utils/tests.py

    r7981 r8003  
    3030...     return stream.getvalue() 
    3131 
     32>>> def produce_xml_fragment(): 
     33...     stream = StringIO() 
     34...     xml = SimplerXMLGenerator(stream, encoding='utf-8') 
     35...     xml.startElement("foo", {"aaa" : "1.0", "bbb": "2.0"}) 
     36...     xml.characters("Hello") 
     37...     xml.endElement("foo") 
     38...     xml.startElement("bar", {}) 
     39...     xml.endElement("bar") 
     40...     return stream.getvalue() 
     41 
    3242# Long values are normalized and are comparable to normal integers ... 
    3343>>> produce_long() 
     
    5464'<?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>' 
    5565 
     66>>> produce_xml_fragment() 
     67'<foo aaa="1.0" bbb="2.0">Hello</foo><bar></bar>' 
     68 
     69>>> produce_xml_fragment() 
     70'<foo bbb="2.0" aaa="1.0">Hello</foo><bar></bar>' 
    5671 
    5772"""