30#include "gmock/gmock-nice-strict.h"
34#include "gmock/gmock.h"
35#include "gtest/gtest-spi.h"
36#include "gtest/gtest.h"
44 MOCK_METHOD0(DoThis,
void());
47 GTEST_DISALLOW_COPY_AND_ASSIGN_(
Mock);
51namespace gmock_nice_strict_test {
53using testing::GMOCK_FLAG(verbose);
54using testing::HasSubstr;
59#if GTEST_HAS_STREAM_REDIRECTION
60using testing::internal::CaptureStdout;
61using testing::internal::GetCapturedStdout;
73 MOCK_METHOD(
void, OnDestroy, ());
82 virtual void DoThis() = 0;
83 virtual int DoThat(
bool flag) = 0;
89 void Delete() {
delete this; }
91 MOCK_METHOD0(DoThis,
void());
92 MOCK_METHOD1(DoThat,
int(
bool flag));
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');
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);
139#if GTEST_HAS_STREAM_REDIRECTION
142TEST(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;
159TEST(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;
178TEST(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;
191TEST(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));
199TEST(NiceMockTest, NoWarningForUninterestingCall) {
200 NiceMock<MockFoo> nice_foo;
204 nice_foo.DoThat(
true);
205 EXPECT_EQ(
"", GetCapturedStdout());
210TEST(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());
223TEST(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;
239TEST(NiceMockTest, AllowsExpectedCall) {
240 NiceMock<MockFoo> nice_foo;
242 EXPECT_CALL(nice_foo, DoThis());
249TEST(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(); },
"");
264TEST(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");
273TEST(NiceMockTest, NonDefaultConstructor) {
274 NiceMock<MockBar> nice_bar(
"hi");
275 EXPECT_EQ(
"hi", nice_bar.str());
278 nice_bar.That(5,
true);
283TEST(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);
292TEST(NiceMockTest, AllowLeak) {
293 NiceMock<MockFoo>* leaked =
new NiceMock<MockFoo>;
294 Mock::AllowLeak(leaked);
295 EXPECT_CALL(*leaked, DoThis());
299TEST(NiceMockTest, MoveOnlyConstructor) {
300 NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
305TEST(NiceMockTest, AcceptsClassNamedMock) {
306 NiceMock< ::Mock> nice;
307 EXPECT_CALL(nice, DoThis());
311TEST(NiceMockTest, IsNiceInDestructor) {
313 NiceMock<CallsMockMethodInDestructor> nice_on_destroy;
318TEST(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
328TEST(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;
345TEST(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;
365TEST(NaggyMockTest, AllowsExpectedCall) {
366 NaggyMock<MockFoo> naggy_foo;
368 EXPECT_CALL(naggy_foo, DoThis());
373TEST(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");
383TEST(NaggyMockTest, NonDefaultConstructor) {
384 NaggyMock<MockBar> naggy_bar(
"hi");
385 EXPECT_EQ(
"hi", naggy_bar.str());
388 naggy_bar.That(5,
true);
393TEST(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);
402TEST(NaggyMockTest, AllowLeak) {
403 NaggyMock<MockFoo>* leaked =
new NaggyMock<MockFoo>;
404 Mock::AllowLeak(leaked);
405 EXPECT_CALL(*leaked, DoThis());
409TEST(NaggyMockTest, MoveOnlyConstructor) {
410 NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
415TEST(NaggyMockTest, AcceptsClassNamedMock) {
416 NaggyMock< ::Mock> naggy;
417 EXPECT_CALL(naggy, DoThis());
421TEST(NaggyMockTest, IsNaggyInDestructor) {
422 const std::string saved_flag = GMOCK_FLAG(verbose);
423 GMOCK_FLAG(verbose) =
"warning";
427 NaggyMock<CallsMockMethodInDestructor> naggy_on_destroy;
431 EXPECT_THAT(GetCapturedStdout(),
432 HasSubstr(
"Uninteresting mock function call"));
434 GMOCK_FLAG(verbose) = saved_flag;
437TEST(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));
445TEST(StrictMockTest, AllowsExpectedCall) {
446 StrictMock<MockFoo> strict_foo;
448 EXPECT_CALL(strict_foo, DoThis());
453TEST(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");
462TEST(StrictMockTest, UninterestingCallFails) {
463 StrictMock<MockFoo> strict_foo;
465 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
466 "Uninteresting mock function call");
471TEST(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");
483TEST(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");
493TEST(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");
502TEST(StrictMockTest, AllowLeak) {
503 StrictMock<MockFoo>* leaked =
new StrictMock<MockFoo>;
504 Mock::AllowLeak(leaked);
505 EXPECT_CALL(*leaked, DoThis());
509TEST(StrictMockTest, MoveOnlyConstructor) {
510 StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
515TEST(StrictMockTest, AcceptsClassNamedMock) {
516 StrictMock< ::Mock> strict;
517 EXPECT_CALL(strict, DoThis());
521TEST(StrictMockTest, IsStrictInDestructor) {
522 EXPECT_NONFATAL_FAILURE(
524 StrictMock<CallsMockMethodInDestructor> strict_on_destroy;
528 "Uninteresting mock function call");
531TEST(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));
Definition gmock-nice-strict_test.cc:40
Definition gmock-nice-strict.h:179
Definition gmock-nice-strict.h:138
Definition gmock-nice-strict.h:221
Definition gmock-nice-strict_test.cc:70
Definition gmock-nice-strict_test.cc:78
Definition gmock-nice-strict_test.cc:99
Definition gmock-nice-strict_test.cc:125
Definition gmock-nice-strict_test.cc:123
Definition gmock-nice-strict_test.cc:86
Definition gmock-nice-strict_test.cc:65