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 #include "gmock/gmock-nice-strict.h"
34 #include "gmock/gmock.h"
35 #include "gtest/gtest-spi.h"
36 #include "gtest/gtest.h"
38 // This must not be defined inside the ::testing namespace, or it will
39 // clash with ::testing::Mock.
44 MOCK_METHOD0(DoThis, void());
47 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
51 namespace gmock_nice_strict_test {
53 using testing::GMOCK_FLAG(verbose);
54 using testing::HasSubstr;
55 using testing::NaggyMock;
56 using testing::NiceMock;
57 using testing::StrictMock;
59 #if GTEST_HAS_STREAM_REDIRECTION
60 using testing::internal::CaptureStdout;
61 using testing::internal::GetCapturedStdout;
64 // Class without default constructor.
65 class NotDefaultConstructible {
67 explicit NotDefaultConstructible(int) {}
70 class CallsMockMethodInDestructor {
72 ~CallsMockMethodInDestructor() { OnDestroy(); }
73 MOCK_METHOD(void, OnDestroy, ());
76 // Defines some mock classes needed by the tests.
82 virtual void DoThis() = 0;
83 virtual int DoThat(bool flag) = 0;
86 class MockFoo : public Foo {
89 void Delete() { delete this; }
91 MOCK_METHOD0(DoThis, void());
92 MOCK_METHOD1(DoThat, int(bool flag));
93 MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
96 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
101 explicit MockBar(const std::string& s) : str_(s) {}
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');
109 virtual ~MockBar() {}
111 const std::string& str() const { return str_; }
113 MOCK_METHOD0(This, int());
114 MOCK_METHOD2(That, std::string(int, bool));
119 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
127 MoveOnly() = default;
129 MoveOnly(const MoveOnly&) = delete;
130 MoveOnly& operator=(const MoveOnly&) = delete;
132 MoveOnly(MoveOnly&&) = default;
133 MoveOnly& operator=(MoveOnly&&) = default;
139 #if GTEST_HAS_STREAM_REDIRECTION
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";
150 raw_foo.DoThat(true);
151 EXPECT_THAT(GetCapturedStdout(),
152 HasSubstr("Uninteresting mock function call"));
154 GMOCK_FLAG(verbose) = saved_flag;
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";
163 MockFoo* const raw_foo = new MockFoo;
165 ON_CALL(*raw_foo, DoThis())
166 .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
170 EXPECT_THAT(GetCapturedStdout(),
171 HasSubstr("Uninteresting mock function call"));
173 GMOCK_FLAG(verbose) = saved_flag;
176 // Tests that a raw mock generates informational logs for
177 // uninteresting calls.
178 TEST(RawMockTest, InfoForUninterestingCall) {
181 const std::string saved_flag = GMOCK_FLAG(verbose);
182 GMOCK_FLAG(verbose) = "info";
185 EXPECT_THAT(GetCapturedStdout(),
186 HasSubstr("Uninteresting mock function call"));
188 GMOCK_FLAG(verbose) = saved_flag;
191 TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
193 EXPECT_TRUE(Mock::IsNaggy(&raw_foo));
194 EXPECT_FALSE(Mock::IsNice(&raw_foo));
195 EXPECT_FALSE(Mock::IsStrict(&raw_foo));
198 // Tests that a nice mock generates no warning for uninteresting calls.
199 TEST(NiceMockTest, NoWarningForUninterestingCall) {
200 NiceMock<MockFoo> nice_foo;
204 nice_foo.DoThat(true);
205 EXPECT_EQ("", GetCapturedStdout());
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>;
213 ON_CALL(*nice_foo, DoThis())
214 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
218 EXPECT_EQ("", GetCapturedStdout());
221 // Tests that a nice mock generates informational logs for
222 // uninteresting calls.
223 TEST(NiceMockTest, InfoForUninterestingCall) {
224 NiceMock<MockFoo> nice_foo;
226 const std::string saved_flag = GMOCK_FLAG(verbose);
227 GMOCK_FLAG(verbose) = "info";
230 EXPECT_THAT(GetCapturedStdout(),
231 HasSubstr("Uninteresting mock function call"));
233 GMOCK_FLAG(verbose) = saved_flag;
236 #endif // GTEST_HAS_STREAM_REDIRECTION
238 // Tests that a nice mock allows expected calls.
239 TEST(NiceMockTest, AllowsExpectedCall) {
240 NiceMock<MockFoo> nice_foo;
242 EXPECT_CALL(nice_foo, DoThis());
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
253 nice_foo.ReturnNonDefaultConstructible();
255 } catch (const std::runtime_error& ex) {
256 EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
259 EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
263 // Tests that an unexpected call on a nice mock fails.
264 TEST(NiceMockTest, UnexpectedCallFails) {
265 NiceMock<MockFoo> nice_foo;
267 EXPECT_CALL(nice_foo, DoThis()).Times(0);
268 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
271 // Tests that NiceMock works with a mock class that has a non-default
273 TEST(NiceMockTest, NonDefaultConstructor) {
274 NiceMock<MockBar> nice_bar("hi");
275 EXPECT_EQ("hi", nice_bar.str());
278 nice_bar.That(5, true);
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());
289 nice_bar.That(5, true);
292 TEST(NiceMockTest, AllowLeak) {
293 NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
294 Mock::AllowLeak(leaked);
295 EXPECT_CALL(*leaked, DoThis());
299 TEST(NiceMockTest, MoveOnlyConstructor) {
300 NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
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());
311 TEST(NiceMockTest, IsNiceInDestructor) {
313 NiceMock<CallsMockMethodInDestructor> nice_on_destroy;
314 // Don't add an expectation for the call before the mock goes out of scope.
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));
325 #if GTEST_HAS_STREAM_REDIRECTION
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";
332 NaggyMock<MockFoo> naggy_foo;
336 naggy_foo.DoThat(true);
337 EXPECT_THAT(GetCapturedStdout(),
338 HasSubstr("Uninteresting mock function call"));
340 GMOCK_FLAG(verbose) = saved_flag;
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";
349 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
351 ON_CALL(*naggy_foo, DoThis())
352 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
356 EXPECT_THAT(GetCapturedStdout(),
357 HasSubstr("Uninteresting mock function call"));
359 GMOCK_FLAG(verbose) = saved_flag;
362 #endif // GTEST_HAS_STREAM_REDIRECTION
364 // Tests that a naggy mock allows expected calls.
365 TEST(NaggyMockTest, AllowsExpectedCall) {
366 NaggyMock<MockFoo> naggy_foo;
368 EXPECT_CALL(naggy_foo, DoThis());
372 // Tests that an unexpected call on a naggy mock fails.
373 TEST(NaggyMockTest, UnexpectedCallFails) {
374 NaggyMock<MockFoo> naggy_foo;
376 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
377 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
378 "called more times than expected");
381 // Tests that NaggyMock works with a mock class that has a non-default
383 TEST(NaggyMockTest, NonDefaultConstructor) {
384 NaggyMock<MockBar> naggy_bar("hi");
385 EXPECT_EQ("hi", naggy_bar.str());
388 naggy_bar.That(5, true);
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());
399 naggy_bar.That(5, true);
402 TEST(NaggyMockTest, AllowLeak) {
403 NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;
404 Mock::AllowLeak(leaked);
405 EXPECT_CALL(*leaked, DoThis());
409 TEST(NaggyMockTest, MoveOnlyConstructor) {
410 NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
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());
421 TEST(NaggyMockTest, IsNaggyInDestructor) {
422 const std::string saved_flag = GMOCK_FLAG(verbose);
423 GMOCK_FLAG(verbose) = "warning";
427 NaggyMock<CallsMockMethodInDestructor> naggy_on_destroy;
428 // Don't add an expectation for the call before the mock goes out of scope.
431 EXPECT_THAT(GetCapturedStdout(),
432 HasSubstr("Uninteresting mock function call"));
434 GMOCK_FLAG(verbose) = saved_flag;
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));
444 // Tests that a strict mock allows expected calls.
445 TEST(StrictMockTest, AllowsExpectedCall) {
446 StrictMock<MockFoo> strict_foo;
448 EXPECT_CALL(strict_foo, DoThis());
452 // Tests that an unexpected call on a strict mock fails.
453 TEST(StrictMockTest, UnexpectedCallFails) {
454 StrictMock<MockFoo> strict_foo;
456 EXPECT_CALL(strict_foo, DoThis()).Times(0);
457 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
458 "called more times than expected");
461 // Tests that an uninteresting call on a strict mock fails.
462 TEST(StrictMockTest, UninterestingCallFails) {
463 StrictMock<MockFoo> strict_foo;
465 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
466 "Uninteresting mock function call");
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>;
474 ON_CALL(*strict_foo, DoThis())
475 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
477 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
478 "Uninteresting mock function call");
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());
487 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
488 "Uninteresting mock function call");
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());
498 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
499 "Uninteresting mock function call");
502 TEST(StrictMockTest, AllowLeak) {
503 StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
504 Mock::AllowLeak(leaked);
505 EXPECT_CALL(*leaked, DoThis());
509 TEST(StrictMockTest, MoveOnlyConstructor) {
510 StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
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());
521 TEST(StrictMockTest, IsStrictInDestructor) {
522 EXPECT_NONFATAL_FAILURE(
524 StrictMock<CallsMockMethodInDestructor> strict_on_destroy;
525 // Don't add an expectation for the call before the mock goes out of
528 "Uninteresting mock function call");
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));
538 } // namespace gmock_nice_strict_test
539 } // namespace testing