3 # Copyright 2009, Google Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following disclaimer
14 # in the documentation and/or other materials provided with the
16 # * Neither the name of Google Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 """Tests that leaked mock objects can be caught be Google Mock."""
34 __author__ = 'wan@google.com (Zhanyong Wan)'
37 import gmock_test_utils
40 PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_leak_test_')
41 TEST_WITH_EXPECT_CALL = [PROGRAM_PATH, '--gtest_filter=*ExpectCall*']
42 TEST_WITH_ON_CALL = [PROGRAM_PATH, '--gtest_filter=*OnCall*']
43 TEST_MULTIPLE_LEAKS = [PROGRAM_PATH, '--gtest_filter=*MultipleLeaked*']
45 environ = gmock_test_utils.environ
46 SetEnvVar = gmock_test_utils.SetEnvVar
48 # Tests in this file run a Google-Test-based test program and expect it
49 # to terminate prematurely. Therefore they are incompatible with
50 # the premature-exit-file protocol by design. Unset the
51 # premature-exit filepath to prevent Google Test from creating
53 SetEnvVar(gmock_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
56 class GMockLeakTest(gmock_test_utils.TestCase):
58 def testCatchesLeakedMockByDefault(self):
61 gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL,
62 env=environ).exit_code)
65 gmock_test_utils.Subprocess(TEST_WITH_ON_CALL,
66 env=environ).exit_code)
68 def testDoesNotCatchLeakedMockWhenDisabled(self):
71 gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL +
72 ['--gmock_catch_leaked_mocks=0'],
73 env=environ).exit_code)
76 gmock_test_utils.Subprocess(TEST_WITH_ON_CALL +
77 ['--gmock_catch_leaked_mocks=0'],
78 env=environ).exit_code)
80 def testCatchesLeakedMockWhenEnabled(self):
83 gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL +
84 ['--gmock_catch_leaked_mocks'],
85 env=environ).exit_code)
88 gmock_test_utils.Subprocess(TEST_WITH_ON_CALL +
89 ['--gmock_catch_leaked_mocks'],
90 env=environ).exit_code)
92 def testCatchesLeakedMockWhenEnabledWithExplictFlagValue(self):
95 gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL +
96 ['--gmock_catch_leaked_mocks=1'],
97 env=environ).exit_code)
99 def testCatchesMultipleLeakedMocks(self):
102 gmock_test_utils.Subprocess(TEST_MULTIPLE_LEAKS +
103 ['--gmock_catch_leaked_mocks'],
104 env=environ).exit_code)
107 if __name__ == '__main__':
108 gmock_test_utils.Main()