5
import util.doctest24 as doctest
7
import xml.etree.ElementTree as ET
9
import elementtree.ElementTree as ET
10
from xml.parsers.expat import ExpatError as XMLParseError
12
RealOutputChecker = doctest.OutputChecker
16
print >> sys.stderr, ' '.join(map(str, msg))
18
class HTMLOutputChecker(RealOutputChecker):
20
def check_output(self, want, got, optionflags):
21
normal = RealOutputChecker.check_output(self, want, got, optionflags)
25
want_xml = make_xml(want)
26
except XMLParseError, e:
30
got_xml = make_xml(got)
34
if xml_compare(want_xml, got_xml):
38
def output_difference(self, example, got, optionflags):
39
actual = RealOutputChecker.output_difference(
40
self, example, got, optionflags)
41
want_xml = got_xml = None
43
want_xml = make_xml(example.want)
44
want_norm = make_string(want_xml)
45
except XMLParseError, e:
46
if example.want.startswith('<'):
47
want_norm = '(bad XML: %s)' % e
48
# '<xml>%s</xml>' % example.want
52
got_xml = make_xml(got)
53
got_norm = make_string(got_xml)
54
except XMLParseError, e:
55
if example.want.startswith('<'):
56
got_norm = '(bad XML: %s)' % e
59
s = '%s\nXML Wanted: %s\nXML Got : %s\n' % (
60
actual, want_norm, got_norm)
61
if got_xml and want_xml:
63
xml_compare(want_xml, got_xml, result.append)
64
s += 'Difference report:\n%s\n' % '\n'.join(result)
67
def xml_compare(x1, x2, reporter=None):
70
reporter('Tags do not match: %s and %s' % (x1.tag, x2.tag))
72
for name, value in x1.attrib.items():
73
if x2.attrib.get(name) != value:
75
reporter('Attributes do not match: %s=%r, %s=%r'
76
% (name, value, name, x2.attrib.get(name)))
78
for name in x2.attrib.keys():
79
if not x1.attrib.has_key(name):
81
reporter('x2 has an attribute x1 is missing: %s'
84
if not text_compare(x1.text, x2.text):
86
reporter('text: %r != %r' % (x1.text, x2.text))
88
if not text_compare(x1.tail, x2.tail):
90
reporter('tail: %r != %r' % (x1.tail, x2.tail))
92
cl1 = x1.getchildren()
93
cl2 = x2.getchildren()
94
if len(cl1) != len(cl2):
96
reporter('children length differs, %i != %i'
97
% (len(cl1), len(cl2)))
100
for c1, c2 in zip(cl1, cl2):
102
if not xml_compare(c1, c2, reporter=reporter):
104
reporter('children %i do not match: %s'
109
def text_compare(t1, t2):
110
if not t1 and not t2:
112
if t1 == '*' or t2 == '*':
114
return (t1 or '').strip() == (t2 or '').strip()
117
return ET.XML('<xml>%s</xml>' % s)
120
if isinstance(xml, (str, unicode)):
125
assert s.startswith('<xml>') and s.endswith('</xml>'), repr(s)
129
doctest.OutputChecker = HTMLOutputChecker