Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #!/usr/bin/env python
0002 #
0003 # Copyright 2009, 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 that leaked mock objects can be caught be Google Mock."""
0033 
0034 __author__ = 'wan@google.com (Zhanyong Wan)'
0035 
0036 
0037 import gmock_test_utils
0038 
0039 
0040 PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_leak_test_')
0041 TEST_WITH_EXPECT_CALL = [PROGRAM_PATH, '--gtest_filter=*ExpectCall*']
0042 TEST_WITH_ON_CALL = [PROGRAM_PATH, '--gtest_filter=*OnCall*']
0043 TEST_MULTIPLE_LEAKS = [PROGRAM_PATH, '--gtest_filter=*MultipleLeaked*']
0044 
0045 environ = gmock_test_utils.environ
0046 SetEnvVar = gmock_test_utils.SetEnvVar
0047 
0048 # Tests in this file run a Google-Test-based test program and expect it
0049 # to terminate prematurely.  Therefore they are incompatible with
0050 # the premature-exit-file protocol by design.  Unset the
0051 # premature-exit filepath to prevent Google Test from creating
0052 # the file.
0053 SetEnvVar(gmock_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
0054 
0055 
0056 class GMockLeakTest(gmock_test_utils.TestCase):
0057 
0058   def testCatchesLeakedMockByDefault(self):
0059     self.assertNotEqual(
0060         0,
0061         gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL,
0062                                     env=environ).exit_code)
0063     self.assertNotEqual(
0064         0,
0065         gmock_test_utils.Subprocess(TEST_WITH_ON_CALL,
0066                                     env=environ).exit_code)
0067 
0068   def testDoesNotCatchLeakedMockWhenDisabled(self):
0069     self.assertEquals(
0070         0,
0071         gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL +
0072                                     ['--gmock_catch_leaked_mocks=0'],
0073                                     env=environ).exit_code)
0074     self.assertEquals(
0075         0,
0076         gmock_test_utils.Subprocess(TEST_WITH_ON_CALL +
0077                                     ['--gmock_catch_leaked_mocks=0'],
0078                                     env=environ).exit_code)
0079 
0080   def testCatchesLeakedMockWhenEnabled(self):
0081     self.assertNotEqual(
0082         0,
0083         gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL +
0084                                     ['--gmock_catch_leaked_mocks'],
0085                                     env=environ).exit_code)
0086     self.assertNotEqual(
0087         0,
0088         gmock_test_utils.Subprocess(TEST_WITH_ON_CALL +
0089                                     ['--gmock_catch_leaked_mocks'],
0090                                     env=environ).exit_code)
0091 
0092   def testCatchesLeakedMockWhenEnabledWithExplictFlagValue(self):
0093     self.assertNotEqual(
0094         0,
0095         gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL +
0096                                     ['--gmock_catch_leaked_mocks=1'],
0097                                     env=environ).exit_code)
0098 
0099   def testCatchesMultipleLeakedMocks(self):
0100     self.assertNotEqual(
0101         0,
0102         gmock_test_utils.Subprocess(TEST_MULTIPLE_LEAKS +
0103                                     ['--gmock_catch_leaked_mocks'],
0104                                     env=environ).exit_code)
0105 
0106 
0107 if __name__ == '__main__':
0108   gmock_test_utils.Main()