diff options
author | Copybara-Service <copybara-worker@google.com> | 2022-10-25 15:39:19 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-10-25 15:39:19 (GMT) |
commit | 3026483ae575e2de942db5e760cf95e973308dd5 (patch) | |
tree | db98cb58df5aabc6ed8295fca7069850474e504e /googlemock/include/gmock | |
parent | 90171d106135fc03db211fcfe928527f3cb74a5e (diff) | |
parent | 09e1c64f86634dd3a38bff59f3c24c5b8ad6675a (diff) | |
download | googletest-3026483ae575e2de942db5e760cf95e973308dd5.zip googletest-3026483ae575e2de942db5e760cf95e973308dd5.tar.gz googletest-3026483ae575e2de942db5e760cf95e973308dd5.tar.bz2 |
Merge pull request #4041 from zloylos:allow-naming-expectations
PiperOrigin-RevId: 483683590
Change-Id: Id22de3a22018324e5c1e21e262ac5e027a83bf3e
Diffstat (limited to 'googlemock/include/gmock')
-rw-r--r-- | googlemock/include/gmock/gmock-spec-builders.h | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/googlemock/include/gmock/gmock-spec-builders.h b/googlemock/include/gmock/gmock-spec-builders.h index 45cc605..ef199ab 100644 --- a/googlemock/include/gmock/gmock-spec-builders.h +++ b/googlemock/include/gmock/gmock-spec-builders.h @@ -706,6 +706,12 @@ class GTEST_API_ ExpectationBase { // describes it to the ostream. 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: friend class ::testing::Expectation; friend class UntypedFunctionMockerBase; @@ -772,6 +778,10 @@ class GTEST_API_ ExpectationBase { 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. bool IsSatisfied() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { g_gmock_mutex.AssertHeld(); @@ -831,6 +841,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 description_; // User-readable name for the expectation. // True if and only if the cardinality is specified explicitly. bool cardinality_specified_; Cardinality cardinality_; // The cardinality of the expectation. @@ -909,6 +920,13 @@ class TypedExpectation<R(Args...)> : public ExpectationBase { 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. TypedExpectation& Times(const Cardinality& a_cardinality) { ExpectationBase::UntypedTimes(a_cardinality); @@ -1199,10 +1217,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_description = GetDescription(); if (IsSaturated()) { // We have an excessive call. 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); DescribeCallCountTo(why); @@ -1217,7 +1240,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_description.empty()) { + *what << "\"" << expectation_description << "\" "; + } + *what << "call matches " << source_text() << "...\n"; return &(GetCurrentAction(mocker, args)); } |