Path: blob/main/contrib/googletest/googlemock/test/gmock_leak_test.py
48255 views
#!/usr/bin/env python1#2# Copyright 2009, Google Inc.3# All rights reserved.4#5# Redistribution and use in source and binary forms, with or without6# modification, are permitted provided that the following conditions are7# met:8#9# * Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# * Redistributions in binary form must reproduce the above12# copyright notice, this list of conditions and the following disclaimer13# in the documentation and/or other materials provided with the14# distribution.15# * Neither the name of Google Inc. nor the names of its16# contributors may be used to endorse or promote products derived from17# this software without specific prior written permission.18#19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.3031"""Tests that leaked mock objects can be caught be Google Mock."""3233from googlemock.test import gmock_test_utils3435PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_leak_test_')36TEST_WITH_EXPECT_CALL = [PROGRAM_PATH, '--gtest_filter=*ExpectCall*']37TEST_WITH_ON_CALL = [PROGRAM_PATH, '--gtest_filter=*OnCall*']38TEST_MULTIPLE_LEAKS = [PROGRAM_PATH, '--gtest_filter=*MultipleLeaked*']3940environ = gmock_test_utils.environ41SetEnvVar = gmock_test_utils.SetEnvVar4243# Tests in this file run a Google-Test-based test program and expect it44# to terminate prematurely. Therefore they are incompatible with45# the premature-exit-file protocol by design. Unset the46# premature-exit filepath to prevent Google Test from creating47# the file.48SetEnvVar(gmock_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)495051class GMockLeakTest(gmock_test_utils.TestCase):5253def testCatchesLeakedMockByDefault(self):54self.assertNotEqual(550,56gmock_test_utils.Subprocess(57TEST_WITH_EXPECT_CALL, env=environ58).exit_code,59)60self.assertNotEqual(610, gmock_test_utils.Subprocess(TEST_WITH_ON_CALL, env=environ).exit_code62)6364def testDoesNotCatchLeakedMockWhenDisabled(self):65self.assertEqual(660,67gmock_test_utils.Subprocess(68TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=0'],69env=environ,70).exit_code,71)72self.assertEqual(730,74gmock_test_utils.Subprocess(75TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks=0'], env=environ76).exit_code,77)7879def testCatchesLeakedMockWhenEnabled(self):80self.assertNotEqual(810,82gmock_test_utils.Subprocess(83TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks'], env=environ84).exit_code,85)86self.assertNotEqual(870,88gmock_test_utils.Subprocess(89TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks'], env=environ90).exit_code,91)9293def testCatchesLeakedMockWhenEnabledWithExplictFlagValue(self):94self.assertNotEqual(950,96gmock_test_utils.Subprocess(97TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=1'],98env=environ,99).exit_code,100)101102def testCatchesMultipleLeakedMocks(self):103self.assertNotEqual(1040,105gmock_test_utils.Subprocess(106TEST_MULTIPLE_LEAKS + ['--gmock_catch_leaked_mocks'], env=environ107).exit_code,108)109110111if __name__ == '__main__':112gmock_test_utils.Main()113114115