summaryrefslogtreecommitdiffstats
path: root/googlemock/include/gmock
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2024-07-16 12:22:04 (GMT)
committerCopybara-Service <copybara-worker@google.com>2024-07-16 12:22:42 (GMT)
commitd49a6654845491fcb2c1d05ef20648c5b7bed783 (patch)
treec6fb2eab346fc8217e1ed30ffcda537b92d9d417 /googlemock/include/gmock
parent417158b8bca158426cbdc0c547fdd7d9fbd46904 (diff)
downloadgoogletest-d49a6654845491fcb2c1d05ef20648c5b7bed783.zip
googletest-d49a6654845491fcb2c1d05ef20648c5b7bed783.tar.gz
googletest-d49a6654845491fcb2c1d05ef20648c5b7bed783.tar.bz2
Use matcher's description in AllOf if matcher has no explanation.
PiperOrigin-RevId: 652798234 Change-Id: I8e92248a2d9faf2a5719fe220145ea563acc14ff
Diffstat (limited to 'googlemock/include/gmock')
-rw-r--r--googlemock/include/gmock/gmock-matchers.h44
1 files changed, 15 insertions, 29 deletions
diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h
index 3daf617..063ee6c 100644
--- a/googlemock/include/gmock/gmock-matchers.h
+++ b/googlemock/include/gmock/gmock-matchers.h
@@ -1300,48 +1300,34 @@ class AllOfMatcherImpl : public MatcherInterface<const T&> {
bool MatchAndExplain(const T& x,
MatchResultListener* listener) const override {
- // This method uses matcher's explanation when explaining the result.
- // However, if matcher doesn't provide one, this method uses matcher's
- // description.
+ // If either matcher1_ or matcher2_ doesn't match x, we only need
+ // to explain why one of them fails.
std::string all_match_result;
- for (const Matcher<T>& matcher : matchers_) {
+
+ for (size_t i = 0; i < matchers_.size(); ++i) {
StringMatchResultListener slistener;
- // Return explanation for first failed matcher.
- if (!matcher.MatchAndExplain(x, &slistener)) {
- const std::string explanation = slistener.str();
- if (!explanation.empty()) {
- *listener << explanation;
+ if (matchers_[i].MatchAndExplain(x, &slistener)) {
+ if (all_match_result.empty()) {
+ all_match_result = slistener.str();
} else {
- *listener << "which doesn't match (" << Describe(matcher) << ")";
+ std::string result = slistener.str();
+ if (!result.empty()) {
+ all_match_result += ", and ";
+ all_match_result += result;
+ }
}
- return false;
- }
- // Keep track of explanations in case all matchers succeed.
- std::string explanation = slistener.str();
- if (explanation.empty()) {
- explanation = Describe(matcher);
- }
- if (all_match_result.empty()) {
- all_match_result = explanation;
} else {
- if (!explanation.empty()) {
- all_match_result += ", and ";
- all_match_result += explanation;
- }
+ *listener << slistener.str();
+ return false;
}
}
+ // Otherwise we need to explain why *both* of them match.
*listener << all_match_result;
return true;
}
private:
- // Returns matcher description as a string.
- std::string Describe(const Matcher<T>& matcher) const {
- StringMatchResultListener listener;
- matcher.DescribeTo(listener.stream());
- return listener.str();
- }
const std::vector<Matcher<T>> matchers_;
};