30#include "gmock/gmock-nice-strict.h"
35#include "gmock/gmock.h"
36#include "gtest/gtest-spi.h"
37#include "gtest/gtest.h"
45 MOCK_METHOD0(DoThis,
void());
49 Mock& operator=(
const Mock&) =
delete;
53namespace gmock_nice_strict_test {
55using testing::HasSubstr;
60#if GTEST_HAS_STREAM_REDIRECTION
61using testing::internal::CaptureStdout;
62using testing::internal::GetCapturedStdout;
74 MOCK_METHOD(
void, OnDestroy, ());
83 virtual void DoThis() = 0;
84 virtual int DoThat(
bool flag) = 0;
90 void Delete() {
delete this; }
92 MOCK_METHOD0(DoThis,
void());
93 MOCK_METHOD1(DoThat,
int(
bool flag));
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') +
114 const std::string& str()
const {
return str_; }
116 MOCK_METHOD0(This,
int());
117 MOCK_METHOD2(That, std::string(
int,
bool));
142#if GTEST_HAS_STREAM_REDIRECTION
145TEST(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);
162TEST(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);
180TEST(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);
193TEST(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));
201TEST(NiceMockTest, NoWarningForUninterestingCall) {
202 NiceMock<MockFoo> nice_foo;
206 nice_foo.DoThat(
true);
207 EXPECT_EQ(
"", GetCapturedStdout());
212TEST(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());
225TEST(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);
241TEST(NiceMockTest, AllowsExpectedCall) {
242 NiceMock<MockFoo> nice_foo;
244 EXPECT_CALL(nice_foo, DoThis());
251TEST(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(); },
"");
266TEST(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");
275TEST(NiceMockTest, NonDefaultConstructor) {
276 NiceMock<MockBar> nice_bar(
"hi");
277 EXPECT_EQ(
"hi", nice_bar.str());
280 nice_bar.That(5,
true);
285TEST(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);
294TEST(NiceMockTest, AllowLeak) {
295 NiceMock<MockFoo>* leaked =
new NiceMock<MockFoo>;
296 Mock::AllowLeak(leaked);
297 EXPECT_CALL(*leaked, DoThis());
301TEST(NiceMockTest, MoveOnlyConstructor) {
302 NiceMock<MockBaz> nice_baz(MockBaz::MoveOnly{});
307TEST(NiceMockTest, AcceptsClassNamedMock) {
308 NiceMock< ::Mock> nice;
309 EXPECT_CALL(nice, DoThis());
313TEST(NiceMockTest, IsNiceInDestructor) {
315 NiceMock<CallsMockMethodInDestructor> nice_on_destroy;
320TEST(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
330TEST(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);
347TEST(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);
367TEST(NaggyMockTest, AllowsExpectedCall) {
368 NaggyMock<MockFoo> naggy_foo;
370 EXPECT_CALL(naggy_foo, DoThis());
375TEST(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");
385TEST(NaggyMockTest, NonDefaultConstructor) {
386 NaggyMock<MockBar> naggy_bar(
"hi");
387 EXPECT_EQ(
"hi", naggy_bar.str());
390 naggy_bar.That(5,
true);
395TEST(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);
404TEST(NaggyMockTest, AllowLeak) {
405 NaggyMock<MockFoo>* leaked =
new NaggyMock<MockFoo>;
406 Mock::AllowLeak(leaked);
407 EXPECT_CALL(*leaked, DoThis());
411TEST(NaggyMockTest, MoveOnlyConstructor) {
412 NaggyMock<MockBaz> naggy_baz(MockBaz::MoveOnly{});
417TEST(NaggyMockTest, AcceptsClassNamedMock) {
418 NaggyMock< ::Mock> naggy;
419 EXPECT_CALL(naggy, DoThis());
423TEST(NaggyMockTest, IsNaggyInDestructor) {
424 const std::string saved_flag = GMOCK_FLAG_GET(verbose);
425 GMOCK_FLAG_SET(verbose,
"warning");
429 NaggyMock<CallsMockMethodInDestructor> naggy_on_destroy;
433 EXPECT_THAT(GetCapturedStdout(),
434 HasSubstr(
"Uninteresting mock function call"));
436 GMOCK_FLAG_SET(verbose, saved_flag);
439TEST(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));
447TEST(StrictMockTest, AllowsExpectedCall) {
448 StrictMock<MockFoo> strict_foo;
450 EXPECT_CALL(strict_foo, DoThis());
455TEST(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");
464TEST(StrictMockTest, UninterestingCallFails) {
465 StrictMock<MockFoo> strict_foo;
467 EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(),
468 "Uninteresting mock function call");
473TEST(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");
485TEST(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");
495TEST(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");
504TEST(StrictMockTest, AllowLeak) {
505 StrictMock<MockFoo>* leaked =
new StrictMock<MockFoo>;
506 Mock::AllowLeak(leaked);
507 EXPECT_CALL(*leaked, DoThis());
511TEST(StrictMockTest, MoveOnlyConstructor) {
512 StrictMock<MockBaz> strict_baz(MockBaz::MoveOnly{});
517TEST(StrictMockTest, AcceptsClassNamedMock) {
518 StrictMock< ::Mock> strict;
519 EXPECT_CALL(strict, DoThis());
523TEST(StrictMockTest, IsStrictInDestructor) {
524 EXPECT_NONFATAL_FAILURE(
526 StrictMock<CallsMockMethodInDestructor> strict_on_destroy;
530 "Uninteresting mock function call");
533TEST(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));
Definition gmock-nice-strict_test.cc:41
Definition gmock-nice-strict.h:192
Definition gmock-nice-strict.h:151
Definition gmock-nice-strict.h:234
Definition gmock-nice-strict_test.cc:71
Definition gmock-nice-strict_test.cc:79
Definition gmock-nice-strict_test.cc:101
Definition gmock-nice-strict_test.cc:128
Definition gmock-nice-strict_test.cc:126
Definition gmock-nice-strict_test.cc:87
Definition gmock-nice-strict_test.cc:66