Skip to content

Allow declaring calls as uninteresting #4749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions googlemock/include/gmock/gmock-spec-builders.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename F>
class OnCallSpec : public UntypedOnCallSpecBase {
Expand Down Expand Up @@ -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<CallReaction> GetCallReaction() const { return call_reaction_; }

private:
// The information in statement
//
Expand All @@ -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<const ArgumentTuple&> extra_matcher_;
Action<F> action_;
std::optional<CallReaction> call_reaction_;
}; // class OnCallSpec

// Possible reactions on uninteresting calls.
enum CallReaction {
kAllow,
kWarn,
kFail,
};

} // namespace internal

// Utilities for manipulating mock objects.
Expand Down Expand Up @@ -1798,9 +1810,16 @@ R FunctionMocker<R(Args...)>::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<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
// value. This definition must be kept in sync with
// the behavior of ReportUninterestingCall().
Expand Down
40 changes: 40 additions & 0 deletions googlemock/test/gmock-nice-strict_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<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
// that deletes the mock object.
TEST(NaggyMockTest, WarningForUninterestingCallAfterDeath) {
Expand Down