Skip to content

Commit

Permalink
Allow naming expectations #3970
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Hananein <i@zloylos.me>
  • Loading branch information
Denis Hananein committed Oct 20, 2022
1 parent e07617d commit f3eb2b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
23 changes: 21 additions & 2 deletions googlemock/include/gmock/gmock-spec-builders.h
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,10 @@ class GTEST_API_ ExpectationBase {
retired_ = true;
}

void SetName(std::string name) { name_ = std::move(name); }

const std::string& GetName() const { return name_; }

// Returns true if and only if this expectation is satisfied.
bool IsSatisfied() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
g_gmock_mutex.AssertHeld();
Expand Down Expand Up @@ -831,6 +835,7 @@ class GTEST_API_ ExpectationBase {
const char* file_; // The file that contains the expectation.
int line_; // The line number of the expectation.
const std::string source_text_; // The EXPECT_CALL(...) source text.
std::string name_;
// True if and only if the cardinality is specified explicitly.
bool cardinality_specified_;
Cardinality cardinality_; // The cardinality of the expectation.
Expand Down Expand Up @@ -909,6 +914,11 @@ class TypedExpectation<R(Args...)> : public ExpectationBase {
return *this;
}

TypedExpectation& Name(std::string name) {
SetName(std::move(name));
return *this;
}

// Implements the .Times() clause.
TypedExpectation& Times(const Cardinality& a_cardinality) {
ExpectationBase::UntypedTimes(a_cardinality);
Expand Down Expand Up @@ -1199,10 +1209,15 @@ class TypedExpectation<R(Args...)> : public ExpectationBase {
::std::ostream* why)
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
g_gmock_mutex.AssertHeld();
const ::std::string& expectation_name = GetName();
if (IsSaturated()) {
// We have an excessive call.
IncrementCallCount();
*what << "Mock function called more times than expected - ";
*what << "Mock function ";
if (!expectation_name.empty()) {
*what << "with name \"" << expectation_name << "\" ";
}
*what << "called more times than expected - ";
mocker->DescribeDefaultActionTo(args, what);
DescribeCallCountTo(why);

Expand All @@ -1217,7 +1232,11 @@ class TypedExpectation<R(Args...)> : public ExpectationBase {
}

// Must be done after IncrementCount()!
*what << "Mock function call matches " << source_text() << "...\n";
*what << "Mock function ";
if (!expectation_name.empty()) {
*what << "with name \"" << expectation_name << "\" ";
}
*what << "call matches " << source_text() << "...\n";
return &(GetCurrentAction(mocker, args));
}

Expand Down
10 changes: 8 additions & 2 deletions googlemock/src/gmock-spec-builders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,14 @@ bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()
} else if (!untyped_expectation->IsSatisfied()) {
expectations_met = false;
::std::stringstream ss;
ss << "Actual function call count doesn't match "
<< untyped_expectation->source_text() << "...\n";

const ::std::string& expectation_name = untyped_expectation->GetName();
ss << "Actual function";
if (!expectation_name.empty()) {
ss << " with name \"" << expectation_name << "\"";
}
ss << " call count doesn't match " << untyped_expectation->source_text()
<< "...\n";
// No need to show the source file location of the expectation
// in the description, as the Expect() call that follows already
// takes care of it.
Expand Down

0 comments on commit f3eb2b7

Please sign in to comment.