summaryrefslogtreecommitdiff
path: root/googlemock/include
diff options
context:
space:
mode:
authorDenis Hananein <i@zloylos.me>2022-10-20 23:13:06 +0200
committerDenis Hananein <i@zloylos.me>2022-10-20 23:13:06 +0200
commitf3eb2b7e38e132402f8f67e86feb97813714c3d7 (patch)
tree7a605fefdfcb5fb577c55b58ac82ed446089efc8 /googlemock/include
parente07617d6c692a96e126f11f85c3e38e46b10b4d0 (diff)
downloadgoogletest-git-f3eb2b7e38e132402f8f67e86feb97813714c3d7.tar.gz
Allow naming expectations #3970
Signed-off-by: Denis Hananein <i@zloylos.me>
Diffstat (limited to 'googlemock/include')
-rw-r--r--googlemock/include/gmock/gmock-spec-builders.h23
1 files changed, 21 insertions, 2 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<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);
@@ -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);
@@ -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));
}