Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:19:59

0001 #!/usr/bin/env python
0002 #
0003 # Copyright 2008, Google Inc.
0004 # All rights reserved.
0005 #
0006 # Redistribution and use in source and binary forms, with or without
0007 # modification, are permitted provided that the following conditions are
0008 # met:
0009 #
0010 #     * Redistributions of source code must retain the above copyright
0011 # notice, this list of conditions and the following disclaimer.
0012 #     * Redistributions in binary form must reproduce the above
0013 # copyright notice, this list of conditions and the following disclaimer
0014 # in the documentation and/or other materials provided with the
0015 # distribution.
0016 #     * Neither the name of Google Inc. nor the names of its
0017 # contributors may be used to endorse or promote products derived from
0018 # this software without specific prior written permission.
0019 #
0020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0023 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0024 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0025 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0026 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0027 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0028 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0029 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0030 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 # The flag for generating the golden file
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   # findall() returns a list of all matches of the regex in output.
0114   # For example, if '(used in test FooTest.Bar)' is in output, the
0115   # list will contain 'FooTest.Bar'.
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   # Disables exception pop-ups on Windows.
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     # The normalized output should match the golden file.
0164     self.assertEquals(golden, output)
0165 
0166     # The raw output should contain 2 leaked mock object errors for
0167     # test GMockOutputTest.CatchesLeakedMocks.
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()