From 6cc5825e84d35069f98a2c7fcc4602246413e690 Mon Sep 17 00:00:00 2001 From: Pawel Grzech Date: Tue, 24 Dec 2019 09:43:47 -0500 Subject: [PATCH] Allow EXPECT_CALL to have extra context information streamed to it --- .../include/gmock/gmock-spec-builders.h | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/googlemock/include/gmock/gmock-spec-builders.h b/googlemock/include/gmock/gmock-spec-builders.h index 80c13b55..7e1be82a 100644 --- a/googlemock/include/gmock/gmock-spec-builders.h +++ b/googlemock/include/gmock/gmock-spec-builders.h @@ -706,6 +706,7 @@ class GTEST_API_ ExpectationBase { const char* file() const { return file_; } int line() const { return line_; } const char* source_text() const { return source_text_.c_str(); } + std::string extra_info() const { return extra_info_.str(); } // Returns the cardinality specified in the expectation spec. const Cardinality& cardinality() const { return cardinality_; } @@ -719,6 +720,11 @@ class GTEST_API_ ExpectationBase { void DescribeCallCountTo(::std::ostream* os) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); + template + ::std::ostream& operator<<(const TYPE& obj) { + return extra_info_ << obj; + } + // If this mock method has an extra matcher (i.e. .With(matcher)), // describes it to the ostream. virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0; @@ -857,6 +863,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::ostringstream extra_info_; // extra context attached to the expectation. // True if and only if the cardinality is specified explicitly. bool cardinality_specified_; Cardinality cardinality_; // The cardinality of the expectation. @@ -1173,7 +1180,15 @@ class TypedExpectation : public ExpectationBase { // we warn the user when the WillOnce() clauses ran out. ::std::stringstream ss; DescribeLocationTo(&ss); - ss << "Actions ran out in " << source_text() << "...\n" + + ss << "Actions ran out in " << source_text(); + + std::string context(extra_info()); + if (context.size() > 0) { + ss << " with [" + context + "]"; + } + + ss << "...\n" << "Called " << count << " times, but only " << action_count << " WillOnce()" << (action_count == 1 ? " is" : "s are") << " specified - "; @@ -1218,7 +1233,14 @@ class TypedExpectation : public ExpectationBase { } // Must be done after IncrementCount()! - *what << "Mock function call matches " << source_text() <<"...\n"; + *what << "Mock function call matches " << source_text(); + + std::string context(extra_info()); + if (context.size() > 0) { + *what << " with [" + context + "]"; + } + + *what << "...\n"; return &(GetCurrentAction(mocker, args)); } @@ -1779,7 +1801,14 @@ class FunctionMocker final : public UntypedFunctionMockerBase { if (count > 1) { *why << "tried expectation #" << i << ": "; } - *why << expectation->source_text() << "...\n"; + *why << expectation->source_text(); + + std::string context(expectation->extra_info()); + if (context.size() > 0) { + *why << " with [" + context + "]"; + } + + *why << "...\n"; expectation->ExplainMatchResultTo(args, why); expectation->DescribeCallCountTo(why); }