gmock_output_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2008, Google Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following disclaimer
14 # in the documentation and/or other materials provided with the
15 # distribution.
16 # * Neither the name of Google Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 r"""Tests the text output of Google C++ Mocking Framework.
33 
34 To update the golden file:
35 gmock_output_test.py --build_dir=BUILD/DIR --gengolden
36 where BUILD/DIR contains the built gmock_output_test_ file.
37 gmock_output_test.py --gengolden
38 gmock_output_test.py
39 
40 """
41 
42 import os
43 import re
44 import sys
45 import gmock_test_utils
46 
47 
48 # The flag for generating the golden file
49 GENGOLDEN_FLAG = '--gengolden'
50 
51 PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_output_test_')
52 COMMAND = [PROGRAM_PATH, '--gtest_stack_trace_depth=0', '--gtest_print_time=0']
53 GOLDEN_NAME = 'gmock_output_test_golden.txt'
54 GOLDEN_PATH = os.path.join(gmock_test_utils.GetSourceDir(), GOLDEN_NAME)
55 
56 
58  """Changes all Windows/Mac line endings in s to UNIX line endings."""
59 
60  return s.replace('\r\n', '\n').replace('\r', '\n')
61 
62 
64  """Removes Google Test result report's header and footer from the output."""
65 
66  output = re.sub(r'.*gtest_main.*\n', '', output)
67  output = re.sub(r'\[.*\d+ tests.*\n', '', output)
68  output = re.sub(r'\[.* test environment .*\n', '', output)
69  output = re.sub(r'\[=+\] \d+ tests .* ran.*', '', output)
70  output = re.sub(r'.* FAILED TESTS\n', '', output)
71  return output
72 
73 
74 def RemoveLocations(output):
75  """Removes all file location info from a Google Test program's output.
76 
77  Args:
78  output: the output of a Google Test program.
79 
80  Returns:
81  output with all file location info (in the form of
82  'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or
83  'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by
84  'FILE:#: '.
85  """
86 
87  return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\:', 'FILE:#:', output)
88 
89 
91  """Normalizes the error marker, which is different on Windows vs on Linux."""
92 
93  return re.sub(r' error: ', ' Failure\n', output)
94 
95 
97  """Removes memory addresses from the test output."""
98 
99  return re.sub(r'@\w+', '@0x#', output)
100 
101 
103  """Removes the test names of leaked mock objects from the test output."""
104 
105  return re.sub(r'\(used in test .+\) ', '', output)
106 
107 
108 def GetLeakyTests(output):
109  """Returns a list of test names that leak mock objects."""
110 
111  # findall() returns a list of all matches of the regex in output.
112  # For example, if '(used in test FooTest.Bar)' is in output, the
113  # list will contain 'FooTest.Bar'.
114  return re.findall(r'\(used in test (.+)\)', output)
115 
116 
118  """Normalizes the output of gmock_output_test_.
119 
120  Args:
121  output: The test output.
122 
123  Returns:
124  A tuple (the normalized test output, the list of test names that have
125  leaked mocks).
126  """
127 
128  output = ToUnixLineEnding(output)
129  output = RemoveReportHeaderAndFooter(output)
130  output = NormalizeErrorMarker(output)
131  output = RemoveLocations(output)
132  output = RemoveMemoryAddresses(output)
133  return (RemoveTestNamesOfLeakedMocks(output), GetLeakyTests(output))
134 
135 
137  """Runs a command in a sub-process, and returns its STDOUT in a string."""
138 
139  return gmock_test_utils.Subprocess(cmd, capture_stderr=False).output
140 
141 
143  """Runs a command and returns its normalized output and a list of leaky tests.
144 
145  Args:
146  cmd: the shell command.
147  """
148 
149  # Disables exception pop-ups on Windows.
150  os.environ['GTEST_CATCH_EXCEPTIONS'] = '1'
152 
153 
155  def testOutput(self):
156  (output, leaky_tests) = GetNormalizedCommandOutputAndLeakyTests(COMMAND)
157  golden_file = open(GOLDEN_PATH, 'rb')
158  golden = golden_file.read()
159  golden_file.close()
160 
161  # The normalized output should match the golden file.
162  self.assertEquals(golden, output)
163 
164  # The raw output should contain 2 leaked mock object errors for
165  # test GMockOutputTest.CatchesLeakedMocks.
166  self.assertEquals(['GMockOutputTest.CatchesLeakedMocks',
167  'GMockOutputTest.CatchesLeakedMocks'],
168  leaky_tests)
169 
170 
171 if __name__ == '__main__':
172  if sys.argv[1:] == [GENGOLDEN_FLAG]:
174  golden_file = open(GOLDEN_PATH, 'wb')
175  golden_file.write(output)
176  golden_file.close()
177  # Suppress the error "googletest was imported but a call to its main()
178  # was never detected."
179  os._exit(0)
180  else:
gmock_output_test.RemoveLocations
def RemoveLocations(output)
Definition: gmock_output_test.py:74
gmock_test_utils.GetSourceDir
def GetSourceDir()
Definition: gmock_test_utils.py:50
gmock_output_test.GetNormalizedOutputAndLeakyTests
def GetNormalizedOutputAndLeakyTests(output)
Definition: gmock_output_test.py:117
gmock_output_test.NormalizeErrorMarker
def NormalizeErrorMarker(output)
Definition: gmock_output_test.py:90
gmock_test_utils.Subprocess
Subprocess
Definition: gmock_test_utils.py:96
gmock_test_utils.Main
def Main()
Definition: gmock_test_utils.py:105
gmock_output_test.ToUnixLineEnding
def ToUnixLineEnding(s)
Definition: gmock_output_test.py:57
gmock_output_test.GetNormalizedCommandOutputAndLeakyTests
def GetNormalizedCommandOutputAndLeakyTests(cmd)
Definition: gmock_output_test.py:142
gmock_output_test.GetLeakyTests
def GetLeakyTests(output)
Definition: gmock_output_test.py:108
gmock_output_test.RemoveTestNamesOfLeakedMocks
def RemoveTestNamesOfLeakedMocks(output)
Definition: gmock_output_test.py:102
gmock_output_test.RemoveReportHeaderAndFooter
def RemoveReportHeaderAndFooter(output)
Definition: gmock_output_test.py:63
gmock_output_test.GMockOutputTest
Definition: gmock_output_test.py:154
gmock_output_test.GMockOutputTest.testOutput
def testOutput(self)
Definition: gmock_output_test.py:155
gmock_output_test.GetShellCommandOutput
def GetShellCommandOutput(cmd)
Definition: gmock_output_test.py:136
gmock_test_utils.GetTestExecutablePath
def GetTestExecutablePath(executable_name)
Definition: gmock_test_utils.py:56
gmock_test_utils.TestCase
TestCase
Definition: gmock_test_utils.py:97
gmock_output_test.RemoveMemoryAddresses
def RemoveMemoryAddresses(output)
Definition: gmock_output_test.py:96


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:52