2008-12-10 05:08:54 +00:00
|
|
|
// Copyright 2008, Google Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2018-08-14 14:04:07 -04:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Tests Google Mock's output in various scenarios. This ensures that
|
|
|
|
// Google Mock's messages are readable and useful.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2022-03-15 13:41:41 -07:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
#include <string>
|
|
|
|
|
2022-03-15 13:41:41 -07:00
|
|
|
#include "gmock/gmock.h"
|
2010-09-14 05:38:21 +00:00
|
|
|
#include "gtest/gtest.h"
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2018-04-10 16:22:50 -04:00
|
|
|
// Silence C4100 (unreferenced formal parameter)
|
2023-01-30 13:44:59 -08:00
|
|
|
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
|
2018-04-06 11:40:04 -04:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
using testing::_;
|
2009-04-22 22:25:31 +00:00
|
|
|
using testing::AnyNumber;
|
2008-12-10 05:08:54 +00:00
|
|
|
using testing::Ge;
|
|
|
|
using testing::InSequence;
|
2013-03-01 07:10:07 +00:00
|
|
|
using testing::NaggyMock;
|
2008-12-10 05:08:54 +00:00
|
|
|
using testing::Ref;
|
|
|
|
using testing::Return;
|
|
|
|
using testing::Sequence;
|
2018-03-22 15:35:37 -04:00
|
|
|
using testing::Value;
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
class MockFoo {
|
|
|
|
public:
|
2023-04-21 10:40:36 -07:00
|
|
|
MockFoo() = default;
|
2009-12-23 00:13:23 +00:00
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
|
|
|
|
MOCK_METHOD2(Bar2, bool(int x, int y));
|
|
|
|
MOCK_METHOD2(Bar3, void(int x, int y));
|
2009-12-23 00:13:23 +00:00
|
|
|
|
|
|
|
private:
|
2022-04-22 11:25:26 -07:00
|
|
|
MockFoo(const MockFoo&) = delete;
|
|
|
|
MockFoo& operator=(const MockFoo&) = delete;
|
2008-12-10 05:08:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class GMockOutputTest : public testing::Test {
|
|
|
|
protected:
|
2013-03-01 07:10:07 +00:00
|
|
|
NaggyMock<MockFoo> foo_;
|
2008-12-10 05:08:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, ExpectedCall) {
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, "info");
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(foo_, Bar2(0, _));
|
|
|
|
foo_.Bar2(0, 0); // Expected call
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, "warning");
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, "info");
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(foo_, Bar3(0, _));
|
|
|
|
foo_.Bar3(0, 0); // Expected call
|
|
|
|
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(verbose, "warning");
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
|
2022-03-15 13:41:41 -07:00
|
|
|
EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
|
2008-12-10 05:08:54 +00:00
|
|
|
foo_.Bar2(2, 2);
|
|
|
|
foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, UnexpectedCall) {
|
|
|
|
EXPECT_CALL(foo_, Bar2(0, _));
|
|
|
|
|
|
|
|
foo_.Bar2(1, 0); // Unexpected call
|
|
|
|
foo_.Bar2(0, 0); // Expected call
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
|
|
|
|
EXPECT_CALL(foo_, Bar3(0, _));
|
|
|
|
|
|
|
|
foo_.Bar3(1, 0); // Unexpected call
|
|
|
|
foo_.Bar3(0, 0); // Expected call
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, ExcessiveCall) {
|
|
|
|
EXPECT_CALL(foo_, Bar2(0, _));
|
|
|
|
|
|
|
|
foo_.Bar2(0, 0); // Expected call
|
|
|
|
foo_.Bar2(0, 1); // Excessive call
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
|
|
|
|
EXPECT_CALL(foo_, Bar3(0, _));
|
|
|
|
|
|
|
|
foo_.Bar3(0, 0); // Expected call
|
|
|
|
foo_.Bar3(0, 1); // Excessive call
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, UninterestingCall) {
|
|
|
|
foo_.Bar2(0, 1); // Uninteresting call
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
|
|
|
|
foo_.Bar3(0, 1); // Uninteresting call
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, RetiredExpectation) {
|
2022-03-15 13:41:41 -07:00
|
|
|
EXPECT_CALL(foo_, Bar2(_, _)).RetiresOnSaturation();
|
2008-12-10 05:08:54 +00:00
|
|
|
EXPECT_CALL(foo_, Bar2(0, 0));
|
|
|
|
|
|
|
|
foo_.Bar2(1, 1);
|
|
|
|
foo_.Bar2(1, 1); // Matches a retired expectation
|
|
|
|
foo_.Bar2(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
|
|
|
|
{
|
|
|
|
InSequence s;
|
|
|
|
EXPECT_CALL(foo_, Bar(_, 0, _));
|
|
|
|
EXPECT_CALL(foo_, Bar2(0, 0));
|
|
|
|
EXPECT_CALL(foo_, Bar2(1, _));
|
|
|
|
}
|
|
|
|
|
|
|
|
foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
|
|
|
|
foo_.Bar("Hi", 0, 0);
|
|
|
|
foo_.Bar2(0, 0);
|
|
|
|
foo_.Bar2(1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
|
|
|
|
Sequence s1, s2;
|
|
|
|
|
2022-03-15 13:41:41 -07:00
|
|
|
EXPECT_CALL(foo_, Bar(_, 0, _)).InSequence(s1);
|
|
|
|
EXPECT_CALL(foo_, Bar2(0, 0)).InSequence(s2);
|
|
|
|
EXPECT_CALL(foo_, Bar2(1, _)).InSequence(s1, s2);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
|
|
|
|
foo_.Bar("Hi", 0, 0);
|
|
|
|
foo_.Bar2(0, 0);
|
|
|
|
foo_.Bar2(1, 0);
|
|
|
|
}
|
|
|
|
|
2009-10-21 06:15:34 +00:00
|
|
|
TEST_F(GMockOutputTest, UnsatisfiedWith) {
|
|
|
|
EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
|
|
|
|
EXPECT_CALL(foo_, Bar(_, _, _));
|
2022-03-15 13:41:41 -07:00
|
|
|
EXPECT_CALL(foo_, Bar2(0, _)).Times(2);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
foo_.Bar2(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, MismatchArguments) {
|
|
|
|
const std::string s = "Hi";
|
|
|
|
EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
|
|
|
|
|
|
|
|
foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
|
|
|
|
foo_.Bar(s, 0, 0);
|
|
|
|
}
|
|
|
|
|
2009-06-09 06:09:53 +00:00
|
|
|
TEST_F(GMockOutputTest, MismatchWith) {
|
2022-03-15 13:41:41 -07:00
|
|
|
EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-06-09 06:09:53 +00:00
|
|
|
foo_.Bar2(2, 3); // Mismatch With()
|
2008-12-10 05:08:54 +00:00
|
|
|
foo_.Bar2(2, 1);
|
|
|
|
}
|
|
|
|
|
2009-06-09 06:09:53 +00:00
|
|
|
TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
|
2022-03-15 13:41:41 -07:00
|
|
|
EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-06-09 06:09:53 +00:00
|
|
|
foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
|
2008-12-10 05:08:54 +00:00
|
|
|
foo_.Bar2(2, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
|
2022-03-15 13:41:41 -07:00
|
|
|
ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
|
|
|
|
ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(foo_, Bar2(2, 2));
|
|
|
|
foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
|
|
|
|
foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
|
|
|
|
foo_.Bar2(2, 2); // Expected call.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
|
2022-03-15 13:41:41 -07:00
|
|
|
ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
|
|
|
|
ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(foo_, Bar2(2, 2));
|
|
|
|
EXPECT_CALL(foo_, Bar2(1, 1));
|
|
|
|
|
|
|
|
foo_.Bar2(2, 2); // Expected call.
|
|
|
|
foo_.Bar2(2, 2); // Excessive call, takes default action #1.
|
|
|
|
foo_.Bar2(1, 1); // Expected call.
|
|
|
|
foo_.Bar2(1, 1); // Excessive call, takes default action #2.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
|
2022-03-15 13:41:41 -07:00
|
|
|
ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
|
|
|
|
ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
|
|
|
|
foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
|
2022-03-15 13:41:41 -07:00
|
|
|
ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2022-03-15 13:41:41 -07:00
|
|
|
EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
|
2008-12-10 05:08:54 +00:00
|
|
|
foo_.Bar2(2, 2);
|
|
|
|
foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
|
|
|
|
}
|
2009-04-22 22:25:31 +00:00
|
|
|
|
|
|
|
TEST_F(GMockOutputTest, CatchesLeakedMocks) {
|
|
|
|
MockFoo* foo1 = new MockFoo;
|
|
|
|
MockFoo* foo2 = new MockFoo;
|
|
|
|
|
|
|
|
// Invokes ON_CALL on foo1.
|
|
|
|
ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
|
|
|
|
|
|
|
|
// Invokes EXPECT_CALL on foo2.
|
|
|
|
EXPECT_CALL(*foo2, Bar2(_, _));
|
|
|
|
EXPECT_CALL(*foo2, Bar2(1, _));
|
|
|
|
EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
|
|
|
|
foo2->Bar2(2, 1);
|
|
|
|
foo2->Bar2(1, 1);
|
|
|
|
|
|
|
|
// Both foo1 and foo2 are deliberately leaked.
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:35:37 -04:00
|
|
|
MATCHER_P2(IsPair, first, second, "") {
|
2023-02-09 15:29:01 +08:00
|
|
|
return Value(arg.first, first) && Value(arg.second, second);
|
2018-03-22 15:35:37 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 16:09:17 -04:00
|
|
|
TEST_F(GMockOutputTest, PrintsMatcher) {
|
|
|
|
const testing::Matcher<int> m1 = Ge(48);
|
2023-02-09 15:29:01 +08:00
|
|
|
EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
|
2018-04-05 16:09:17 -04:00
|
|
|
}
|
|
|
|
|
2009-05-05 23:14:47 +00:00
|
|
|
void TestCatchesLeakedMocksInAdHocTests() {
|
|
|
|
MockFoo* foo = new MockFoo;
|
|
|
|
|
|
|
|
// Invokes EXPECT_CALL on foo.
|
|
|
|
EXPECT_CALL(*foo, Bar2(_, _));
|
|
|
|
foo->Bar2(2, 1);
|
|
|
|
|
|
|
|
// foo is deliberately leaked.
|
|
|
|
}
|
|
|
|
|
2022-03-15 13:41:41 -07:00
|
|
|
int main(int argc, char** argv) {
|
2009-04-22 22:25:31 +00:00
|
|
|
testing::InitGoogleMock(&argc, argv);
|
|
|
|
// Ensures that the tests pass no matter what value of
|
|
|
|
// --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
|
2021-09-14 14:59:42 -04:00
|
|
|
GMOCK_FLAG_SET(catch_leaked_mocks, true);
|
|
|
|
GMOCK_FLAG_SET(verbose, "warning");
|
2009-04-22 22:25:31 +00:00
|
|
|
|
2009-05-05 23:14:47 +00:00
|
|
|
TestCatchesLeakedMocksInAdHocTests();
|
2009-04-22 22:25:31 +00:00
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|
2018-04-10 16:22:50 -04:00
|
|
|
|
2023-01-30 13:44:59 -08:00
|
|
|
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
|