blob: 2edafe3443719b916c4f6f6d7937eadea4d12fbd [file] [log] [blame]
Dan Alberte3ea0582015-03-13 23:06:01 -07001/*
2 * Copyright (C) 2015 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
Elliott Hughesf5224432016-03-23 15:04:52 -070017#ifndef ANDROID_BASE_TEST_UTILS_H
18#define ANDROID_BASE_TEST_UTILS_H
Dan Alberte3ea0582015-03-13 23:06:01 -070019
Josh Gao8f1fa002017-04-27 19:48:44 -070020#include <regex>
Alex Valléed0ac74c2015-05-06 16:26:00 -040021#include <string>
22
Elliott Hughesb6351622015-12-04 22:00:26 -080023#include <android-base/macros.h>
Alex Valléed0ac74c2015-05-06 16:26:00 -040024
Dan Alberte3ea0582015-03-13 23:06:01 -070025class TemporaryFile {
26 public:
27 TemporaryFile();
Yabin Cuia11c57e2017-12-06 14:20:07 -080028 explicit TemporaryFile(const std::string& tmp_dir);
Dan Alberte3ea0582015-03-13 23:06:01 -070029 ~TemporaryFile();
30
Tianjie Xu1852b802017-09-11 12:01:09 -070031 // Release the ownership of fd, caller is reponsible for closing the
32 // fd or stream properly.
33 int release();
34
Dan Alberte3ea0582015-03-13 23:06:01 -070035 int fd;
Alex Valléed0ac74c2015-05-06 16:26:00 -040036 char path[1024];
Dan Alberte3ea0582015-03-13 23:06:01 -070037
38 private:
Alex Valléed0ac74c2015-05-06 16:26:00 -040039 void init(const std::string& tmp_dir);
40
41 DISALLOW_COPY_AND_ASSIGN(TemporaryFile);
Dan Alberte3ea0582015-03-13 23:06:01 -070042};
43
Alex Valléed0ac74c2015-05-06 16:26:00 -040044class TemporaryDir {
45 public:
46 TemporaryDir();
47 ~TemporaryDir();
48
49 char path[1024];
50
51 private:
52 bool init(const std::string& tmp_dir);
53
54 DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
55};
Alex Valléed0ac74c2015-05-06 16:26:00 -040056
Wei Wangcaee0eb2016-10-21 09:23:39 -070057class CapturedStderr {
58 public:
59 CapturedStderr();
60 ~CapturedStderr();
61
62 int fd() const;
63
64 private:
65 void init();
66 void reset();
67
68 TemporaryFile temp_file_;
69 int old_stderr_;
70
71 DISALLOW_COPY_AND_ASSIGN(CapturedStderr);
72};
73
Josh Gao8f1fa002017-04-27 19:48:44 -070074#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
Elliott Hughesf5224432016-03-23 15:04:52 -0700102#endif // ANDROID_BASE_TEST_UTILS_H