• 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 #pragma once
18 
19 #include <regex>
20 #include <string>
21 
22 #include <android-base/file.h>
23 #include <android-base/macros.h>
24 
25 class CapturedStdFd {
26  public:
27   CapturedStdFd(int std_fd);
28   ~CapturedStdFd();
29 
30   std::string str();
31 
32   void Start();
33   void Stop();
34   void Reset();
35 
36  private:
37   int fd() const;
38 
39   TemporaryFile temp_file_;
40   int std_fd_;
41   int old_fd_ = -1;
42 
43   DISALLOW_COPY_AND_ASSIGN(CapturedStdFd);
44 };
45 
46 class CapturedStderr : public CapturedStdFd {
47  public:
CapturedStderr()48   CapturedStderr() : CapturedStdFd(STDERR_FILENO) {}
49 };
50 
51 class CapturedStdout : public CapturedStdFd {
52  public:
CapturedStdout()53   CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
54 };
55 
56 #define ASSERT_MATCH(str, pattern)                                             \
57   do {                                                                         \
58     if (!std::regex_search((str), std::regex((pattern)))) {                    \
59       FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
60     }                                                                          \
61   } while (0)
62 
63 #define ASSERT_NOT_MATCH(str, pattern)                                                     \
64   do {                                                                                     \
65     if (std::regex_search((str), std::regex((pattern)))) {                                 \
66       FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
67     }                                                                                      \
68   } while (0)
69 
70 #define EXPECT_MATCH(str, pattern)                                                    \
71   do {                                                                                \
72     if (!std::regex_search((str), std::regex((pattern)))) {                           \
73       ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
74     }                                                                                 \
75   } while (0)
76 
77 #define EXPECT_NOT_MATCH(str, pattern)                                                            \
78   do {                                                                                            \
79     if (std::regex_search((str), std::regex((pattern)))) {                                        \
80       ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
81     }                                                                                             \
82   } while (0)
83