79using testing::GMOCK_FLAG(
verbose);
92using testing::SaveArg;
105#if GTEST_HAS_STREAM_REDIRECTION
113class MockIncomplete {
121void PrintTo(
const Incomplete& x, ::std::ostream*
os);
123TEST(MockMethodTest, CanInstantiateWithIncompleteArgType) {
128 MockIncomplete incomplete;
135void PrintTo(
const Incomplete& , ::std::ostream*
os) {
142class NonDefaultConstructible {
144 explicit NonDefaultConstructible(
int ) {}
153 MOCK_METHOD0(ReturnNonDefaultConstructible, NonDefaultConstructible());
172class ReferenceHoldingMock {
174 ReferenceHoldingMock() {}
176 MOCK_METHOD1(AcceptReference,
void(linked_ptr<MockA>*));
187#define Method MethodW
194class MockCC :
public CC {
205TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
212TEST(OnCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
219TEST(ExpectCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
226TEST(ExpectCallSyntaxTest, WorksWithMethodNameExpandedFromMacro) {
236TEST(OnCallSyntaxTest, EvaluatesFirstArgumentOnce) {
244TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) {
254TEST(OnCallSyntaxTest, WithIsOptional) {
258 .WillByDefault(Return());
261 .WillByDefault(Return());
264TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) {
271 .WillByDefault(Return(Result()));
272 },
".With() cannot appear more than once in an ON_CALL()");
275TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) {
284TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) {
289 .WillByDefault(Return())
290 .WillByDefault(Return());
291 },
".WillByDefault() must appear exactly once in an ON_CALL()");
296TEST(ExpectCallSyntaxTest, EvaluatesFirstArgumentOnce) {
305TEST(ExpectCallSyntaxTest, EvaluatesSecondArgumentOnce) {
316TEST(ExpectCallSyntaxTest, WithIsOptional) {
326TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) {
333 },
".With() cannot appear more than once in an EXPECT_CALL()");
338TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) {
345 },
".With() must be the first clause in an EXPECT_CALL()");
353 },
".With() must be the first clause in an EXPECT_CALL()");
358TEST(ExpectCallSyntaxTest, TimesCanBeInferred) {
366 .WillRepeatedly(Return());
373TEST(ExpectCallSyntaxTest, TimesCanAppearAtMostOnce) {
380 },
".Times() cannot appear more than once in an EXPECT_CALL()");
386TEST(ExpectCallSyntaxTest, TimesMustBeBeforeInSequence) {
394 },
".Times() cannot appear after ");
399TEST(ExpectCallSyntaxTest, InSequenceIsOptional) {
411TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) {
422TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeAfter) {
432 },
".InSequence() cannot appear after ");
437TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWillOnce) {
445 },
".InSequence() cannot appear after ");
450TEST(ExpectCallSyntaxTest, AfterMustBeBeforeWillOnce) {
458 },
".After() cannot appear after ");
464TEST(ExpectCallSyntaxTest, WillIsOptional) {
475TEST(ExpectCallSyntaxTest, WillCanAppearMultipleTimes) {
485TEST(ExpectCallSyntaxTest, WillMustBeBeforeWillRepeatedly) {
490 .WillRepeatedly(Return())
492 },
".WillOnce() cannot appear after ");
497TEST(ExpectCallSyntaxTest, WillRepeatedlyIsOptional) {
504 .WillRepeatedly(Return());
511TEST(ExpectCallSyntaxTest, WillRepeatedlyCannotAppearMultipleTimes) {
516 .WillRepeatedly(Return())
517 .WillRepeatedly(Return());
518 },
".WillRepeatedly() cannot appear more than once in an "
522TEST(ExpectCallSyntaxTest, WillRepeatedlyMustBeBeforeRetiresOnSaturation) {
527 .RetiresOnSaturation()
528 .WillRepeatedly(Return());
529 },
".WillRepeatedly() cannot appear after ");
532TEST(ExpectCallSyntaxTest, RetiresOnSaturationIsOptional) {
537 .RetiresOnSaturation();
543TEST(ExpectCallSyntaxTest, RetiresOnSaturationCannotAppearMultipleTimes) {
548 .RetiresOnSaturation()
549 .RetiresOnSaturation();
550 },
".RetiresOnSaturation() cannot appear more than once");
555TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) {
564 },
"to be called once");
570 },
"to be called once");
573#if GTEST_HAS_STREAM_REDIRECTION
577TEST(ExpectCallSyntaxTest, DoesNotWarnOnAdequateActionCount) {
589 .WillRepeatedly(Return(1));
593 .Times(Between(1, 2))
595 .WillOnce(Return(2));
602 .WillRepeatedly(Return(2));
613TEST(ExpectCallSyntaxTest, WarnsOnTooManyActions) {
621 .WillOnce(Return(1));
625 .WillOnce(Return(2));
630 .RetiresOnSaturation();
636 .WillRepeatedly(Return(1));
640 .WillRepeatedly(Return(2));
649 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
650 "Expected to be never called, but has 1 WillOnce().",
654 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
655 "Expected to be called at most once, "
656 "but has 2 WillOnce()s.",
660 "Too many actions specified in EXPECT_CALL(b, DoB(1))...\n"
661 "Expected to be called once, but has 2 WillOnce()s.",
665 "Too many actions specified in EXPECT_CALL(b, DoB())...\n"
666 "Expected to be never called, but has 0 WillOnce()s "
667 "and a WillRepeatedly().",
671 "Too many actions specified in EXPECT_CALL(b, DoB(2))...\n"
672 "Expected to be called once, but has 1 WillOnce() "
673 "and a WillRepeatedly().",
679TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) {
683 .Times(Between(2, 3))
684 .WillOnce(Return(1));
691 "Too few actions specified in EXPECT_CALL(b, DoB())...\n"
692 "Expected to be called between 2 and 3 times, "
693 "but has only 1 WillOnce().",
698TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) {
699 int original_behavior = testing::GMOCK_FLAG(default_mock_behavior);
701 testing::GMOCK_FLAG(default_mock_behavior) = kAllow;
710 testing::GMOCK_FLAG(default_mock_behavior) = kWarn;
721 testing::GMOCK_FLAG(default_mock_behavior) = kFail;
725 },
"Uninteresting mock function call");
728 testing::GMOCK_FLAG(default_mock_behavior) = -1;
738 testing::GMOCK_FLAG(default_mock_behavior) = 3;
749 testing::GMOCK_FLAG(default_mock_behavior) = original_behavior;
758TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCall) {
767TEST(OnCallTest, TakesBuiltInDefaultActionWhenNoOnCallMatches) {
770 .WillByDefault(Return(1));
777TEST(OnCallTest, PicksLastMatchingOnCall) {
780 .WillByDefault(Return(3));
782 .WillByDefault(Return(2));
784 .WillByDefault(Return(1));
793TEST(ExpectCallTest, AllowsAnyCallWhenNoSpec) {
806TEST(ExpectCallTest, PicksLastMatchingExpectCall) {
809 .WillRepeatedly(Return(2));
811 .WillRepeatedly(Return(1));
817TEST(ExpectCallTest, CatchesTooFewCalls) {
824 },
"Actual function call count doesn't match EXPECT_CALL(b, DoB(5))...\n"
825 " Expected: to be called at least twice\n"
826 " Actual: called once - unsatisfied and active");
831TEST(ExpectCallTest, InfersCardinalityWhenThereIsNoWillRepeatedly) {
836 .WillOnce(Return(2));
846 .WillOnce(Return(2));
849 },
"to be called twice");
855 .WillOnce(Return(2));
863TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) {
868 .WillRepeatedly(Return(2));
877 .WillRepeatedly(Return(2));
888 .WillRepeatedly(Return(2));
889 },
"to be called at least once");
894TEST(ExpectCallTest, NthMatchTakesNthAction) {
899 .WillOnce(Return(3));
908TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) {
912 .WillRepeatedly(Return(2));
919#if GTEST_HAS_STREAM_REDIRECTION
923TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) {
930 .WillOnce(Return(2));
945 HasSubstr(
"Actions ran out in EXPECT_CALL(b, DoB())...\n"
946 "Called 3 times, but only 2 WillOnce()s are specified"
947 " - returning default value."));
949 HasSubstr(
"Actions ran out in EXPECT_CALL(b, DoB())...\n"
950 "Called 4 times, but only 2 WillOnce()s are specified"
951 " - returning default value."));
954TEST(FunctionMockerMessageTest, ReportsExpectCallLocationForExhausedActions) {
956 std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
957 EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1));
968TEST(FunctionMockerMessageTest,
969 ReportsDefaultActionLocationOfUninterestingCallsForNaggyMock) {
970 std::string on_call_location;
974 on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
975 ON_CALL(b, DoB(_)).WillByDefault(Return(0));
984TEST(UninterestingCallTest, DoesDefaultAction) {
989 .WillByDefault(Return(
true));
999TEST(UnexpectedCallTest, DoesDefaultAction) {
1004 .WillByDefault(Return(
true));
1007 bool result =
false;
1009 "Unexpected mock function call");
1019 "Unexpected mock function call");
1025TEST(UnexpectedCallTest, GeneratesFailureForVoidFunction) {
1035 "Unexpected mock function call - returning directly.\n"
1036 " Function call: DoA(9)\n"
1037 "Google Mock tried the following 1 expectation, but it didn't match:");
1040 " Expected arg #0: is equal to 1\n"
1042 " Expected: to be called once\n"
1043 " Actual: called once - saturated and active");
1052 "Unexpected mock function call - returning directly.\n"
1053 " Function call: DoA(2)\n"
1054 "Google Mock tried the following 2 expectations, but none matched:");
1057 "tried expectation #0: EXPECT_CALL(a2, DoA(1))...\n"
1058 " Expected arg #0: is equal to 1\n"
1060 " Expected: to be called once\n"
1061 " Actual: called once - saturated and active");
1064 "tried expectation #1: EXPECT_CALL(a2, DoA(3))...\n"
1065 " Expected arg #0: is equal to 3\n"
1067 " Expected: to be called once\n"
1068 " Actual: never called - unsatisfied and active");
1074TEST(UnexpectedCallTest, GeneartesFailureForNonVoidFunction) {
1080 "Unexpected mock function call - returning default value.\n"
1081 " Function call: DoB(2)\n"
1083 "Google Mock tried the following 1 expectation, but it didn't match:");
1086 " Expected arg #0: is equal to 1\n"
1088 " Expected: to be called once\n"
1089 " Actual: called once - saturated and active");
1094TEST(UnexpectedCallTest, RetiredExpectation) {
1097 .RetiresOnSaturation();
1102 " Expected: the expectation is active\n"
1103 " Actual: it is retired");
1108TEST(UnexpectedCallTest, UnmatchedArguments) {
1114 " Expected arg #0: is equal to 1\n"
1121TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) {
1132 .InSequence(s1, s2);
1153 "(?s)the following immediate pre-requisites are not satisfied:\n"
1154 ".*: pre-requisite #0\n"
1155 ".*: pre-requisite #1"));
1156#elif GTEST_USES_POSIX_RE
1160 "the following immediate pre-requisites are not satisfied:\n"
1161 "(.|\n)*: pre-requisite #0\n"
1162 "(.|\n)*: pre-requisite #1"));
1166 "the following immediate pre-requisites are not satisfied:"));
1167 EXPECT_THAT(
r.message(), ContainsRegex(
": pre-requisite #0"));
1168 EXPECT_THAT(
r.message(), ContainsRegex(
": pre-requisite #1"));
1176TEST(UndefinedReturnValueTest,
1177 ReturnValueIsMandatoryWhenNotDefaultConstructible) {
1182#if GTEST_HAS_EXCEPTIONS
1191TEST(ExcessiveCallTest, DoesDefaultAction) {
1196 .WillByDefault(Return(
true));
1199 bool result =
false;
1201 "Mock function called more times than expected");
1211 "Mock function called more times than expected");
1217TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) {
1223 "Mock function called more times than expected - returning directly.\n"
1224 " Function call: DoA(9)\n"
1225 " Expected: to be never called\n"
1226 " Actual: called once - over-saturated and active");
1231TEST(ExcessiveCallTest, GeneratesFailureForNonVoidFunction) {
1237 "Mock function called more times than expected - "
1238 "returning default value.\n"
1239 " Function call: DoB(2)\n"
1241 " Expected: to be called once\n"
1242 " Actual: called twice - over-saturated and active");
1247TEST(InSequenceTest, AllExpectationInScopeAreInSequence) {
1258 },
"Unexpected mock function call");
1264TEST(InSequenceTest, NestedInSequence) {
1281 },
"Unexpected mock function call");
1287TEST(InSequenceTest, ExpectationsOutOfScopeAreNotAffected) {
1299 },
"Unexpected mock function call");
1307TEST(SequenceTest, AnyOrderIsOkByDefault) {
1314 .Times(AnyNumber());
1326 .Times(AnyNumber());
1335TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo1) {
1338 .WillByDefault(Return(Result()));
1359TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo2) {
1362 .WillByDefault(Return(Result()));
1380 PartialOrderTest() {
1382 .WillByDefault(Return(Result()));
1406TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag1) {
1417TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag2) {
1427TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag3) {
1437TEST_F(PartialOrderTest, CallsMustConformToSpecifiedDag4) {
1447TEST(SequenceTest, Retirement) {
1455 .RetiresOnSaturation();
1466TEST(ExpectationTest, ConstrutorsWork) {
1479 Expectation e7 =
EXPECT_CALL(
a, DoA(7)).WillOnce(Return());
1480 Expectation e8 =
EXPECT_CALL(
a, DoA(8)).WillRepeatedly(Return());
1481 Expectation e9 =
EXPECT_CALL(
a, DoA(9)).RetiresOnSaturation();
1483 Expectation e10 = e2;
1498TEST(ExpectationTest, AssignmentWorks) {
1513TEST(ExpectationSetTest, MemberTypesAreCorrect) {
1517TEST(ExpectationSetTest, ConstructorsWork) {
1521 const Expectation e2;
1524 ExpectationSet es3 = e1;
1525 ExpectationSet es4(e1);
1526 ExpectationSet es5 = e2;
1527 ExpectationSet es6(e2);
1528 ExpectationSet es7 = es2;
1546TEST(ExpectationSetTest, AssignmentWorks) {
1548 ExpectationSet es2 = Expectation();
1556TEST(ExpectationSetTest, InsertionWorks) {
1568 ExpectationSet::const_iterator it1 = es1.begin();
1569 ExpectationSet::const_iterator it2 = it1;
1576TEST(ExpectationSetTest, SizeWorks) {
1580 es += Expectation();
1590TEST(ExpectationSetTest, IsEnumerable) {
1594 es += Expectation();
1595 ExpectationSet::const_iterator it = es.begin();
1604TEST(AfterTest, SucceedsWhenPartialOrderIsSatisfied) {
1617TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) {
1635TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo1) {
1657TEST(AfterTest, CallsMustBeInStrictOrderWhenSpecifiedSo2) {
1681TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) {
1684 .WillByDefault(Return(Result()));
1703TEST(AfterTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo2) {
1724TEST(AfterTest, CanBeUsedWithInSequence) {
1743TEST(AfterTest, CanBeCalledManyTimes) {
1760TEST(AfterTest, AcceptsUpToFiveArguments) {
1768 .After(e1, e2, e3, es1, es2);
1779TEST(AfterTest, AcceptsDuplicatedInput) {
1782 .WillByDefault(Return(Result()));
1793 .After(e1, e2, es, e1);
1806TEST(AfterTest, ChangesToExpectationSetHaveNoEffectAfterwards) {
1823TEST(DeletingMockEarlyTest, Success1) {
1824 MockB*
const b1 =
new MockB;
1825 MockA*
const a =
new MockA;
1826 MockB*
const b2 =
new MockB;
1831 .WillOnce(Return(1));
1834 .WillRepeatedly(Return(
true));
1837 .WillRepeatedly(Return(2));
1851TEST(DeletingMockEarlyTest, Success2) {
1852 MockB*
const b1 =
new MockB;
1853 MockA*
const a =
new MockA;
1854 MockB*
const b2 =
new MockB;
1859 .WillOnce(Return(1));
1861 .Times(AnyNumber());
1864 .WillRepeatedly(Return(2));
1879# pragma warning(push)
1880# pragma warning(disable:4100)
1883ACTION_P(Delete, ptr) {
delete ptr; }
1886# pragma warning(pop)
1889TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) {
1890 MockA*
const a =
new MockA;
1895TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningValue) {
1896 MockA*
const a =
new MockA;
1898 .WillOnce(DoAll(Delete(
a), Return(Result())));
1899 a->ReturnResult(42);
1903TEST(DeletingMockEarlyTest, Failure1) {
1904 MockB*
const b1 =
new MockB;
1905 MockA*
const a =
new MockA;
1906 MockB*
const b2 =
new MockB;
1911 .WillOnce(Return(1));
1913 .Times(AnyNumber());
1916 .WillRepeatedly(Return(2));
1922 },
"Unexpected mock function call");
1929TEST(DeletingMockEarlyTest, Failure2) {
1930 MockB*
const b1 =
new MockB;
1931 MockA*
const a =
new MockA;
1932 MockB*
const b2 =
new MockB;
1938 .Times(AnyNumber());
1940 .Times(AnyNumber());
1944 "Actual: never called");
1946 "Unexpected mock function call");
1948 "Unexpected mock function call");
1953class EvenNumberCardinality :
public CardinalityInterface {
1956 virtual bool IsSatisfiedByCallCount(
int call_count)
const {
1957 return call_count % 2 == 0;
1961 virtual bool IsSaturatedByCallCount(
int )
const {
1966 virtual void DescribeTo(::std::ostream*
os)
const {
1967 *
os <<
"called even number of times";
1971Cardinality EvenNumber() {
1972 return Cardinality(
new EvenNumberCardinality);
1975TEST(ExpectationBaseTest,
1976 AllPrerequisitesAreSatisfiedWorksForNonMonotonicCardinality) {
1977 MockA*
a =
new MockA;
1981 .Times(EvenNumber())
1987 .Times(AnyNumber());
2001inline void operator<<(::std::ostream&
os,
const Printable&) {
2006 Unprintable() :
value(0) {}
2014 MOCK_METHOD6(VoidMethod,
void(
bool cond,
int n, std::string
s,
void*
p,
2015 const Printable& x, Unprintable y));
2024 VerboseFlagPreservingFixture()
2025 : saved_verbose_flag_(GMOCK_FLAG(verbose)) {}
2027 ~VerboseFlagPreservingFixture() {
GMOCK_FLAG(verbose) = saved_verbose_flag_; }
2030 const std::string saved_verbose_flag_;
2035#if GTEST_HAS_STREAM_REDIRECTION
2040TEST(FunctionCallMessageTest,
2041 UninterestingCallOnNaggyMockGeneratesNoStackTraceWhenVerboseWarning) {
2045 c.VoidMethod(
false, 5,
"Hi", NULL, Printable(), Unprintable());
2054TEST(FunctionCallMessageTest,
2055 UninterestingCallOnNaggyMockGeneratesFyiWithStackTraceWhenVerboseInfo) {
2059 c.VoidMethod(
false, 5,
"Hi", NULL, Printable(), Unprintable());
2085TEST(FunctionCallMessageTest,
2086 UninterestingCallOnNaggyMockPrintsArgumentsAndReturnValue) {
2094 "Uninteresting mock function call - returning default value.\n"
2095 " Function call: DoB()\n"
2096 " Returns: 0\n", output1.c_str());
2102 c.VoidMethod(
false, 5,
"Hi", NULL, Printable(), Unprintable());
2106 "Uninteresting mock function call - returning directly\\.\n"
2107 " Function call: VoidMethod"
2108 "\\(false, 5, \"Hi\", NULL, @.+ "
2109 "Printable, 4-byte object <00-00 00-00>\\)"));
2115class GMockVerboseFlagTest :
public VerboseFlagPreservingFixture {
2121 void VerifyOutput(
const std::string& output,
bool should_print,
2122 const std::string& expected_substring,
2123 const std::string& function_name) {
2125 EXPECT_THAT(output.c_str(), HasSubstr(expected_substring));
2129 EXPECT_THAT(output.c_str(), HasSubstr(function_name));
2132 static_cast<void>(function_name);
2140 void TestExpectedCall(
bool should_print) {
2144 .WillOnce(Return(
true));
2152 "Mock function call matches EXPECT_CALL(a, DoA(5))...\n"
2153 " Function call: DoA(5)\n"
2163 "Mock function call matches EXPECT_CALL(a, Binary(_, 1))...\n"
2164 " Function call: Binary(2, 1)\n"
2171 void TestUninterestingCallOnNaggyMock(
bool should_print) {
2173 const std::string note =
2174 "NOTE: You can safely ignore the above warning unless this "
2175 "call should not happen. Do not suppress it by blindly adding "
2176 "an EXPECT_CALL() if you don't mean to enforce the call. "
2178 "https://github.com/google/googletest/blob/master/googlemock/docs/"
2180 "knowing-when-to-expect for details.";
2188 "\nGMOCK WARNING:\n"
2189 "Uninteresting mock function call - returning directly.\n"
2190 " Function call: DoA(5)\n" +
2200 "\nGMOCK WARNING:\n"
2201 "Uninteresting mock function call - returning default value.\n"
2202 " Function call: Binary(2, 1)\n"
2203 " Returns: false\n" +
2211TEST_F(GMockVerboseFlagTest, Info) {
2213 TestExpectedCall(
true);
2214 TestUninterestingCallOnNaggyMock(
true);
2219TEST_F(GMockVerboseFlagTest, Warning) {
2221 TestExpectedCall(
false);
2222 TestUninterestingCallOnNaggyMock(
true);
2229 TestExpectedCall(
false);
2230 TestUninterestingCallOnNaggyMock(
false);
2235TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) {
2237 TestExpectedCall(
false);
2238 TestUninterestingCallOnNaggyMock(
true);
2248void PrintTo(PrintMeNot , ::std::ostream* ) {
2249 ADD_FAILURE() <<
"Google Mock is printing a value that shouldn't be "
2250 <<
"printed even to an internal buffer.";
2253class LogTestHelper {
2263class GMockLogTest :
public VerboseFlagPreservingFixture {
2265 LogTestHelper helper_;
2268TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) {
2271 .WillOnce(Return(PrintMeNot()));
2272 helper_.Foo(PrintMeNot());
2275TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) {
2278 .WillOnce(Return(PrintMeNot()));
2279 helper_.Foo(PrintMeNot());
2282TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) {
2285 .WillByDefault(Return(PrintMeNot()));
2286 helper_.Foo(PrintMeNot());
2291TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) {
2292 MockA*
a =
new MockA;
2296TEST(AllowLeakTest, CanBeCalledBeforeOnCall) {
2297 MockA*
a =
new MockA;
2299 ON_CALL(*
a, DoA(_)).WillByDefault(Return());
2303TEST(AllowLeakTest, CanBeCalledAfterOnCall) {
2304 MockA*
a =
new MockA;
2305 ON_CALL(*
a, DoA(_)).WillByDefault(Return());
2309TEST(AllowLeakTest, CanBeCalledBeforeExpectCall) {
2310 MockA*
a =
new MockA;
2316TEST(AllowLeakTest, CanBeCalledAfterExpectCall) {
2317 MockA*
a =
new MockA;
2322TEST(AllowLeakTest, WorksWhenBothOnCallAndExpectCallArePresent) {
2323 MockA*
a =
new MockA;
2324 ON_CALL(*
a, DoA(_)).WillByDefault(Return());
2331TEST(VerifyAndClearExpectationsTest, NoMethodHasExpectations) {
2333 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2344TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndSucceed) {
2347 .WillOnce(Return(1));
2349 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2360TEST(VerifyAndClearExpectationsTest, SomeMethodsHaveExpectationsAndFail) {
2363 .WillOnce(Return(1));
2366 "Actual: never called");
2377TEST(VerifyAndClearExpectationsTest, AllMethodsHaveExpectations) {
2380 .WillOnce(Return(1));
2382 .WillOnce(Return(2));
2385 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&b));
2395TEST(VerifyAndClearExpectationsTest, AMethodHasManyExpectations) {
2398 .WillOnce(Return(1));
2400 .WillOnce(Return(2));
2404 "Actual: never called");
2415TEST(VerifyAndClearExpectationsTest, CanCallManyTimes) {
2419 Mock::VerifyAndClearExpectations(&b);
2422 .WillOnce(Return(1));
2424 Mock::VerifyAndClearExpectations(&b);
2425 Mock::VerifyAndClearExpectations(&b);
2435TEST(VerifyAndClearTest, NoMethodHasDefaultActions) {
2438 Mock::VerifyAndClear(&b);
2444TEST(VerifyAndClearTest, SomeMethodsHaveDefaultActions) {
2447 .WillByDefault(Return(1));
2449 Mock::VerifyAndClear(&b);
2457TEST(VerifyAndClearTest, AllMethodsHaveDefaultActions) {
2460 .WillByDefault(Return(1));
2462 .WillByDefault(Return(2));
2464 Mock::VerifyAndClear(&b);
2475TEST(VerifyAndClearTest, AMethodHasManyDefaultActions) {
2478 .WillByDefault(Return(1));
2480 .WillByDefault(Return(2));
2482 Mock::VerifyAndClear(&b);
2492TEST(VerifyAndClearTest, CanCallManyTimes) {
2495 .WillByDefault(Return(1));
2496 Mock::VerifyAndClear(&b);
2497 Mock::VerifyAndClear(&b);
2500 .WillByDefault(Return(1));
2501 Mock::VerifyAndClear(&b);
2508TEST(VerifyAndClearTest, Success) {
2511 .WillByDefault(Return(1));
2513 .WillOnce(Return(2));
2526TEST(VerifyAndClearTest, Failure) {
2529 .WillByDefault(Return(1));
2531 .WillOnce(Return(2));
2536 "Actual: never called");
2547TEST(VerifyAndClearTest, Const) {
2550 .WillByDefault(Return(1));
2553 .WillOnce(DoDefault())
2554 .WillOnce(Return(2));
2568TEST(VerifyAndClearTest, CanSetDefaultActionsAndExpectationsAfterwards) {
2571 .WillByDefault(Return(1));
2573 .WillOnce(Return(2));
2576 Mock::VerifyAndClear(&b);
2579 .WillOnce(Return(3));
2581 .WillByDefault(Return(4));
2589TEST(VerifyAndClearTest, DoesNotAffectOtherMockObjects) {
2595 .WillByDefault(Return(
true));
2597 .WillOnce(DoDefault())
2598 .WillOnce(Return(
false));
2601 .WillByDefault(Return(1));
2603 .WillOnce(Return(2));
2606 .WillByDefault(Return(3));
2610 Mock::VerifyAndClear(&b2);
2621TEST(VerifyAndClearTest,
2622 DestroyingChainedMocksDoesNotDeadlockThroughExpectations) {
2623 linked_ptr<MockA>
a(
new MockA);
2624 ReferenceHoldingMock test_mock;
2628 .WillRepeatedly(SetArgPointee<0>(
a));
2641TEST(VerifyAndClearTest,
2642 DestroyingChainedMocksDoesNotDeadlockThroughDefaultAction) {
2643 linked_ptr<MockA>
a(
new MockA);
2644 ReferenceHoldingMock test_mock;
2647 ON_CALL(test_mock, AcceptReference(_))
2648 .WillByDefault(SetArgPointee<0>(
a));
2666TEST(SynchronizationTest, CanCallMockMethodInAction) {
2670 .WillByDefault(IgnoreResult(InvokeWithoutArgs(&c,
2671 &MockC::NonVoidMethod)));
2674 .WillOnce(Invoke(&
a, &MockA::DoA))
2675 .RetiresOnSaturation();
2685TEST(ParameterlessExpectationsTest, CanSetExpectationsWithoutMatchers) {
2688 ON_CALL(
a, DoA).WillByDefault(SaveArg<0>(&do_a_arg0));
2689 int do_a_47_arg0 = 0;
2690 ON_CALL(
a, DoA(47)).WillByDefault(SaveArg<0>(&do_a_47_arg0));
2705TEST(ParameterlessExpectationsTest, CanSetExpectationsForOverloadedMethods) {
2707 ON_CALL(b, DoB()).WillByDefault(Return(9));
2708 ON_CALL(b, DoB(5)).WillByDefault(Return(11));
2715struct MockWithConstMethods {
2721TEST(ParameterlessExpectationsTest, CanSetExpectationsForConstMethods) {
2722 MockWithConstMethods mock;
2724 ON_CALL(mock, Bar).WillByDefault(Return(33));
2730class MockConstOverload {
2736TEST(ParameterlessExpectationsTest,
2737 CanSetExpectationsForConstOverloadedMethods) {
2738 MockConstOverload mock;
2739 ON_CALL(mock, Overloaded(_)).WillByDefault(Return(7));
2740 ON_CALL(mock, Overloaded(5)).WillByDefault(Return(9));
2741 ON_CALL(Const(mock), Overloaded(5)).WillByDefault(Return(11));
2742 ON_CALL(Const(mock), Overloaded(7)).WillByDefault(Return(13));
2748 const MockConstOverload& const_mock = mock;
2759#if GMOCK_RENAME_MAIN
2760int gmock_main(
int argc,
char **
argv) {
2767 testing::GMOCK_FLAG(catch_leaked_mocks) =
true;
const TestPartResult & GetTestPartResult(int index) const
void SetCallCount(int n, ExpectationBase *exp)
DataStream & operator<<(DataStream &ds, const float64_t &v)
#define ACTION_P(name, p0)
#define MOCK_CONST_METHOD2(m,...)
#define MOCK_METHOD0(m,...)
#define MOCK_CONST_METHOD1(m,...)
#define MOCK_METHOD2(m,...)
#define MOCK_METHOD1(m,...)
#define MOCK_CONST_METHOD0(m,...)
#define MOCK_METHOD6(m,...)
#define EXPECT_THAT(value, matcher)
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
void PrintTo(EnumWithPrintTo e, std::ostream *os)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define TEST_F(test_fixture, test_name)
#define ASSERT_EQ(val1, val2)
#define EXPECT_EQ(val1, val2)
#define ASSERT_FALSE(condition)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define EXPECT_ANY_THROW(statement)
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(s1, s2)
#define TEST(test_case_name, test_name)
#define ASSERT_TRUE(condition)
#define EXPECT_FALSE(condition)
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2)
constexpr enabler dummy
An instance to use in EnableIf.
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
const char kErrorVerbosity[]
const char kInfoVerbosity[]
GTEST_API_ void CaptureStdout()
const char kWarningVerbosity[]
GTEST_API_ std::string GetCapturedStdout()
internal::Ne2Matcher Ne()
GTEST_API_ Cardinality AtLeast(int n)
PolymorphicMatcher< internal::HasSubstrMatcher< std::string > > HasSubstr(const std::string &substring)
GTEST_API_ AssertionResult IsNotSubstring(const char *needle_expr, const char *haystack_expr, const char *needle, const char *haystack)
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
GTEST_API_ AssertionResult IsSubstring(const char *needle_expr, const char *haystack_expr, const char *needle, const char *haystack)
internal::Lt2Matcher Lt()
GTEST_API_ Cardinality Between(int min, int max)
internal::Gt2Matcher Gt()
PolymorphicAction< internal::ReturnVoidAction > Return()
const internal::AnythingMatcher _
GTEST_API_ Cardinality AtMost(int n)
bool StaticAssertTypeEq()
PolymorphicMatcher< internal::MatchesRegexMatcher > ContainsRegex(const internal::RE *regex)
const T & Const(const T &x)
GTEST_API_ Cardinality AnyNumber()
internal::Eq2Matcher Eq()
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
internal::DoBothAction< Action1, Action2 > DoAll(Action1 a1, Action2 a2)
PolymorphicAction< internal::SetArgumentPointeeAction< N, T, internal::IsAProtocolMessage< T >::value > > SetArgPointee(const T &x)
internal::DoDefaultAction DoDefault()
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a