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