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.
30 // Utilities for testing Google Test itself and code that uses Google Test
31 // (e.g. frameworks built on top of Google Test).
33 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
34 #define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
36 #include "gtest/gtest.h"
38 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
39 /* class A needs to have dll-interface to be used by clients of class B */)
43 // This helper class can be used to mock out Google Test failure reporting
44 // so that we can test Google Test or code that builds on Google Test.
46 // An object of this class appends a TestPartResult object to the
47 // TestPartResultArray object given in the constructor whenever a Google Test
48 // failure is reported. It can either intercept only failures that are
49 // generated in the same thread that created this object or it can intercept
50 // all generated failures. The scope of this mock object can be controlled with
51 // the second argument to the two arguments constructor.
52 class GTEST_API_ ScopedFakeTestPartResultReporter
53 : public TestPartResultReporterInterface {
55 // The two possible mocking modes of this object.
57 INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures.
58 INTERCEPT_ALL_THREADS // Intercepts all failures.
61 // The c'tor sets this object as the test part result reporter used
62 // by Google Test. The 'result' parameter specifies where to report the
63 // results. This reporter will only catch failures generated in the current
65 explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
67 // Same as above, but you can choose the interception scope of this object.
68 ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
69 TestPartResultArray* result);
71 // The d'tor restores the previous test part result reporter.
72 ~ScopedFakeTestPartResultReporter() override;
74 // Appends the TestPartResult object to the TestPartResultArray
75 // received in the constructor.
77 // This method is from the TestPartResultReporterInterface
79 void ReportTestPartResult(const TestPartResult& result) override;
84 const InterceptMode intercept_mode_;
85 TestPartResultReporterInterface* old_reporter_;
86 TestPartResultArray* const result_;
88 ScopedFakeTestPartResultReporter(const ScopedFakeTestPartResultReporter&) =
90 ScopedFakeTestPartResultReporter& operator=(
91 const ScopedFakeTestPartResultReporter&) = delete;
96 // A helper class for implementing EXPECT_FATAL_FAILURE() and
97 // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given
98 // TestPartResultArray contains exactly one failure that has the given
99 // type and contains the given substring. If that's not the case, a
100 // non-fatal failure will be generated.
101 class GTEST_API_ SingleFailureChecker {
103 // The constructor remembers the arguments.
104 SingleFailureChecker(const TestPartResultArray* results,
105 TestPartResult::Type type, const std::string& substr);
106 ~SingleFailureChecker();
109 const TestPartResultArray* const results_;
110 const TestPartResult::Type type_;
111 const std::string substr_;
113 SingleFailureChecker(const SingleFailureChecker&) = delete;
114 SingleFailureChecker& operator=(const SingleFailureChecker&) = delete;
117 } // namespace internal
119 } // namespace testing
121 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
123 // A set of macros for testing Google Test assertions or code that's expected
124 // to generate Google Test fatal failures (e.g. a failure from an ASSERT_EQ, but
125 // not a non-fatal failure, as from EXPECT_EQ). It verifies that the given
126 // statement will cause exactly one fatal Google Test failure with 'substr'
127 // being part of the failure message.
129 // There are two different versions of this macro. EXPECT_FATAL_FAILURE only
130 // affects and considers failures generated in the current thread and
131 // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
133 // The verification of the assertion is done correctly even when the statement
134 // throws an exception or aborts the current function.
136 // Known restrictions:
137 // - 'statement' cannot reference local non-static variables or
138 // non-static members of the current object.
139 // - 'statement' cannot return a value.
140 // - You cannot stream a failure message to this macro.
142 // Note that even though the implementations of the following two
143 // macros are much alike, we cannot refactor them to use a common
144 // helper macro, due to some peculiarity in how the preprocessor
145 // works. The AcceptsMacroThatExpandsToUnprotectedComma test in
146 // gtest_unittest.cc will fail to compile if we do that.
147 #define EXPECT_FATAL_FAILURE(statement, substr) \
149 class GTestExpectFatalFailureHelper { \
151 static void Execute() { statement; } \
153 ::testing::TestPartResultArray gtest_failures; \
154 ::testing::internal::SingleFailureChecker gtest_checker( \
155 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
157 ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
158 ::testing::ScopedFakeTestPartResultReporter:: \
159 INTERCEPT_ONLY_CURRENT_THREAD, \
161 GTestExpectFatalFailureHelper::Execute(); \
163 } while (::testing::internal::AlwaysFalse())
165 #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
167 class GTestExpectFatalFailureHelper { \
169 static void Execute() { statement; } \
171 ::testing::TestPartResultArray gtest_failures; \
172 ::testing::internal::SingleFailureChecker gtest_checker( \
173 >est_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
175 ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
176 ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
178 GTestExpectFatalFailureHelper::Execute(); \
180 } while (::testing::internal::AlwaysFalse())
182 // A macro for testing Google Test assertions or code that's expected to
183 // generate Google Test non-fatal failures (e.g. a failure from an EXPECT_EQ,
184 // but not from an ASSERT_EQ). It asserts that the given statement will cause
185 // exactly one non-fatal Google Test failure with 'substr' being part of the
188 // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
189 // affects and considers failures generated in the current thread and
190 // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
192 // 'statement' is allowed to reference local variables and members of
193 // the current object.
195 // The verification of the assertion is done correctly even when the statement
196 // throws an exception or aborts the current function.
198 // Known restrictions:
199 // - You cannot stream a failure message to this macro.
201 // Note that even though the implementations of the following two
202 // macros are much alike, we cannot refactor them to use a common
203 // helper macro, due to some peculiarity in how the preprocessor
204 // works. If we do that, the code won't compile when the user gives
205 // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
206 // expands to code containing an unprotected comma. The
207 // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
210 // For the same reason, we have to write
211 // if (::testing::internal::AlwaysTrue()) { statement; }
213 // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
214 // to avoid an MSVC warning on unreachable code.
215 #define EXPECT_NONFATAL_FAILURE(statement, substr) \
217 ::testing::TestPartResultArray gtest_failures; \
218 ::testing::internal::SingleFailureChecker gtest_checker( \
219 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \
222 ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
223 ::testing::ScopedFakeTestPartResultReporter:: \
224 INTERCEPT_ONLY_CURRENT_THREAD, \
226 if (::testing::internal::AlwaysTrue()) { \
230 } while (::testing::internal::AlwaysFalse())
232 #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
234 ::testing::TestPartResultArray gtest_failures; \
235 ::testing::internal::SingleFailureChecker gtest_checker( \
236 >est_failures, ::testing::TestPartResult::kNonFatalFailure, \
239 ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
240 ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
242 if (::testing::internal::AlwaysTrue()) { \
246 } while (::testing::internal::AlwaysFalse())
248 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_