Imported Upstream version 1.12.0
[platform/upstream/gtest.git] / googletest / test / gtest_xml_test_utils.py
1 # Copyright 2006, Google Inc.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """Unit test utilities for gtest_xml_output"""
31
32 import re
33 from xml.dom import minidom, Node
34 from googletest.test import gtest_test_utils
35
36 GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.xml'
37
38 class GTestXMLTestCase(gtest_test_utils.TestCase):
39   """
40   Base class for tests of Google Test's XML output functionality.
41   """
42
43
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
103   identifying_attribute = {
104       'testsuites': 'name',
105       'testsuite': 'name',
106       'testcase': 'name',
107       'failure': 'message',
108       'skipped': 'message',
109       'property': 'name',
110   }
111
112   def _GetChildren(self, element):
113     """
114     Fetches all of the child nodes of element, a DOM Element object.
115     Returns them as the values of a dictionary keyed by the IDs of the
116     children.  For <testsuites>, <testsuite>, <testcase>, and <property>
117     elements, the ID is the value of their "name" attribute; for <failure>
118     elements, it is the value of the "message" attribute; for <properties>
119     elements, it is the value of their parent's "name" attribute plus the
120     literal string "properties"; CDATA sections and non-whitespace
121     text nodes are concatenated into a single CDATA section with ID
122     "detail".  An exception is raised if any element other than the above
123     four is encountered, if two child elements with the same identifying
124     attributes are encountered, or if any other type of node is encountered.
125     """
126
127     children = {}
128     for child in element.childNodes:
129       if child.nodeType == Node.ELEMENT_NODE:
130         if child.tagName == 'properties':
131           self.assert_(child.parentNode is not None,
132                        'Encountered <properties> element without a parent')
133           child_id = child.parentNode.getAttribute('name') + '-properties'
134         else:
135           self.assert_(child.tagName in self.identifying_attribute,
136                        'Encountered unknown element <%s>' % child.tagName)
137           child_id = child.getAttribute(
138               self.identifying_attribute[child.tagName])
139         self.assert_(child_id not in children)
140         children[child_id] = child
141       elif child.nodeType in [Node.TEXT_NODE, Node.CDATA_SECTION_NODE]:
142         if 'detail' not in children:
143           if (child.nodeType == Node.CDATA_SECTION_NODE or
144               not child.nodeValue.isspace()):
145             children['detail'] = child.ownerDocument.createCDATASection(
146                 child.nodeValue)
147         else:
148           children['detail'].nodeValue += child.nodeValue
149       else:
150         self.fail('Encountered unexpected node type %d' % child.nodeType)
151     return children
152
153   def NormalizeXml(self, element):
154     """
155     Normalizes Google Test's XML output to eliminate references to transient
156     information that may change from run to run.
157
158     *  The "time" attribute of <testsuites>, <testsuite> and <testcase>
159        elements is replaced with a single asterisk, if it contains
160        only digit characters.
161     *  The "timestamp" attribute of <testsuites> elements is replaced with a
162        single asterisk, if it contains a valid ISO8601 datetime value.
163     *  The "type_param" attribute of <testcase> elements is replaced with a
164        single asterisk (if it sn non-empty) as it is the type name returned
165        by the compiler and is platform dependent.
166     *  The line info reported in the first line of the "message"
167        attribute and CDATA section of <failure> elements is replaced with the
168        file's basename and a single asterisk for the line number.
169     *  The directory names in file paths are removed.
170     *  The stack traces are removed.
171     """
172
173     if element.tagName == 'testcase':
174       source_file = element.getAttributeNode('file')
175       if source_file:
176         source_file.value = re.sub(r'^.*[/\\](.*)', '\\1', source_file.value)
177     if element.tagName in ('testsuites', 'testsuite', 'testcase'):
178       timestamp = element.getAttributeNode('timestamp')
179       timestamp.value = re.sub(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$',
180                                '*', timestamp.value)
181     if element.tagName in ('testsuites', 'testsuite', 'testcase'):
182       time = element.getAttributeNode('time')
183       time.value = re.sub(r'^\d+(\.\d+)?$', '*', time.value)
184       type_param = element.getAttributeNode('type_param')
185       if type_param and type_param.value:
186         type_param.value = '*'
187     elif element.tagName == 'failure' or element.tagName == 'skipped':
188       source_line_pat = r'^.*[/\\](.*:)\d+\n'
189       # Replaces the source line information with a normalized form.
190       message = element.getAttributeNode('message')
191       message.value = re.sub(source_line_pat, '\\1*\n', message.value)
192       for child in element.childNodes:
193         if child.nodeType == Node.CDATA_SECTION_NODE:
194           # Replaces the source line information with a normalized form.
195           cdata = re.sub(source_line_pat, '\\1*\n', child.nodeValue)
196           # Removes the actual stack trace.
197           child.nodeValue = re.sub(r'Stack trace:\n(.|\n)*',
198                                    'Stack trace:\n*', cdata)
199     for child in element.childNodes:
200       if child.nodeType == Node.ELEMENT_NODE:
201         self.NormalizeXml(child)