Path: blob/main/contrib/googletest/googlemock/test/gmock-nice-strict_test.cc
48255 views
// Copyright 2008, 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#include "gmock/gmock-nice-strict.h"3031#include <string>32#include <utility>3334#include "gmock/gmock.h"35#include "gtest/gtest-spi.h"36#include "gtest/gtest.h"3738// This must not be defined inside the ::testing namespace, or it will39// clash with ::testing::Mock.40class Mock {41public:42Mock() = default;4344MOCK_METHOD0(DoThis, void());4546private:47Mock(const Mock&) = delete;48Mock& operator=(const Mock&) = delete;49};5051namespace testing {52namespace gmock_nice_strict_test {5354using testing::HasSubstr;55using testing::NaggyMock;56using testing::NiceMock;57using testing::StrictMock;5859#if GTEST_HAS_STREAM_REDIRECTION60using testing::internal::CaptureStdout;61using testing::internal::GetCapturedStdout;62#endif6364// Class without default constructor.65class NotDefaultConstructible {66public:67explicit NotDefaultConstructible(int) {}68};6970class CallsMockMethodInDestructor {71public:72~CallsMockMethodInDestructor() { OnDestroy(); }73MOCK_METHOD(void, OnDestroy, ());74};7576// Defines some mock classes needed by the tests.7778class Foo {79public:80virtual ~Foo() = default;8182virtual void DoThis() = 0;83virtual int DoThat(bool flag) = 0;84};8586class MockFoo : public Foo {87public:88MockFoo() = default;89void Delete() { delete this; }9091MOCK_METHOD0(DoThis, void());92MOCK_METHOD1(DoThat, int(bool flag));93MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());9495private:96MockFoo(const MockFoo&) = delete;97MockFoo& operator=(const MockFoo&) = delete;98};99100class MockBar {101public:102explicit MockBar(const std::string& s) : str_(s) {}103104MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,105const std::string& a7, const std::string& a8, bool a9, bool a10) {106str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +107static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') +108(a10 ? 'T' : 'F');109}110111virtual ~MockBar() = default;112113const std::string& str() const { return str_; }114115MOCK_METHOD0(This, int());116MOCK_METHOD2(That, std::string(int, bool));117118private:119std::string str_;120121MockBar(const MockBar&) = delete;122MockBar& operator=(const MockBar&) = delete;123};124125class MockBaz {126public:127class MoveOnly {128public:129MoveOnly() = default;130131MoveOnly(const MoveOnly&) = delete;132MoveOnly& operator=(const MoveOnly&) = delete;133134MoveOnly(MoveOnly&&) = default;135MoveOnly& operator=(MoveOnly&&) = default;136};137138MockBaz(MoveOnly) {}139};140141#if GTEST_HAS_STREAM_REDIRECTION142143// Tests that a raw mock generates warnings for uninteresting calls.144TEST(RawMockTest, WarningForUninterestingCall) {145const std::string saved_flag = GMOCK_FLAG_GET(verbose);146GMOCK_FLAG_SET(verbose, "warning");147148MockFoo raw_foo;149150CaptureStdout();151raw_foo.DoThis();152raw_foo.DoThat(true);153EXPECT_THAT(GetCapturedStdout(),154HasSubstr("Uninteresting mock function call"));155156GMOCK_FLAG_SET(verbose, saved_flag);157}158159// Tests that a raw mock generates warnings for uninteresting calls160// that delete the mock object.161TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {162const std::string saved_flag = GMOCK_FLAG_GET(verbose);163GMOCK_FLAG_SET(verbose, "warning");164165MockFoo* const raw_foo = new MockFoo;166167ON_CALL(*raw_foo, DoThis()).WillByDefault(Invoke(raw_foo, &MockFoo::Delete));168169CaptureStdout();170raw_foo->DoThis();171EXPECT_THAT(GetCapturedStdout(),172HasSubstr("Uninteresting mock function call"));173174GMOCK_FLAG_SET(verbose, saved_flag);175}176177// Tests that a raw mock generates informational logs for178// uninteresting calls.179TEST(RawMockTest, InfoForUninterestingCall) {180MockFoo raw_foo;181182const std::string saved_flag = GMOCK_FLAG_GET(verbose);183GMOCK_FLAG_SET(verbose, "info");184CaptureStdout();185raw_foo.DoThis();186EXPECT_THAT(GetCapturedStdout(),187HasSubstr("Uninteresting mock function call"));188189GMOCK_FLAG_SET(verbose, saved_flag);190}191192TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {193MockFoo raw_foo;194EXPECT_TRUE(Mock::IsNaggy(&raw_foo));195EXPECT_FALSE(Mock::IsNice(&raw_foo));196EXPECT_FALSE(Mock::IsStrict(&raw_foo));197}198199// Tests that a nice mock generates no warning for uninteresting calls.200TEST(NiceMockTest, NoWarningForUninterestingCall) {201NiceMock<MockFoo> nice_foo;202203CaptureStdout();204nice_foo.DoThis();205nice_foo.DoThat(true);206EXPECT_EQ("", GetCapturedStdout());207}208209// Tests that a nice mock generates no warning for uninteresting calls210// that delete the mock object.211TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {212NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;213214ON_CALL(*nice_foo, DoThis())215.WillByDefault(Invoke(nice_foo, &MockFoo::Delete));216217CaptureStdout();218nice_foo->DoThis();219EXPECT_EQ("", GetCapturedStdout());220}221222// Tests that a nice mock generates informational logs for223// uninteresting calls.224TEST(NiceMockTest, InfoForUninterestingCall) {225NiceMock<MockFoo> nice_foo;226227const std::string saved_flag = GMOCK_FLAG_GET(verbose);228GMOCK_FLAG_SET(verbose, "info");229CaptureStdout();230nice_foo.DoThis();231EXPECT_THAT(GetCapturedStdout(),232HasSubstr("Uninteresting mock function call"));233234GMOCK_FLAG_SET(verbose, saved_flag);235}236237#endif // GTEST_HAS_STREAM_REDIRECTION238239// Tests that a nice mock allows expected calls.240TEST(NiceMockTest, AllowsExpectedCall) {241NiceMock<MockFoo> nice_foo;242243EXPECT_CALL(nice_foo, DoThis());244nice_foo.DoThis();245}246247// Tests that an unexpected call on a nice mock which returns a248// not-default-constructible type throws an exception and the exception contains249// the method's name.250TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {251NiceMock<MockFoo> nice_foo;252#if GTEST_HAS_EXCEPTIONS253try {254nice_foo.ReturnNonDefaultConstructible();255FAIL();256} catch (const std::runtime_error& ex) {257EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));258}259#else260EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");261#endif262}263264// Tests that an unexpected call on a nice mock fails.265TEST(NiceMockTest, UnexpectedCallFails) {266NiceMock<MockFoo> nice_foo;267268EXPECT_CALL(nice_foo, DoThis()).Times(0);269EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");270}271272// Tests that NiceMock works with a mock class that has a non-default273// constructor.274TEST(NiceMockTest, NonDefaultConstructor) {275NiceMock<MockBar> nice_bar("hi");276EXPECT_EQ("hi", nice_bar.str());277278nice_bar.This();279nice_bar.That(5, true);280}281282// Tests that NiceMock works with a mock class that has a 10-ary283// non-default constructor.284TEST(NiceMockTest, NonDefaultConstructor10) {285NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,286false);287EXPECT_EQ("abcdefghTF", nice_bar.str());288289nice_bar.This();290nice_bar.That(5, true);291}292293TEST(NiceMockTest, AllowLeak) {294NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;295Mock::AllowLeak(leaked);296EXPECT_CALL(*leaked, DoThis());297leaked->DoThis();298}299300TEST(NiceMockTest, MoveOnlyConstructor) {301NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});302}303304// Tests that NiceMock<Mock> compiles where Mock is a user-defined305// class (as opposed to ::testing::Mock).306TEST(NiceMockTest, AcceptsClassNamedMock) {307NiceMock< ::Mock> nice;308EXPECT_CALL(nice, DoThis());309nice.DoThis();310}311312TEST(NiceMockTest, IsNiceInDestructor) {313{314NiceMock<CallsMockMethodInDestructor> nice_on_destroy;315// Don't add an expectation for the call before the mock goes out of scope.316}317}318319TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) {320NiceMock<MockFoo> nice_foo;321EXPECT_FALSE(Mock::IsNaggy(&nice_foo));322EXPECT_TRUE(Mock::IsNice(&nice_foo));323EXPECT_FALSE(Mock::IsStrict(&nice_foo));324}325326#if GTEST_HAS_STREAM_REDIRECTION327328// Tests that a naggy mock generates warnings for uninteresting calls.329TEST(NaggyMockTest, WarningForUninterestingCall) {330const std::string saved_flag = GMOCK_FLAG_GET(verbose);331GMOCK_FLAG_SET(verbose, "warning");332333NaggyMock<MockFoo> naggy_foo;334335CaptureStdout();336naggy_foo.DoThis();337naggy_foo.DoThat(true);338EXPECT_THAT(GetCapturedStdout(),339HasSubstr("Uninteresting mock function call"));340341GMOCK_FLAG_SET(verbose, saved_flag);342}343344// Tests that a naggy mock generates a warning for an uninteresting call345// that deletes the mock object.346TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {347const std::string saved_flag = GMOCK_FLAG_GET(verbose);348GMOCK_FLAG_SET(verbose, "warning");349350NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;351352ON_CALL(*naggy_foo, DoThis())353.WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));354355CaptureStdout();356naggy_foo->DoThis();357EXPECT_THAT(GetCapturedStdout(),358HasSubstr("Uninteresting mock function call"));359360GMOCK_FLAG_SET(verbose, saved_flag);361}362363#endif // GTEST_HAS_STREAM_REDIRECTION364365// Tests that a naggy mock allows expected calls.366TEST(NaggyMockTest, AllowsExpectedCall) {367NaggyMock<MockFoo> naggy_foo;368369EXPECT_CALL(naggy_foo, DoThis());370naggy_foo.DoThis();371}372373// Tests that an unexpected call on a naggy mock fails.374TEST(NaggyMockTest, UnexpectedCallFails) {375NaggyMock<MockFoo> naggy_foo;376377EXPECT_CALL(naggy_foo, DoThis()).Times(0);378EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),379"called more times than expected");380}381382// Tests that NaggyMock works with a mock class that has a non-default383// constructor.384TEST(NaggyMockTest, NonDefaultConstructor) {385NaggyMock<MockBar> naggy_bar("hi");386EXPECT_EQ("hi", naggy_bar.str());387388naggy_bar.This();389naggy_bar.That(5, true);390}391392// Tests that NaggyMock works with a mock class that has a 10-ary393// non-default constructor.394TEST(NaggyMockTest, NonDefaultConstructor10) {395NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5', "6", "7", true,396false);397EXPECT_EQ("01234567TF", naggy_bar.str());398399naggy_bar.This();400naggy_bar.That(5, true);401}402403TEST(NaggyMockTest, AllowLeak) {404NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;405Mock::AllowLeak(leaked);406EXPECT_CALL(*leaked, DoThis());407leaked->DoThis();408}409410TEST(NaggyMockTest, MoveOnlyConstructor) {411NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});412}413414// Tests that NaggyMock<Mock> compiles where Mock is a user-defined415// class (as opposed to ::testing::Mock).416TEST(NaggyMockTest, AcceptsClassNamedMock) {417NaggyMock< ::Mock> naggy;418EXPECT_CALL(naggy, DoThis());419naggy.DoThis();420}421422TEST(NaggyMockTest, IsNaggyInDestructor) {423const std::string saved_flag = GMOCK_FLAG_GET(verbose);424GMOCK_FLAG_SET(verbose, "warning");425CaptureStdout();426427{428NaggyMock<CallsMockMethodInDestructor> naggy_on_destroy;429// Don't add an expectation for the call before the mock goes out of scope.430}431432EXPECT_THAT(GetCapturedStdout(),433HasSubstr("Uninteresting mock function call"));434435GMOCK_FLAG_SET(verbose, saved_flag);436}437438TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) {439NaggyMock<MockFoo> naggy_foo;440EXPECT_TRUE(Mock::IsNaggy(&naggy_foo));441EXPECT_FALSE(Mock::IsNice(&naggy_foo));442EXPECT_FALSE(Mock::IsStrict(&naggy_foo));443}444445// Tests that a strict mock allows expected calls.446TEST(StrictMockTest, AllowsExpectedCall) {447StrictMock<MockFoo> strict_foo;448449EXPECT_CALL(strict_foo, DoThis());450strict_foo.DoThis();451}452453// Tests that an unexpected call on a strict mock fails.454TEST(StrictMockTest, UnexpectedCallFails) {455StrictMock<MockFoo> strict_foo;456457EXPECT_CALL(strict_foo, DoThis()).Times(0);458EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),459"called more times than expected");460}461462// Tests that an uninteresting call on a strict mock fails.463TEST(StrictMockTest, UninterestingCallFails) {464StrictMock<MockFoo> strict_foo;465466EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),467"Uninteresting mock function call");468}469470// Tests that an uninteresting call on a strict mock fails, even if471// the call deletes the mock object.472TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {473StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;474475ON_CALL(*strict_foo, DoThis())476.WillByDefault(Invoke(strict_foo, &MockFoo::Delete));477478EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),479"Uninteresting mock function call");480}481482// Tests that StrictMock works with a mock class that has a483// non-default constructor.484TEST(StrictMockTest, NonDefaultConstructor) {485StrictMock<MockBar> strict_bar("hi");486EXPECT_EQ("hi", strict_bar.str());487488EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),489"Uninteresting mock function call");490}491492// Tests that StrictMock works with a mock class that has a 10-ary493// non-default constructor.494TEST(StrictMockTest, NonDefaultConstructor10) {495StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,496false);497EXPECT_EQ("abcdefghTF", strict_bar.str());498499EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),500"Uninteresting mock function call");501}502503TEST(StrictMockTest, AllowLeak) {504StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;505Mock::AllowLeak(leaked);506EXPECT_CALL(*leaked, DoThis());507leaked->DoThis();508}509510TEST(StrictMockTest, MoveOnlyConstructor) {511StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});512}513514// Tests that StrictMock<Mock> compiles where Mock is a user-defined515// class (as opposed to ::testing::Mock).516TEST(StrictMockTest, AcceptsClassNamedMock) {517StrictMock< ::Mock> strict;518EXPECT_CALL(strict, DoThis());519strict.DoThis();520}521522TEST(StrictMockTest, IsStrictInDestructor) {523EXPECT_NONFATAL_FAILURE(524{525StrictMock<CallsMockMethodInDestructor> strict_on_destroy;526// Don't add an expectation for the call before the mock goes out of527// scope.528},529"Uninteresting mock function call");530}531532TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) {533StrictMock<MockFoo> strict_foo;534EXPECT_FALSE(Mock::IsNaggy(&strict_foo));535EXPECT_FALSE(Mock::IsNice(&strict_foo));536EXPECT_TRUE(Mock::IsStrict(&strict_foo));537}538539} // namespace gmock_nice_strict_test540} // namespace testing541542543