Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 09:01:17

0001 // Copyright 2007, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 
0030 // Utilities for testing Google Test itself and code that uses Google Test
0031 // (e.g. frameworks built on top of Google Test).
0032 
0033 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
0034 #define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
0035 
0036 #include <string>
0037 
0038 #include "gtest/gtest.h"
0039 
0040 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0041 /* class A needs to have dll-interface to be used by clients of class B */)
0042 
0043 namespace testing {
0044 
0045 // This helper class can be used to mock out Google Test failure reporting
0046 // so that we can test Google Test or code that builds on Google Test.
0047 //
0048 // An object of this class appends a TestPartResult object to the
0049 // TestPartResultArray object given in the constructor whenever a Google Test
0050 // failure is reported. It can either intercept only failures that are
0051 // generated in the same thread that created this object or it can intercept
0052 // all generated failures. The scope of this mock object can be controlled with
0053 // the second argument to the two arguments constructor.
0054 class GTEST_API_ ScopedFakeTestPartResultReporter
0055     : public TestPartResultReporterInterface {
0056  public:
0057   // The two possible mocking modes of this object.
0058   enum InterceptMode {
0059     INTERCEPT_ONLY_CURRENT_THREAD,  // Intercepts only thread local failures.
0060     INTERCEPT_ALL_THREADS           // Intercepts all failures.
0061   };
0062 
0063   // The c'tor sets this object as the test part result reporter used
0064   // by Google Test.  The 'result' parameter specifies where to report the
0065   // results. This reporter will only catch failures generated in the current
0066   // thread. DEPRECATED
0067   explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
0068 
0069   // Same as above, but you can choose the interception scope of this object.
0070   ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
0071                                    TestPartResultArray* result);
0072 
0073   // The d'tor restores the previous test part result reporter.
0074   ~ScopedFakeTestPartResultReporter() override;
0075 
0076   // Appends the TestPartResult object to the TestPartResultArray
0077   // received in the constructor.
0078   //
0079   // This method is from the TestPartResultReporterInterface
0080   // interface.
0081   void ReportTestPartResult(const TestPartResult& result) override;
0082 
0083  private:
0084   void Init();
0085 
0086   const InterceptMode intercept_mode_;
0087   TestPartResultReporterInterface* old_reporter_;
0088   TestPartResultArray* const result_;
0089 
0090   ScopedFakeTestPartResultReporter(const ScopedFakeTestPartResultReporter&) =
0091       delete;
0092   ScopedFakeTestPartResultReporter& operator=(
0093       const ScopedFakeTestPartResultReporter&) = delete;
0094 };
0095 
0096 namespace internal {
0097 
0098 // A helper class for implementing EXPECT_FATAL_FAILURE() and
0099 // EXPECT_NONFATAL_FAILURE().  Its destructor verifies that the given
0100 // TestPartResultArray contains exactly one failure that has the given
0101 // type and contains the given substring.  If that's not the case, a
0102 // non-fatal failure will be generated.
0103 class GTEST_API_ SingleFailureChecker {
0104  public:
0105   // The constructor remembers the arguments.
0106   SingleFailureChecker(const TestPartResultArray* results,
0107                        TestPartResult::Type type, const std::string& substr);
0108   ~SingleFailureChecker();
0109 
0110  private:
0111   const TestPartResultArray* const results_;
0112   const TestPartResult::Type type_;
0113   const std::string substr_;
0114 
0115   SingleFailureChecker(const SingleFailureChecker&) = delete;
0116   SingleFailureChecker& operator=(const SingleFailureChecker&) = delete;
0117 };
0118 
0119 }  // namespace internal
0120 
0121 }  // namespace testing
0122 
0123 GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
0124 
0125 // A set of macros for testing Google Test assertions or code that's expected
0126 // to generate Google Test fatal failures (e.g. a failure from an ASSERT_EQ, but
0127 // not a non-fatal failure, as from EXPECT_EQ).  It verifies that the given
0128 // statement will cause exactly one fatal Google Test failure with 'substr'
0129 // being part of the failure message.
0130 //
0131 // There are two different versions of this macro. EXPECT_FATAL_FAILURE only
0132 // affects and considers failures generated in the current thread and
0133 // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
0134 //
0135 // The verification of the assertion is done correctly even when the statement
0136 // throws an exception or aborts the current function.
0137 //
0138 // Known restrictions:
0139 //   - 'statement' cannot reference local non-static variables or
0140 //     non-static members of the current object.
0141 //   - 'statement' cannot return a value.
0142 //   - You cannot stream a failure message to this macro.
0143 //
0144 // Note that even though the implementations of the following two
0145 // macros are much alike, we cannot refactor them to use a common
0146 // helper macro, due to some peculiarity in how the preprocessor
0147 // works.  The AcceptsMacroThatExpandsToUnprotectedComma test in
0148 // gtest_unittest.cc will fail to compile if we do that.
0149 #define EXPECT_FATAL_FAILURE(statement, substr)                               \
0150   do {                                                                        \
0151     class GTestExpectFatalFailureHelper {                                     \
0152      public:                                                                  \
0153       static void Execute() { statement; }                                    \
0154     };                                                                        \
0155     ::testing::TestPartResultArray gtest_failures;                            \
0156     ::testing::internal::SingleFailureChecker gtest_checker(                  \
0157         &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
0158     {                                                                         \
0159       ::testing::ScopedFakeTestPartResultReporter gtest_reporter(             \
0160           ::testing::ScopedFakeTestPartResultReporter::                       \
0161               INTERCEPT_ONLY_CURRENT_THREAD,                                  \
0162           &gtest_failures);                                                   \
0163       GTestExpectFatalFailureHelper::Execute();                               \
0164     }                                                                         \
0165   } while (::testing::internal::AlwaysFalse())
0166 
0167 #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)                \
0168   do {                                                                        \
0169     class GTestExpectFatalFailureHelper {                                     \
0170      public:                                                                  \
0171       static void Execute() { statement; }                                    \
0172     };                                                                        \
0173     ::testing::TestPartResultArray gtest_failures;                            \
0174     ::testing::internal::SingleFailureChecker gtest_checker(                  \
0175         &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
0176     {                                                                         \
0177       ::testing::ScopedFakeTestPartResultReporter gtest_reporter(             \
0178           ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
0179           &gtest_failures);                                                   \
0180       GTestExpectFatalFailureHelper::Execute();                               \
0181     }                                                                         \
0182   } while (::testing::internal::AlwaysFalse())
0183 
0184 // A macro for testing Google Test assertions or code that's expected to
0185 // generate Google Test non-fatal failures (e.g. a failure from an EXPECT_EQ,
0186 // but not from an ASSERT_EQ). It asserts that the given statement will cause
0187 // exactly one non-fatal Google Test failure with 'substr' being part of the
0188 // failure message.
0189 //
0190 // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
0191 // affects and considers failures generated in the current thread and
0192 // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
0193 //
0194 // 'statement' is allowed to reference local variables and members of
0195 // the current object.
0196 //
0197 // The verification of the assertion is done correctly even when the statement
0198 // throws an exception or aborts the current function.
0199 //
0200 // Known restrictions:
0201 //   - You cannot stream a failure message to this macro.
0202 //
0203 // Note that even though the implementations of the following two
0204 // macros are much alike, we cannot refactor them to use a common
0205 // helper macro, due to some peculiarity in how the preprocessor
0206 // works.  If we do that, the code won't compile when the user gives
0207 // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
0208 // expands to code containing an unprotected comma.  The
0209 // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
0210 // catches that.
0211 //
0212 // For the same reason, we have to write
0213 //   if (::testing::internal::AlwaysTrue()) { statement; }
0214 // instead of
0215 //   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
0216 // to avoid an MSVC warning on unreachable code.
0217 #define EXPECT_NONFATAL_FAILURE(statement, substr)                    \
0218   do {                                                                \
0219     ::testing::TestPartResultArray gtest_failures;                    \
0220     ::testing::internal::SingleFailureChecker gtest_checker(          \
0221         &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
0222         (substr));                                                    \
0223     {                                                                 \
0224       ::testing::ScopedFakeTestPartResultReporter gtest_reporter(     \
0225           ::testing::ScopedFakeTestPartResultReporter::               \
0226               INTERCEPT_ONLY_CURRENT_THREAD,                          \
0227           &gtest_failures);                                           \
0228       if (::testing::internal::AlwaysTrue()) {                        \
0229         statement;                                                    \
0230       }                                                               \
0231     }                                                                 \
0232   } while (::testing::internal::AlwaysFalse())
0233 
0234 #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)             \
0235   do {                                                                        \
0236     ::testing::TestPartResultArray gtest_failures;                            \
0237     ::testing::internal::SingleFailureChecker gtest_checker(                  \
0238         &gtest_failures, ::testing::TestPartResult::kNonFatalFailure,         \
0239         (substr));                                                            \
0240     {                                                                         \
0241       ::testing::ScopedFakeTestPartResultReporter gtest_reporter(             \
0242           ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
0243           &gtest_failures);                                                   \
0244       if (::testing::internal::AlwaysTrue()) {                                \
0245         statement;                                                            \
0246       }                                                                       \
0247     }                                                                         \
0248   } while (::testing::internal::AlwaysFalse())
0249 
0250 #endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_