1#!/usr/bin/env python
2#
3# Copyright 2009, Google Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met:
9#
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
15# distribution.
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.
19#
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.
31
32"""Tests that leaked mock objects can be caught be Google Mock."""
33
34from googlemock.test import gmock_test_utils
35
36PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_leak_test_')
37TEST_WITH_EXPECT_CALL = [PROGRAM_PATH, '--gtest_filter=*ExpectCall*']
38TEST_WITH_ON_CALL = [PROGRAM_PATH, '--gtest_filter=*OnCall*']
39TEST_MULTIPLE_LEAKS = [PROGRAM_PATH, '--gtest_filter=*MultipleLeaked*']
40
41environ = gmock_test_utils.environ
42SetEnvVar = gmock_test_utils.SetEnvVar
43
44# Tests in this file run a Google-Test-based test program and expect it
45# to terminate prematurely.  Therefore they are incompatible with
46# the premature-exit-file protocol by design.  Unset the
47# premature-exit filepath to prevent Google Test from creating
48# the file.
49SetEnvVar(gmock_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
50
51
52class GMockLeakTest(gmock_test_utils.TestCase):
53
54  def testCatchesLeakedMockByDefault(self):
55    self.assertNotEqual(
56        0,
57        gmock_test_utils.Subprocess(
58            TEST_WITH_EXPECT_CALL, env=environ
59        ).exit_code,
60    )
61    self.assertNotEqual(
62        0, gmock_test_utils.Subprocess(TEST_WITH_ON_CALL, env=environ).exit_code
63    )
64
65  def testDoesNotCatchLeakedMockWhenDisabled(self):
66    self.assertEqual(
67        0,
68        gmock_test_utils.Subprocess(
69            TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=0'],
70            env=environ,
71        ).exit_code,
72    )
73    self.assertEqual(
74        0,
75        gmock_test_utils.Subprocess(
76            TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks=0'], env=environ
77        ).exit_code,
78    )
79
80  def testCatchesLeakedMockWhenEnabled(self):
81    self.assertNotEqual(
82        0,
83        gmock_test_utils.Subprocess(
84            TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks'], env=environ
85        ).exit_code,
86    )
87    self.assertNotEqual(
88        0,
89        gmock_test_utils.Subprocess(
90            TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks'], env=environ
91        ).exit_code,
92    )
93
94  def testCatchesLeakedMockWhenEnabledWithExplictFlagValue(self):
95    self.assertNotEqual(
96        0,
97        gmock_test_utils.Subprocess(
98            TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=1'],
99            env=environ,
100        ).exit_code,
101    )
102
103  def testCatchesMultipleLeakedMocks(self):
104    self.assertNotEqual(
105        0,
106        gmock_test_utils.Subprocess(
107            TEST_MULTIPLE_LEAKS + ['--gmock_catch_leaked_mocks'], env=environ
108        ).exit_code,
109    )
110
111
112if __name__ == '__main__':
113  gmock_test_utils.Main()
114