blob: 429cc6adda63b2e719a7b4b24950e64ee60021ba [file] [log] [blame]
zhanyong.wandf35a762009-04-22 22:25:31 +00001#!/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
34__author__ = 'wan@google.com (Zhanyong Wan)'
35
zhanyong.wandf35a762009-04-22 22:25:31 +000036import os
37import unittest
38
zhanyong.wan19eb9e92009-11-24 20:23:18 +000039import gmock_test_utils
zhanyong.wandf35a762009-04-22 22:25:31 +000040
zhanyong.wandf35a762009-04-22 22:25:31 +000041
zhanyong.wan19eb9e92009-11-24 20:23:18 +000042PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_leak_test_')
43TEST_WITH_EXPECT_CALL = [PROGRAM_PATH, '--gtest_filter=*ExpectCall*']
44TEST_WITH_ON_CALL = [PROGRAM_PATH, '--gtest_filter=*OnCall*']
45TEST_MULTIPLE_LEAKS = [PROGRAM_PATH, '--gtest_filter=*MultipleLeaked*']
zhanyong.wandf35a762009-04-22 22:25:31 +000046
47
48class GMockLeakTest(unittest.TestCase):
49
zhanyong.wanbf0d0a42009-04-29 23:52:29 +000050 def testCatchesLeakedMockByDefault(self):
zhanyong.wan19eb9e92009-11-24 20:23:18 +000051 self.assertNotEqual(
52 0,
53 gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL).exit_code)
54 self.assertNotEqual(
55 0,
56 gmock_test_utils.Subprocess(TEST_WITH_ON_CALL).exit_code)
zhanyong.wandf35a762009-04-22 22:25:31 +000057
58 def testDoesNotCatchLeakedMockWhenDisabled(self):
59 self.assertEquals(
zhanyong.wan19eb9e92009-11-24 20:23:18 +000060 0,
61 gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL +
62 ['--gmock_catch_leaked_mocks=0']).exit_code)
zhanyong.wandf35a762009-04-22 22:25:31 +000063 self.assertEquals(
zhanyong.wan19eb9e92009-11-24 20:23:18 +000064 0,
65 gmock_test_utils.Subprocess(TEST_WITH_ON_CALL +
66 ['--gmock_catch_leaked_mocks=0']).exit_code)
zhanyong.wandf35a762009-04-22 22:25:31 +000067
68 def testCatchesLeakedMockWhenEnabled(self):
69 self.assertNotEqual(
zhanyong.wan19eb9e92009-11-24 20:23:18 +000070 0,
71 gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL +
72 ['--gmock_catch_leaked_mocks']).exit_code)
zhanyong.wandf35a762009-04-22 22:25:31 +000073 self.assertNotEqual(
zhanyong.wan19eb9e92009-11-24 20:23:18 +000074 0,
75 gmock_test_utils.Subprocess(TEST_WITH_ON_CALL +
76 ['--gmock_catch_leaked_mocks']).exit_code)
zhanyong.wandf35a762009-04-22 22:25:31 +000077
78 def testCatchesLeakedMockWhenEnabledWithExplictFlagValue(self):
79 self.assertNotEqual(
zhanyong.wan19eb9e92009-11-24 20:23:18 +000080 0,
81 gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL +
82 ['--gmock_catch_leaked_mocks=1']).exit_code)
zhanyong.wandf35a762009-04-22 22:25:31 +000083
84 def testCatchesMultipleLeakedMocks(self):
85 self.assertNotEqual(
zhanyong.wan19eb9e92009-11-24 20:23:18 +000086 0,
87 gmock_test_utils.Subprocess(TEST_MULTIPLE_LEAKS +
88 ['--gmock_catch_leaked_mocks']).exit_code)
zhanyong.wandf35a762009-04-22 22:25:31 +000089
90
91if __name__ == '__main__':
92 gmock_test_utils.Main()