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