1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Utilities for testing Google Test itself and code that uses Google Test
32 // (e.g. frameworks built on top of Google Test).
34 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
35 #define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
37 #include "gtest/gtest.h"
39 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
40 /* class A needs to have dll-interface to be used by clients of class B */)
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.
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 {
56 // The two possible mocking modes of this object.
58 INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures.
59 INTERCEPT_ALL_THREADS // Intercepts all failures.
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
66 explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
68 // Same as above, but you can choose the interception scope of this object.
69 ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
70 TestPartResultArray* result);
72 // The d'tor restores the previous test part result reporter.
73 ~ScopedFakeTestPartResultReporter() override;
75 // Appends the TestPartResult object to the TestPartResultArray
76 // received in the constructor.
78 // This method is from the TestPartResultReporterInterface
80 void ReportTestPartResult(const TestPartResult& result) override;
85 const InterceptMode intercept_mode_;
86 TestPartResultReporterInterface* old_reporter_;
87 TestPartResultArray* const result_;
89 GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);
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 {
101 // The constructor remembers the arguments.
102 SingleFailureChecker(const TestPartResultArray* results,
103 TestPartResult::Type type, const std::string& substr);
104 ~SingleFailureChecker();
106 const TestPartResultArray* const results_;
107 const TestPartResult::Type type_;
108 const std::string substr_;
110 GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
113 } // namespace internal
115 } // namespace testing
117 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
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.
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.
128 // The verification of the assertion is done correctly even when the statement
129 // throws an exception or aborts the current function.
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.
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) \
144 class GTestExpectFatalFailureHelper {\
146 static void Execute() { statement; }\
148 ::testing::TestPartResultArray gtest_failures;\
149 ::testing::internal::SingleFailureChecker gtest_checker(\
150 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
152 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
153 ::testing::ScopedFakeTestPartResultReporter:: \
154 INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\
155 GTestExpectFatalFailureHelper::Execute();\
157 } while (::testing::internal::AlwaysFalse())
159 #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
161 class GTestExpectFatalFailureHelper {\
163 static void Execute() { statement; }\
165 ::testing::TestPartResultArray gtest_failures;\
166 ::testing::internal::SingleFailureChecker gtest_checker(\
167 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
169 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
170 ::testing::ScopedFakeTestPartResultReporter:: \
171 INTERCEPT_ALL_THREADS, >est_failures);\
172 GTestExpectFatalFailureHelper::Execute();\
174 } while (::testing::internal::AlwaysFalse())
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.
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.
185 // 'statement' is allowed to reference local variables and members of
186 // the current object.
188 // The verification of the assertion is done correctly even when the statement
189 // throws an exception or aborts the current function.
191 // Known restrictions:
192 // - You cannot stream a failure message to this macro.
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
203 // For the same reason, we have to write
204 // if (::testing::internal::AlwaysTrue()) { statement; }
206 // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
207 // to avoid an MSVC warning on unreachable code.
208 #define EXPECT_NONFATAL_FAILURE(statement, substr) \
210 ::testing::TestPartResultArray gtest_failures;\
211 ::testing::internal::SingleFailureChecker gtest_checker(\
212 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \
215 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
216 ::testing::ScopedFakeTestPartResultReporter:: \
217 INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\
218 if (::testing::internal::AlwaysTrue()) { statement; }\
220 } while (::testing::internal::AlwaysFalse())
222 #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
224 ::testing::TestPartResultArray gtest_failures;\
225 ::testing::internal::SingleFailureChecker gtest_checker(\
226 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \
229 ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
230 ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
232 if (::testing::internal::AlwaysTrue()) { statement; }\
234 } while (::testing::internal::AlwaysFalse())
236 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_