summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorTreehugger Robot2017-12-19 21:24:39 -0600
committerGerrit Code Review2017-12-19 21:24:39 -0600
commitdf9fbc7d75ba4148c4849924bf945fd44f01abb3 (patch)
tree9efcad025385b4e067769e50ed456643256eb4d7 /base
parente6b83cfe5a6c4015993676774869ac63d38dc5e9 (diff)
parent30171a8b475df2989276f65dcc80fbaa786e1fba (diff)
downloadplatform-system-core-df9fbc7d75ba4148c4849924bf945fd44f01abb3.tar.gz
platform-system-core-df9fbc7d75ba4148c4849924bf945fd44f01abb3.tar.xz
platform-system-core-df9fbc7d75ba4148c4849924bf945fd44f01abb3.zip
Merge "base: extract {ASSERT,EXPECT}_MATCH helpers from debuggerd_test."
Diffstat (limited to 'base')
-rw-r--r--base/Android.bp1
-rw-r--r--base/include/android-base/test_utils.h29
-rw-r--r--base/test_utils_test.cpp46
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
22namespace android {
23namespace base {
24
25TEST(TestUtilsTest, AssertMatch) {
26 ASSERT_MATCH("foobar", R"(fo+baz?r)");
27 EXPECT_FATAL_FAILURE(ASSERT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
28}
29
30TEST(TestUtilsTest, AssertNotMatch) {
31 ASSERT_NOT_MATCH("foobar", R"(foobaz)");
32 EXPECT_FATAL_FAILURE(ASSERT_NOT_MATCH("foobar", R"(foobar)"), "regex mismatch");
33}
34
35TEST(TestUtilsTest, ExpectMatch) {
36 EXPECT_MATCH("foobar", R"(fo+baz?r)");
37 EXPECT_NONFATAL_FAILURE(EXPECT_MATCH("foobar", R"(foobaz)"), "regex mismatch");
38}
39
40TEST(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