44 def AssertEquivalentNodes(self, expected_node, actual_node):
45 """
46 Asserts that actual_node (a DOM node object) is equivalent to
47 expected_node (another DOM node object), in that either both of
48 them are CDATA nodes and have the same value, or both are DOM
49 elements and actual_node meets all of the following conditions:
50
51 * It has the same tag name as expected_node.
52 * It has the same set of attributes as expected_node, each with
53 the same value as the corresponding attribute of expected_node.
54 Exceptions are any attribute named "time", which needs only be
55 convertible to a floating-point number and any attribute named
56 "type_param" which only has to be non-empty.
57 * It has an equivalent set of child nodes (including elements and
58 CDATA sections) as expected_node. Note that we ignore the
59 order of the children as they are not guaranteed to be in any
60 particular order.
61 """
62
63 if expected_node.nodeType == Node.CDATA_SECTION_NODE:
64 self.assertEquals(Node.CDATA_SECTION_NODE, actual_node.nodeType)
65 self.assertEquals(expected_node.nodeValue, actual_node.nodeValue)
66 return
67
68 self.assertEquals(Node.ELEMENT_NODE, actual_node.nodeType)
69 self.assertEquals(Node.ELEMENT_NODE, expected_node.nodeType)
70 self.assertEquals(expected_node.tagName, actual_node.tagName)
71
72 expected_attributes = expected_node.attributes
73 actual_attributes = actual_node.attributes
74 self.assertEquals(
75 expected_attributes.length, actual_attributes.length,
76 'attribute numbers differ in element %s:\nExpected: %r\nActual: %r' % (
77 actual_node.tagName, expected_attributes.keys(),
78 actual_attributes.keys()))
79 for i in range(expected_attributes.length):
80 expected_attr = expected_attributes.item(i)
81 actual_attr = actual_attributes.get(expected_attr.name)
82 self.assert_(
83 actual_attr is not None,
84 'expected attribute %s not found in element %s' %
85 (expected_attr.name, actual_node.tagName))
86 self.assertEquals(
87 expected_attr.value, actual_attr.value,
88 ' values of attribute %s in element %s differ: %s vs %s' %
89 (expected_attr.name, actual_node.tagName,
90 expected_attr.value, actual_attr.value))
91
92 expected_children = self._GetChildren(expected_node)
93 actual_children = self._GetChildren(actual_node)
94 self.assertEquals(
95 len(expected_children), len(actual_children),
96 'number of child elements differ in element ' + actual_node.tagName)
97 for child_id, child in expected_children.items():
98 self.assert_(child_id in actual_children,
99 '<%s> is not in <%s> (in element %s)' %
100 (child_id, actual_children, actual_node.tagName))
101 self.AssertEquivalentNodes(child, actual_children[child_id])
102