Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | # |
michael@0 | 3 | # Copyright 2006, Google Inc. |
michael@0 | 4 | # All rights reserved. |
michael@0 | 5 | # |
michael@0 | 6 | # Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | # modification, are permitted provided that the following conditions are |
michael@0 | 8 | # met: |
michael@0 | 9 | # |
michael@0 | 10 | # * Redistributions of source code must retain the above copyright |
michael@0 | 11 | # notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | # * Redistributions in binary form must reproduce the above |
michael@0 | 13 | # copyright notice, this list of conditions and the following disclaimer |
michael@0 | 14 | # in the documentation and/or other materials provided with the |
michael@0 | 15 | # distribution. |
michael@0 | 16 | # * Neither the name of Google Inc. nor the names of its |
michael@0 | 17 | # contributors may be used to endorse or promote products derived from |
michael@0 | 18 | # this software without specific prior written permission. |
michael@0 | 19 | # |
michael@0 | 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | |
michael@0 | 32 | """Unit test utilities for gtest_xml_output""" |
michael@0 | 33 | |
michael@0 | 34 | __author__ = 'eefacm@gmail.com (Sean Mcafee)' |
michael@0 | 35 | |
michael@0 | 36 | import re |
michael@0 | 37 | from xml.dom import minidom, Node |
michael@0 | 38 | |
michael@0 | 39 | import gtest_test_utils |
michael@0 | 40 | |
michael@0 | 41 | |
michael@0 | 42 | GTEST_OUTPUT_FLAG = '--gtest_output' |
michael@0 | 43 | GTEST_DEFAULT_OUTPUT_FILE = 'test_detail.xml' |
michael@0 | 44 | |
michael@0 | 45 | class GTestXMLTestCase(gtest_test_utils.TestCase): |
michael@0 | 46 | """ |
michael@0 | 47 | Base class for tests of Google Test's XML output functionality. |
michael@0 | 48 | """ |
michael@0 | 49 | |
michael@0 | 50 | |
michael@0 | 51 | def AssertEquivalentNodes(self, expected_node, actual_node): |
michael@0 | 52 | """ |
michael@0 | 53 | Asserts that actual_node (a DOM node object) is equivalent to |
michael@0 | 54 | expected_node (another DOM node object), in that either both of |
michael@0 | 55 | them are CDATA nodes and have the same value, or both are DOM |
michael@0 | 56 | elements and actual_node meets all of the following conditions: |
michael@0 | 57 | |
michael@0 | 58 | * It has the same tag name as expected_node. |
michael@0 | 59 | * It has the same set of attributes as expected_node, each with |
michael@0 | 60 | the same value as the corresponding attribute of expected_node. |
michael@0 | 61 | Exceptions are any attribute named "time", which needs only be |
michael@0 | 62 | convertible to a floating-point number and any attribute named |
michael@0 | 63 | "type_param" which only has to be non-empty. |
michael@0 | 64 | * It has an equivalent set of child nodes (including elements and |
michael@0 | 65 | CDATA sections) as expected_node. Note that we ignore the |
michael@0 | 66 | order of the children as they are not guaranteed to be in any |
michael@0 | 67 | particular order. |
michael@0 | 68 | """ |
michael@0 | 69 | |
michael@0 | 70 | if expected_node.nodeType == Node.CDATA_SECTION_NODE: |
michael@0 | 71 | self.assertEquals(Node.CDATA_SECTION_NODE, actual_node.nodeType) |
michael@0 | 72 | self.assertEquals(expected_node.nodeValue, actual_node.nodeValue) |
michael@0 | 73 | return |
michael@0 | 74 | |
michael@0 | 75 | self.assertEquals(Node.ELEMENT_NODE, actual_node.nodeType) |
michael@0 | 76 | self.assertEquals(Node.ELEMENT_NODE, expected_node.nodeType) |
michael@0 | 77 | self.assertEquals(expected_node.tagName, actual_node.tagName) |
michael@0 | 78 | |
michael@0 | 79 | expected_attributes = expected_node.attributes |
michael@0 | 80 | actual_attributes = actual_node .attributes |
michael@0 | 81 | self.assertEquals( |
michael@0 | 82 | expected_attributes.length, actual_attributes.length, |
michael@0 | 83 | 'attribute numbers differ in element ' + actual_node.tagName) |
michael@0 | 84 | for i in range(expected_attributes.length): |
michael@0 | 85 | expected_attr = expected_attributes.item(i) |
michael@0 | 86 | actual_attr = actual_attributes.get(expected_attr.name) |
michael@0 | 87 | self.assert_( |
michael@0 | 88 | actual_attr is not None, |
michael@0 | 89 | 'expected attribute %s not found in element %s' % |
michael@0 | 90 | (expected_attr.name, actual_node.tagName)) |
michael@0 | 91 | self.assertEquals(expected_attr.value, actual_attr.value, |
michael@0 | 92 | ' values of attribute %s in element %s differ' % |
michael@0 | 93 | (expected_attr.name, actual_node.tagName)) |
michael@0 | 94 | |
michael@0 | 95 | expected_children = self._GetChildren(expected_node) |
michael@0 | 96 | actual_children = self._GetChildren(actual_node) |
michael@0 | 97 | self.assertEquals( |
michael@0 | 98 | len(expected_children), len(actual_children), |
michael@0 | 99 | 'number of child elements differ in element ' + actual_node.tagName) |
michael@0 | 100 | for child_id, child in expected_children.iteritems(): |
michael@0 | 101 | self.assert_(child_id in actual_children, |
michael@0 | 102 | '<%s> is not in <%s> (in element %s)' % |
michael@0 | 103 | (child_id, actual_children, actual_node.tagName)) |
michael@0 | 104 | self.AssertEquivalentNodes(child, actual_children[child_id]) |
michael@0 | 105 | |
michael@0 | 106 | identifying_attribute = { |
michael@0 | 107 | 'testsuites': 'name', |
michael@0 | 108 | 'testsuite': 'name', |
michael@0 | 109 | 'testcase': 'name', |
michael@0 | 110 | 'failure': 'message', |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | def _GetChildren(self, element): |
michael@0 | 114 | """ |
michael@0 | 115 | Fetches all of the child nodes of element, a DOM Element object. |
michael@0 | 116 | Returns them as the values of a dictionary keyed by the IDs of the |
michael@0 | 117 | children. For <testsuites>, <testsuite> and <testcase> elements, the ID |
michael@0 | 118 | is the value of their "name" attribute; for <failure> elements, it is |
michael@0 | 119 | the value of the "message" attribute; CDATA sections and non-whitespace |
michael@0 | 120 | text nodes are concatenated into a single CDATA section with ID |
michael@0 | 121 | "detail". An exception is raised if any element other than the above |
michael@0 | 122 | four is encountered, if two child elements with the same identifying |
michael@0 | 123 | attributes are encountered, or if any other type of node is encountered. |
michael@0 | 124 | """ |
michael@0 | 125 | |
michael@0 | 126 | children = {} |
michael@0 | 127 | for child in element.childNodes: |
michael@0 | 128 | if child.nodeType == Node.ELEMENT_NODE: |
michael@0 | 129 | self.assert_(child.tagName in self.identifying_attribute, |
michael@0 | 130 | 'Encountered unknown element <%s>' % child.tagName) |
michael@0 | 131 | childID = child.getAttribute(self.identifying_attribute[child.tagName]) |
michael@0 | 132 | self.assert_(childID not in children) |
michael@0 | 133 | children[childID] = child |
michael@0 | 134 | elif child.nodeType in [Node.TEXT_NODE, Node.CDATA_SECTION_NODE]: |
michael@0 | 135 | if 'detail' not in children: |
michael@0 | 136 | if (child.nodeType == Node.CDATA_SECTION_NODE or |
michael@0 | 137 | not child.nodeValue.isspace()): |
michael@0 | 138 | children['detail'] = child.ownerDocument.createCDATASection( |
michael@0 | 139 | child.nodeValue) |
michael@0 | 140 | else: |
michael@0 | 141 | children['detail'].nodeValue += child.nodeValue |
michael@0 | 142 | else: |
michael@0 | 143 | self.fail('Encountered unexpected node type %d' % child.nodeType) |
michael@0 | 144 | return children |
michael@0 | 145 | |
michael@0 | 146 | def NormalizeXml(self, element): |
michael@0 | 147 | """ |
michael@0 | 148 | Normalizes Google Test's XML output to eliminate references to transient |
michael@0 | 149 | information that may change from run to run. |
michael@0 | 150 | |
michael@0 | 151 | * The "time" attribute of <testsuites>, <testsuite> and <testcase> |
michael@0 | 152 | elements is replaced with a single asterisk, if it contains |
michael@0 | 153 | only digit characters. |
michael@0 | 154 | * The "timestamp" attribute of <testsuites> elements is replaced with a |
michael@0 | 155 | single asterisk, if it contains a valid ISO8601 datetime value. |
michael@0 | 156 | * The "type_param" attribute of <testcase> elements is replaced with a |
michael@0 | 157 | single asterisk (if it sn non-empty) as it is the type name returned |
michael@0 | 158 | by the compiler and is platform dependent. |
michael@0 | 159 | * The line info reported in the first line of the "message" |
michael@0 | 160 | attribute and CDATA section of <failure> elements is replaced with the |
michael@0 | 161 | file's basename and a single asterisk for the line number. |
michael@0 | 162 | * The directory names in file paths are removed. |
michael@0 | 163 | * The stack traces are removed. |
michael@0 | 164 | """ |
michael@0 | 165 | |
michael@0 | 166 | if element.tagName == 'testsuites': |
michael@0 | 167 | timestamp = element.getAttributeNode('timestamp') |
michael@0 | 168 | timestamp.value = re.sub(r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d$', |
michael@0 | 169 | '*', timestamp.value) |
michael@0 | 170 | if element.tagName in ('testsuites', 'testsuite', 'testcase'): |
michael@0 | 171 | time = element.getAttributeNode('time') |
michael@0 | 172 | time.value = re.sub(r'^\d+(\.\d+)?$', '*', time.value) |
michael@0 | 173 | type_param = element.getAttributeNode('type_param') |
michael@0 | 174 | if type_param and type_param.value: |
michael@0 | 175 | type_param.value = '*' |
michael@0 | 176 | elif element.tagName == 'failure': |
michael@0 | 177 | source_line_pat = r'^.*[/\\](.*:)\d+\n' |
michael@0 | 178 | # Replaces the source line information with a normalized form. |
michael@0 | 179 | message = element.getAttributeNode('message') |
michael@0 | 180 | message.value = re.sub(source_line_pat, '\\1*\n', message.value) |
michael@0 | 181 | for child in element.childNodes: |
michael@0 | 182 | if child.nodeType == Node.CDATA_SECTION_NODE: |
michael@0 | 183 | # Replaces the source line information with a normalized form. |
michael@0 | 184 | cdata = re.sub(source_line_pat, '\\1*\n', child.nodeValue) |
michael@0 | 185 | # Removes the actual stack trace. |
michael@0 | 186 | child.nodeValue = re.sub(r'\nStack trace:\n(.|\n)*', |
michael@0 | 187 | '', cdata) |
michael@0 | 188 | for child in element.childNodes: |
michael@0 | 189 | if child.nodeType == Node.ELEMENT_NODE: |
michael@0 | 190 | self.NormalizeXml(child) |