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