Make the regex matchers generic for char and wchar_t.
Test: treehugger
Change-Id: I441787f0ffc1f4c211bff737a5ded7cfc978c791
diff --git a/include/android-base/test_utils.h b/include/android-base/test_utils.h
index f3d7cb0..4086b83 100644
--- a/include/android-base/test_utils.h
+++ b/include/android-base/test_utils.h
@@ -18,6 +18,7 @@
#include <regex>
#include <string>
+#include <type_traits>
#include <android-base/file.h>
#include <android-base/macros.h>
@@ -53,34 +54,37 @@
CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
};
-#define ASSERT_MATCH(str, pattern) \
- do { \
- auto __s = (str); \
- if (!std::regex_search(__s, std::regex((pattern)))) { \
- FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << __s; \
- } \
+#define __LIBBASE_GENERIC_REGEX_SEARCH(__s, __pattern) \
+ (std::regex_search(__s, std::basic_regex<std::decay<decltype(__s[0])>::type>((__pattern))))
+
+#define ASSERT_MATCH(__string, __pattern) \
+ do { \
+ auto __s = (__string); \
+ if (!__LIBBASE_GENERIC_REGEX_SEARCH(__s, (__pattern))) { \
+ FAIL() << "regex mismatch: expected " << (__pattern) << " in:\n" << __s; \
+ } \
} while (0)
-#define ASSERT_NOT_MATCH(str, pattern) \
- do { \
- auto __s = (str); \
- if (std::regex_search(__s, std::regex((pattern)))) { \
- FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << __s; \
- } \
+#define ASSERT_NOT_MATCH(__string, __pattern) \
+ do { \
+ auto __s = (__string); \
+ if (__LIBBASE_GENERIC_REGEX_SEARCH(__s, (__pattern))) { \
+ FAIL() << "regex mismatch: expected to not find " << (__pattern) << " in:\n" << __s; \
+ } \
} while (0)
-#define EXPECT_MATCH(str, pattern) \
- do { \
- auto __s = (str); \
- if (!std::regex_search(__s, std::regex((pattern)))) { \
- ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << __s; \
- } \
+#define EXPECT_MATCH(__string, __pattern) \
+ do { \
+ auto __s = (__string); \
+ if (!__LIBBASE_GENERIC_REGEX_SEARCH(__s, (__pattern))) { \
+ ADD_FAILURE() << "regex mismatch: expected " << (__pattern) << " in:\n" << __s; \
+ } \
} while (0)
-#define EXPECT_NOT_MATCH(str, pattern) \
- do { \
- auto __s = (str); \
- if (std::regex_search(__s, std::regex((pattern)))) { \
- ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << __s; \
- } \
+#define EXPECT_NOT_MATCH(__string, __pattern) \
+ do { \
+ auto __s = (__string); \
+ if (__LIBBASE_GENERIC_REGEX_SEARCH(__s, (__pattern))) { \
+ ADD_FAILURE() << "regex mismatch: expected to not find " << (__pattern) << " in:\n" << __s; \
+ } \
} while (0)
diff --git a/test_utils_test.cpp b/test_utils_test.cpp
index d08d5d6..10e8a2a 100644
--- a/test_utils_test.cpp
+++ b/test_utils_test.cpp
@@ -27,21 +27,33 @@
TEST(TestUtilsTest, AssertMatch) {
ASSERT_MATCH("foobar", R"(fo+baz?r)");
EXPECT_FATAL_FAILURE(ASSERT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
+
+ ASSERT_MATCH(L"foobar", LR"(fo+baz?r)");
+ EXPECT_FATAL_FAILURE(ASSERT_MATCH(L"foobar", LR"(foobaz)"), "regex mismatch");
}
TEST(TestUtilsTest, AssertNotMatch) {
ASSERT_NOT_MATCH("foobar", R"(foobaz)");
EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
+
+ ASSERT_NOT_MATCH(L"foobar", LR"(foobaz)");
+ EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH(L"foobar", LR"(foobar)"), "regex mismatch");
}
TEST(TestUtilsTest, ExpectMatch) {
EXPECT_MATCH("foobar", R"(fo+baz?r)");
EXPECT_NONFATAL_FAILURE(EXPECT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
+
+ EXPECT_MATCH(L"foobar", LR"(fo+baz?r)");
+ EXPECT_NONFATAL_FAILURE(EXPECT_MATCH(L"foobar", LR"(foobaz)"), "regex mismatch");
}
TEST(TestUtilsTest, ExpectNotMatch) {
EXPECT_NOT_MATCH("foobar", R"(foobaz)");
EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
+
+ EXPECT_NOT_MATCH(L"foobar", LR"(foobaz)");
+ EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH(L"foobar", LR"(foobar)"), "regex mismatch");
}
TEST(TestUtilsTest, CaptureStdout_smoke) {