1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29 
30 //
31 // Utilities for testing Google Test itself and code that uses Google Test
32 // (e.g. frameworks built on top of Google Test).
33 
34 // GOOGLETEST_CM0004 DO NOT DELETE
35 
36 #ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_
37 #define GTEST_INCLUDE_GTEST_GTEST_SPI_H_
38 
39 #include "gtest/gtest.h"
40 
41 GTEST_DISABLE_MSC_WARNINGS_PUSH_(
42     4251 /* class A needs to have dll-interface to be used by clients of class B */)
43 
44 namespace testing {
45 
46 // This helper class can be used to mock out Google Test failure reporting
47 // so that we can test Google Test or code that builds on Google Test.
48 //
49 // An object of this class appends a TestPartResult object to the
50 // TestPartResultArray object given in the constructor whenever a Google Test
51 // failure is reported. It can either intercept only failures that are
52 // generated in the same thread that created this object or it can intercept
53 // all generated failures. The scope of this mock object can be controlled with
54 // the second argument to the two arguments constructor.
55 class GTEST_API_ ScopedFakeTestPartResultReporter
56     : public TestPartResultReporterInterface {
57  public:
58   // The two possible mocking modes of this object.
59   enum InterceptMode {
60     INTERCEPT_ONLY_CURRENT_THREAD,  // Intercepts only thread local failures.
61     INTERCEPT_ALL_THREADS           // Intercepts all failures.
62   };
63 
64   // The c'tor sets this object as the test part result reporter used
65   // by Google Test.  The 'result' parameter specifies where to report the
66   // results. This reporter will only catch failures generated in the current
67   // thread. DEPRECATED
68   explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
69 
70   // Same as above, but you can choose the interception scope of this object.
71   ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
72                                    TestPartResultArray* result);
73 
74   // The d'tor restores the previous test part result reporter.
75   ~ScopedFakeTestPartResultReporter() override;
76 
77   // Appends the TestPartResult object to the TestPartResultArray
78   // received in the constructor.
79   //
80   // This method is from the TestPartResultReporterInterface
81   // interface.
82   void ReportTestPartResult(const TestPartResult& result) override;
83 
84  private:
85   void Init();
86 
87   const InterceptMode intercept_mode_;
88   TestPartResultReporterInterface* old_reporter_;
89   TestPartResultArray* const result_;
90 
91   GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);
92 };
93 
94 namespace internal {
95 
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 {
102  public:
103   // The constructor remembers the arguments.
104   SingleFailureChecker(const TestPartResultArray* results,
105                        TestPartResult::Type type, const std::string& substr);
106   ~SingleFailureChecker();
107 
108  private:
109   const TestPartResultArray* const results_;
110   const TestPartResult::Type type_;
111   const std::string substr_;
112 
113   GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
114 };
115 
116 }  // namespace internal
117 
118 }  // namespace testing
119 
120 GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
121 
122 // A set of macros for testing Google Test assertions or code that's expected
123 // to generate Google Test fatal failures.  It verifies that the given
124 // statement will cause exactly one fatal Google Test failure with 'substr'
125 // being part of the failure message.
126 //
127 // There are two different versions of this macro. EXPECT_FATAL_FAILURE only
128 // affects and considers failures generated in the current thread and
129 // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
130 //
131 // The verification of the assertion is done correctly even when the statement
132 // throws an exception or aborts the current function.
133 //
134 // Known restrictions:
135 //   - 'statement' cannot reference local non-static variables or
136 //     non-static members of the current object.
137 //   - 'statement' cannot return a value.
138 //   - You cannot stream a failure message to this macro.
139 //
140 // Note that even though the implementations of the following two
141 // macros are much alike, we cannot refactor them to use a common
142 // helper macro, due to some peculiarity in how the preprocessor
143 // works.  The AcceptsMacroThatExpandsToUnprotectedComma test in
144 // gtest_unittest.cc will fail to compile if we do that.
145 #define EXPECT_FATAL_FAILURE(statement, substr)                               \
146   do {                                                                        \
147     class GTestExpectFatalFailureHelper {                                     \
148      public:                                                                  \
149       static void Execute() { statement; }                                    \
150     };                                                                        \
151     ::testing::TestPartResultArray gtest_failures;                            \
152     ::testing::internal::SingleFailureChecker gtest_checker(                  \
153         &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
154     {                                                                         \
155       ::testing::ScopedFakeTestPartResultReporter gtest_reporter(             \
156           ::testing::ScopedFakeTestPartResultReporter::                       \
157               INTERCEPT_ONLY_CURRENT_THREAD,                                  \
158           &gtest_failures);                                                   \
159       GTestExpectFatalFailureHelper::Execute();                               \
160     }                                                                         \
161   } while (::testing::internal::AlwaysFalse())
162 
163 #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)                \
164   do {                                                                        \
165     class GTestExpectFatalFailureHelper {                                     \
166      public:                                                                  \
167       static void Execute() { statement; }                                    \
168     };                                                                        \
169     ::testing::TestPartResultArray gtest_failures;                            \
170     ::testing::internal::SingleFailureChecker gtest_checker(                  \
171         &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
172     {                                                                         \
173       ::testing::ScopedFakeTestPartResultReporter gtest_reporter(             \
174           ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
175           &gtest_failures);                                                   \
176       GTestExpectFatalFailureHelper::Execute();                               \
177     }                                                                         \
178   } while (::testing::internal::AlwaysFalse())
179 
180 // A macro for testing Google Test assertions or code that's expected to
181 // generate Google Test non-fatal failures.  It asserts that the given
182 // statement will cause exactly one non-fatal Google Test failure with 'substr'
183 // being part of the failure message.
184 //
185 // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
186 // affects and considers failures generated in the current thread and
187 // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
188 //
189 // 'statement' is allowed to reference local variables and members of
190 // the current object.
191 //
192 // The verification of the assertion is done correctly even when the statement
193 // throws an exception or aborts the current function.
194 //
195 // Known restrictions:
196 //   - You cannot stream a failure message to this macro.
197 //
198 // Note that even though the implementations of the following two
199 // macros are much alike, we cannot refactor them to use a common
200 // helper macro, due to some peculiarity in how the preprocessor
201 // works.  If we do that, the code won't compile when the user gives
202 // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
203 // expands to code containing an unprotected comma.  The
204 // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
205 // catches that.
206 //
207 // For the same reason, we have to write
208 //   if (::testing::internal::AlwaysTrue()) { statement; }
209 // instead of
210 //   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
211 // to avoid an MSVC warning on unreachable code.
212 #define EXPECT_NONFATAL_FAILURE(statement, substr)                    \
213   do {                                                                \
214     ::testing::TestPartResultArray gtest_failures;                    \
215     ::testing::internal::SingleFailureChecker gtest_checker(          \
216         &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
217         (substr));                                                    \
218     {                                                                 \
219       ::testing::ScopedFakeTestPartResultReporter gtest_reporter(     \
220           ::testing::ScopedFakeTestPartResultReporter::               \
221               INTERCEPT_ONLY_CURRENT_THREAD,                          \
222           &gtest_failures);                                           \
223       if (::testing::internal::AlwaysTrue()) {                        \
224         statement;                                                    \
225       }                                                               \
226     }                                                                 \
227   } while (::testing::internal::AlwaysFalse())
228 
229 #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)             \
230   do {                                                                        \
231     ::testing::TestPartResultArray gtest_failures;                            \
232     ::testing::internal::SingleFailureChecker gtest_checker(                  \
233         &gtest_failures, ::testing::TestPartResult::kNonFatalFailure,         \
234         (substr));                                                            \
235     {                                                                         \
236       ::testing::ScopedFakeTestPartResultReporter gtest_reporter(             \
237           ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
238           &gtest_failures);                                                   \
239       if (::testing::internal::AlwaysTrue()) {                                \
240         statement;                                                            \
241       }                                                                       \
242     }                                                                         \
243   } while (::testing::internal::AlwaysFalse())
244 
245 #endif  // GTEST_INCLUDE_GTEST_GTEST_SPI_H_
246