mirror of
https://github.com/google/googletest.git
synced 2025-12-06 16:57:00 +08:00
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:
parent
cd430b47a5
commit
6489e631ea
@ -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 {
|
||||
@ -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
|
||||
//
|
||||
@ -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.
|
||||
@ -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().
|
||||
|
||||
@ -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<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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user