32 r"""Tests the text output of Google C++ Mocking Framework.
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
45 import gmock_test_utils
49 GENGOLDEN_FLAG =
'--gengolden'
52 COMMAND = [PROGRAM_PATH,
'--gtest_stack_trace_depth=0',
'--gtest_print_time=0']
53 GOLDEN_NAME =
'gmock_output_test_golden.txt'
58 """Changes all Windows/Mac line endings in s to UNIX line endings."""
60 return s.replace(
'\r\n',
'\n').replace(
'\r',
'\n')
64 """Removes Google Test result report's header and footer from the output."""
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)
75 """Removes all file location info from a Google Test program's output.
78 output: the output of a Google Test program.
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
87 return re.sub(
r'.*[/\\](.+)(\:\d+|\(\d+\))\:',
'FILE:#:', output)
91 """Normalizes the error marker, which is different on Windows vs on Linux."""
93 return re.sub(
r' error: ',
' Failure\n', output)
97 """Removes memory addresses from the test output."""
99 return re.sub(
r'@\w+',
'@0x#', output)
103 """Removes the test names of leaked mock objects from the test output."""
105 return re.sub(
r'\(used in test .+\) ',
'', output)
109 """Returns a list of test names that leak mock objects."""
114 return re.findall(
r'\(used in test (.+)\)', output)
118 """Normalizes the output of gmock_output_test_.
121 output: The test output.
124 A tuple (the normalized test output, the list of test names that have
137 """Runs a command in a sub-process, and returns its STDOUT in a string."""
143 """Runs a command and returns its normalized output and a list of leaky tests.
146 cmd: the shell command.
150 os.environ[
'GTEST_CATCH_EXCEPTIONS'] =
'1'
157 golden_file = open(GOLDEN_PATH,
'rb')
158 golden = golden_file.read()
162 self.assertEquals(golden, output)
166 self.assertEquals([
'GMockOutputTest.CatchesLeakedMocks',
167 'GMockOutputTest.CatchesLeakedMocks'],
171 if __name__ ==
'__main__':
172 if sys.argv[1:] == [GENGOLDEN_FLAG]:
174 golden_file = open(GOLDEN_PATH,
'wb')
175 golden_file.write(output)