|
||||
File indexing completed on 2025-01-18 10:01:06
0001 // Copyright 2007, Google Inc. 0002 // All rights reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 0030 // Utilities for testing Google Test itself and code that uses Google Test 0031 // (e.g. frameworks built on top of Google Test). 0032 0033 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ 0034 #define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ 0035 0036 #include "gtest/gtest.h" 0037 0038 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 0039 /* class A needs to have dll-interface to be used by clients of class B */) 0040 0041 namespace testing { 0042 0043 // This helper class can be used to mock out Google Test failure reporting 0044 // so that we can test Google Test or code that builds on Google Test. 0045 // 0046 // An object of this class appends a TestPartResult object to the 0047 // TestPartResultArray object given in the constructor whenever a Google Test 0048 // failure is reported. It can either intercept only failures that are 0049 // generated in the same thread that created this object or it can intercept 0050 // all generated failures. The scope of this mock object can be controlled with 0051 // the second argument to the two arguments constructor. 0052 class GTEST_API_ ScopedFakeTestPartResultReporter 0053 : public TestPartResultReporterInterface { 0054 public: 0055 // The two possible mocking modes of this object. 0056 enum InterceptMode { 0057 INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures. 0058 INTERCEPT_ALL_THREADS // Intercepts all failures. 0059 }; 0060 0061 // The c'tor sets this object as the test part result reporter used 0062 // by Google Test. The 'result' parameter specifies where to report the 0063 // results. This reporter will only catch failures generated in the current 0064 // thread. DEPRECATED 0065 explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result); 0066 0067 // Same as above, but you can choose the interception scope of this object. 0068 ScopedFakeTestPartResultReporter(InterceptMode intercept_mode, 0069 TestPartResultArray* result); 0070 0071 // The d'tor restores the previous test part result reporter. 0072 ~ScopedFakeTestPartResultReporter() override; 0073 0074 // Appends the TestPartResult object to the TestPartResultArray 0075 // received in the constructor. 0076 // 0077 // This method is from the TestPartResultReporterInterface 0078 // interface. 0079 void ReportTestPartResult(const TestPartResult& result) override; 0080 0081 private: 0082 void Init(); 0083 0084 const InterceptMode intercept_mode_; 0085 TestPartResultReporterInterface* old_reporter_; 0086 TestPartResultArray* const result_; 0087 0088 ScopedFakeTestPartResultReporter(const ScopedFakeTestPartResultReporter&) = 0089 delete; 0090 ScopedFakeTestPartResultReporter& operator=( 0091 const ScopedFakeTestPartResultReporter&) = delete; 0092 }; 0093 0094 namespace internal { 0095 0096 // A helper class for implementing EXPECT_FATAL_FAILURE() and 0097 // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given 0098 // TestPartResultArray contains exactly one failure that has the given 0099 // type and contains the given substring. If that's not the case, a 0100 // non-fatal failure will be generated. 0101 class GTEST_API_ SingleFailureChecker { 0102 public: 0103 // The constructor remembers the arguments. 0104 SingleFailureChecker(const TestPartResultArray* results, 0105 TestPartResult::Type type, const std::string& substr); 0106 ~SingleFailureChecker(); 0107 0108 private: 0109 const TestPartResultArray* const results_; 0110 const TestPartResult::Type type_; 0111 const std::string substr_; 0112 0113 SingleFailureChecker(const SingleFailureChecker&) = delete; 0114 SingleFailureChecker& operator=(const SingleFailureChecker&) = delete; 0115 }; 0116 0117 } // namespace internal 0118 0119 } // namespace testing 0120 0121 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 0122 0123 // A set of macros for testing Google Test assertions or code that's expected 0124 // to generate Google Test fatal failures (e.g. a failure from an ASSERT_EQ, but 0125 // not a non-fatal failure, as from EXPECT_EQ). It verifies that the given 0126 // statement will cause exactly one fatal Google Test failure with 'substr' 0127 // being part of the failure message. 0128 // 0129 // There are two different versions of this macro. EXPECT_FATAL_FAILURE only 0130 // affects and considers failures generated in the current thread and 0131 // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 0132 // 0133 // The verification of the assertion is done correctly even when the statement 0134 // throws an exception or aborts the current function. 0135 // 0136 // Known restrictions: 0137 // - 'statement' cannot reference local non-static variables or 0138 // non-static members of the current object. 0139 // - 'statement' cannot return a value. 0140 // - You cannot stream a failure message to this macro. 0141 // 0142 // Note that even though the implementations of the following two 0143 // macros are much alike, we cannot refactor them to use a common 0144 // helper macro, due to some peculiarity in how the preprocessor 0145 // works. The AcceptsMacroThatExpandsToUnprotectedComma test in 0146 // gtest_unittest.cc will fail to compile if we do that. 0147 #define EXPECT_FATAL_FAILURE(statement, substr) \ 0148 do { \ 0149 class GTestExpectFatalFailureHelper { \ 0150 public: \ 0151 static void Execute() { statement; } \ 0152 }; \ 0153 ::testing::TestPartResultArray gtest_failures; \ 0154 ::testing::internal::SingleFailureChecker gtest_checker( \ 0155 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \ 0156 { \ 0157 ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \ 0158 ::testing::ScopedFakeTestPartResultReporter:: \ 0159 INTERCEPT_ONLY_CURRENT_THREAD, \ 0160 >est_failures); \ 0161 GTestExpectFatalFailureHelper::Execute(); \ 0162 } \ 0163 } while (::testing::internal::AlwaysFalse()) 0164 0165 #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 0166 do { \ 0167 class GTestExpectFatalFailureHelper { \ 0168 public: \ 0169 static void Execute() { statement; } \ 0170 }; \ 0171 ::testing::TestPartResultArray gtest_failures; \ 0172 ::testing::internal::SingleFailureChecker gtest_checker( \ 0173 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \ 0174 { \ 0175 ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \ 0176 ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \ 0177 >est_failures); \ 0178 GTestExpectFatalFailureHelper::Execute(); \ 0179 } \ 0180 } while (::testing::internal::AlwaysFalse()) 0181 0182 // A macro for testing Google Test assertions or code that's expected to 0183 // generate Google Test non-fatal failures (e.g. a failure from an EXPECT_EQ, 0184 // but not from an ASSERT_EQ). It asserts that the given statement will cause 0185 // exactly one non-fatal Google Test failure with 'substr' being part of the 0186 // failure message. 0187 // 0188 // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only 0189 // affects and considers failures generated in the current thread and 0190 // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 0191 // 0192 // 'statement' is allowed to reference local variables and members of 0193 // the current object. 0194 // 0195 // The verification of the assertion is done correctly even when the statement 0196 // throws an exception or aborts the current function. 0197 // 0198 // Known restrictions: 0199 // - You cannot stream a failure message to this macro. 0200 // 0201 // Note that even though the implementations of the following two 0202 // macros are much alike, we cannot refactor them to use a common 0203 // helper macro, due to some peculiarity in how the preprocessor 0204 // works. If we do that, the code won't compile when the user gives 0205 // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that 0206 // expands to code containing an unprotected comma. The 0207 // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc 0208 // catches that. 0209 // 0210 // For the same reason, we have to write 0211 // if (::testing::internal::AlwaysTrue()) { statement; } 0212 // instead of 0213 // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) 0214 // to avoid an MSVC warning on unreachable code. 0215 #define EXPECT_NONFATAL_FAILURE(statement, substr) \ 0216 do { \ 0217 ::testing::TestPartResultArray gtest_failures; \ 0218 ::testing::internal::SingleFailureChecker gtest_checker( \ 0219 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 0220 (substr)); \ 0221 { \ 0222 ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \ 0223 ::testing::ScopedFakeTestPartResultReporter:: \ 0224 INTERCEPT_ONLY_CURRENT_THREAD, \ 0225 >est_failures); \ 0226 if (::testing::internal::AlwaysTrue()) { \ 0227 statement; \ 0228 } \ 0229 } \ 0230 } while (::testing::internal::AlwaysFalse()) 0231 0232 #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 0233 do { \ 0234 ::testing::TestPartResultArray gtest_failures; \ 0235 ::testing::internal::SingleFailureChecker gtest_checker( \ 0236 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 0237 (substr)); \ 0238 { \ 0239 ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \ 0240 ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \ 0241 >est_failures); \ 0242 if (::testing::internal::AlwaysTrue()) { \ 0243 statement; \ 0244 } \ 0245 } \ 0246 } while (::testing::internal::AlwaysFalse()) 0247 0248 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |