• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef ANDROID_BASE_TEST_UTILS_H
18 #define ANDROID_BASE_TEST_UTILS_H
19 
20 #include <regex>
21 #include <string>
22 
23 #include <android-base/macros.h>
24 
25 class TemporaryFile {
26  public:
27   TemporaryFile();
28   explicit TemporaryFile(const std::string& tmp_dir);
29   ~TemporaryFile();
30 
31   // Release the ownership of fd, caller is reponsible for closing the
32   // fd or stream properly.
33   int release();
34 
35   int fd;
36   char path[1024];
37 
38  private:
39   void init(const std::string& tmp_dir);
40 
41   DISALLOW_COPY_AND_ASSIGN(TemporaryFile);
42 };
43 
44 class 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 };
56 
57 class 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 
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 
102 #endif  // ANDROID_BASE_TEST_UTILS_H
103