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.
31 #include "gmock/gmock-generated-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 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock);
52 namespace gmock_nice_strict_test {
54 using testing::GMOCK_FLAG(verbose);
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 // Defines some mock classes needed by the tests.
77 virtual void DoThis() = 0;
78 virtual int DoThat(bool flag) = 0;
81 class MockFoo : public Foo {
84 void Delete() { delete this; }
86 MOCK_METHOD0(DoThis, void());
87 MOCK_METHOD1(DoThat, int(bool flag));
88 MOCK_METHOD0(ReturnNonDefaultConstructible, NotDefaultConstructible());
91 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
96 explicit MockBar(const std::string& s) : str_(s) {}
98 MockBar(char a1, char a2, std::string a3, std::string a4, int a5, int a6,
99 const std::string& a7, const std::string& a8, bool a9, bool a10) {
100 str_ = std::string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) +
101 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F');
104 virtual ~MockBar() {}
106 const std::string& str() const { return str_; }
108 MOCK_METHOD0(This, int());
109 MOCK_METHOD2(That, std::string(int, bool));
114 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar);
117 #if GTEST_GTEST_LANG_CXX11
122 MoveOnly() = default;
124 MoveOnly(const MoveOnly&) = delete;
125 operator=(const MoveOnly&) = delete;
127 MoveOnly(MoveOnly&&) = default;
128 operator=(MoveOnly&&) = default;
133 #endif // GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
135 #if GTEST_HAS_STREAM_REDIRECTION
137 // Tests that a raw mock generates warnings for uninteresting calls.
138 TEST(RawMockTest, WarningForUninterestingCall) {
139 const std::string saved_flag = GMOCK_FLAG(verbose);
140 GMOCK_FLAG(verbose) = "warning";
146 raw_foo.DoThat(true);
147 EXPECT_THAT(GetCapturedStdout(),
148 HasSubstr("Uninteresting mock function call"));
150 GMOCK_FLAG(verbose) = saved_flag;
153 // Tests that a raw mock generates warnings for uninteresting calls
154 // that delete the mock object.
155 TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
156 const std::string saved_flag = GMOCK_FLAG(verbose);
157 GMOCK_FLAG(verbose) = "warning";
159 MockFoo* const raw_foo = new MockFoo;
161 ON_CALL(*raw_foo, DoThis())
162 .WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
166 EXPECT_THAT(GetCapturedStdout(),
167 HasSubstr("Uninteresting mock function call"));
169 GMOCK_FLAG(verbose) = saved_flag;
172 // Tests that a raw mock generates informational logs for
173 // uninteresting calls.
174 TEST(RawMockTest, InfoForUninterestingCall) {
177 const std::string saved_flag = GMOCK_FLAG(verbose);
178 GMOCK_FLAG(verbose) = "info";
181 EXPECT_THAT(GetCapturedStdout(),
182 HasSubstr("Uninteresting mock function call"));
184 GMOCK_FLAG(verbose) = saved_flag;
187 // Tests that a nice mock generates no warning for uninteresting calls.
188 TEST(NiceMockTest, NoWarningForUninterestingCall) {
189 NiceMock<MockFoo> nice_foo;
193 nice_foo.DoThat(true);
194 EXPECT_EQ("", GetCapturedStdout());
197 // Tests that a nice mock generates no warning for uninteresting calls
198 // that delete the mock object.
199 TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) {
200 NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>;
202 ON_CALL(*nice_foo, DoThis())
203 .WillByDefault(Invoke(nice_foo, &MockFoo::Delete));
207 EXPECT_EQ("", GetCapturedStdout());
210 // Tests that a nice mock generates informational logs for
211 // uninteresting calls.
212 TEST(NiceMockTest, InfoForUninterestingCall) {
213 NiceMock<MockFoo> nice_foo;
215 const std::string saved_flag = GMOCK_FLAG(verbose);
216 GMOCK_FLAG(verbose) = "info";
219 EXPECT_THAT(GetCapturedStdout(),
220 HasSubstr("Uninteresting mock function call"));
222 GMOCK_FLAG(verbose) = saved_flag;
225 #endif // GTEST_HAS_STREAM_REDIRECTION
227 // Tests that a nice mock allows expected calls.
228 TEST(NiceMockTest, AllowsExpectedCall) {
229 NiceMock<MockFoo> nice_foo;
231 EXPECT_CALL(nice_foo, DoThis());
235 // Tests that an unexpected call on a nice mock which returns a
236 // not-default-constructible type throws an exception and the exception contains
237 // the method's name.
238 TEST(NiceMockTest, ThrowsExceptionForUnknownReturnTypes) {
239 NiceMock<MockFoo> nice_foo;
240 #if GTEST_HAS_EXCEPTIONS
242 nice_foo.ReturnNonDefaultConstructible();
244 } catch (const std::runtime_error& ex) {
245 EXPECT_THAT(ex.what(), HasSubstr("ReturnNonDefaultConstructible"));
248 EXPECT_DEATH_IF_SUPPORTED({ nice_foo.ReturnNonDefaultConstructible(); }, "");
252 // Tests that an unexpected call on a nice mock fails.
253 TEST(NiceMockTest, UnexpectedCallFails) {
254 NiceMock<MockFoo> nice_foo;
256 EXPECT_CALL(nice_foo, DoThis()).Times(0);
257 EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected");
260 // Tests that NiceMock works with a mock class that has a non-default
262 TEST(NiceMockTest, NonDefaultConstructor) {
263 NiceMock<MockBar> nice_bar("hi");
264 EXPECT_EQ("hi", nice_bar.str());
267 nice_bar.That(5, true);
270 // Tests that NiceMock works with a mock class that has a 10-ary
271 // non-default constructor.
272 TEST(NiceMockTest, NonDefaultConstructor10) {
273 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f',
274 "g", "h", true, false);
275 EXPECT_EQ("abcdefghTF", nice_bar.str());
278 nice_bar.That(5, true);
281 TEST(NiceMockTest, AllowLeak) {
282 NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
283 Mock::AllowLeak(leaked);
284 EXPECT_CALL(*leaked, DoThis());
288 #if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
290 TEST(NiceMockTest, MoveOnlyConstructor) {
291 NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly());
294 #endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
296 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
297 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
298 // class (as opposed to ::testing::Mock). We had to work around an
299 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
300 // NiceMock to be looked up in the wrong context, and this test
301 // ensures that our fix works.
303 // We have to skip this test on Symbian and Windows Mobile, as it
304 // causes the program to crash there, for reasons unclear to us yet.
305 TEST(NiceMockTest, AcceptsClassNamedMock) {
306 NiceMock< ::Mock> nice;
307 EXPECT_CALL(nice, DoThis());
310 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
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 #if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
398 TEST(NaggyMockTest, MoveOnlyConstructor) {
399 NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly());
402 #endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
404 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
405 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
406 // class (as opposed to ::testing::Mock). We had to work around an
407 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
408 // NaggyMock to be looked up in the wrong context, and this test
409 // ensures that our fix works.
411 // We have to skip this test on Symbian and Windows Mobile, as it
412 // causes the program to crash there, for reasons unclear to us yet.
413 TEST(NaggyMockTest, AcceptsClassNamedMock) {
414 NaggyMock< ::Mock> naggy;
415 EXPECT_CALL(naggy, DoThis());
418 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
420 // Tests that a strict mock allows expected calls.
421 TEST(StrictMockTest, AllowsExpectedCall) {
422 StrictMock<MockFoo> strict_foo;
424 EXPECT_CALL(strict_foo, DoThis());
428 // Tests that an unexpected call on a strict mock fails.
429 TEST(StrictMockTest, UnexpectedCallFails) {
430 StrictMock<MockFoo> strict_foo;
432 EXPECT_CALL(strict_foo, DoThis()).Times(0);
433 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
434 "called more times than expected");
437 // Tests that an uninteresting call on a strict mock fails.
438 TEST(StrictMockTest, UninterestingCallFails) {
439 StrictMock<MockFoo> strict_foo;
441 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
442 "Uninteresting mock function call");
445 // Tests that an uninteresting call on a strict mock fails, even if
446 // the call deletes the mock object.
447 TEST(StrictMockTest, UninterestingCallFailsAfterDeath) {
448 StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>;
450 ON_CALL(*strict_foo, DoThis())
451 .WillByDefault(Invoke(strict_foo, &MockFoo::Delete));
453 EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(),
454 "Uninteresting mock function call");
457 // Tests that StrictMock works with a mock class that has a
458 // non-default constructor.
459 TEST(StrictMockTest, NonDefaultConstructor) {
460 StrictMock<MockBar> strict_bar("hi");
461 EXPECT_EQ("hi", strict_bar.str());
463 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
464 "Uninteresting mock function call");
467 // Tests that StrictMock works with a mock class that has a 10-ary
468 // non-default constructor.
469 TEST(StrictMockTest, NonDefaultConstructor10) {
470 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f',
471 "g", "h", true, false);
472 EXPECT_EQ("abcdefghTF", strict_bar.str());
474 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true),
475 "Uninteresting mock function call");
478 TEST(StrictMockTest, AllowLeak) {
479 StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
480 Mock::AllowLeak(leaked);
481 EXPECT_CALL(*leaked, DoThis());
485 #if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
487 TEST(StrictMockTest, MoveOnlyConstructor) {
488 StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly());
491 #endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
493 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
494 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
495 // class (as opposed to ::testing::Mock). We had to work around an
496 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
497 // StrictMock to be looked up in the wrong context, and this test
498 // ensures that our fix works.
500 // We have to skip this test on Symbian and Windows Mobile, as it
501 // causes the program to crash there, for reasons unclear to us yet.
502 TEST(StrictMockTest, AcceptsClassNamedMock) {
503 StrictMock< ::Mock> strict;
504 EXPECT_CALL(strict, DoThis());
507 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
509 } // namespace gmock_nice_strict_test
510 } // namespace testing