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 // Author: wan@google.com (Zhanyong Wan)
32 #include "gmock/gmock-generated-nice-strict.h"
36 #include "gmock/gmock.h"
37 #include "gtest/gtest-spi.h"
38 #include "gtest/gtest.h"
40 // This must not be defined inside the ::testing namespace, or it will
41 // clash with ::testing::Mock.
46 MOCK_METHOD0(DoThis
, void());
49 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock
);
53 namespace gmock_nice_strict_test
{
55 using testing::GMOCK_FLAG(verbose
);
56 using testing::HasSubstr
;
57 using testing::NaggyMock
;
58 using testing::NiceMock
;
59 using testing::StrictMock
;
61 #if GTEST_HAS_STREAM_REDIRECTION
62 using testing::internal::CaptureStdout
;
63 using testing::internal::GetCapturedStdout
;
66 // Class without default constructor.
67 class NotDefaultConstructible
{
69 explicit NotDefaultConstructible(int) {}
72 // Defines some mock classes needed by the tests.
78 virtual void DoThis() = 0;
79 virtual int DoThat(bool flag
) = 0;
82 class MockFoo
: public Foo
{
85 void Delete() { delete this; }
87 MOCK_METHOD0(DoThis
, void());
88 MOCK_METHOD1(DoThat
, int(bool flag
));
89 MOCK_METHOD0(ReturnNonDefaultConstructible
, NotDefaultConstructible());
92 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo
);
97 explicit MockBar(const std::string
& s
) : str_(s
) {}
99 MockBar(char a1
, char a2
, std::string a3
, std::string a4
, int a5
, int a6
,
100 const std::string
& a7
, const std::string
& a8
, bool a9
, bool a10
) {
101 str_
= std::string() + a1
+ a2
+ a3
+ a4
+ static_cast<char>(a5
) +
102 static_cast<char>(a6
) + a7
+ a8
+ (a9
? 'T' : 'F') + (a10
? 'T' : 'F');
105 virtual ~MockBar() {}
107 const std::string
& str() const { return str_
; }
109 MOCK_METHOD0(This
, int());
110 MOCK_METHOD2(That
, std::string(int, bool));
115 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar
);
118 #if GTEST_GTEST_LANG_CXX11
123 MoveOnly() = default;
125 MoveOnly(const MoveOnly
&) = delete;
126 operator=(const MoveOnly
&) = delete;
128 MoveOnly(MoveOnly
&&) = default;
129 operator=(MoveOnly
&&) = default;
134 #endif // GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
136 #if GTEST_HAS_STREAM_REDIRECTION
138 // Tests that a raw mock generates warnings for uninteresting calls.
139 TEST(RawMockTest
, WarningForUninterestingCall
) {
140 const std::string saved_flag
= GMOCK_FLAG(verbose
);
141 GMOCK_FLAG(verbose
) = "warning";
147 raw_foo
.DoThat(true);
148 EXPECT_THAT(GetCapturedStdout(),
149 HasSubstr("Uninteresting mock function call"));
151 GMOCK_FLAG(verbose
) = saved_flag
;
154 // Tests that a raw mock generates warnings for uninteresting calls
155 // that delete the mock object.
156 TEST(RawMockTest
, WarningForUninterestingCallAfterDeath
) {
157 const std::string saved_flag
= GMOCK_FLAG(verbose
);
158 GMOCK_FLAG(verbose
) = "warning";
160 MockFoo
* const raw_foo
= new MockFoo
;
162 ON_CALL(*raw_foo
, DoThis())
163 .WillByDefault(Invoke(raw_foo
, &MockFoo::Delete
));
167 EXPECT_THAT(GetCapturedStdout(),
168 HasSubstr("Uninteresting mock function call"));
170 GMOCK_FLAG(verbose
) = saved_flag
;
173 // Tests that a raw mock generates informational logs for
174 // uninteresting calls.
175 TEST(RawMockTest
, InfoForUninterestingCall
) {
178 const std::string saved_flag
= GMOCK_FLAG(verbose
);
179 GMOCK_FLAG(verbose
) = "info";
182 EXPECT_THAT(GetCapturedStdout(),
183 HasSubstr("Uninteresting mock function call"));
185 GMOCK_FLAG(verbose
) = saved_flag
;
188 // Tests that a nice mock generates no warning for uninteresting calls.
189 TEST(NiceMockTest
, NoWarningForUninterestingCall
) {
190 NiceMock
<MockFoo
> nice_foo
;
194 nice_foo
.DoThat(true);
195 EXPECT_EQ("", GetCapturedStdout());
198 // Tests that a nice mock generates no warning for uninteresting calls
199 // that delete the mock object.
200 TEST(NiceMockTest
, NoWarningForUninterestingCallAfterDeath
) {
201 NiceMock
<MockFoo
>* const nice_foo
= new NiceMock
<MockFoo
>;
203 ON_CALL(*nice_foo
, DoThis())
204 .WillByDefault(Invoke(nice_foo
, &MockFoo::Delete
));
208 EXPECT_EQ("", GetCapturedStdout());
211 // Tests that a nice mock generates informational logs for
212 // uninteresting calls.
213 TEST(NiceMockTest
, InfoForUninterestingCall
) {
214 NiceMock
<MockFoo
> nice_foo
;
216 const std::string saved_flag
= GMOCK_FLAG(verbose
);
217 GMOCK_FLAG(verbose
) = "info";
220 EXPECT_THAT(GetCapturedStdout(),
221 HasSubstr("Uninteresting mock function call"));
223 GMOCK_FLAG(verbose
) = saved_flag
;
226 #endif // GTEST_HAS_STREAM_REDIRECTION
228 // Tests that a nice mock allows expected calls.
229 TEST(NiceMockTest
, AllowsExpectedCall
) {
230 NiceMock
<MockFoo
> nice_foo
;
232 EXPECT_CALL(nice_foo
, DoThis());
236 // Tests that an unexpected call on a nice mock which returns a
237 // not-default-constructible type throws an exception and the exception contains
238 // the method's name.
239 TEST(NiceMockTest
, ThrowsExceptionForUnknownReturnTypes
) {
240 NiceMock
<MockFoo
> nice_foo
;
241 #if GTEST_HAS_EXCEPTIONS
243 nice_foo
.ReturnNonDefaultConstructible();
245 } catch (const std::runtime_error
& ex
) {
246 EXPECT_THAT(ex
.what(), HasSubstr("ReturnNonDefaultConstructible"));
249 EXPECT_DEATH_IF_SUPPORTED({ nice_foo
.ReturnNonDefaultConstructible(); }, "");
253 // Tests that an unexpected call on a nice mock fails.
254 TEST(NiceMockTest
, UnexpectedCallFails
) {
255 NiceMock
<MockFoo
> nice_foo
;
257 EXPECT_CALL(nice_foo
, DoThis()).Times(0);
258 EXPECT_NONFATAL_FAILURE(nice_foo
.DoThis(), "called more times than expected");
261 // Tests that NiceMock works with a mock class that has a non-default
263 TEST(NiceMockTest
, NonDefaultConstructor
) {
264 NiceMock
<MockBar
> nice_bar("hi");
265 EXPECT_EQ("hi", nice_bar
.str());
268 nice_bar
.That(5, true);
271 // Tests that NiceMock works with a mock class that has a 10-ary
272 // non-default constructor.
273 TEST(NiceMockTest
, NonDefaultConstructor10
) {
274 NiceMock
<MockBar
> nice_bar('a', 'b', "c", "d", 'e', 'f',
275 "g", "h", true, false);
276 EXPECT_EQ("abcdefghTF", nice_bar
.str());
279 nice_bar
.That(5, true);
282 TEST(NiceMockTest
, AllowLeak
) {
283 NiceMock
<MockFoo
>* leaked
= new NiceMock
<MockFoo
>;
284 Mock::AllowLeak(leaked
);
285 EXPECT_CALL(*leaked
, DoThis());
289 #if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
291 TEST(NiceMockTest
, MoveOnlyConstructor
) {
292 NiceMock
<MockBaz
> nice_baz(MockBaz::MoveOnly());
295 #endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
297 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
298 // Tests that NiceMock<Mock> compiles where Mock is a user-defined
299 // class (as opposed to ::testing::Mock). We had to work around an
300 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
301 // NiceMock to be looked up in the wrong context, and this test
302 // ensures that our fix works.
304 // We have to skip this test on Symbian and Windows Mobile, as it
305 // causes the program to crash there, for reasons unclear to us yet.
306 TEST(NiceMockTest
, AcceptsClassNamedMock
) {
307 NiceMock
< ::Mock
> nice
;
308 EXPECT_CALL(nice
, DoThis());
311 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
313 #if GTEST_HAS_STREAM_REDIRECTION
315 // Tests that a naggy mock generates warnings for uninteresting calls.
316 TEST(NaggyMockTest
, WarningForUninterestingCall
) {
317 const std::string saved_flag
= GMOCK_FLAG(verbose
);
318 GMOCK_FLAG(verbose
) = "warning";
320 NaggyMock
<MockFoo
> naggy_foo
;
324 naggy_foo
.DoThat(true);
325 EXPECT_THAT(GetCapturedStdout(),
326 HasSubstr("Uninteresting mock function call"));
328 GMOCK_FLAG(verbose
) = saved_flag
;
331 // Tests that a naggy mock generates a warning for an uninteresting call
332 // that deletes the mock object.
333 TEST(NaggyMockTest
, WarningForUninterestingCallAfterDeath
) {
334 const std::string saved_flag
= GMOCK_FLAG(verbose
);
335 GMOCK_FLAG(verbose
) = "warning";
337 NaggyMock
<MockFoo
>* const naggy_foo
= new NaggyMock
<MockFoo
>;
339 ON_CALL(*naggy_foo
, DoThis())
340 .WillByDefault(Invoke(naggy_foo
, &MockFoo::Delete
));
344 EXPECT_THAT(GetCapturedStdout(),
345 HasSubstr("Uninteresting mock function call"));
347 GMOCK_FLAG(verbose
) = saved_flag
;
350 #endif // GTEST_HAS_STREAM_REDIRECTION
352 // Tests that a naggy mock allows expected calls.
353 TEST(NaggyMockTest
, AllowsExpectedCall
) {
354 NaggyMock
<MockFoo
> naggy_foo
;
356 EXPECT_CALL(naggy_foo
, DoThis());
360 // Tests that an unexpected call on a naggy mock fails.
361 TEST(NaggyMockTest
, UnexpectedCallFails
) {
362 NaggyMock
<MockFoo
> naggy_foo
;
364 EXPECT_CALL(naggy_foo
, DoThis()).Times(0);
365 EXPECT_NONFATAL_FAILURE(naggy_foo
.DoThis(),
366 "called more times than expected");
369 // Tests that NaggyMock works with a mock class that has a non-default
371 TEST(NaggyMockTest
, NonDefaultConstructor
) {
372 NaggyMock
<MockBar
> naggy_bar("hi");
373 EXPECT_EQ("hi", naggy_bar
.str());
376 naggy_bar
.That(5, true);
379 // Tests that NaggyMock works with a mock class that has a 10-ary
380 // non-default constructor.
381 TEST(NaggyMockTest
, NonDefaultConstructor10
) {
382 NaggyMock
<MockBar
> naggy_bar('0', '1', "2", "3", '4', '5',
383 "6", "7", true, false);
384 EXPECT_EQ("01234567TF", naggy_bar
.str());
387 naggy_bar
.That(5, true);
390 TEST(NaggyMockTest
, AllowLeak
) {
391 NaggyMock
<MockFoo
>* leaked
= new NaggyMock
<MockFoo
>;
392 Mock::AllowLeak(leaked
);
393 EXPECT_CALL(*leaked
, DoThis());
397 #if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
399 TEST(NaggyMockTest
, MoveOnlyConstructor
) {
400 NaggyMock
<MockBaz
> naggy_baz(MockBaz::MoveOnly());
403 #endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
405 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
406 // Tests that NaggyMock<Mock> compiles where Mock is a user-defined
407 // class (as opposed to ::testing::Mock). We had to work around an
408 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
409 // NaggyMock to be looked up in the wrong context, and this test
410 // ensures that our fix works.
412 // We have to skip this test on Symbian and Windows Mobile, as it
413 // causes the program to crash there, for reasons unclear to us yet.
414 TEST(NaggyMockTest
, AcceptsClassNamedMock
) {
415 NaggyMock
< ::Mock
> naggy
;
416 EXPECT_CALL(naggy
, DoThis());
419 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
421 // Tests that a strict mock allows expected calls.
422 TEST(StrictMockTest
, AllowsExpectedCall
) {
423 StrictMock
<MockFoo
> strict_foo
;
425 EXPECT_CALL(strict_foo
, DoThis());
429 // Tests that an unexpected call on a strict mock fails.
430 TEST(StrictMockTest
, UnexpectedCallFails
) {
431 StrictMock
<MockFoo
> strict_foo
;
433 EXPECT_CALL(strict_foo
, DoThis()).Times(0);
434 EXPECT_NONFATAL_FAILURE(strict_foo
.DoThis(),
435 "called more times than expected");
438 // Tests that an uninteresting call on a strict mock fails.
439 TEST(StrictMockTest
, UninterestingCallFails
) {
440 StrictMock
<MockFoo
> strict_foo
;
442 EXPECT_NONFATAL_FAILURE(strict_foo
.DoThis(),
443 "Uninteresting mock function call");
446 // Tests that an uninteresting call on a strict mock fails, even if
447 // the call deletes the mock object.
448 TEST(StrictMockTest
, UninterestingCallFailsAfterDeath
) {
449 StrictMock
<MockFoo
>* const strict_foo
= new StrictMock
<MockFoo
>;
451 ON_CALL(*strict_foo
, DoThis())
452 .WillByDefault(Invoke(strict_foo
, &MockFoo::Delete
));
454 EXPECT_NONFATAL_FAILURE(strict_foo
->DoThis(),
455 "Uninteresting mock function call");
458 // Tests that StrictMock works with a mock class that has a
459 // non-default constructor.
460 TEST(StrictMockTest
, NonDefaultConstructor
) {
461 StrictMock
<MockBar
> strict_bar("hi");
462 EXPECT_EQ("hi", strict_bar
.str());
464 EXPECT_NONFATAL_FAILURE(strict_bar
.That(5, true),
465 "Uninteresting mock function call");
468 // Tests that StrictMock works with a mock class that has a 10-ary
469 // non-default constructor.
470 TEST(StrictMockTest
, NonDefaultConstructor10
) {
471 StrictMock
<MockBar
> strict_bar('a', 'b', "c", "d", 'e', 'f',
472 "g", "h", true, false);
473 EXPECT_EQ("abcdefghTF", strict_bar
.str());
475 EXPECT_NONFATAL_FAILURE(strict_bar
.That(5, true),
476 "Uninteresting mock function call");
479 TEST(StrictMockTest
, AllowLeak
) {
480 StrictMock
<MockFoo
>* leaked
= new StrictMock
<MockFoo
>;
481 Mock::AllowLeak(leaked
);
482 EXPECT_CALL(*leaked
, DoThis());
486 #if GTEST_GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
488 TEST(StrictMockTest
, MoveOnlyConstructor
) {
489 StrictMock
<MockBaz
> strict_baz(MockBaz::MoveOnly());
492 #endif // GTEST_LANG_CXX11 && GTEST_HAS_STD_MOVE_
494 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
495 // Tests that StrictMock<Mock> compiles where Mock is a user-defined
496 // class (as opposed to ::testing::Mock). We had to work around an
497 // MSVC 8.0 bug that caused the symbol Mock used in the definition of
498 // StrictMock to be looked up in the wrong context, and this test
499 // ensures that our fix works.
501 // We have to skip this test on Symbian and Windows Mobile, as it
502 // causes the program to crash there, for reasons unclear to us yet.
503 TEST(StrictMockTest
, AcceptsClassNamedMock
) {
504 StrictMock
< ::Mock
> strict
;
505 EXPECT_CALL(strict
, DoThis());
508 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
510 } // namespace gmock_nice_strict_test
511 } // namespace testing