1 // Copyright 2007, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 // 31 // Utilities for testing Google Test itself and code that uses Google Test 32 // (e.g. frameworks built on top of Google Test). 33 34 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ 35 #define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ 36 37 #include "gtest/gtest.h" 38 39 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 40 /* class A needs to have dll-interface to be used by clients of class B */) 41 42 namespace testing { 43 44 // This helper class can be used to mock out Google Test failure reporting 45 // so that we can test Google Test or code that builds on Google Test. 46 // 47 // An object of this class appends a TestPartResult object to the 48 // TestPartResultArray object given in the constructor whenever a Google Test 49 // failure is reported. It can either intercept only failures that are 50 // generated in the same thread that created this object or it can intercept 51 // all generated failures. The scope of this mock object can be controlled with 52 // the second argument to the two arguments constructor. 53 class GTEST_API_ ScopedFakeTestPartResultReporter 54 : public TestPartResultReporterInterface { 55 public: 56 // The two possible mocking modes of this object. 57 enum InterceptMode { 58 INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures. 59 INTERCEPT_ALL_THREADS // Intercepts all failures. 60 }; 61 62 // The c'tor sets this object as the test part result reporter used 63 // by Google Test. The 'result' parameter specifies where to report the 64 // results. This reporter will only catch failures generated in the current 65 // thread. DEPRECATED 66 explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result); 67 68 // Same as above, but you can choose the interception scope of this object. 69 ScopedFakeTestPartResultReporter(InterceptMode intercept_mode, 70 TestPartResultArray* result); 71 72 // The d'tor restores the previous test part result reporter. 73 ~ScopedFakeTestPartResultReporter() override; 74 75 // Appends the TestPartResult object to the TestPartResultArray 76 // received in the constructor. 77 // 78 // This method is from the TestPartResultReporterInterface 79 // interface. 80 void ReportTestPartResult(const TestPartResult& result) override; 81 82 private: 83 void Init(); 84 85 const InterceptMode intercept_mode_; 86 TestPartResultReporterInterface* old_reporter_; 87 TestPartResultArray* const result_; 88 89 GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter); 90 }; 91 92 namespace internal { 93 94 // A helper class for implementing EXPECT_FATAL_FAILURE() and 95 // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given 96 // TestPartResultArray contains exactly one failure that has the given 97 // type and contains the given substring. If that's not the case, a 98 // non-fatal failure will be generated. 99 class GTEST_API_ SingleFailureChecker { 100 public: 101 // The constructor remembers the arguments. 102 SingleFailureChecker(const TestPartResultArray* results, 103 TestPartResult::Type type, const std::string& substr); 104 ~SingleFailureChecker(); 105 private: 106 const TestPartResultArray* const results_; 107 const TestPartResult::Type type_; 108 const std::string substr_; 109 110 GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker); 111 }; 112 113 } // namespace internal 114 115 } // namespace testing 116 117 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 118 119 // A set of macros for testing Google Test assertions or code that's expected 120 // to generate Google Test fatal failures. It verifies that the given 121 // statement will cause exactly one fatal Google Test failure with 'substr' 122 // being part of the failure message. 123 // 124 // There are two different versions of this macro. EXPECT_FATAL_FAILURE only 125 // affects and considers failures generated in the current thread and 126 // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 127 // 128 // The verification of the assertion is done correctly even when the statement 129 // throws an exception or aborts the current function. 130 // 131 // Known restrictions: 132 // - 'statement' cannot reference local non-static variables or 133 // non-static members of the current object. 134 // - 'statement' cannot return a value. 135 // - You cannot stream a failure message to this macro. 136 // 137 // Note that even though the implementations of the following two 138 // macros are much alike, we cannot refactor them to use a common 139 // helper macro, due to some peculiarity in how the preprocessor 140 // works. The AcceptsMacroThatExpandsToUnprotectedComma test in 141 // gtest_unittest.cc will fail to compile if we do that. 142 #define EXPECT_FATAL_FAILURE(statement, substr) \ 143 do { \ 144 class GTestExpectFatalFailureHelper {\ 145 public:\ 146 static void Execute() { statement; }\ 147 };\ 148 ::testing::TestPartResultArray gtest_failures;\ 149 ::testing::internal::SingleFailureChecker gtest_checker(\ 150 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ 151 {\ 152 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 153 ::testing::ScopedFakeTestPartResultReporter:: \ 154 INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ 155 GTestExpectFatalFailureHelper::Execute();\ 156 }\ 157 } while (::testing::internal::AlwaysFalse()) 158 159 #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 160 do { \ 161 class GTestExpectFatalFailureHelper {\ 162 public:\ 163 static void Execute() { statement; }\ 164 };\ 165 ::testing::TestPartResultArray gtest_failures;\ 166 ::testing::internal::SingleFailureChecker gtest_checker(\ 167 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ 168 {\ 169 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 170 ::testing::ScopedFakeTestPartResultReporter:: \ 171 INTERCEPT_ALL_THREADS, >est_failures);\ 172 GTestExpectFatalFailureHelper::Execute();\ 173 }\ 174 } while (::testing::internal::AlwaysFalse()) 175 176 // A macro for testing Google Test assertions or code that's expected to 177 // generate Google Test non-fatal failures. It asserts that the given 178 // statement will cause exactly one non-fatal Google Test failure with 'substr' 179 // being part of the failure message. 180 // 181 // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only 182 // affects and considers failures generated in the current thread and 183 // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 184 // 185 // 'statement' is allowed to reference local variables and members of 186 // the current object. 187 // 188 // The verification of the assertion is done correctly even when the statement 189 // throws an exception or aborts the current function. 190 // 191 // Known restrictions: 192 // - You cannot stream a failure message to this macro. 193 // 194 // Note that even though the implementations of the following two 195 // macros are much alike, we cannot refactor them to use a common 196 // helper macro, due to some peculiarity in how the preprocessor 197 // works. If we do that, the code won't compile when the user gives 198 // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that 199 // expands to code containing an unprotected comma. The 200 // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc 201 // catches that. 202 // 203 // For the same reason, we have to write 204 // if (::testing::internal::AlwaysTrue()) { statement; } 205 // instead of 206 // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) 207 // to avoid an MSVC warning on unreachable code. 208 #define EXPECT_NONFATAL_FAILURE(statement, substr) \ 209 do {\ 210 ::testing::TestPartResultArray gtest_failures;\ 211 ::testing::internal::SingleFailureChecker gtest_checker(\ 212 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 213 (substr));\ 214 {\ 215 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 216 ::testing::ScopedFakeTestPartResultReporter:: \ 217 INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ 218 if (::testing::internal::AlwaysTrue()) { statement; }\ 219 }\ 220 } while (::testing::internal::AlwaysFalse()) 221 222 #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 223 do {\ 224 ::testing::TestPartResultArray gtest_failures;\ 225 ::testing::internal::SingleFailureChecker gtest_checker(\ 226 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 227 (substr));\ 228 {\ 229 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 230 ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \ 231 >est_failures);\ 232 if (::testing::internal::AlwaysTrue()) { statement; }\ 233 }\ 234 } while (::testing::internal::AlwaysFalse()) 235 236 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ 237