00001 // Copyright 2007, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: wan@google.com (Zhanyong Wan) 00031 // 00032 // Utilities for testing Google Test itself and code that uses Google Test 00033 // (e.g. frameworks built on top of Google Test). 00034 00035 #ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_ 00036 #define GTEST_INCLUDE_GTEST_GTEST_SPI_H_ 00037 00038 #include "gtest/gtest.h" 00039 00040 namespace testing { 00041 00042 // This helper class can be used to mock out Google Test failure reporting 00043 // so that we can test Google Test or code that builds on Google Test. 00044 // 00045 // An object of this class appends a TestPartResult object to the 00046 // TestPartResultArray object given in the constructor whenever a Google Test 00047 // failure is reported. It can either intercept only failures that are 00048 // generated in the same thread that created this object or it can intercept 00049 // all generated failures. The scope of this mock object can be controlled with 00050 // the second argument to the two arguments constructor. 00051 class GTEST_API_ ScopedFakeTestPartResultReporter 00052 : public TestPartResultReporterInterface { 00053 public: 00054 // The two possible mocking modes of this object. 00055 enum InterceptMode { 00056 INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures. 00057 INTERCEPT_ALL_THREADS // Intercepts all failures. 00058 }; 00059 00060 // The c'tor sets this object as the test part result reporter used 00061 // by Google Test. The 'result' parameter specifies where to report the 00062 // results. This reporter will only catch failures generated in the current 00063 // thread. DEPRECATED 00064 explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result); 00065 00066 // Same as above, but you can choose the interception scope of this object. 00067 ScopedFakeTestPartResultReporter(InterceptMode intercept_mode, 00068 TestPartResultArray* result); 00069 00070 // The d'tor restores the previous test part result reporter. 00071 virtual ~ScopedFakeTestPartResultReporter(); 00072 00073 // Appends the TestPartResult object to the TestPartResultArray 00074 // received in the constructor. 00075 // 00076 // This method is from the TestPartResultReporterInterface 00077 // interface. 00078 virtual void ReportTestPartResult(const TestPartResult& result); 00079 private: 00080 void Init(); 00081 00082 const InterceptMode intercept_mode_; 00083 TestPartResultReporterInterface* old_reporter_; 00084 TestPartResultArray* const result_; 00085 00086 GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter); 00087 }; 00088 00089 namespace internal { 00090 00091 // A helper class for implementing EXPECT_FATAL_FAILURE() and 00092 // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given 00093 // TestPartResultArray contains exactly one failure that has the given 00094 // type and contains the given substring. If that's not the case, a 00095 // non-fatal failure will be generated. 00096 class GTEST_API_ SingleFailureChecker { 00097 public: 00098 // The constructor remembers the arguments. 00099 SingleFailureChecker(const TestPartResultArray* results, 00100 TestPartResult::Type type, 00101 const string& substr); 00102 ~SingleFailureChecker(); 00103 private: 00104 const TestPartResultArray* const results_; 00105 const TestPartResult::Type type_; 00106 const string substr_; 00107 00108 GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker); 00109 }; 00110 00111 } // namespace internal 00112 00113 } // namespace testing 00114 00115 // A set of macros for testing Google Test assertions or code that's expected 00116 // to generate Google Test fatal failures. It verifies that the given 00117 // statement will cause exactly one fatal Google Test failure with 'substr' 00118 // being part of the failure message. 00119 // 00120 // There are two different versions of this macro. EXPECT_FATAL_FAILURE only 00121 // affects and considers failures generated in the current thread and 00122 // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 00123 // 00124 // The verification of the assertion is done correctly even when the statement 00125 // throws an exception or aborts the current function. 00126 // 00127 // Known restrictions: 00128 // - 'statement' cannot reference local non-static variables or 00129 // non-static members of the current object. 00130 // - 'statement' cannot return a value. 00131 // - You cannot stream a failure message to this macro. 00132 // 00133 // Note that even though the implementations of the following two 00134 // macros are much alike, we cannot refactor them to use a common 00135 // helper macro, due to some peculiarity in how the preprocessor 00136 // works. The AcceptsMacroThatExpandsToUnprotectedComma test in 00137 // gtest_unittest.cc will fail to compile if we do that. 00138 #define EXPECT_FATAL_FAILURE(statement, substr) \ 00139 do { \ 00140 class GTestExpectFatalFailureHelper {\ 00141 public:\ 00142 static void Execute() { statement; }\ 00143 };\ 00144 ::testing::TestPartResultArray gtest_failures;\ 00145 ::testing::internal::SingleFailureChecker gtest_checker(\ 00146 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ 00147 {\ 00148 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 00149 ::testing::ScopedFakeTestPartResultReporter:: \ 00150 INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ 00151 GTestExpectFatalFailureHelper::Execute();\ 00152 }\ 00153 } while (::testing::internal::AlwaysFalse()) 00154 00155 #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 00156 do { \ 00157 class GTestExpectFatalFailureHelper {\ 00158 public:\ 00159 static void Execute() { statement; }\ 00160 };\ 00161 ::testing::TestPartResultArray gtest_failures;\ 00162 ::testing::internal::SingleFailureChecker gtest_checker(\ 00163 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ 00164 {\ 00165 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 00166 ::testing::ScopedFakeTestPartResultReporter:: \ 00167 INTERCEPT_ALL_THREADS, >est_failures);\ 00168 GTestExpectFatalFailureHelper::Execute();\ 00169 }\ 00170 } while (::testing::internal::AlwaysFalse()) 00171 00172 // A macro for testing Google Test assertions or code that's expected to 00173 // generate Google Test non-fatal failures. It asserts that the given 00174 // statement will cause exactly one non-fatal Google Test failure with 'substr' 00175 // being part of the failure message. 00176 // 00177 // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only 00178 // affects and considers failures generated in the current thread and 00179 // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 00180 // 00181 // 'statement' is allowed to reference local variables and members of 00182 // the current object. 00183 // 00184 // The verification of the assertion is done correctly even when the statement 00185 // throws an exception or aborts the current function. 00186 // 00187 // Known restrictions: 00188 // - You cannot stream a failure message to this macro. 00189 // 00190 // Note that even though the implementations of the following two 00191 // macros are much alike, we cannot refactor them to use a common 00192 // helper macro, due to some peculiarity in how the preprocessor 00193 // works. If we do that, the code won't compile when the user gives 00194 // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that 00195 // expands to code containing an unprotected comma. The 00196 // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc 00197 // catches that. 00198 // 00199 // For the same reason, we have to write 00200 // if (::testing::internal::AlwaysTrue()) { statement; } 00201 // instead of 00202 // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) 00203 // to avoid an MSVC warning on unreachable code. 00204 #define EXPECT_NONFATAL_FAILURE(statement, substr) \ 00205 do {\ 00206 ::testing::TestPartResultArray gtest_failures;\ 00207 ::testing::internal::SingleFailureChecker gtest_checker(\ 00208 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 00209 (substr));\ 00210 {\ 00211 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 00212 ::testing::ScopedFakeTestPartResultReporter:: \ 00213 INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ 00214 if (::testing::internal::AlwaysTrue()) { statement; }\ 00215 }\ 00216 } while (::testing::internal::AlwaysFalse()) 00217 00218 #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 00219 do {\ 00220 ::testing::TestPartResultArray gtest_failures;\ 00221 ::testing::internal::SingleFailureChecker gtest_checker(\ 00222 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 00223 (substr));\ 00224 {\ 00225 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 00226 ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS,\ 00227 >est_failures);\ 00228 if (::testing::internal::AlwaysTrue()) { statement; }\ 00229 }\ 00230 } while (::testing::internal::AlwaysFalse()) 00231 00232 #endif // GTEST_INCLUDE_GTEST_GTEST_SPI_H_