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