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