1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Tests Google Mock's output in various scenarios. This ensures that
31 // Google Mock's messages are readable and useful.
37 #include "gmock/gmock.h"
38 #include "gtest/gtest.h"
40 // Silence C4100 (unreferenced formal parameter)
41 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
44 using testing::AnyNumber;
46 using testing::InSequence;
47 using testing::NaggyMock;
49 using testing::Return;
50 using testing::Sequence;
57 MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
58 MOCK_METHOD2(Bar2, bool(int x, int y));
59 MOCK_METHOD2(Bar3, void(int x, int y));
62 MockFoo(const MockFoo&) = delete;
63 MockFoo& operator=(const MockFoo&) = delete;
66 class GMockOutputTest : public testing::Test {
68 NaggyMock<MockFoo> foo_;
71 TEST_F(GMockOutputTest, ExpectedCall) {
72 GMOCK_FLAG_SET(verbose, "info");
74 EXPECT_CALL(foo_, Bar2(0, _));
75 foo_.Bar2(0, 0); // Expected call
77 GMOCK_FLAG_SET(verbose, "warning");
80 TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
81 GMOCK_FLAG_SET(verbose, "info");
83 EXPECT_CALL(foo_, Bar3(0, _));
84 foo_.Bar3(0, 0); // Expected call
86 GMOCK_FLAG_SET(verbose, "warning");
89 TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
90 EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
92 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
95 TEST_F(GMockOutputTest, UnexpectedCall) {
96 EXPECT_CALL(foo_, Bar2(0, _));
98 foo_.Bar2(1, 0); // Unexpected call
99 foo_.Bar2(0, 0); // Expected call
102 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
103 EXPECT_CALL(foo_, Bar3(0, _));
105 foo_.Bar3(1, 0); // Unexpected call
106 foo_.Bar3(0, 0); // Expected call
109 TEST_F(GMockOutputTest, ExcessiveCall) {
110 EXPECT_CALL(foo_, Bar2(0, _));
112 foo_.Bar2(0, 0); // Expected call
113 foo_.Bar2(0, 1); // Excessive call
116 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
117 EXPECT_CALL(foo_, Bar3(0, _));
119 foo_.Bar3(0, 0); // Expected call
120 foo_.Bar3(0, 1); // Excessive call
123 TEST_F(GMockOutputTest, UninterestingCall) {
124 foo_.Bar2(0, 1); // Uninteresting call
127 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
128 foo_.Bar3(0, 1); // Uninteresting call
131 TEST_F(GMockOutputTest, RetiredExpectation) {
132 EXPECT_CALL(foo_, Bar2(_, _)).RetiresOnSaturation();
133 EXPECT_CALL(foo_, Bar2(0, 0));
136 foo_.Bar2(1, 1); // Matches a retired expectation
140 TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
143 EXPECT_CALL(foo_, Bar(_, 0, _));
144 EXPECT_CALL(foo_, Bar2(0, 0));
145 EXPECT_CALL(foo_, Bar2(1, _));
148 foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
149 foo_.Bar("Hi", 0, 0);
154 TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
157 EXPECT_CALL(foo_, Bar(_, 0, _)).InSequence(s1);
158 EXPECT_CALL(foo_, Bar2(0, 0)).InSequence(s2);
159 EXPECT_CALL(foo_, Bar2(1, _)).InSequence(s1, s2);
161 foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
162 foo_.Bar("Hi", 0, 0);
167 TEST_F(GMockOutputTest, UnsatisfiedWith) {
168 EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
171 TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
172 EXPECT_CALL(foo_, Bar(_, _, _));
173 EXPECT_CALL(foo_, Bar2(0, _)).Times(2);
178 TEST_F(GMockOutputTest, MismatchArguments) {
179 const std::string s = "Hi";
180 EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
182 foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
186 TEST_F(GMockOutputTest, MismatchWith) {
187 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
189 foo_.Bar2(2, 3); // Mismatch With()
193 TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
194 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
196 foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
200 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
201 ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
202 ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2
204 EXPECT_CALL(foo_, Bar2(2, 2));
205 foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
206 foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
207 foo_.Bar2(2, 2); // Expected call.
210 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
211 ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
212 ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2
214 EXPECT_CALL(foo_, Bar2(2, 2));
215 EXPECT_CALL(foo_, Bar2(1, 1));
217 foo_.Bar2(2, 2); // Expected call.
218 foo_.Bar2(2, 2); // Excessive call, takes default action #1.
219 foo_.Bar2(1, 1); // Expected call.
220 foo_.Bar2(1, 1); // Excessive call, takes default action #2.
223 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
224 ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
225 ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2
227 foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
228 foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
231 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
232 ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
234 EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
236 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
239 TEST_F(GMockOutputTest, CatchesLeakedMocks) {
240 MockFoo* foo1 = new MockFoo;
241 MockFoo* foo2 = new MockFoo;
243 // Invokes ON_CALL on foo1.
244 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
246 // Invokes EXPECT_CALL on foo2.
247 EXPECT_CALL(*foo2, Bar2(_, _));
248 EXPECT_CALL(*foo2, Bar2(1, _));
249 EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
253 // Both foo1 and foo2 are deliberately leaked.
256 MATCHER_P2(IsPair, first, second, "") {
257 return Value(arg.first, first) && Value(arg.second, second);
260 TEST_F(GMockOutputTest, PrintsMatcher) {
261 const testing::Matcher<int> m1 = Ge(48);
262 EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
265 void TestCatchesLeakedMocksInAdHocTests() {
266 MockFoo* foo = new MockFoo;
268 // Invokes EXPECT_CALL on foo.
269 EXPECT_CALL(*foo, Bar2(_, _));
272 // foo is deliberately leaked.
275 int main(int argc, char** argv) {
276 testing::InitGoogleMock(&argc, argv);
277 // Ensures that the tests pass no matter what value of
278 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
279 GMOCK_FLAG_SET(catch_leaked_mocks, true);
280 GMOCK_FLAG_SET(verbose, "warning");
282 TestCatchesLeakedMocksInAdHocTests();
283 return RUN_ALL_TESTS();
286 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100