blob: b95fa07ce8618e9b279e088813aede3848997bfa [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();
Yabin Cuib7abae12018-03-08 17:03:04 -080034 // Don't remove the temporary file in the destructor.
35 void DoNotRemove() { remove_file_ = false; }
Tianjie Xu1852b802017-09-11 12:01:09 -070036
Dan Alberte3ea0582015-03-13 23:06:01 -070037 int fd;
Alex Valléed0ac74c2015-05-06 16:26:00 -040038 char path[1024];
Dan Alberte3ea0582015-03-13 23:06:01 -070039
40 private:
Alex Valléed0ac74c2015-05-06 16:26:00 -040041 void init(const std::string& tmp_dir);
42
Yabin Cuib7abae12018-03-08 17:03:04 -080043 bool remove_file_ = true;
44
Alex Valléed0ac74c2015-05-06 16:26:00 -040045 DISALLOW_COPY_AND_ASSIGN(TemporaryFile);
Dan Alberte3ea0582015-03-13 23:06:01 -070046};
47
Alex Valléed0ac74c2015-05-06 16:26:00 -040048class TemporaryDir {
49 public:
50 TemporaryDir();
51 ~TemporaryDir();
52
53 char path[1024];
54
55 private:
56 bool init(const std::string& tmp_dir);
57
58 DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
59};
Alex Valléed0ac74c2015-05-06 16:26:00 -040060
Wei Wangcaee0eb2016-10-21 09:23:39 -070061class CapturedStderr {
62 public:
63 CapturedStderr();
64 ~CapturedStderr();
65
66 int fd() const;
67
68 private:
69 void init();
70 void reset();
71
72 TemporaryFile temp_file_;
73 int old_stderr_;
74
75 DISALLOW_COPY_AND_ASSIGN(CapturedStderr);
76};
77
Josh Gao8f1fa002017-04-27 19:48:44 -070078#define ASSERT_MATCH(str, pattern) \
79 do { \
80 if (!std::regex_search((str), std::regex((pattern)))) { \
81 FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
82 } \
83 } while (0)
84
85#define ASSERT_NOT_MATCH(str, pattern) \
86 do { \
87 if (std::regex_search((str), std::regex((pattern)))) { \
88 FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
89 } \
90 } while (0)
91
92#define EXPECT_MATCH(str, pattern) \
93 do { \
94 if (!std::regex_search((str), std::regex((pattern)))) { \
95 ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
96 } \
97 } while (0)
98
99#define EXPECT_NOT_MATCH(str, pattern) \
100 do { \
101 if (std::regex_search((str), std::regex((pattern)))) { \
102 ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
103 } \
104 } while (0)
105
Elliott Hughesf5224432016-03-23 15:04:52 -0700106#endif // ANDROID_BASE_TEST_UTILS_H