| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | f522443 | 2016-03-23 15:04:52 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_BASE_TEST_UTILS_H |
| 18 | #define ANDROID_BASE_TEST_UTILS_H |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 19 | |
| Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 20 | #include <regex> |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 21 | #include <string> |
| 22 | |
| Elliott Hughes | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 23 | #include <android-base/macros.h> |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 24 | |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 25 | class TemporaryFile { |
| 26 | public: |
| 27 | TemporaryFile(); |
| Yabin Cui | a11c57e | 2017-12-06 14:20:07 -0800 | [diff] [blame] | 28 | explicit TemporaryFile(const std::string& tmp_dir); |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 29 | ~TemporaryFile(); |
| 30 | |
| Tianjie Xu | 1852b80 | 2017-09-11 12:01:09 -0700 | [diff] [blame] | 31 | // Release the ownership of fd, caller is reponsible for closing the |
| 32 | // fd or stream properly. |
| 33 | int release(); |
| Yabin Cui | b7abae1 | 2018-03-08 17:03:04 -0800 | [diff] [blame^] | 34 | // Don't remove the temporary file in the destructor. |
| 35 | void DoNotRemove() { remove_file_ = false; } |
| Tianjie Xu | 1852b80 | 2017-09-11 12:01:09 -0700 | [diff] [blame] | 36 | |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 37 | int fd; |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 38 | char path[1024]; |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 39 | |
| 40 | private: |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 41 | void init(const std::string& tmp_dir); |
| 42 | |
| Yabin Cui | b7abae1 | 2018-03-08 17:03:04 -0800 | [diff] [blame^] | 43 | bool remove_file_ = true; |
| 44 | |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 45 | DISALLOW_COPY_AND_ASSIGN(TemporaryFile); |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 48 | class 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ée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 60 | |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 61 | class 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 Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 78 | #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 Hughes | f522443 | 2016-03-23 15:04:52 -0700 | [diff] [blame] | 106 | #endif // ANDROID_BASE_TEST_UTILS_H |