Merge pull request #4041 from zloylos:allow-naming-expectations

PiperOrigin-RevId: 483683590
Change-Id: Id22de3a22018324e5c1e21e262ac5e027a83bf3e
This commit is contained in:
Copybara-Service 2022-10-25 08:39:19 -07:00
commit 3026483ae5
3 changed files with 48 additions and 11 deletions

View File

@ -706,6 +706,12 @@ class GTEST_API_ ExpectationBase {
// describes it to the ostream. // describes it to the ostream.
virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0; virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0;
// Do not rely on this for correctness.
// This is only for making human-readable test output easier to understand.
void UntypedDescription(std::string description) {
description_ = std::move(description);
}
protected: protected:
friend class ::testing::Expectation; friend class ::testing::Expectation;
friend class UntypedFunctionMockerBase; friend class UntypedFunctionMockerBase;
@ -772,6 +778,10 @@ class GTEST_API_ ExpectationBase {
retired_ = true; retired_ = true;
} }
// Returns a human-readable description of this expectation.
// Do not rely on this for correctness. It is only for human readability.
const std::string& GetDescription() const { return description_; }
// Returns true if and only if this expectation is satisfied. // Returns true if and only if this expectation is satisfied.
bool IsSatisfied() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { bool IsSatisfied() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
g_gmock_mutex.AssertHeld(); g_gmock_mutex.AssertHeld();
@ -831,6 +841,7 @@ class GTEST_API_ ExpectationBase {
const char* file_; // The file that contains the expectation. const char* file_; // The file that contains the expectation.
int line_; // The line number of the expectation. int line_; // The line number of the expectation.
const std::string source_text_; // The EXPECT_CALL(...) source text. const std::string source_text_; // The EXPECT_CALL(...) source text.
std::string description_; // User-readable name for the expectation.
// True if and only if the cardinality is specified explicitly. // True if and only if the cardinality is specified explicitly.
bool cardinality_specified_; bool cardinality_specified_;
Cardinality cardinality_; // The cardinality of the expectation. Cardinality cardinality_; // The cardinality of the expectation.
@ -909,6 +920,13 @@ class TypedExpectation<R(Args...)> : public ExpectationBase {
return *this; return *this;
} }
// Do not rely on this for correctness.
// This is only for making human-readable test output easier to understand.
TypedExpectation& Description(std::string name) {
ExpectationBase::UntypedDescription(std::move(name));
return *this;
}
// Implements the .Times() clause. // Implements the .Times() clause.
TypedExpectation& Times(const Cardinality& a_cardinality) { TypedExpectation& Times(const Cardinality& a_cardinality) {
ExpectationBase::UntypedTimes(a_cardinality); ExpectationBase::UntypedTimes(a_cardinality);
@ -1199,10 +1217,15 @@ class TypedExpectation<R(Args...)> : public ExpectationBase {
::std::ostream* why) ::std::ostream* why)
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
g_gmock_mutex.AssertHeld(); g_gmock_mutex.AssertHeld();
const ::std::string& expectation_description = GetDescription();
if (IsSaturated()) { if (IsSaturated()) {
// We have an excessive call. // We have an excessive call.
IncrementCallCount(); IncrementCallCount();
*what << "Mock function called more times than expected - "; *what << "Mock function ";
if (!expectation_description.empty()) {
*what << "\"" << expectation_description << "\" ";
}
*what << "called more times than expected - ";
mocker->DescribeDefaultActionTo(args, what); mocker->DescribeDefaultActionTo(args, what);
DescribeCallCountTo(why); DescribeCallCountTo(why);
@ -1217,7 +1240,11 @@ class TypedExpectation<R(Args...)> : public ExpectationBase {
} }
// Must be done after IncrementCount()! // Must be done after IncrementCount()!
*what << "Mock function call matches " << source_text() << "...\n"; *what << "Mock function ";
if (!expectation_description.empty()) {
*what << "\"" << expectation_description << "\" ";
}
*what << "call matches " << source_text() << "...\n";
return &(GetCurrentAction(mocker, args)); return &(GetCurrentAction(mocker, args));
} }

View File

@ -409,8 +409,15 @@ bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()
} else if (!untyped_expectation->IsSatisfied()) { } else if (!untyped_expectation->IsSatisfied()) {
expectations_met = false; expectations_met = false;
::std::stringstream ss; ::std::stringstream ss;
ss << "Actual function call count doesn't match "
<< untyped_expectation->source_text() << "...\n"; const ::std::string& expectation_name =
untyped_expectation->GetDescription();
ss << "Actual function ";
if (!expectation_name.empty()) {
ss << "\"" << expectation_name << "\" ";
}
ss << "call count doesn't match " << untyped_expectation->source_text()
<< "...\n";
// No need to show the source file location of the expectation // No need to show the source file location of the expectation
// in the description, as the Expect() call that follows already // in the description, as the Expect() call that follows already
// takes care of it. // takes care of it.

View File

@ -739,11 +739,12 @@ TEST(ExpectCallTest, CatchesTooFewCalls) {
EXPECT_NONFATAL_FAILURE( EXPECT_NONFATAL_FAILURE(
{ // NOLINT { // NOLINT
MockB b; MockB b;
EXPECT_CALL(b, DoB(5)).Times(AtLeast(2)); EXPECT_CALL(b, DoB(5)).Description("DoB Method").Times(AtLeast(2));
b.DoB(5); b.DoB(5);
}, },
"Actual function call count doesn't match EXPECT_CALL(b, DoB(5))...\n" "Actual function \"DoB Method\" call count "
"doesn't match EXPECT_CALL(b, DoB(5))...\n"
" Expected: to be called at least twice\n" " Expected: to be called at least twice\n"
" Actual: called once - unsatisfied and active"); " Actual: called once - unsatisfied and active");
} }
@ -1148,10 +1149,11 @@ TEST(ExcessiveCallTest, DoesDefaultAction) {
// When there is no ON_CALL(), the default value for the return type // When there is no ON_CALL(), the default value for the return type
// should be returned. // should be returned.
MockB b; MockB b;
EXPECT_CALL(b, DoB(0)).Times(0); EXPECT_CALL(b, DoB(0)).Description("DoB Method").Times(0);
int n = -1; int n = -1;
EXPECT_NONFATAL_FAILURE(n = b.DoB(0), EXPECT_NONFATAL_FAILURE(
"Mock function called more times than expected"); n = b.DoB(0),
"Mock function \"DoB Method\" called more times than expected");
EXPECT_EQ(0, n); EXPECT_EQ(0, n);
} }
@ -1159,10 +1161,11 @@ TEST(ExcessiveCallTest, DoesDefaultAction) {
// the failure message contains the argument values. // the failure message contains the argument values.
TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) { TEST(ExcessiveCallTest, GeneratesFailureForVoidFunction) {
MockA a; MockA a;
EXPECT_CALL(a, DoA(_)).Times(0); EXPECT_CALL(a, DoA(_)).Description("DoA Method").Times(0);
EXPECT_NONFATAL_FAILURE( EXPECT_NONFATAL_FAILURE(
a.DoA(9), a.DoA(9),
"Mock function called more times than expected - returning directly.\n" "Mock function \"DoA Method\" called more times than expected - "
"returning directly.\n"
" Function call: DoA(9)\n" " Function call: DoA(9)\n"
" Expected: to be never called\n" " Expected: to be never called\n"
" Actual: called once - over-saturated and active"); " Actual: called once - over-saturated and active");