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 // Defines some mock classes needed by the tests.
76 virtual void DoThis() = 0;
77 virtual int DoThat(bool flag) = 0;
80 class MockFoo : public Foo {
83 void Delete() { delete this; }
85 MOCK_METHOD0(DoThis, void());
86 MOCK_METHOD1(DoThat, int(bool flag));
87 MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
90 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
95 explicit MockBar(const std::string& s) : str_(s) {}
97 MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,
98 const std::string& a7, const std::string& a8, bool a9, bool a10) {
99 str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
100 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
103 virtual ~MockBar() {}
105 const std::string& str() const { return str_; }
107 MOCK_METHOD0(This, int());
108 MOCK_METHOD2(That, std::string(int, bool));
113 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
121 MoveOnly() = default;
123 MoveOnly(const MoveOnly&) = delete;
124 MoveOnly& operator=(const MoveOnly&) = delete;
126 MoveOnly(MoveOnly&&) = default;
127 MoveOnly& operator=(MoveOnly&&) = default;
133 #if GTEST_HAS_STREAM_REDIRECTION
135 // Tests that a raw mock generates warnings for uninteresting calls.
136 TEST(RawMockTest, WarningForUninterestingCall) {
137 const std::string saved_flag = GMOCK_FLAG(verbose);
138 GMOCK_FLAG(verbose) = "warning";
144 raw_foo.DoThat(true);
145 EXPECT_THAT(GetCapturedStdout(),
146 HasSubstr("Uninteresting mock function call"));
148 GMOCK_FLAG(verbose) = saved_flag;
151 // Tests that a raw mock generates warnings for uninteresting calls
152 // that delete the mock object.
153 TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
154 const std::string saved_flag = GMOCK_FLAG(verbose);
155 GMOCK_FLAG(verbose) = "warning";
157 MockFoo* const raw_foo = new MockFoo;
159 ON_CALL(*raw_foo, DoThis())
160 .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
164 EXPECT_THAT(GetCapturedStdout(),
165 HasSubstr("Uninteresting mock function call"));
167 GMOCK_FLAG(verbose) = saved_flag;
170 // Tests that a raw mock generates informational logs for
171 // uninteresting calls.
172 TEST(RawMockTest, InfoForUninterestingCall) {
175 const std::string saved_flag = GMOCK_FLAG(verbose);
176 GMOCK_FLAG(verbose) = "info";
179 EXPECT_THAT(GetCapturedStdout(),
180 HasSubstr("Uninteresting mock function call"));
182 GMOCK_FLAG(verbose) = saved_flag;
185 TEST(RawMockTest, IsNaggy_IsNice_IsStrict) {
187 EXPECT_TRUE(Mock::IsNaggy(&raw_foo));
188 EXPECT_FALSE(Mock::IsNice(&raw_foo));
189 EXPECT_FALSE(Mock::IsStrict(&raw_foo));
192 // Tests that a nice mock generates no warning for uninteresting calls.
193 TEST(NiceMockTest, NoWarningForUninterestingCall) {
194 NiceMock<MockFoo> nice_foo;
198 nice_foo.DoThat(true);
199 EXPECT_EQ("", GetCapturedStdout());
202 // Tests that a nice mock generates no warning for uninteresting calls
203 // that delete the mock object.
204 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
205 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
207 ON_CALL(*nice_foo, DoThis())
208 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
212 EXPECT_EQ("", GetCapturedStdout());
215 // Tests that a nice mock generates informational logs for
216 // uninteresting calls.
217 TEST(NiceMockTest, InfoForUninterestingCall) {
218 NiceMock<MockFoo> nice_foo;
220 const std::string saved_flag = GMOCK_FLAG(verbose);
221 GMOCK_FLAG(verbose) = "info";
224 EXPECT_THAT(GetCapturedStdout(),
225 HasSubstr("Uninteresting mock function call"));
227 GMOCK_FLAG(verbose) = saved_flag;
230 #endif // GTEST_HAS_STREAM_REDIRECTION
232 // Tests that a nice mock allows expected calls.
233 TEST(NiceMockTest, AllowsExpectedCall) {
234 NiceMock<MockFoo> nice_foo;
236 EXPECT_CALL(nice_foo, DoThis());
240 // Tests that an unexpected call on a nice mock which returns a
241 // not-default-constructible type throws an exception and the exception contains
242 // the method's name.
243 TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
244 NiceMock<MockFoo> nice_foo;
245 #if GTEST_HAS_EXCEPTIONS
247 nice_foo.ReturnNonDefaultConstructible();
249 } catch (const std::runtime_error& ex) {
250 EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
253 EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
257 // Tests that an unexpected call on a nice mock fails.
258 TEST(NiceMockTest, UnexpectedCallFails) {
259 NiceMock<MockFoo> nice_foo;
261 EXPECT_CALL(nice_foo, DoThis()).Times(0);
262 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
265 // Tests that NiceMock works with a mock class that has a non-default
267 TEST(NiceMockTest, NonDefaultConstructor) {
268 NiceMock<MockBar> nice_bar("hi");
269 EXPECT_EQ("hi", nice_bar.str());
272 nice_bar.That(5, true);
275 // Tests that NiceMock works with a mock class that has a 10-ary
276 // non-default constructor.
277 TEST(NiceMockTest, NonDefaultConstructor10) {
278 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
279 "g", "h", true, false);
280 EXPECT_EQ("abcdefghTF", nice_bar.str());
283 nice_bar.That(5, true);
286 TEST(NiceMockTest, AllowLeak) {
287 NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
288 Mock::AllowLeak(leaked);
289 EXPECT_CALL(*leaked, DoThis());
293 TEST(NiceMockTest, MoveOnlyConstructor) {
294 NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
297 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
298 // class (as opposed to ::testing::Mock).
299 TEST(NiceMockTest, AcceptsClassNamedMock) {
300 NiceMock< ::Mock> nice;
301 EXPECT_CALL(nice, DoThis());
305 TEST(NiceMockTest, IsNaggy_IsNice_IsStrict) {
306 NiceMock<MockFoo> nice_foo;
307 EXPECT_FALSE(Mock::IsNaggy(&nice_foo));
308 EXPECT_TRUE(Mock::IsNice(&nice_foo));
309 EXPECT_FALSE(Mock::IsStrict(&nice_foo));
312 #if GTEST_HAS_STREAM_REDIRECTION
314 // Tests that a naggy mock generates warnings for uninteresting calls.
315 TEST(NaggyMockTest, WarningForUninterestingCall) {
316 const std::string saved_flag = GMOCK_FLAG(verbose);
317 GMOCK_FLAG(verbose) = "warning";
319 NaggyMock<MockFoo> naggy_foo;
323 naggy_foo.DoThat(true);
324 EXPECT_THAT(GetCapturedStdout(),
325 HasSubstr("Uninteresting mock function call"));
327 GMOCK_FLAG(verbose) = saved_flag;
330 // Tests that a naggy mock generates a warning for an uninteresting call
331 // that deletes the mock object.
332 TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
333 const std::string saved_flag = GMOCK_FLAG(verbose);
334 GMOCK_FLAG(verbose) = "warning";
336 NaggyMock<MockFoo>* const naggy_foo = new NaggyMock<MockFoo>;
338 ON_CALL(*naggy_foo, DoThis())
339 .WillByDefault(Invoke(naggy_foo, &MockFoo::Delete));
343 EXPECT_THAT(GetCapturedStdout(),
344 HasSubstr("Uninteresting mock function call"));
346 GMOCK_FLAG(verbose) = saved_flag;
349 #endif // GTEST_HAS_STREAM_REDIRECTION
351 // Tests that a naggy mock allows expected calls.
352 TEST(NaggyMockTest, AllowsExpectedCall) {
353 NaggyMock<MockFoo> naggy_foo;
355 EXPECT_CALL(naggy_foo, DoThis());
359 // Tests that an unexpected call on a naggy mock fails.
360 TEST(NaggyMockTest, UnexpectedCallFails) {
361 NaggyMock<MockFoo> naggy_foo;
363 EXPECT_CALL(naggy_foo, DoThis()).Times(0);
364 EXPECT_NONFATAL_FAILURE(naggy_foo.DoThis(),
365 "called more times than expected");
368 // Tests that NaggyMock works with a mock class that has a non-default
370 TEST(NaggyMockTest, NonDefaultConstructor) {
371 NaggyMock<MockBar> naggy_bar("hi");
372 EXPECT_EQ("hi", naggy_bar.str());
375 naggy_bar.That(5, true);
378 // Tests that NaggyMock works with a mock class that has a 10-ary
379 // non-default constructor.
380 TEST(NaggyMockTest, NonDefaultConstructor10) {
381 NaggyMock<MockBar> naggy_bar('0', '1', "2", "3", '4', '5',
382 "6", "7", true, false);
383 EXPECT_EQ("01234567TF", naggy_bar.str());
386 naggy_bar.That(5, true);
389 TEST(NaggyMockTest, AllowLeak) {
390 NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;
391 Mock::AllowLeak(leaked);
392 EXPECT_CALL(*leaked, DoThis());
396 TEST(NaggyMockTest, MoveOnlyConstructor) {
397 NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
400 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
401 // class (as opposed to ::testing::Mock).
402 TEST(NaggyMockTest, AcceptsClassNamedMock) {
403 NaggyMock< ::Mock> naggy;
404 EXPECT_CALL(naggy, DoThis());
408 TEST(NaggyMockTest, IsNaggy_IsNice_IsStrict) {
409 NaggyMock<MockFoo> naggy_foo;
410 EXPECT_TRUE(Mock::IsNaggy(&naggy_foo));
411 EXPECT_FALSE(Mock::IsNice(&naggy_foo));
412 EXPECT_FALSE(Mock::IsStrict(&naggy_foo));
415 // Tests that a strict mock allows expected calls.
416 TEST(StrictMockTest, AllowsExpectedCall) {
417 StrictMock<MockFoo> strict_foo;
419 EXPECT_CALL(strict_foo, DoThis());
423 // Tests that an unexpected call on a strict mock fails.
424 TEST(StrictMockTest, UnexpectedCallFails) {
425 StrictMock<MockFoo> strict_foo;
427 EXPECT_CALL(strict_foo, DoThis()).Times(0);
428 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
429 "called more times than expected");
432 // Tests that an uninteresting call on a strict mock fails.
433 TEST(StrictMockTest, UninterestingCallFails) {
434 StrictMock<MockFoo> strict_foo;
436 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
437 "Uninteresting mock function call");
440 // Tests that an uninteresting call on a strict mock fails, even if
441 // the call deletes the mock object.
442 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
443 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
445 ON_CALL(*strict_foo, DoThis())
446 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
448 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
449 "Uninteresting mock function call");
452 // Tests that StrictMock works with a mock class that has a
453 // non-default constructor.
454 TEST(StrictMockTest, NonDefaultConstructor) {
455 StrictMock<MockBar> strict_bar("hi");
456 EXPECT_EQ("hi", strict_bar.str());
458 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
459 "Uninteresting mock function call");
462 // Tests that StrictMock works with a mock class that has a 10-ary
463 // non-default constructor.
464 TEST(StrictMockTest, NonDefaultConstructor10) {
465 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
466 "g", "h", true, false);
467 EXPECT_EQ("abcdefghTF", strict_bar.str());
469 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
470 "Uninteresting mock function call");
473 TEST(StrictMockTest, AllowLeak) {
474 StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
475 Mock::AllowLeak(leaked);
476 EXPECT_CALL(*leaked, DoThis());
480 TEST(StrictMockTest, MoveOnlyConstructor) {
481 StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
484 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
485 // class (as opposed to ::testing::Mock).
486 TEST(StrictMockTest, AcceptsClassNamedMock) {
487 StrictMock< ::Mock> strict;
488 EXPECT_CALL(strict, DoThis());
492 TEST(StrictMockTest, IsNaggy_IsNice_IsStrict) {
493 StrictMock<MockFoo> strict_foo;
494 EXPECT_FALSE(Mock::IsNaggy(&strict_foo));
495 EXPECT_FALSE(Mock::IsNice(&strict_foo));
496 EXPECT_TRUE(Mock::IsStrict(&strict_foo));
499 } // namespace gmock_nice_strict_test
500 } // namespace testing