|
|
|||
File indexing completed on 2026-04-06 08:14:47
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 // Author: wan@google.com (Zhanyong Wan) 0031 // 0032 // Utilities for testing Google Test itself and code that uses Google Test 0033 // (e.g. frameworks built on top of Google Test). 0034 0035 #ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_ 0036 #define GTEST_INCLUDE_GTEST_GTEST_SPI_H_ 0037 0038 #include "gtest/gtest.h" 0039 0040 namespace testing { 0041 0042 // This helper class can be used to mock out Google Test failure reporting 0043 // so that we can test Google Test or code that builds on Google Test. 0044 // 0045 // An object of this class appends a TestPartResult object to the 0046 // TestPartResultArray object given in the constructor whenever a Google Test 0047 // failure is reported. It can either intercept only failures that are 0048 // generated in the same thread that created this object or it can intercept 0049 // all generated failures. The scope of this mock object can be controlled with 0050 // the second argument to the two arguments constructor. 0051 class GTEST_API_ ScopedFakeTestPartResultReporter 0052 : public TestPartResultReporterInterface { 0053 public: 0054 // The two possible mocking modes of this object. 0055 enum InterceptMode { 0056 INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures. 0057 INTERCEPT_ALL_THREADS // Intercepts all failures. 0058 }; 0059 0060 // The c'tor sets this object as the test part result reporter used 0061 // by Google Test. The 'result' parameter specifies where to report the 0062 // results. This reporter will only catch failures generated in the current 0063 // thread. DEPRECATED 0064 explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result); 0065 0066 // Same as above, but you can choose the interception scope of this object. 0067 ScopedFakeTestPartResultReporter(InterceptMode intercept_mode, 0068 TestPartResultArray* result); 0069 0070 // The d'tor restores the previous test part result reporter. 0071 virtual ~ScopedFakeTestPartResultReporter(); 0072 0073 // Appends the TestPartResult object to the TestPartResultArray 0074 // received in the constructor. 0075 // 0076 // This method is from the TestPartResultReporterInterface 0077 // interface. 0078 virtual void ReportTestPartResult(const TestPartResult& result); 0079 private: 0080 void Init(); 0081 0082 const InterceptMode intercept_mode_; 0083 TestPartResultReporterInterface* old_reporter_; 0084 TestPartResultArray* const result_; 0085 0086 GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter); 0087 }; 0088 0089 namespace internal { 0090 0091 // A helper class for implementing EXPECT_FATAL_FAILURE() and 0092 // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given 0093 // TestPartResultArray contains exactly one failure that has the given 0094 // type and contains the given substring. If that's not the case, a 0095 // non-fatal failure will be generated. 0096 class GTEST_API_ SingleFailureChecker { 0097 public: 0098 // The constructor remembers the arguments. 0099 SingleFailureChecker(const TestPartResultArray* results, 0100 TestPartResult::Type type, 0101 const string& substr); 0102 ~SingleFailureChecker(); 0103 private: 0104 const TestPartResultArray* const results_; 0105 const TestPartResult::Type type_; 0106 const string substr_; 0107 0108 GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker); 0109 }; 0110 0111 } // namespace internal 0112 0113 } // namespace testing 0114 0115 // A set of macros for testing Google Test assertions or code that's expected 0116 // to generate Google Test fatal failures. It verifies that the given 0117 // statement will cause exactly one fatal Google Test failure with 'substr' 0118 // being part of the failure message. 0119 // 0120 // There are two different versions of this macro. EXPECT_FATAL_FAILURE only 0121 // affects and considers failures generated in the current thread and 0122 // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 0123 // 0124 // The verification of the assertion is done correctly even when the statement 0125 // throws an exception or aborts the current function. 0126 // 0127 // Known restrictions: 0128 // - 'statement' cannot reference local non-static variables or 0129 // non-static members of the current object. 0130 // - 'statement' cannot return a value. 0131 // - You cannot stream a failure message to this macro. 0132 // 0133 // Note that even though the implementations of the following two 0134 // macros are much alike, we cannot refactor them to use a common 0135 // helper macro, due to some peculiarity in how the preprocessor 0136 // works. The AcceptsMacroThatExpandsToUnprotectedComma test in 0137 // gtest_unittest.cc will fail to compile if we do that. 0138 #define EXPECT_FATAL_FAILURE(statement, substr) \ 0139 do { \ 0140 class GTestExpectFatalFailureHelper {\ 0141 public:\ 0142 static void Execute() { statement; }\ 0143 };\ 0144 ::testing::TestPartResultArray gtest_failures;\ 0145 ::testing::internal::SingleFailureChecker gtest_checker(\ 0146 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ 0147 {\ 0148 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 0149 ::testing::ScopedFakeTestPartResultReporter:: \ 0150 INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ 0151 GTestExpectFatalFailureHelper::Execute();\ 0152 }\ 0153 } while (::testing::internal::AlwaysFalse()) 0154 0155 #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 0156 do { \ 0157 class GTestExpectFatalFailureHelper {\ 0158 public:\ 0159 static void Execute() { statement; }\ 0160 };\ 0161 ::testing::TestPartResultArray gtest_failures;\ 0162 ::testing::internal::SingleFailureChecker gtest_checker(\ 0163 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ 0164 {\ 0165 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 0166 ::testing::ScopedFakeTestPartResultReporter:: \ 0167 INTERCEPT_ALL_THREADS, >est_failures);\ 0168 GTestExpectFatalFailureHelper::Execute();\ 0169 }\ 0170 } while (::testing::internal::AlwaysFalse()) 0171 0172 // A macro for testing Google Test assertions or code that's expected to 0173 // generate Google Test non-fatal failures. It asserts that the given 0174 // statement will cause exactly one non-fatal Google Test failure with 'substr' 0175 // being part of the failure message. 0176 // 0177 // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only 0178 // affects and considers failures generated in the current thread and 0179 // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 0180 // 0181 // 'statement' is allowed to reference local variables and members of 0182 // the current object. 0183 // 0184 // The verification of the assertion is done correctly even when the statement 0185 // throws an exception or aborts the current function. 0186 // 0187 // Known restrictions: 0188 // - You cannot stream a failure message to this macro. 0189 // 0190 // Note that even though the implementations of the following two 0191 // macros are much alike, we cannot refactor them to use a common 0192 // helper macro, due to some peculiarity in how the preprocessor 0193 // works. If we do that, the code won't compile when the user gives 0194 // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that 0195 // expands to code containing an unprotected comma. The 0196 // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc 0197 // catches that. 0198 // 0199 // For the same reason, we have to write 0200 // if (::testing::internal::AlwaysTrue()) { statement; } 0201 // instead of 0202 // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) 0203 // to avoid an MSVC warning on unreachable code. 0204 #define EXPECT_NONFATAL_FAILURE(statement, substr) \ 0205 do {\ 0206 ::testing::TestPartResultArray gtest_failures;\ 0207 ::testing::internal::SingleFailureChecker gtest_checker(\ 0208 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 0209 (substr));\ 0210 {\ 0211 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 0212 ::testing::ScopedFakeTestPartResultReporter:: \ 0213 INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ 0214 if (::testing::internal::AlwaysTrue()) { statement; }\ 0215 }\ 0216 } while (::testing::internal::AlwaysFalse()) 0217 0218 #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 0219 do {\ 0220 ::testing::TestPartResultArray gtest_failures;\ 0221 ::testing::internal::SingleFailureChecker gtest_checker(\ 0222 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 0223 (substr));\ 0224 {\ 0225 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 0226 ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \ 0227 >est_failures);\ 0228 if (::testing::internal::AlwaysTrue()) { statement; }\ 0229 }\ 0230 } while (::testing::internal::AlwaysFalse()) 0231 0232 #endif // GTEST_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 |
|