Path: blob/main/contrib/googletest/googlemock/test/gmock_output_test_.cc
48254 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// Tests Google Mock's output in various scenarios. This ensures that30// Google Mock's messages are readable and useful.3132#include <stdio.h>3334#include <string>3536#include "gmock/gmock.h"37#include "gtest/gtest.h"3839// Silence C4100 (unreferenced formal parameter)40GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)4142using testing::_;43using testing::AnyNumber;44using testing::Ge;45using testing::InSequence;46using testing::NaggyMock;47using testing::Ref;48using testing::Return;49using testing::Sequence;50using testing::Value;5152class MockFoo {53public:54MockFoo() = default;5556MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));57MOCK_METHOD2(Bar2, bool(int x, int y));58MOCK_METHOD2(Bar3, void(int x, int y));5960private:61MockFoo(const MockFoo&) = delete;62MockFoo& operator=(const MockFoo&) = delete;63};6465class GMockOutputTest : public testing::Test {66protected:67NaggyMock<MockFoo> foo_;68};6970TEST_F(GMockOutputTest, ExpectedCall) {71GMOCK_FLAG_SET(verbose, "info");7273EXPECT_CALL(foo_, Bar2(0, _));74foo_.Bar2(0, 0); // Expected call7576GMOCK_FLAG_SET(verbose, "warning");77}7879TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {80GMOCK_FLAG_SET(verbose, "info");8182EXPECT_CALL(foo_, Bar3(0, _));83foo_.Bar3(0, 0); // Expected call8485GMOCK_FLAG_SET(verbose, "warning");86}8788TEST_F(GMockOutputTest, ExplicitActionsRunOut) {89EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));90foo_.Bar2(2, 2);91foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.92}9394TEST_F(GMockOutputTest, UnexpectedCall) {95EXPECT_CALL(foo_, Bar2(0, _));9697foo_.Bar2(1, 0); // Unexpected call98foo_.Bar2(0, 0); // Expected call99}100101TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {102EXPECT_CALL(foo_, Bar3(0, _));103104foo_.Bar3(1, 0); // Unexpected call105foo_.Bar3(0, 0); // Expected call106}107108TEST_F(GMockOutputTest, ExcessiveCall) {109EXPECT_CALL(foo_, Bar2(0, _));110111foo_.Bar2(0, 0); // Expected call112foo_.Bar2(0, 1); // Excessive call113}114115TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {116EXPECT_CALL(foo_, Bar3(0, _));117118foo_.Bar3(0, 0); // Expected call119foo_.Bar3(0, 1); // Excessive call120}121122TEST_F(GMockOutputTest, UninterestingCall) {123foo_.Bar2(0, 1); // Uninteresting call124}125126TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {127foo_.Bar3(0, 1); // Uninteresting call128}129130TEST_F(GMockOutputTest, RetiredExpectation) {131EXPECT_CALL(foo_, Bar2(_, _)).RetiresOnSaturation();132EXPECT_CALL(foo_, Bar2(0, 0));133134foo_.Bar2(1, 1);135foo_.Bar2(1, 1); // Matches a retired expectation136foo_.Bar2(0, 0);137}138139TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {140{141InSequence s;142EXPECT_CALL(foo_, Bar(_, 0, _));143EXPECT_CALL(foo_, Bar2(0, 0));144EXPECT_CALL(foo_, Bar2(1, _));145}146147foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite148foo_.Bar("Hi", 0, 0);149foo_.Bar2(0, 0);150foo_.Bar2(1, 0);151}152153TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {154Sequence s1, s2;155156EXPECT_CALL(foo_, Bar(_, 0, _)).InSequence(s1);157EXPECT_CALL(foo_, Bar2(0, 0)).InSequence(s2);158EXPECT_CALL(foo_, Bar2(1, _)).InSequence(s1, s2);159160foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites161foo_.Bar("Hi", 0, 0);162foo_.Bar2(0, 0);163foo_.Bar2(1, 0);164}165166TEST_F(GMockOutputTest, UnsatisfiedWith) {167EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());168}169170TEST_F(GMockOutputTest, UnsatisfiedExpectation) {171EXPECT_CALL(foo_, Bar(_, _, _));172EXPECT_CALL(foo_, Bar2(0, _)).Times(2);173174foo_.Bar2(0, 1);175}176177TEST_F(GMockOutputTest, MismatchArguments) {178const std::string s = "Hi";179EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));180181foo_.Bar("Ho", 0, -0.1); // Mismatch arguments182foo_.Bar(s, 0, 0);183}184185TEST_F(GMockOutputTest, MismatchWith) {186EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());187188foo_.Bar2(2, 3); // Mismatch With()189foo_.Bar2(2, 1);190}191192TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {193EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());194195foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()196foo_.Bar2(2, 1);197}198199TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {200ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1201ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2202203EXPECT_CALL(foo_, Bar2(2, 2));204foo_.Bar2(1, 0); // Unexpected call, takes default action #2.205foo_.Bar2(0, 0); // Unexpected call, takes default action #1.206foo_.Bar2(2, 2); // Expected call.207}208209TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {210ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1211ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2212213EXPECT_CALL(foo_, Bar2(2, 2));214EXPECT_CALL(foo_, Bar2(1, 1));215216foo_.Bar2(2, 2); // Expected call.217foo_.Bar2(2, 2); // Excessive call, takes default action #1.218foo_.Bar2(1, 1); // Expected call.219foo_.Bar2(1, 1); // Excessive call, takes default action #2.220}221222TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {223ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1224ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2225226foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.227foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.228}229230TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {231ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1232233EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));234foo_.Bar2(2, 2);235foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.236}237238TEST_F(GMockOutputTest, CatchesLeakedMocks) {239MockFoo* foo1 = new MockFoo;240MockFoo* foo2 = new MockFoo;241242// Invokes ON_CALL on foo1.243ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));244245// Invokes EXPECT_CALL on foo2.246EXPECT_CALL(*foo2, Bar2(_, _));247EXPECT_CALL(*foo2, Bar2(1, _));248EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());249foo2->Bar2(2, 1);250foo2->Bar2(1, 1);251252// Both foo1 and foo2 are deliberately leaked.253}254255MATCHER_P2(IsPair, first, second, "") {256return Value(arg.first, first) && Value(arg.second, second);257}258259TEST_F(GMockOutputTest, PrintsMatcher) {260const testing::Matcher<int> m1 = Ge(48);261EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));262}263264void TestCatchesLeakedMocksInAdHocTests() {265MockFoo* foo = new MockFoo;266267// Invokes EXPECT_CALL on foo.268EXPECT_CALL(*foo, Bar2(_, _));269foo->Bar2(2, 1);270271// foo is deliberately leaked.272}273274int main(int argc, char** argv) {275testing::InitGoogleMock(&argc, argv);276// Ensures that the tests pass no matter what value of277// --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.278GMOCK_FLAG_SET(catch_leaked_mocks, true);279GMOCK_FLAG_SET(verbose, "warning");280281TestCatchesLeakedMocksInAdHocTests();282return RUN_ALL_TESTS();283}284285GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100286287288