Changeset 8003
- Timestamp:
- 07/20/08 00:46:41 (4 months ago)
- Files:
-
- django/trunk/django/test/testcases.py (modified) (2 diffs)
- django/trunk/tests/regressiontests/test_utils/tests.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/test/testcases.py
r7981 r8003 59 59 Based on http://codespeak.net/svn/lxml/trunk/src/lxml/doctestcompare.py 60 60 """ 61 62 # We use this to distinguish the output of repr() from an XML element:63 _repr_re = re.compile(r'^<[^>]+ (at|object) ')64 65 61 _norm_whitespace_re = re.compile(r'[ \t\n][ \t\n]+') 66 62 def norm_whitespace(v): 67 63 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))73 64 74 65 def child_text(element): … … 105 96 want = want.replace('\\n','\n') 106 97 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 113 106 # Parse the want and got strings, and compare the parsings. 114 107 try: django/trunk/tests/regressiontests/test_utils/tests.py
r7981 r8003 30 30 ... return stream.getvalue() 31 31 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 32 42 # Long values are normalized and are comparable to normal integers ... 33 43 >>> produce_long() … … 54 64 '<?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>' 55 65 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>' 56 71 57 72 """
