diff options
author | Josh Gao | 2017-04-27 21:48:44 -0500 |
---|---|---|
committer | Josh Gao | 2017-12-19 19:16:12 -0600 |
commit | 30171a8b475df2989276f65dcc80fbaa786e1fba (patch) | |
tree | ce803dcca3337951975fa15c1d5759a313849f57 /base | |
parent | 51c2088f3be507f83c7580dd06c5e3c271116285 (diff) | |
download | platform-system-core-30171a8b475df2989276f65dcc80fbaa786e1fba.tar.gz platform-system-core-30171a8b475df2989276f65dcc80fbaa786e1fba.tar.xz platform-system-core-30171a8b475df2989276f65dcc80fbaa786e1fba.zip |
base: extract {ASSERT,EXPECT}_MATCH helpers from debuggerd_test.
Add some helper macros that perform regex string matching to
<android-base/test_utils.h>.
Test: libbase_test32/64 on host
Change-Id: I1b0f03dc73f8b4fdfb8ac6c75d59ef421e0e9640
Diffstat (limited to 'base')
-rw-r--r-- | base/Android.bp | 1 | ||||
-rw-r--r-- | base/include/android-base/test_utils.h | 29 | ||||
-rw-r--r-- | base/test_utils_test.cpp | 46 |
3 files changed, 76 insertions, 0 deletions
diff --git a/base/Android.bp b/base/Android.bp index ad0edf4a9..01800afad 100644 --- a/base/Android.bp +++ b/base/Android.bp | |||
@@ -116,6 +116,7 @@ cc_test { | |||
116 | "stringprintf_test.cpp", | 116 | "stringprintf_test.cpp", |
117 | "strings_test.cpp", | 117 | "strings_test.cpp", |
118 | "test_main.cpp", | 118 | "test_main.cpp", |
119 | "test_utils_test.cpp", | ||
119 | ], | 120 | ], |
120 | target: { | 121 | target: { |
121 | android: { | 122 | android: { |
diff --git a/base/include/android-base/test_utils.h b/base/include/android-base/test_utils.h index 4cfa06ba5..2edafe344 100644 --- a/base/include/android-base/test_utils.h +++ b/base/include/android-base/test_utils.h | |||
@@ -17,6 +17,7 @@ | |||
17 | #ifndef ANDROID_BASE_TEST_UTILS_H | 17 | #ifndef ANDROID_BASE_TEST_UTILS_H |
18 | #define ANDROID_BASE_TEST_UTILS_H | 18 | #define ANDROID_BASE_TEST_UTILS_H |
19 | 19 | ||
20 | #include <regex> | ||
20 | #include <string> | 21 | #include <string> |
21 | 22 | ||
22 | #include <android-base/macros.h> | 23 | #include <android-base/macros.h> |
@@ -70,4 +71,32 @@ class CapturedStderr { | |||
70 | DISALLOW_COPY_AND_ASSIGN(CapturedStderr); | 71 | DISALLOW_COPY_AND_ASSIGN(CapturedStderr); |
71 | }; | 72 | }; |
72 | 73 | ||
74 | #define ASSERT_MATCH(str, pattern) \ | ||
75 | do { \ | ||
76 | if (!std::regex_search((str), std::regex((pattern)))) { \ | ||
77 | FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \ | ||
78 | } \ | ||
79 | } while (0) | ||
80 | |||
81 | #define ASSERT_NOT_MATCH(str, pattern) \ | ||
82 | do { \ | ||
83 | if (std::regex_search((str), std::regex((pattern)))) { \ | ||
84 | FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \ | ||
85 | } \ | ||
86 | } while (0) | ||
87 | |||
88 | #define EXPECT_MATCH(str, pattern) \ | ||
89 | do { \ | ||
90 | if (!std::regex_search((str), std::regex((pattern)))) { \ | ||
91 | ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \ | ||
92 | } \ | ||
93 | } while (0) | ||
94 | |||
95 | #define EXPECT_NOT_MATCH(str, pattern) \ | ||
96 | do { \ | ||
97 | if (std::regex_search((str), std::regex((pattern)))) { \ | ||
98 | ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \ | ||
99 | } \ | ||
100 | } while (0) | ||
101 | |||
73 | #endif // ANDROID_BASE_TEST_UTILS_H | 102 | #endif // ANDROID_BASE_TEST_UTILS_H |
diff --git a/base/test_utils_test.cpp b/base/test_utils_test.cpp new file mode 100644 index 000000000..597271a72 --- /dev/null +++ b/base/test_utils_test.cpp | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 The Android Open Source Project | ||
3 | * | ||
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | * you may not use this file except in compliance with the License. | ||
6 | * You may obtain a copy of the License at | ||
7 | * | ||
8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | * | ||
10 | * Unless required by applicable law or agreed to in writing, software | ||
11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | * See the License for the specific language governing permissions and | ||
14 | * limitations under the License. | ||
15 | */ | ||
16 | |||
17 | #include "android-base/test_utils.h" | ||
18 | |||
19 | #include <gtest/gtest-spi.h> | ||
20 | #include <gtest/gtest.h> | ||
21 | |||
22 | namespace android { | ||
23 | namespace base { | ||
24 | |||
25 | TEST(TestUtilsTest, AssertMatch) { | ||
26 | ASSERT_MATCH("foobar", R"(fo+baz?r)"); | ||
27 | EXPECT_FATAL_FAILURE(ASSERT_MATCH("foobar", R"(foobaz)"), "regex mismatch"); | ||
28 | } | ||
29 | |||
30 | TEST(TestUtilsTest, AssertNotMatch) { | ||
31 | ASSERT_NOT_MATCH("foobar", R"(foobaz)"); | ||
32 | EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch"); | ||
33 | } | ||
34 | |||
35 | TEST(TestUtilsTest, ExpectMatch) { | ||
36 | EXPECT_MATCH("foobar", R"(fo+baz?r)"); | ||
37 | EXPECT_NONFATAL_FAILURE(EXPECT_MATCH("foobar", R"(foobaz)"), "regex mismatch"); | ||
38 | } | ||
39 | |||
40 | TEST(TestUtilsTest, ExpectNotMatch) { | ||
41 | EXPECT_NOT_MATCH("foobar", R"(foobaz)"); | ||
42 | EXPECT_NONFATAL_FAILURE(EXPECT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch"); | ||
43 | } | ||
44 | |||
45 | } // namespace base | ||
46 | } // namespace android | ||