From f3eb2b7e38e132402f8f67e86feb97813714c3d7 Mon Sep 17 00:00:00 2001 From: Denis Hananein Date: Thu, 20 Oct 2022 23:13:06 +0200 Subject: [PATCH 1/3] Allow naming expectations #3970 Signed-off-by: Denis Hananein --- .../include/gmock/gmock-spec-builders.h | 23 +++++++++++++++++-- googlemock/src/gmock-spec-builders.cc | 10 ++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/googlemock/include/gmock/gmock-spec-builders.h b/googlemock/include/gmock/gmock-spec-builders.h index 45cc6051..1d706758 100644 --- a/googlemock/include/gmock/gmock-spec-builders.h +++ b/googlemock/include/gmock/gmock-spec-builders.h @@ -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(); @@ -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. @@ -909,6 +914,11 @@ class TypedExpectation : 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); @@ -1199,10 +1209,15 @@ class TypedExpectation : 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); @@ -1217,7 +1232,11 @@ class TypedExpectation : 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)); } diff --git a/googlemock/src/gmock-spec-builders.cc b/googlemock/src/gmock-spec-builders.cc index b333e53d..e5f4609f 100644 --- a/googlemock/src/gmock-spec-builders.cc +++ b/googlemock/src/gmock-spec-builders.cc @@ -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. From 0d263789198e021b688942c55d483ad670c66f22 Mon Sep 17 00:00:00 2001 From: Denis Hananein Date: Fri, 21 Oct 2022 14:03:45 +0200 Subject: [PATCH 2/3] Change messages Signed-off-by: Denis Hananein --- googlemock/include/gmock/gmock-spec-builders.h | 12 ++++++------ googlemock/src/gmock-spec-builders.cc | 7 +++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/googlemock/include/gmock/gmock-spec-builders.h b/googlemock/include/gmock/gmock-spec-builders.h index 1d706758..1168d22d 100644 --- a/googlemock/include/gmock/gmock-spec-builders.h +++ b/googlemock/include/gmock/gmock-spec-builders.h @@ -1213,11 +1213,11 @@ class TypedExpectation : public ExpectationBase { if (IsSaturated()) { // We have an excessive call. IncrementCallCount(); - *what << "Mock function "; + *what << "Mock function called more times than expected "; if (!expectation_name.empty()) { - *what << "with name \"" << expectation_name << "\" "; + *what << "for \"" << expectation_name << "\" "; } - *what << "called more times than expected - "; + *what << " - "; mocker->DescribeDefaultActionTo(args, what); DescribeCallCountTo(why); @@ -1232,11 +1232,11 @@ class TypedExpectation : public ExpectationBase { } // Must be done after IncrementCount()! - *what << "Mock function "; + *what << "Mock function call matches "; if (!expectation_name.empty()) { - *what << "with name \"" << expectation_name << "\" "; + *what << "\"" << expectation_name << "\" "; } - *what << "call matches " << source_text() << "...\n"; + *what << source_text() << "...\n"; return &(GetCurrentAction(mocker, args)); } diff --git a/googlemock/src/gmock-spec-builders.cc b/googlemock/src/gmock-spec-builders.cc index e5f4609f..66fb434b 100644 --- a/googlemock/src/gmock-spec-builders.cc +++ b/googlemock/src/gmock-spec-builders.cc @@ -411,12 +411,11 @@ bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked() ::std::stringstream ss; const ::std::string& expectation_name = untyped_expectation->GetName(); - ss << "Actual function"; + ss << "Actual function call count doesn't match "; if (!expectation_name.empty()) { - ss << " with name \"" << expectation_name << "\""; + ss << "\"" << expectation_name << "\""; } - ss << " call count doesn't match " << untyped_expectation->source_text() - << "...\n"; + ss << 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. From 09e1c64f86634dd3a38bff59f3c24c5b8ad6675a Mon Sep 17 00:00:00 2001 From: Denis Hananein Date: Fri, 21 Oct 2022 15:37:38 +0200 Subject: [PATCH 3/3] Fix format without expectation name Signed-off-by: Denis Hananein --- googlemock/include/gmock/gmock-spec-builders.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/googlemock/include/gmock/gmock-spec-builders.h b/googlemock/include/gmock/gmock-spec-builders.h index 1168d22d..f6a5eadc 100644 --- a/googlemock/include/gmock/gmock-spec-builders.h +++ b/googlemock/include/gmock/gmock-spec-builders.h @@ -1213,9 +1213,9 @@ class TypedExpectation : public ExpectationBase { if (IsSaturated()) { // We have an excessive call. IncrementCallCount(); - *what << "Mock function called more times than expected "; + *what << "Mock function called more times than expected"; if (!expectation_name.empty()) { - *what << "for \"" << expectation_name << "\" "; + *what << " for \"" << expectation_name << "\""; } *what << " - "; mocker->DescribeDefaultActionTo(args, what);