Path: blob/master/dep/googletest/include/gtest/gtest-spi.h
4806 views
// Copyright 2007, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Utilities for testing Google Test itself and code that uses Google Test30// (e.g. frameworks built on top of Google Test).3132#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_33#define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_3435#include <string>3637#include "gtest/gtest.h"3839GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \40/* class A needs to have dll-interface to be used by clients of class B */)4142namespace testing {4344// This helper class can be used to mock out Google Test failure reporting45// so that we can test Google Test or code that builds on Google Test.46//47// An object of this class appends a TestPartResult object to the48// TestPartResultArray object given in the constructor whenever a Google Test49// failure is reported. It can either intercept only failures that are50// generated in the same thread that created this object or it can intercept51// all generated failures. The scope of this mock object can be controlled with52// the second argument to the two arguments constructor.53class GTEST_API_ ScopedFakeTestPartResultReporter54: public TestPartResultReporterInterface {55public:56// The two possible mocking modes of this object.57enum InterceptMode {58INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures.59INTERCEPT_ALL_THREADS // Intercepts all failures.60};6162// The c'tor sets this object as the test part result reporter used63// by Google Test. The 'result' parameter specifies where to report the64// results. This reporter will only catch failures generated in the current65// thread. DEPRECATED66explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);6768// Same as above, but you can choose the interception scope of this object.69ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,70TestPartResultArray* result);7172// The d'tor restores the previous test part result reporter.73~ScopedFakeTestPartResultReporter() override;7475// Appends the TestPartResult object to the TestPartResultArray76// received in the constructor.77//78// This method is from the TestPartResultReporterInterface79// interface.80void ReportTestPartResult(const TestPartResult& result) override;8182private:83void Init();8485const InterceptMode intercept_mode_;86TestPartResultReporterInterface* old_reporter_;87TestPartResultArray* const result_;8889ScopedFakeTestPartResultReporter(const ScopedFakeTestPartResultReporter&) =90delete;91ScopedFakeTestPartResultReporter& operator=(92const ScopedFakeTestPartResultReporter&) = delete;93};9495namespace internal {9697// A helper class for implementing EXPECT_FATAL_FAILURE() and98// EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given99// TestPartResultArray contains exactly one failure that has the given100// type and contains the given substring. If that's not the case, a101// non-fatal failure will be generated.102class GTEST_API_ SingleFailureChecker {103public:104// The constructor remembers the arguments.105SingleFailureChecker(const TestPartResultArray* results,106TestPartResult::Type type, const std::string& substr);107~SingleFailureChecker();108109private:110const TestPartResultArray* const results_;111const TestPartResult::Type type_;112const std::string substr_;113114SingleFailureChecker(const SingleFailureChecker&) = delete;115SingleFailureChecker& operator=(const SingleFailureChecker&) = delete;116};117118} // namespace internal119120} // namespace testing121122GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251123124// A set of macros for testing Google Test assertions or code that's expected125// to generate Google Test fatal failures (e.g. a failure from an ASSERT_EQ, but126// not a non-fatal failure, as from EXPECT_EQ). It verifies that the given127// statement will cause exactly one fatal Google Test failure with 'substr'128// being part of the failure message.129//130// There are two different versions of this macro. EXPECT_FATAL_FAILURE only131// affects and considers failures generated in the current thread and132// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.133//134// The verification of the assertion is done correctly even when the statement135// throws an exception or aborts the current function.136//137// Known restrictions:138// - 'statement' cannot reference local non-static variables or139// non-static members of the current object.140// - 'statement' cannot return a value.141// - You cannot stream a failure message to this macro.142//143// Note that even though the implementations of the following two144// macros are much alike, we cannot refactor them to use a common145// helper macro, due to some peculiarity in how the preprocessor146// works. The AcceptsMacroThatExpandsToUnprotectedComma test in147// gtest_unittest.cc will fail to compile if we do that.148#define EXPECT_FATAL_FAILURE(statement, substr) \149do { \150class GTestExpectFatalFailureHelper { \151public: \152static void Execute() { statement; } \153}; \154::testing::TestPartResultArray gtest_failures; \155::testing::internal::SingleFailureChecker gtest_checker( \156>est_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \157{ \158::testing::ScopedFakeTestPartResultReporter gtest_reporter( \159::testing::ScopedFakeTestPartResultReporter:: \160INTERCEPT_ONLY_CURRENT_THREAD, \161>est_failures); \162GTestExpectFatalFailureHelper::Execute(); \163} \164} while (::testing::internal::AlwaysFalse())165166#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \167do { \168class GTestExpectFatalFailureHelper { \169public: \170static void Execute() { statement; } \171}; \172::testing::TestPartResultArray gtest_failures; \173::testing::internal::SingleFailureChecker gtest_checker( \174>est_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \175{ \176::testing::ScopedFakeTestPartResultReporter gtest_reporter( \177::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \178>est_failures); \179GTestExpectFatalFailureHelper::Execute(); \180} \181} while (::testing::internal::AlwaysFalse())182183// A macro for testing Google Test assertions or code that's expected to184// generate Google Test non-fatal failures (e.g. a failure from an EXPECT_EQ,185// but not from an ASSERT_EQ). It asserts that the given statement will cause186// exactly one non-fatal Google Test failure with 'substr' being part of the187// failure message.188//189// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only190// affects and considers failures generated in the current thread and191// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.192//193// 'statement' is allowed to reference local variables and members of194// the current object.195//196// The verification of the assertion is done correctly even when the statement197// throws an exception or aborts the current function.198//199// Known restrictions:200// - You cannot stream a failure message to this macro.201//202// Note that even though the implementations of the following two203// macros are much alike, we cannot refactor them to use a common204// helper macro, due to some peculiarity in how the preprocessor205// works. If we do that, the code won't compile when the user gives206// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that207// expands to code containing an unprotected comma. The208// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc209// catches that.210//211// For the same reason, we have to write212// if (::testing::internal::AlwaysTrue()) { statement; }213// instead of214// GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)215// to avoid an MSVC warning on unreachable code.216#define EXPECT_NONFATAL_FAILURE(statement, substr) \217do { \218::testing::TestPartResultArray gtest_failures; \219::testing::internal::SingleFailureChecker gtest_checker( \220>est_failures, ::testing::TestPartResult::kNonFatalFailure, \221(substr)); \222{ \223::testing::ScopedFakeTestPartResultReporter gtest_reporter( \224::testing::ScopedFakeTestPartResultReporter:: \225INTERCEPT_ONLY_CURRENT_THREAD, \226>est_failures); \227if (::testing::internal::AlwaysTrue()) { \228statement; \229} \230} \231} while (::testing::internal::AlwaysFalse())232233#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \234do { \235::testing::TestPartResultArray gtest_failures; \236::testing::internal::SingleFailureChecker gtest_checker( \237>est_failures, ::testing::TestPartResult::kNonFatalFailure, \238(substr)); \239{ \240::testing::ScopedFakeTestPartResultReporter gtest_reporter( \241::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \242>est_failures); \243if (::testing::internal::AlwaysTrue()) { \244statement; \245} \246} \247} while (::testing::internal::AlwaysFalse())248249#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_250251252