Path: blob/main/contrib/googletest/googlemock/test/gmock_leak_test_.cc
48255 views
// Copyright 2009, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Mock - a framework for writing C++ mock classes.30//31// This program is for verifying that a leaked mock object can be32// caught by Google Mock's leak detector.3334#include "gmock/gmock.h"3536namespace {3738using ::testing::Return;3940class FooInterface {41public:42virtual ~FooInterface() = default;43virtual void DoThis() = 0;44};4546class MockFoo : public FooInterface {47public:48MockFoo() = default;4950MOCK_METHOD0(DoThis, void());5152private:53MockFoo(const MockFoo&) = delete;54MockFoo& operator=(const MockFoo&) = delete;55};5657TEST(LeakTest, LeakedMockWithExpectCallCausesFailureWhenLeakCheckingIsEnabled) {58MockFoo* foo = new MockFoo;5960EXPECT_CALL(*foo, DoThis());61foo->DoThis();6263// In order to test the leak detector, we deliberately leak foo.6465// Makes sure Google Mock's leak detector can change the exit code66// to 1 even when the code is already exiting with 0.67exit(0);68}6970TEST(LeakTest, LeakedMockWithOnCallCausesFailureWhenLeakCheckingIsEnabled) {71MockFoo* foo = new MockFoo;7273ON_CALL(*foo, DoThis()).WillByDefault(Return());7475// In order to test the leak detector, we deliberately leak foo.7677// Makes sure Google Mock's leak detector can change the exit code78// to 1 even when the code is already exiting with 0.79exit(0);80}8182TEST(LeakTest, CatchesMultipleLeakedMockObjects) {83MockFoo* foo1 = new MockFoo;84MockFoo* foo2 = new MockFoo;8586ON_CALL(*foo1, DoThis()).WillByDefault(Return());87EXPECT_CALL(*foo2, DoThis());88foo2->DoThis();8990// In order to test the leak detector, we deliberately leak foo1 and91// foo2.9293// Makes sure Google Mock's leak detector can change the exit code94// to 1 even when the code is already exiting with 0.95exit(0);96}9798} // namespace99100101