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"
35 #include "gmock/gmock.h"
36 #include "gtest/gtest-spi.h"
37 #include "gtest/gtest.h"
39 // This must not be defined inside the ::testing namespace, or it will
40 // clash with ::testing::Mock.
45 MOCK_METHOD0(DoThis, void());
48 Mock(const Mock&) = delete;
49 Mock& operator=(const Mock&) = delete;
53 namespace gmock_nice_strict_test {
55 using testing::HasSubstr;
56 using testing::NaggyMock;
57 using testing::NiceMock;
58 using testing::StrictMock;
60 #if GTEST_HAS_STREAM_REDIRECTION
61 using testing::internal::CaptureStdout;
62 using testing::internal::GetCapturedStdout;
65 // Class without default constructor.
66 class NotDefaultConstructible {
68 explicit NotDefaultConstructible(int) {}
71 class CallsMockMethodInDestructor {
73 ~CallsMockMethodInDestructor() { OnDestroy(); }
74 MOCK_METHOD(void, OnDestroy, ());
77 // Defines some mock classes needed by the tests.
83 virtual void DoThis() = 0;
84 virtual int DoThat(bool flag) = 0;
87 class MockFoo : public Foo {
90 void Delete() { delete this; }
92 MOCK_METHOD0(DoThis, void());
93 MOCK_METHOD1(DoThat, int(bool flag));
94 MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
97 MockFoo(const MockFoo&) = delete;
98 MockFoo& operator=(const MockFoo&) = delete;
103 explicit MockBar(const std::string& s) : str_(s) {}
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') +
112 virtual ~MockBar() {}
114 const std::string& str() const { return str_; }
116 MOCK_METHOD0(This, int());
117 MOCK_METHOD2(That, std::string(int, bool));
122 MockBar(const MockBar&) = delete;
123 MockBar& operator=(const MockBar&) = delete;
130 MoveOnly() = default;
132 MoveOnly(const MoveOnly&) = delete;
133 MoveOnly& operator=(const MoveOnly&) = delete;
135 MoveOnly(MoveOnly&&) = default;
136 MoveOnly& operator=(MoveOnly&&) = default;
142 #if GTEST_HAS_STREAM_REDIRECTION
144 // Tests that a raw mock generates warnings for uninteresting calls.
145 TEST(RawMockTest, WarningForUninterestingCall) {
146 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
147 GMOCK_FLAG_SET(verbose, "warning");
153 raw_foo.DoThat(true);
154 EXPECT_THAT(GetCapturedStdout(),
155 HasSubstr("Uninteresting mock function call"));
157 GMOCK_FLAG_SET(verbose, saved_flag);
160 // Tests that a raw mock generates warnings for uninteresting calls
161 // that delete the mock object.
162 TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
163 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
164 GMOCK_FLAG_SET(verbose, "warning");
166 MockFoo* const raw_foo = new MockFoo;
168 ON_CALL(*raw_foo, DoThis()).WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
172 EXPECT_THAT(GetCapturedStdout(),
173 HasSubstr("Uninteresting mock function call"));
175 GMOCK_FLAG_SET(verbose, saved_flag);
178 // Tests that a raw mock generates informational logs for
179 // uninteresting calls.
180 TEST(RawMockTest, InfoForUninterestingCall) {
183 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
184 GMOCK_FLAG_SET(verbose, "info");
187 EXPECT_THAT(GetCapturedStdout(),
188 HasSubstr("Uninteresting mock function call"));
190 GMOCK_FLAG_SET(verbose, saved_flag);
193 TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
195 EXPECT_TRUE(Mock::IsNaggy(&raw_foo));
196 EXPECT_FALSE(Mock::IsNice(&raw_foo));
197 EXPECT_FALSE(Mock::IsStrict(&raw_foo));
200 // Tests that a nice mock generates no warning for uninteresting calls.
201 TEST(NiceMockTest, NoWarningForUninterestingCall) {
202 NiceMock<MockFoo> nice_foo;
206 nice_foo.DoThat(true);
207 EXPECT_EQ("", GetCapturedStdout());
210 // Tests that a nice mock generates no warning for uninteresting calls
211 // that delete the mock object.
212 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
213 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
215 ON_CALL(*nice_foo, DoThis())
216 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
220 EXPECT_EQ("", GetCapturedStdout());
223 // Tests that a nice mock generates informational logs for
224 // uninteresting calls.
225 TEST(NiceMockTest, InfoForUninterestingCall) {
226 NiceMock<MockFoo> nice_foo;
228 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
229 GMOCK_FLAG_SET(verbose, "info");
232 EXPECT_THAT(GetCapturedStdout(),
233 HasSubstr("Uninteresting mock function call"));
235 GMOCK_FLAG_SET(verbose, saved_flag);
238 #endif // GTEST_HAS_STREAM_REDIRECTION
240 // Tests that a nice mock allows expected calls.
241 TEST(NiceMockTest, AllowsExpectedCall) {
242 NiceMock<MockFoo> nice_foo;
244 EXPECT_CALL(nice_foo, DoThis());
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.
251 TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
252 NiceMock<MockFoo> nice_foo;
253 #if GTEST_HAS_EXCEPTIONS
255 nice_foo.ReturnNonDefaultConstructible();
257 } catch (const std::runtime_error& ex) {
258 EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
261 EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
265 // Tests that an unexpected call on a nice mock fails.
266 TEST(NiceMockTest, UnexpectedCallFails) {
267 NiceMock<MockFoo> nice_foo;
269 EXPECT_CALL(nice_foo, DoThis()).Times(0);
270 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
273 // Tests that NiceMock works with a mock class that has a non-default
275 TEST(NiceMockTest, NonDefaultConstructor) {
276 NiceMock<MockBar> nice_bar("hi");
277 EXPECT_EQ("hi", nice_bar.str());
280 nice_bar.That(5, true);
283 // Tests that NiceMock works with a mock class that has a 10-ary
284 // non-default constructor.
285 TEST(NiceMockTest, NonDefaultConstructor10) {
286 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,
288 EXPECT_EQ("abcdefghTF", nice_bar.str());
291 nice_bar.That(5, true);
294 TEST(NiceMockTest, AllowLeak) {
295 NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
296 Mock::AllowLeak(leaked);
297 EXPECT_CALL(*leaked, DoThis());
301 TEST(NiceMockTest, MoveOnlyConstructor) {
302 NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
305 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
306 // class (as opposed to ::testing::Mock).
307 TEST(NiceMockTest, AcceptsClassNamedMock) {
308 NiceMock< ::Mock> nice;
309 EXPECT_CALL(nice, DoThis());
313 TEST(NiceMockTest, IsNiceInDestructor) {
315 NiceMock<CallsMockMethodInDestructor> nice_on_destroy;
316 // Don't add an expectation for the call before the mock goes out of scope.
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));
327 #if GTEST_HAS_STREAM_REDIRECTION
329 // Tests that a naggy mock generates warnings for uninteresting calls.
330 TEST(NaggyMockTest, WarningForUninterestingCall) {
331 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
332 GMOCK_FLAG_SET(verbose, "warning");
334 NaggyMock<MockFoo> naggy_foo;
338 naggy_foo.DoThat(true);
339 EXPECT_THAT(GetCapturedStdout(),
340 HasSubstr("Uninteresting mock function call"));
342 GMOCK_FLAG_SET(verbose, saved_flag);
345 // Tests that a naggy mock generates a warning for an uninteresting call
346 // that deletes the mock object.
347 TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
348 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
349 GMOCK_FLAG_SET(verbose, "warning");
351 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
353 ON_CALL(*naggy_foo, DoThis())
354 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
358 EXPECT_THAT(GetCapturedStdout(),
359 HasSubstr("Uninteresting mock function call"));
361 GMOCK_FLAG_SET(verbose, saved_flag);
364 #endif // GTEST_HAS_STREAM_REDIRECTION
366 // Tests that a naggy mock allows expected calls.
367 TEST(NaggyMockTest, AllowsExpectedCall) {
368 NaggyMock<MockFoo> naggy_foo;
370 EXPECT_CALL(naggy_foo, DoThis());
374 // Tests that an unexpected call on a naggy mock fails.
375 TEST(NaggyMockTest, UnexpectedCallFails) {
376 NaggyMock<MockFoo> naggy_foo;
378 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
379 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
380 "called more times than expected");
383 // Tests that NaggyMock works with a mock class that has a non-default
385 TEST(NaggyMockTest, NonDefaultConstructor) {
386 NaggyMock<MockBar> naggy_bar("hi");
387 EXPECT_EQ("hi", naggy_bar.str());
390 naggy_bar.That(5, true);
393 // Tests that NaggyMock works with a mock class that has a 10-ary
394 // non-default constructor.
395 TEST(NaggyMockTest, NonDefaultConstructor10) {
396 NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5', "6", "7", true,
398 EXPECT_EQ("01234567TF", naggy_bar.str());
401 naggy_bar.That(5, true);
404 TEST(NaggyMockTest, AllowLeak) {
405 NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;
406 Mock::AllowLeak(leaked);
407 EXPECT_CALL(*leaked, DoThis());
411 TEST(NaggyMockTest, MoveOnlyConstructor) {
412 NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
415 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
416 // class (as opposed to ::testing::Mock).
417 TEST(NaggyMockTest, AcceptsClassNamedMock) {
418 NaggyMock< ::Mock> naggy;
419 EXPECT_CALL(naggy, DoThis());
423 TEST(NaggyMockTest, IsNaggyInDestructor) {
424 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
425 GMOCK_FLAG_SET(verbose, "warning");
429 NaggyMock<CallsMockMethodInDestructor> naggy_on_destroy;
430 // Don't add an expectation for the call before the mock goes out of scope.
433 EXPECT_THAT(GetCapturedStdout(),
434 HasSubstr("Uninteresting mock function call"));
436 GMOCK_FLAG_SET(verbose, saved_flag);
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));
446 // Tests that a strict mock allows expected calls.
447 TEST(StrictMockTest, AllowsExpectedCall) {
448 StrictMock<MockFoo> strict_foo;
450 EXPECT_CALL(strict_foo, DoThis());
454 // Tests that an unexpected call on a strict mock fails.
455 TEST(StrictMockTest, UnexpectedCallFails) {
456 StrictMock<MockFoo> strict_foo;
458 EXPECT_CALL(strict_foo, DoThis()).Times(0);
459 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
460 "called more times than expected");
463 // Tests that an uninteresting call on a strict mock fails.
464 TEST(StrictMockTest, UninterestingCallFails) {
465 StrictMock<MockFoo> strict_foo;
467 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
468 "Uninteresting mock function call");
471 // Tests that an uninteresting call on a strict mock fails, even if
472 // the call deletes the mock object.
473 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
474 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
476 ON_CALL(*strict_foo, DoThis())
477 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
479 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
480 "Uninteresting mock function call");
483 // Tests that StrictMock works with a mock class that has a
484 // non-default constructor.
485 TEST(StrictMockTest, NonDefaultConstructor) {
486 StrictMock<MockBar> strict_bar("hi");
487 EXPECT_EQ("hi", strict_bar.str());
489 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
490 "Uninteresting mock function call");
493 // Tests that StrictMock works with a mock class that has a 10-ary
494 // non-default constructor.
495 TEST(StrictMockTest, NonDefaultConstructor10) {
496 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', "g", "h", true,
498 EXPECT_EQ("abcdefghTF", strict_bar.str());
500 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
501 "Uninteresting mock function call");
504 TEST(StrictMockTest, AllowLeak) {
505 StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
506 Mock::AllowLeak(leaked);
507 EXPECT_CALL(*leaked, DoThis());
511 TEST(StrictMockTest, MoveOnlyConstructor) {
512 StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
515 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
516 // class (as opposed to ::testing::Mock).
517 TEST(StrictMockTest, AcceptsClassNamedMock) {
518 StrictMock< ::Mock> strict;
519 EXPECT_CALL(strict, DoThis());
523 TEST(StrictMockTest, IsStrictInDestructor) {
524 EXPECT_NONFATAL_FAILURE(
526 StrictMock<CallsMockMethodInDestructor> strict_on_destroy;
527 // Don't add an expectation for the call before the mock goes out of
530 "Uninteresting mock function call");
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));
540 } // namespace gmock_nice_strict_test
541 } // namespace testing