From 6489e631ea9c26345f7fdc84ea2289271d83f795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Pr=C3=B6tel?= Date: Thu, 24 Apr 2025 21:09:32 +0200 Subject: [PATCH] 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. --- .../include/gmock/gmock-spec-builders.h | 35 ++++++++++++---- googlemock/test/gmock-nice-strict_test.cc | 40 +++++++++++++++++++ 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/googlemock/include/gmock/gmock-spec-builders.h b/googlemock/include/gmock/gmock-spec-builders.h index c4c42b7c59..0118e411a3 100644 --- a/googlemock/include/gmock/gmock-spec-builders.h +++ b/googlemock/include/gmock/gmock-spec-builders.h @@ -276,6 +276,13 @@ class UntypedOnCallSpecBase { Clause last_clause_; }; // class UntypedOnCallSpecBase +// Possible reactions on uninteresting calls. +enum CallReaction { + kAllow, + kWarn, + kFail, +}; + // This template class implements an ON_CALL spec. template class OnCallSpec : public UntypedOnCallSpecBase { @@ -332,6 +339,16 @@ class OnCallSpec : public UntypedOnCallSpecBase { 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 GetCallReaction() const { return call_reaction_; } + private: // The information in statement // @@ -346,18 +363,13 @@ class OnCallSpec : public UntypedOnCallSpecBase { // matchers => matchers_ // multi-argument-matcher => extra_matcher_ // action => action_ + // optional call reaction => call_reaction_ ArgumentMatcherTuple matchers_; Matcher extra_matcher_; Action action_; + std::optional call_reaction_; }; // class OnCallSpec -// Possible reactions on uninteresting calls. -enum CallReaction { - kAllow, - kWarn, - kFail, -}; - } // namespace internal // Utilities for manipulating mock objects. @@ -1798,9 +1810,16 @@ R FunctionMocker::InvokeWith(ArgumentTuple&& args) // made on this mock object BEFORE performing the action, // because the action may DELETE the mock object and make the // following expression meaningless. - const CallReaction reaction = + CallReaction reaction = 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* 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 // value. This definition must be kept in sync with // the behavior of ReportUninterestingCall(). diff --git a/googlemock/test/gmock-nice-strict_test.cc b/googlemock/test/gmock-nice-strict_test.cc index 95f0969035..493c92c4c0 100644 --- a/googlemock/test/gmock-nice-strict_test.cc +++ b/googlemock/test/gmock-nice-strict_test.cc @@ -157,6 +157,26 @@ TEST(RawMockTest, WarningForUninterestingCall) { 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 // that delete the mock object. TEST(RawMockTest, WarningForUninterestingCallAfterDeath) { @@ -342,6 +362,26 @@ TEST(NaggyMockTest, WarningForUninterestingCall) { 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 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 // that deletes the mock object. TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {