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 auto __s = (str); \ 59 if (!std::regex_search(__s, std::regex((pattern)))) { \ 60 FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << __s; \ 61 } \ 62 } while (0) 63 64 #define ASSERT_NOT_MATCH(str, pattern) \ 65 do { \ 66 auto __s = (str); \ 67 if (std::regex_search(__s, std::regex((pattern)))) { \ 68 FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << __s; \ 69 } \ 70 } while (0) 71 72 #define EXPECT_MATCH(str, pattern) \ 73 do { \ 74 auto __s = (str); \ 75 if (!std::regex_search(__s, std::regex((pattern)))) { \ 76 ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << __s; \ 77 } \ 78 } while (0) 79 80 #define EXPECT_NOT_MATCH(str, pattern) \ 81 do { \ 82 auto __s = (str); \ 83 if (std::regex_search(__s, std::regex((pattern)))) { \ 84 ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << __s; \ 85 } \ 86 } while (0) 87