1 // Copyright 2008, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29
30 #include "gmock/gmock-nice-strict.h"
31
32 #include <string>
33 #include <utility>
34
35 #include "gmock/gmock.h"
36 #include "gtest/gtest-spi.h"
37 #include "gtest/gtest.h"
38
39 // This must not be defined inside the ::testing namespace, or it will
40 // clash with ::testing::Mock.
41 class Mock {
42 public:
43 Mock() = default;
44
45 MOCK_METHOD0(DoThis, void());
46
47 private:
48 Mock(const Mock&) = delete;
49 Mock& operator=(const Mock&) = delete;
50 };
51
52 namespace testing {
53 namespace gmock_nice_strict_test {
54
55 using testing::HasSubstr;
56 using testing::NaggyMock;
57 using testing::NiceMock;
58 using testing::StrictMock;
59
60 #if GTEST_HAS_STREAM_REDIRECTION
61 using testing::internal::CaptureStdout;
62 using testing::internal::GetCapturedStdout;
63 #endif
64
65 // Class without default constructor.
66 class NotDefaultConstructible {
67 public:
NotDefaultConstructible(int)68 explicit NotDefaultConstructible(int) {}
69 };
70
71 class CallsMockMethodInDestructor {
72 public:
~CallsMockMethodInDestructor()73 ~CallsMockMethodInDestructor() { OnDestroy(); }
74 MOCK_METHOD(void, OnDestroy, ());
75 };
76
77 // Defines some mock classes needed by the tests.
78
79 class Foo {
80 public:
81 virtual ~Foo() = default;
82
83 virtual void DoThis() = 0;
84 virtual int DoThat(bool flag) = 0;
85 };
86
87 class MockFoo : public Foo {
88 public:
89 MockFoo() = default;
Delete()90 void Delete() { delete this; }
91
92 MOCK_METHOD0(DoThis, void());
93 MOCK_METHOD1(DoThat, int(bool flag));
94 MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
95
96 private:
97 MockFoo(const MockFoo&) = delete;
98 MockFoo& operator=(const MockFoo&) = delete;
99 };
100
101 class MockBar {
102 public:
MockBar(const std::string & s)103 explicit MockBar(const std::string& s) : str_(s) {}
104
MockBar(char a1,char a2,std::string a3,std::string a4,int a5,int a6,const std::string & a7,const std::string & a8,bool a9,bool a10)105 MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,
106 const std::string& a7, const std::string& a8, bool a9, bool a10) {
107 str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
108 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') +
109 (a10 ? 'T' : 'F');
110 }
111
112 virtual ~MockBar() = default;
113
str() const114 const std::string& str() const { return str_; }
115
116 MOCK_METHOD0(This, int());
117 MOCK_METHOD2(That, std::string(int, bool));
118
119 private:
120 std::string str_;
121
122 MockBar(const MockBar&) = delete;
123 MockBar& operator=(const MockBar&) = delete;
124 };
125
126 class MockBaz {
127 public:
128 class MoveOnly {
129 public:
130 MoveOnly() = default;
131
132 MoveOnly(const MoveOnly&) = delete;
133 MoveOnly& operator=(const MoveOnly&) = delete;
134
135 MoveOnly(MoveOnly&&) = default;
136 MoveOnly& operator=(MoveOnly&&) = default;
137 };
138
MockBaz(MoveOnly)139 MockBaz(MoveOnly) {}
140 };
141
142 #if GTEST_HAS_STREAM_REDIRECTION
143
144 // Tests that a raw mock generates warnings for uninteresting calls.
TEST(RawMockTest,WarningForUninterestingCall)145 TEST(RawMockTest, WarningForUninterestingCall) {
146 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
147 GMOCK_FLAG_SET(verbose, "warning");
148
149 MockFoo raw_foo;
150
151 CaptureStdout();
152 raw_foo.DoThis();
153 raw_foo.DoThat(true);
154 EXPECT_THAT(GetCapturedStdout(),
155 HasSubstr("Uninteresting mock function call"));
156
157 GMOCK_FLAG_SET(verbose, saved_flag);
158 }
159
160 // Tests that a raw mock generates warnings for uninteresting calls
161 // that delete the mock object.
TEST(RawMockTest,WarningForUninterestingCallAfterDeath)162 TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
163 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
164 GMOCK_FLAG_SET(verbose, "warning");
165
166 MockFoo* const raw_foo = new MockFoo;
167
168 ON_CALL(*raw_foo, DoThis()).WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
169
170 CaptureStdout();
171 raw_foo->DoThis();
172 EXPECT_THAT(GetCapturedStdout(),
173 HasSubstr("Uninteresting mock function call"));
174
175 GMOCK_FLAG_SET(verbose, saved_flag);
176 }
177
178 // Tests that a raw mock generates informational logs for
179 // uninteresting calls.
TEST(RawMockTest,InfoForUninterestingCall)180 TEST(RawMockTest, InfoForUninterestingCall) {
181 MockFoo raw_foo;
182
183 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
184 GMOCK_FLAG_SET(verbose, "info");
185 CaptureStdout();
186 raw_foo.DoThis();
187 EXPECT_THAT(GetCapturedStdout(),
188 HasSubstr("Uninteresting mock function call"));
189
190 GMOCK_FLAG_SET(verbose, saved_flag);
191 }
192
TEST(RawMockTest,IsNaggy_IsNice_IsStrict)193 TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
194 MockFoo raw_foo;
195 EXPECT_TRUE(Mock::IsNaggy(&raw_foo));
196 EXPECT_FALSE(Mock::IsNice(&raw_foo));
197 EXPECT_FALSE(Mock::IsStrict(&raw_foo));
198 }
199
200 // Tests that a nice mock generates no warning for uninteresting calls.
TEST(NiceMockTest,NoWarningForUninterestingCall)201 TEST(NiceMockTest, NoWarningForUninterestingCall) {
202 NiceMock<MockFoo> nice_foo;
203
204 CaptureStdout();
205 nice_foo.DoThis();
206 nice_foo.DoThat(true);
207 EXPECT_EQ("", GetCapturedStdout());
208 }
209
210 // Tests that a nice mock generates no warning for uninteresting calls
211 // that delete the mock object.
TEST(NiceMockTest,NoWarningForUninterestingCallAfterDeath)212 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
213 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
214
215 ON_CALL(*nice_foo, DoThis())
216 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
217
218 CaptureStdout();
219 nice_foo->DoThis();
220 EXPECT_EQ("", GetCapturedStdout());
221 }
222
223 // Tests that a nice mock generates informational logs for
224 // uninteresting calls.
TEST(NiceMockTest,InfoForUninterestingCall)225 TEST(NiceMockTest, InfoForUninterestingCall) {
226 NiceMock<MockFoo> nice_foo;
227
228 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
229 GMOCK_FLAG_SET(verbose, "info");
230 CaptureStdout();
231 nice_foo.DoThis();
232 EXPECT_THAT(GetCapturedStdout(),
233 HasSubstr("Uninteresting mock function call"));
234
235 GMOCK_FLAG_SET(verbose, saved_flag);
236 }
237
238 #endif // GTEST_HAS_STREAM_REDIRECTION
239
240 // Tests that a nice mock allows expected calls.
TEST(NiceMockTest,AllowsExpectedCall)241 TEST(NiceMockTest, AllowsExpectedCall) {
242 NiceMock<MockFoo> nice_foo;
243
244 EXPECT_CALL(nice_foo, DoThis());
245 nice_foo.DoThis();
246 }
247
248 // Tests that an unexpected call on a nice mock which returns a
249 // not-default-constructible type throws an exception and the exception contains
250 // the method's name.
TEST(NiceMockTest,ThrowsExceptionForUnknownReturnTypes)251 TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
252 NiceMock<MockFoo> nice_foo;
253 #if GTEST_HAS_EXCEPTIONS
254 try {
255 nice_foo.ReturnNonDefaultConstructible();
256 FAIL();
257 } catch (const std::runtime_error& ex) {
258 EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
259 }
260 #else
261 EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
262 #endif
263 }
264
265 // Tests that an unexpected call on a nice mock fails.
TEST(NiceMockTest,UnexpectedCallFails)266 TEST(NiceMockTest, UnexpectedCallFails) {
267 NiceMock<MockFoo> nice_foo;
268
269 EXPECT_CALL(nice_foo, DoThis()).Times(0);
270 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
271 }
272
273 // Tests that NiceMock works with a mock class that has a non-default
274 // constructor.
TEST(NiceMockTest,NonDefaultConstructor)275 TEST(NiceMockTest, NonDefaultConstructor) {
276 NiceMock<MockBar> nice_bar("hi");
277 EXPECT_EQ("hi", nice_bar.str());
278
279 nice_bar.This();
280 nice_bar.That(5, true);
281 }
282
283 // Tests that NiceMock works with a mock class that has a 10-ary
284 // non-default constructor.
TEST(NiceMockTest,NonDefaultConstructor10)285 TEST(NiceMockTest, NonDefaultConstructor10) {
286 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,
287 false);
288 EXPECT_EQ("abcdefghTF", nice_bar.str());
289
290 nice_bar.This();
291 nice_bar.That(5, true);
292 }
293
TEST(NiceMockTest,AllowLeak)294 TEST(NiceMockTest, AllowLeak) {
295 NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
296 Mock::AllowLeak(leaked);
297 EXPECT_CALL(*leaked, DoThis());
298 leaked->DoThis();
299 }
300
TEST(NiceMockTest,MoveOnlyConstructor)301 TEST(NiceMockTest, MoveOnlyConstructor) {
302 NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
303 }
304
305 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
306 // class (as opposed to ::testing::Mock).
TEST(NiceMockTest,AcceptsClassNamedMock)307 TEST(NiceMockTest, AcceptsClassNamedMock) {
308 NiceMock< ::Mock> nice;
309 EXPECT_CALL(nice, DoThis());
310 nice.DoThis();
311 }
312
TEST(NiceMockTest,IsNiceInDestructor)313 TEST(NiceMockTest, IsNiceInDestructor) {
314 {
315 NiceMock<CallsMockMethodInDestructor> nice_on_destroy;
316 // Don't add an expectation for the call before the mock goes out of scope.
317 }
318 }
319
TEST(NiceMockTest,IsNaggy_IsNice_IsStrict)320 TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) {
321 NiceMock<MockFoo> nice_foo;
322 EXPECT_FALSE(Mock::IsNaggy(&nice_foo));
323 EXPECT_TRUE(Mock::IsNice(&nice_foo));
324 EXPECT_FALSE(Mock::IsStrict(&nice_foo));
325 }
326
327 #if GTEST_HAS_STREAM_REDIRECTION
328
329 // Tests that a naggy mock generates warnings for uninteresting calls.
TEST(NaggyMockTest,WarningForUninterestingCall)330 TEST(NaggyMockTest, WarningForUninterestingCall) {
331 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
332 GMOCK_FLAG_SET(verbose, "warning");
333
334 NaggyMock<MockFoo> naggy_foo;
335
336 CaptureStdout();
337 naggy_foo.DoThis();
338 naggy_foo.DoThat(true);
339 EXPECT_THAT(GetCapturedStdout(),
340 HasSubstr("Uninteresting mock function call"));
341
342 GMOCK_FLAG_SET(verbose, saved_flag);
343 }
344
345 // Tests that a naggy mock generates a warning for an uninteresting call
346 // that deletes the mock object.
TEST(NaggyMockTest,WarningForUninterestingCallAfterDeath)347 TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
348 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
349 GMOCK_FLAG_SET(verbose, "warning");
350
351 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
352
353 ON_CALL(*naggy_foo, DoThis())
354 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
355
356 CaptureStdout();
357 naggy_foo->DoThis();
358 EXPECT_THAT(GetCapturedStdout(),
359 HasSubstr("Uninteresting mock function call"));
360
361 GMOCK_FLAG_SET(verbose, saved_flag);
362 }
363
364 #endif // GTEST_HAS_STREAM_REDIRECTION
365
366 // Tests that a naggy mock allows expected calls.
TEST(NaggyMockTest,AllowsExpectedCall)367 TEST(NaggyMockTest, AllowsExpectedCall) {
368 NaggyMock<MockFoo> naggy_foo;
369
370 EXPECT_CALL(naggy_foo, DoThis());
371 naggy_foo.DoThis();
372 }
373
374 // Tests that an unexpected call on a naggy mock fails.
TEST(NaggyMockTest,UnexpectedCallFails)375 TEST(NaggyMockTest, UnexpectedCallFails) {
376 NaggyMock<MockFoo> naggy_foo;
377
378 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
379 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
380 "called more times than expected");
381 }
382
383 // Tests that NaggyMock works with a mock class that has a non-default
384 // constructor.
TEST(NaggyMockTest,NonDefaultConstructor)385 TEST(NaggyMockTest, NonDefaultConstructor) {
386 NaggyMock<MockBar> naggy_bar("hi");
387 EXPECT_EQ("hi", naggy_bar.str());
388
389 naggy_bar.This();
390 naggy_bar.That(5, true);
391 }
392
393 // Tests that NaggyMock works with a mock class that has a 10-ary
394 // non-default constructor.
TEST(NaggyMockTest,NonDefaultConstructor10)395 TEST(NaggyMockTest, NonDefaultConstructor10) {
396 NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5', "6", "7", true,
397 false);
398 EXPECT_EQ("01234567TF", naggy_bar.str());
399
400 naggy_bar.This();
401 naggy_bar.That(5, true);
402 }
403
TEST(NaggyMockTest,AllowLeak)404 TEST(NaggyMockTest, AllowLeak) {
405 NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;
406 Mock::AllowLeak(leaked);
407 EXPECT_CALL(*leaked, DoThis());
408 leaked->DoThis();
409 }
410
TEST(NaggyMockTest,MoveOnlyConstructor)411 TEST(NaggyMockTest, MoveOnlyConstructor) {
412 NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
413 }
414
415 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
416 // class (as opposed to ::testing::Mock).
TEST(NaggyMockTest,AcceptsClassNamedMock)417 TEST(NaggyMockTest, AcceptsClassNamedMock) {
418 NaggyMock< ::Mock> naggy;
419 EXPECT_CALL(naggy, DoThis());
420 naggy.DoThis();
421 }
422
TEST(NaggyMockTest,IsNaggyInDestructor)423 TEST(NaggyMockTest, IsNaggyInDestructor) {
424 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
425 GMOCK_FLAG_SET(verbose, "warning");
426 CaptureStdout();
427
428 {
429 NaggyMock<CallsMockMethodInDestructor> naggy_on_destroy;
430 // Don't add an expectation for the call before the mock goes out of scope.
431 }
432
433 EXPECT_THAT(GetCapturedStdout(),
434 HasSubstr("Uninteresting mock function call"));
435
436 GMOCK_FLAG_SET(verbose, saved_flag);
437 }
438
TEST(NaggyMockTest,IsNaggy_IsNice_IsStrict)439 TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) {
440 NaggyMock<MockFoo> naggy_foo;
441 EXPECT_TRUE(Mock::IsNaggy(&naggy_foo));
442 EXPECT_FALSE(Mock::IsNice(&naggy_foo));
443 EXPECT_FALSE(Mock::IsStrict(&naggy_foo));
444 }
445
446 // Tests that a strict mock allows expected calls.
TEST(StrictMockTest,AllowsExpectedCall)447 TEST(StrictMockTest, AllowsExpectedCall) {
448 StrictMock<MockFoo> strict_foo;
449
450 EXPECT_CALL(strict_foo, DoThis());
451 strict_foo.DoThis();
452 }
453
454 // Tests that an unexpected call on a strict mock fails.
TEST(StrictMockTest,UnexpectedCallFails)455 TEST(StrictMockTest, UnexpectedCallFails) {
456 StrictMock<MockFoo> strict_foo;
457
458 EXPECT_CALL(strict_foo, DoThis()).Times(0);
459 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
460 "called more times than expected");
461 }
462
463 // Tests that an uninteresting call on a strict mock fails.
TEST(StrictMockTest,UninterestingCallFails)464 TEST(StrictMockTest, UninterestingCallFails) {
465 StrictMock<MockFoo> strict_foo;
466
467 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
468 "Uninteresting mock function call");
469 }
470
471 // Tests that an uninteresting call on a strict mock fails, even if
472 // the call deletes the mock object.
TEST(StrictMockTest,UninterestingCallFailsAfterDeath)473 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
474 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
475
476 ON_CALL(*strict_foo, DoThis())
477 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
478
479 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
480 "Uninteresting mock function call");
481 }
482
483 // Tests that StrictMock works with a mock class that has a
484 // non-default constructor.
TEST(StrictMockTest,NonDefaultConstructor)485 TEST(StrictMockTest, NonDefaultConstructor) {
486 StrictMock<MockBar> strict_bar("hi");
487 EXPECT_EQ("hi", strict_bar.str());
488
489 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
490 "Uninteresting mock function call");
491 }
492
493 // Tests that StrictMock works with a mock class that has a 10-ary
494 // non-default constructor.
TEST(StrictMockTest,NonDefaultConstructor10)495 TEST(StrictMockTest, NonDefaultConstructor10) {
496 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,
497 false);
498 EXPECT_EQ("abcdefghTF", strict_bar.str());
499
500 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
501 "Uninteresting mock function call");
502 }
503
TEST(StrictMockTest,AllowLeak)504 TEST(StrictMockTest, AllowLeak) {
505 StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
506 Mock::AllowLeak(leaked);
507 EXPECT_CALL(*leaked, DoThis());
508 leaked->DoThis();
509 }
510
TEST(StrictMockTest,MoveOnlyConstructor)511 TEST(StrictMockTest, MoveOnlyConstructor) {
512 StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
513 }
514
515 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
516 // class (as opposed to ::testing::Mock).
TEST(StrictMockTest,AcceptsClassNamedMock)517 TEST(StrictMockTest, AcceptsClassNamedMock) {
518 StrictMock< ::Mock> strict;
519 EXPECT_CALL(strict, DoThis());
520 strict.DoThis();
521 }
522
TEST(StrictMockTest,IsStrictInDestructor)523 TEST(StrictMockTest, IsStrictInDestructor) {
524 EXPECT_NONFATAL_FAILURE(
525 {
526 StrictMock<CallsMockMethodInDestructor> strict_on_destroy;
527 // Don't add an expectation for the call before the mock goes out of
528 // scope.
529 },
530 "Uninteresting mock function call");
531 }
532
TEST(StrictMockTest,IsNaggy_IsNice_IsStrict)533 TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) {
534 StrictMock<MockFoo> strict_foo;
535 EXPECT_FALSE(Mock::IsNaggy(&strict_foo));
536 EXPECT_FALSE(Mock::IsNice(&strict_foo));
537 EXPECT_TRUE(Mock::IsStrict(&strict_foo));
538 }
539
540 } // namespace gmock_nice_strict_test
541 } // namespace testing
542