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
32import re
33from xml.dom import minidom, Node
34from googletest.test import gtest_test_utils
35
36GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.xml'
37
38
39class GTestXMLTestCase(gtest_test_utils.TestCase):
40  """Base class for tests of Google Test's XML output functionality."""
41
42  def AssertEquivalentNodes(self, expected_node, actual_node):
43    """Asserts that actual_node is equivalent to expected_node.
44
45    Asserts that actual_node (a DOM node object) is equivalent to
46    expected_node (another DOM node object), in that either both of
47    them are CDATA nodes and have the same value, or both are DOM
48    elements and actual_node meets all of the following conditions:
49
50    *  It has the same tag name as expected_node.
51    *  It has the same set of attributes as expected_node, each with
52       the same value as the corresponding attribute of expected_node.
53       Exceptions are any attribute named "time", which needs only be
54       convertible to a floating-point number and any attribute named
55       "type_param" which only has to be non-empty.
56    *  It has an equivalent set of child nodes (including elements and
57       CDATA sections) as expected_node.  Note that we ignore the
58       order of the children as they are not guaranteed to be in any
59       particular order.
60
61    Args:
62      expected_node: expected DOM node object
63      actual_node: actual DOM node object
64    """
65
66    if expected_node.nodeType == Node.CDATA_SECTION_NODE:
67      self.assertEqual(Node.CDATA_SECTION_NODE, actual_node.nodeType)
68      self.assertEqual(expected_node.nodeValue, actual_node.nodeValue)
69      return
70
71    self.assertEqual(Node.ELEMENT_NODE, actual_node.nodeType)
72    self.assertEqual(Node.ELEMENT_NODE, expected_node.nodeType)
73    self.assertEqual(expected_node.tagName, actual_node.tagName)
74
75    expected_attributes = expected_node.attributes
76    actual_attributes = actual_node.attributes
77    self.assertEqual(
78        expected_attributes.length,
79        actual_attributes.length,
80        'attribute numbers differ in element %s:\nExpected: %r\nActual: %r'
81        % (
82            actual_node.tagName,
83            expected_attributes.keys(),
84            actual_attributes.keys(),
85        ),
86    )
87    for i in range(expected_attributes.length):
88      expected_attr = expected_attributes.item(i)
89      actual_attr = actual_attributes.get(expected_attr.name)
90      self.assertTrue(
91          actual_attr is not None,
92          'expected attribute %s not found in element %s'
93          % (expected_attr.name, actual_node.tagName),
94      )
95      self.assertEqual(
96          expected_attr.value,
97          actual_attr.value,
98          ' values of attribute %s in element %s differ: %s vs %s'
99          % (
100              expected_attr.name,
101              actual_node.tagName,
102              expected_attr.value,
103              actual_attr.value,
104          ),
105      )
106
107    expected_children = self._GetChildren(expected_node)
108    actual_children = self._GetChildren(actual_node)
109    self.assertEqual(
110        len(expected_children),
111        len(actual_children),
112        'number of child elements differ in element ' + actual_node.tagName,
113    )
114    for child_id, child in expected_children.items():
115      self.assertTrue(
116          child_id in actual_children,
117          '<%s> is not in <%s> (in element %s)'
118          % (child_id, actual_children, actual_node.tagName),
119      )
120      self.AssertEquivalentNodes(child, actual_children[child_id])
121
122  identifying_attribute = {
123      'testsuites': 'name',
124      'testsuite': 'name',
125      'testcase': 'name',
126      'failure': 'message',
127      'skipped': 'message',
128      'property': 'name',
129  }
130
131  def _GetChildren(self, element):
132    """Fetches all of the child nodes of element, a DOM Element object.
133
134    Returns them as the values of a dictionary keyed by the IDs of the children.
135    For <testsuites>, <testsuite>, <testcase>, and <property> elements, the ID
136    is the value of their "name" attribute; for <failure> elements, it is the
137    value of the "message" attribute; for <properties> elements, it is the value
138    of their parent's "name" attribute plus the literal string "properties";
139    CDATA sections and non-whitespace text nodes are concatenated into a single
140    CDATA section with ID "detail".  An exception is raised if any element other
141    than the above four is encountered, if two child elements with the same
142    identifying attributes are encountered, or if any other type of node is
143    encountered.
144
145    Args:
146      element: DOM Element object
147
148    Returns:
149      Dictionary where keys are the IDs of the children.
150    """
151
152    children = {}
153    for child in element.childNodes:
154      if child.nodeType == Node.ELEMENT_NODE:
155        if child.tagName == 'properties':
156          self.assertTrue(
157              child.parentNode is not None,
158              'Encountered <properties> element without a parent',
159          )
160          child_id = child.parentNode.getAttribute('name') + '-properties'
161        else:
162          self.assertTrue(
163              child.tagName in self.identifying_attribute,
164              'Encountered unknown element <%s>' % child.tagName,
165          )
166          child_id = child.getAttribute(
167              self.identifying_attribute[child.tagName]
168          )
169        self.assertNotIn(child_id, children)
170        children[child_id] = child
171      elif child.nodeType in [Node.TEXT_NODE, Node.CDATA_SECTION_NODE]:
172        if 'detail' not in children:
173          if (
174              child.nodeType == Node.CDATA_SECTION_NODE
175              or not child.nodeValue.isspace()
176          ):
177            children['detail'] = child.ownerDocument.createCDATASection(
178                child.nodeValue
179            )
180        else:
181          children['detail'].nodeValue += child.nodeValue
182      else:
183        self.fail('Encountered unexpected node type %d' % child.nodeType)
184    return children
185
186  def NormalizeXml(self, element):
187    """Normalizes XML that may change from run to run.
188
189    Normalizes Google Test's XML output to eliminate references to transient
190    information that may change from run to run.
191
192    *  The "time" attribute of <testsuites>, <testsuite> and <testcase>
193       elements is replaced with a single asterisk, if it contains
194       only digit characters.
195    *  The "timestamp" attribute of <testsuites> elements is replaced with a
196       single asterisk, if it contains a valid ISO8601 datetime value.
197    *  The "type_param" attribute of <testcase> elements is replaced with a
198       single asterisk (if it sn non-empty) as it is the type name returned
199       by the compiler and is platform dependent.
200    *  The line info reported in the first line of the "message"
201       attribute and CDATA section of <failure> elements is replaced with the
202       file's basename and a single asterisk for the line number.
203    *  The directory names in file paths are removed.
204    *  The stack traces are removed.
205
206    Args:
207      element: DOM element to normalize
208    """
209
210    if element.tagName == 'testcase':
211      source_file = element.getAttributeNode('file')
212      if source_file:
213        source_file.value = re.sub(r'^.*[/\\](.*)', '\\1', source_file.value)
214    if element.tagName in ('testsuites', 'testsuite', 'testcase'):
215      timestamp = element.getAttributeNode('timestamp')
216      timestamp.value = re.sub(
217          r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d$', '*', timestamp.value
218      )
219    if element.tagName in ('testsuites', 'testsuite', 'testcase'):
220      time = element.getAttributeNode('time')
221      # The value for exact N seconds has a trailing decimal point (e.g., "10."
222      # instead of "10")
223      time.value = re.sub(r'^\d+\.(\d+)?$', '*', time.value)
224      type_param = element.getAttributeNode('type_param')
225      if type_param and type_param.value:
226        type_param.value = '*'
227    elif element.tagName == 'failure' or element.tagName == 'skipped':
228      source_line_pat = r'^.*[/\\](.*:)\d+\n'
229      # Replaces the source line information with a normalized form.
230      message = element.getAttributeNode('message')
231      message.value = re.sub(source_line_pat, '\\1*\n', message.value)
232      for child in element.childNodes:
233        if child.nodeType == Node.CDATA_SECTION_NODE:
234          # Replaces the source line information with a normalized form.
235          cdata = re.sub(source_line_pat, '\\1*\n', child.nodeValue)
236          # Removes the actual stack trace.
237          child.nodeValue = re.sub(
238              r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', cdata
239          )
240    for child in element.childNodes:
241      if child.nodeType == Node.ELEMENT_NODE:
242        self.NormalizeXml(child)
243