xref: /freebsd/contrib/googletest/googlemock/test/gmock_leak_test.py (revision 28f6c2f292806bf31230a959bc4b19d7081669a7)
1b89a7cc2SEnji Cooper#!/usr/bin/env python
2b89a7cc2SEnji Cooper#
3b89a7cc2SEnji Cooper# Copyright 2009, Google Inc.
4b89a7cc2SEnji Cooper# All rights reserved.
5b89a7cc2SEnji Cooper#
6b89a7cc2SEnji Cooper# Redistribution and use in source and binary forms, with or without
7b89a7cc2SEnji Cooper# modification, are permitted provided that the following conditions are
8b89a7cc2SEnji Cooper# met:
9b89a7cc2SEnji Cooper#
10b89a7cc2SEnji Cooper#     * Redistributions of source code must retain the above copyright
11b89a7cc2SEnji Cooper# notice, this list of conditions and the following disclaimer.
12b89a7cc2SEnji Cooper#     * Redistributions in binary form must reproduce the above
13b89a7cc2SEnji Cooper# copyright notice, this list of conditions and the following disclaimer
14b89a7cc2SEnji Cooper# in the documentation and/or other materials provided with the
15b89a7cc2SEnji Cooper# distribution.
16b89a7cc2SEnji Cooper#     * Neither the name of Google Inc. nor the names of its
17b89a7cc2SEnji Cooper# contributors may be used to endorse or promote products derived from
18b89a7cc2SEnji Cooper# this software without specific prior written permission.
19b89a7cc2SEnji Cooper#
20b89a7cc2SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21b89a7cc2SEnji Cooper# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22b89a7cc2SEnji Cooper# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23b89a7cc2SEnji Cooper# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24b89a7cc2SEnji Cooper# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25b89a7cc2SEnji Cooper# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26b89a7cc2SEnji Cooper# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27b89a7cc2SEnji Cooper# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28b89a7cc2SEnji Cooper# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29b89a7cc2SEnji Cooper# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30b89a7cc2SEnji Cooper# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31b89a7cc2SEnji Cooper
32b89a7cc2SEnji Cooper"""Tests that leaked mock objects can be caught be Google Mock."""
33b89a7cc2SEnji Cooper
34*28f6c2f2SEnji Cooperfrom googlemock.test import gmock_test_utils
35b89a7cc2SEnji Cooper
36b89a7cc2SEnji CooperPROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_leak_test_')
37b89a7cc2SEnji CooperTEST_WITH_EXPECT_CALL = [PROGRAM_PATH, '--gtest_filter=*ExpectCall*']
38b89a7cc2SEnji CooperTEST_WITH_ON_CALL = [PROGRAM_PATH, '--gtest_filter=*OnCall*']
39b89a7cc2SEnji CooperTEST_MULTIPLE_LEAKS = [PROGRAM_PATH, '--gtest_filter=*MultipleLeaked*']
40b89a7cc2SEnji Cooper
41b89a7cc2SEnji Cooperenviron = gmock_test_utils.environ
42b89a7cc2SEnji CooperSetEnvVar = gmock_test_utils.SetEnvVar
43b89a7cc2SEnji Cooper
44b89a7cc2SEnji Cooper# Tests in this file run a Google-Test-based test program and expect it
45b89a7cc2SEnji Cooper# to terminate prematurely.  Therefore they are incompatible with
46b89a7cc2SEnji Cooper# the premature-exit-file protocol by design.  Unset the
47b89a7cc2SEnji Cooper# premature-exit filepath to prevent Google Test from creating
48b89a7cc2SEnji Cooper# the file.
49b89a7cc2SEnji CooperSetEnvVar(gmock_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
50b89a7cc2SEnji Cooper
51b89a7cc2SEnji Cooper
52b89a7cc2SEnji Cooperclass GMockLeakTest(gmock_test_utils.TestCase):
53b89a7cc2SEnji Cooper
54b89a7cc2SEnji Cooper  def testCatchesLeakedMockByDefault(self):
55b89a7cc2SEnji Cooper    self.assertNotEqual(
56b89a7cc2SEnji Cooper        0,
57*28f6c2f2SEnji Cooper        gmock_test_utils.Subprocess(
58*28f6c2f2SEnji Cooper            TEST_WITH_EXPECT_CALL, env=environ
59*28f6c2f2SEnji Cooper        ).exit_code,
60*28f6c2f2SEnji Cooper    )
61b89a7cc2SEnji Cooper    self.assertNotEqual(
62*28f6c2f2SEnji Cooper        0, gmock_test_utils.Subprocess(TEST_WITH_ON_CALL, env=environ).exit_code
63*28f6c2f2SEnji Cooper    )
64b89a7cc2SEnji Cooper
65b89a7cc2SEnji Cooper  def testDoesNotCatchLeakedMockWhenDisabled(self):
66*28f6c2f2SEnji Cooper    self.assertEqual(
67b89a7cc2SEnji Cooper        0,
68*28f6c2f2SEnji Cooper        gmock_test_utils.Subprocess(
69*28f6c2f2SEnji Cooper            TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=0'],
70*28f6c2f2SEnji Cooper            env=environ,
71*28f6c2f2SEnji Cooper        ).exit_code,
72*28f6c2f2SEnji Cooper    )
73*28f6c2f2SEnji Cooper    self.assertEqual(
74b89a7cc2SEnji Cooper        0,
75*28f6c2f2SEnji Cooper        gmock_test_utils.Subprocess(
76*28f6c2f2SEnji Cooper            TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks=0'], env=environ
77*28f6c2f2SEnji Cooper        ).exit_code,
78*28f6c2f2SEnji Cooper    )
79b89a7cc2SEnji Cooper
80b89a7cc2SEnji Cooper  def testCatchesLeakedMockWhenEnabled(self):
81b89a7cc2SEnji Cooper    self.assertNotEqual(
82b89a7cc2SEnji Cooper        0,
83*28f6c2f2SEnji Cooper        gmock_test_utils.Subprocess(
84*28f6c2f2SEnji Cooper            TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks'], env=environ
85*28f6c2f2SEnji Cooper        ).exit_code,
86*28f6c2f2SEnji Cooper    )
87b89a7cc2SEnji Cooper    self.assertNotEqual(
88b89a7cc2SEnji Cooper        0,
89*28f6c2f2SEnji Cooper        gmock_test_utils.Subprocess(
90*28f6c2f2SEnji Cooper            TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks'], env=environ
91*28f6c2f2SEnji Cooper        ).exit_code,
92*28f6c2f2SEnji Cooper    )
93b89a7cc2SEnji Cooper
94b89a7cc2SEnji Cooper  def testCatchesLeakedMockWhenEnabledWithExplictFlagValue(self):
95b89a7cc2SEnji Cooper    self.assertNotEqual(
96b89a7cc2SEnji Cooper        0,
97*28f6c2f2SEnji Cooper        gmock_test_utils.Subprocess(
98*28f6c2f2SEnji Cooper            TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=1'],
99*28f6c2f2SEnji Cooper            env=environ,
100*28f6c2f2SEnji Cooper        ).exit_code,
101*28f6c2f2SEnji Cooper    )
102b89a7cc2SEnji Cooper
103b89a7cc2SEnji Cooper  def testCatchesMultipleLeakedMocks(self):
104b89a7cc2SEnji Cooper    self.assertNotEqual(
105b89a7cc2SEnji Cooper        0,
106*28f6c2f2SEnji Cooper        gmock_test_utils.Subprocess(
107*28f6c2f2SEnji Cooper            TEST_MULTIPLE_LEAKS + ['--gmock_catch_leaked_mocks'], env=environ
108*28f6c2f2SEnji Cooper        ).exit_code,
109*28f6c2f2SEnji Cooper    )
110b89a7cc2SEnji Cooper
111b89a7cc2SEnji Cooper
112b89a7cc2SEnji Cooperif __name__ == '__main__':
113b89a7cc2SEnji Cooper  gmock_test_utils.Main()
114