summaryrefslogtreecommitdiffstats
path: root/googlemock/include/gmock
diff options
context:
space:
mode:
authorDino Radakovic <dinor@google.com>2022-07-25 21:37:44 (GMT)
committerCopybara-Service <copybara-worker@google.com>2022-07-25 21:38:26 (GMT)
commitb1e9b6323a17002b8359fef338c57bc0437caf57 (patch)
tree09166083100b0f3bf853d9e7e74de71daf523cc6 /googlemock/include/gmock
parent7735334a46da480a749945c0f645155d90d73855 (diff)
downloadgoogletest-b1e9b6323a17002b8359fef338c57bc0437caf57.zip
googletest-b1e9b6323a17002b8359fef338c57bc0437caf57.tar.gz
googletest-b1e9b6323a17002b8359fef338c57bc0437caf57.tar.bz2
Add IsEmpty overload for C-style strings
Fixes #3937 PiperOrigin-RevId: 463180144 Change-Id: I21b528acc4c3f3aba4234642be01fcead7fe7f00
Diffstat (limited to 'googlemock/include/gmock')
-rw-r--r--googlemock/include/gmock/gmock-more-matchers.h45
1 files changed, 38 insertions, 7 deletions
diff --git a/googlemock/include/gmock/gmock-more-matchers.h b/googlemock/include/gmock/gmock-more-matchers.h
index 47aaf98..d9a9210 100644
--- a/googlemock/include/gmock/gmock-more-matchers.h
+++ b/googlemock/include/gmock/gmock-more-matchers.h
@@ -40,6 +40,9 @@
#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_
#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MORE_MATCHERS_H_
+#include <ostream>
+#include <string>
+
#include "gmock/gmock-matchers.h"
namespace testing {
@@ -56,14 +59,42 @@ namespace testing {
#endif
#endif
-// Defines a matcher that matches an empty container. The container must
-// support both size() and empty(), which all STL-like containers provide.
-MATCHER(IsEmpty, negation ? "isn't empty" : "is empty") {
- if (arg.empty()) {
- return true;
+namespace internal {
+
+// Implements the polymorphic IsEmpty matcher, which
+// can be used as a Matcher<T> as long as T is either a container that defines
+// empty() and size() (e.g. std::vector or std::string), or a C-style string.
+class IsEmptyMatcher {
+ public:
+ // Matches anything that defines empty() and size().
+ template <typename MatcheeContainerType>
+ bool MatchAndExplain(const MatcheeContainerType& c,
+ MatchResultListener* listener) const {
+ if (c.empty()) {
+ return true;
+ }
+ *listener << "whose size is " << c.size();
+ return false;
+ }
+
+ // Matches C-style strings.
+ bool MatchAndExplain(const char* s, MatchResultListener* listener) const {
+ return MatchAndExplain(std::string(s), listener);
}
- *result_listener << "whose size is " << arg.size();
- return false;
+
+ // Describes what this matcher matches.
+ void DescribeTo(std::ostream* os) const { *os << "is empty"; }
+
+ void DescribeNegationTo(std::ostream* os) const { *os << "isn't empty"; }
+};
+
+} // namespace internal
+
+// Creates a polymorphic matcher that matches an empty container or C-style
+// string. The container must support both size() and empty(), which all
+// STL-like containers provide.
+inline PolymorphicMatcher<internal::IsEmptyMatcher> IsEmpty() {
+ return MakePolymorphicMatcher(internal::IsEmptyMatcher());
}
// Define a matcher that matches a value that evaluates in boolean