File indexing completed on 2025-08-06 08:19:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 """Tests the text output of Google C++ Mocking Framework.
0033
0034 SYNOPSIS
0035 gmock_output_test.py --build_dir=BUILD/DIR --gengolden
0036 # where BUILD/DIR contains the built gmock_output_test_ file.
0037 gmock_output_test.py --gengolden
0038 gmock_output_test.py
0039 """
0040
0041 __author__ = 'wan@google.com (Zhanyong Wan)'
0042
0043 import os
0044 import re
0045 import sys
0046
0047 import gmock_test_utils
0048
0049
0050
0051 GENGOLDEN_FLAG = '--gengolden'
0052
0053 PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_output_test_')
0054 COMMAND = [PROGRAM_PATH, '--gtest_stack_trace_depth=0', '--gtest_print_time=0']
0055 GOLDEN_NAME = 'gmock_output_test_golden.txt'
0056 GOLDEN_PATH = os.path.join(gmock_test_utils.GetSourceDir(), GOLDEN_NAME)
0057
0058
0059 def ToUnixLineEnding(s):
0060 """Changes all Windows/Mac line endings in s to UNIX line endings."""
0061
0062 return s.replace('\r\n', '\n').replace('\r', '\n')
0063
0064
0065 def RemoveReportHeaderAndFooter(output):
0066 """Removes Google Test result report's header and footer from the output."""
0067
0068 output = re.sub(r'.*gtest_main.*\n', '', output)
0069 output = re.sub(r'\[.*\d+ tests.*\n', '', output)
0070 output = re.sub(r'\[.* test environment .*\n', '', output)
0071 output = re.sub(r'\[=+\] \d+ tests .* ran.*', '', output)
0072 output = re.sub(r'.* FAILED TESTS\n', '', output)
0073 return output
0074
0075
0076 def RemoveLocations(output):
0077 """Removes all file location info from a Google Test program's output.
0078
0079 Args:
0080 output: the output of a Google Test program.
0081
0082 Returns:
0083 output with all file location info (in the form of
0084 'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or
0085 'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by
0086 'FILE:#: '.
0087 """
0088
0089 return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\:', 'FILE:#:', output)
0090
0091
0092 def NormalizeErrorMarker(output):
0093 """Normalizes the error marker, which is different on Windows vs on Linux."""
0094
0095 return re.sub(r' error: ', ' Failure\n', output)
0096
0097
0098 def RemoveMemoryAddresses(output):
0099 """Removes memory addresses from the test output."""
0100
0101 return re.sub(r'@\w+', '@0x#', output)
0102
0103
0104 def RemoveTestNamesOfLeakedMocks(output):
0105 """Removes the test names of leaked mock objects from the test output."""
0106
0107 return re.sub(r'\(used in test .+\) ', '', output)
0108
0109
0110 def GetLeakyTests(output):
0111 """Returns a list of test names that leak mock objects."""
0112
0113
0114
0115
0116 return re.findall(r'\(used in test (.+)\)', output)
0117
0118
0119 def GetNormalizedOutputAndLeakyTests(output):
0120 """Normalizes the output of gmock_output_test_.
0121
0122 Args:
0123 output: The test output.
0124
0125 Returns:
0126 A tuple (the normalized test output, the list of test names that have
0127 leaked mocks).
0128 """
0129
0130 output = ToUnixLineEnding(output)
0131 output = RemoveReportHeaderAndFooter(output)
0132 output = NormalizeErrorMarker(output)
0133 output = RemoveLocations(output)
0134 output = RemoveMemoryAddresses(output)
0135 return (RemoveTestNamesOfLeakedMocks(output), GetLeakyTests(output))
0136
0137
0138 def GetShellCommandOutput(cmd):
0139 """Runs a command in a sub-process, and returns its STDOUT in a string."""
0140
0141 return gmock_test_utils.Subprocess(cmd, capture_stderr=False).output
0142
0143
0144 def GetNormalizedCommandOutputAndLeakyTests(cmd):
0145 """Runs a command and returns its normalized output and a list of leaky tests.
0146
0147 Args:
0148 cmd: the shell command.
0149 """
0150
0151
0152 os.environ['GTEST_CATCH_EXCEPTIONS'] = '1'
0153 return GetNormalizedOutputAndLeakyTests(GetShellCommandOutput(cmd))
0154
0155
0156 class GMockOutputTest(gmock_test_utils.TestCase):
0157 def testOutput(self):
0158 (output, leaky_tests) = GetNormalizedCommandOutputAndLeakyTests(COMMAND)
0159 golden_file = open(GOLDEN_PATH, 'rb')
0160 golden = golden_file.read()
0161 golden_file.close()
0162
0163
0164 self.assertEquals(golden, output)
0165
0166
0167
0168 self.assertEquals(['GMockOutputTest.CatchesLeakedMocks',
0169 'GMockOutputTest.CatchesLeakedMocks'],
0170 leaky_tests)
0171
0172
0173 if __name__ == '__main__':
0174 if sys.argv[1:] == [GENGOLDEN_FLAG]:
0175 (output, _) = GetNormalizedCommandOutputAndLeakyTests(COMMAND)
0176 golden_file = open(GOLDEN_PATH, 'wb')
0177 golden_file.write(output)
0178 golden_file.close()
0179 else:
0180 gmock_test_utils.Main()