Allow declaring calls as uninteresting:

ON_CALL(mock, foo(_)).WillByDefault(...).Uninteresting();

This will supress 'Uninteresting call' warnings in raw and naggy mocks for the declared calls but will still give warnings for other calls.
This commit is contained in:
Ingo Prötel 2025-04-24 21:09:32 +02:00
parent cd430b47a5
commit 6489e631ea
2 changed files with 67 additions and 8 deletions

View File

@ -276,6 +276,13 @@ class UntypedOnCallSpecBase {
Clause last_clause_; Clause last_clause_;
}; // class UntypedOnCallSpecBase }; // class UntypedOnCallSpecBase
// Possible reactions on uninteresting calls.
enum CallReaction {
kAllow,
kWarn,
kFail,
};
// This template class implements an ON_CALL spec. // This template class implements an ON_CALL spec.
template <typename F> template <typename F>
class OnCallSpec : public UntypedOnCallSpecBase { class OnCallSpec : public UntypedOnCallSpecBase {
@ -332,6 +339,16 @@ class OnCallSpec : public UntypedOnCallSpecBase {
return action_; return action_;
} }
OnCallSpec& Uninteresting() {
AssertSpecProperty(not call_reaction_,
"Uninteresting() may only be called "
"once in an ON_CALL().");
call_reaction_ = CallReaction::kAllow;
return *this;
}
std::optional<CallReaction> GetCallReaction() const { return call_reaction_; }
private: private:
// The information in statement // The information in statement
// //
@ -346,18 +363,13 @@ class OnCallSpec : public UntypedOnCallSpecBase {
// matchers => matchers_ // matchers => matchers_
// multi-argument-matcher => extra_matcher_ // multi-argument-matcher => extra_matcher_
// action => action_ // action => action_
// optional call reaction => call_reaction_
ArgumentMatcherTuple matchers_; ArgumentMatcherTuple matchers_;
Matcher<const ArgumentTuple&> extra_matcher_; Matcher<const ArgumentTuple&> extra_matcher_;
Action<F> action_; Action<F> action_;
std::optional<CallReaction> call_reaction_;
}; // class OnCallSpec }; // class OnCallSpec
// Possible reactions on uninteresting calls.
enum CallReaction {
kAllow,
kWarn,
kFail,
};
} // namespace internal } // namespace internal
// Utilities for manipulating mock objects. // Utilities for manipulating mock objects.
@ -1798,9 +1810,16 @@ R FunctionMocker<R(Args...)>::InvokeWith(ArgumentTuple&& args)
// made on this mock object BEFORE performing the action, // made on this mock object BEFORE performing the action,
// because the action may DELETE the mock object and make the // because the action may DELETE the mock object and make the
// following expression meaningless. // following expression meaningless.
const CallReaction reaction = CallReaction reaction =
Mock::GetReactionOnUninterestingCalls(MockObject()); Mock::GetReactionOnUninterestingCalls(MockObject());
// Check if there is an OnCallSpec that marks this call as a
// 'known' uninteresting call that should be allowed regardless.
const OnCallSpec<F>* const spec = this->FindOnCallSpec(args);
if (spec && spec->GetCallReaction()) {
reaction = *spec->GetCallReaction();
}
// True if and only if we need to print this call's arguments and return // True if and only if we need to print this call's arguments and return
// value. This definition must be kept in sync with // value. This definition must be kept in sync with
// the behavior of ReportUninterestingCall(). // the behavior of ReportUninterestingCall().

View File

@ -157,6 +157,26 @@ TEST(RawMockTest, WarningForUninterestingCall) {
GMOCK_FLAG_SET(verbose, saved_flag); GMOCK_FLAG_SET(verbose, saved_flag);
} }
// Tests that a raw mock generates no warnings for declared uninteresting calls.
TEST(RawMockTest, NoWarningForDeclaredUninterestingCall) {
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
GMOCK_FLAG_SET(verbose, "warning");
MockFoo raw_foo;
ON_CALL(raw_foo, DoThis()).WillByDefault(InvokeWithoutArgs([](){})).Uninteresting();
ON_CALL(raw_foo, DoThat(_)).WillByDefault(Return(1)).Uninteresting();
CaptureStdout();
raw_foo.DoThis();
raw_foo.DoThat(true);
EXPECT_THAT(GetCapturedStdout(),
Not(HasSubstr("Uninteresting mock function call")));
GMOCK_FLAG_SET(verbose, saved_flag);
}
// Tests that a raw mock generates warnings for uninteresting calls // Tests that a raw mock generates warnings for uninteresting calls
// that delete the mock object. // that delete the mock object.
TEST(RawMockTest, WarningForUninterestingCallAfterDeath) { TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
@ -342,6 +362,26 @@ TEST(NaggyMockTest, WarningForUninterestingCall) {
GMOCK_FLAG_SET(verbose, saved_flag); GMOCK_FLAG_SET(verbose, saved_flag);
} }
// Tests that a raw mock generates no warnings for declared uninteresting calls.
TEST(NaggyMockTest, NoWarningForDeclaredUninterestingCall) {
const std::string saved_flag = GMOCK_FLAG_GET(verbose);
GMOCK_FLAG_SET(verbose, "warning");
NaggyMock<MockFoo> naggy_foo;
ON_CALL(naggy_foo, DoThis()).WillByDefault(InvokeWithoutArgs([](){})).Uninteresting();
ON_CALL(naggy_foo, DoThat(_)).WillByDefault(Return(1)).Uninteresting();
CaptureStdout();
naggy_foo.DoThis();
naggy_foo.DoThat(true);
EXPECT_THAT(GetCapturedStdout(),
Not(HasSubstr("Uninteresting mock function call")));
GMOCK_FLAG_SET(verbose, saved_flag);
}
// Tests that a naggy mock generates a warning for an uninteresting call // Tests that a naggy mock generates a warning for an uninteresting call
// that deletes the mock object. // that deletes the mock object.
TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) { TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {