Blame googletest/include/gtest/gtest-spi.h

Packit bd1cd8
// Copyright 2007, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
//
Packit bd1cd8
// Utilities for testing Google Test itself and code that uses Google Test
Packit bd1cd8
// (e.g. frameworks built on top of Google Test).
Packit bd1cd8
Packit bd1cd8
#ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_
Packit bd1cd8
#define GTEST_INCLUDE_GTEST_GTEST_SPI_H_
Packit bd1cd8
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
Packit bd1cd8
// This helper class can be used to mock out Google Test failure reporting
Packit bd1cd8
// so that we can test Google Test or code that builds on Google Test.
Packit bd1cd8
//
Packit bd1cd8
// An object of this class appends a TestPartResult object to the
Packit bd1cd8
// TestPartResultArray object given in the constructor whenever a Google Test
Packit bd1cd8
// failure is reported. It can either intercept only failures that are
Packit bd1cd8
// generated in the same thread that created this object or it can intercept
Packit bd1cd8
// all generated failures. The scope of this mock object can be controlled with
Packit bd1cd8
// the second argument to the two arguments constructor.
Packit bd1cd8
class GTEST_API_ ScopedFakeTestPartResultReporter
Packit bd1cd8
    : public TestPartResultReporterInterface {
Packit bd1cd8
 public:
Packit bd1cd8
  // The two possible mocking modes of this object.
Packit bd1cd8
  enum InterceptMode {
Packit bd1cd8
    INTERCEPT_ONLY_CURRENT_THREAD,  // Intercepts only thread local failures.
Packit bd1cd8
    INTERCEPT_ALL_THREADS           // Intercepts all failures.
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  // The c'tor sets this object as the test part result reporter used
Packit bd1cd8
  // by Google Test.  The 'result' parameter specifies where to report the
Packit bd1cd8
  // results. This reporter will only catch failures generated in the current
Packit bd1cd8
  // thread. DEPRECATED
Packit bd1cd8
  explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
Packit bd1cd8
Packit bd1cd8
  // Same as above, but you can choose the interception scope of this object.
Packit bd1cd8
  ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
Packit bd1cd8
                                   TestPartResultArray* result);
Packit bd1cd8
Packit bd1cd8
  // The d'tor restores the previous test part result reporter.
Packit bd1cd8
  virtual ~ScopedFakeTestPartResultReporter();
Packit bd1cd8
Packit bd1cd8
  // Appends the TestPartResult object to the TestPartResultArray
Packit bd1cd8
  // received in the constructor.
Packit bd1cd8
  //
Packit bd1cd8
  // This method is from the TestPartResultReporterInterface
Packit bd1cd8
  // interface.
Packit bd1cd8
  virtual void ReportTestPartResult(const TestPartResult& result);
Packit bd1cd8
 private:
Packit bd1cd8
  void Init();
Packit bd1cd8
Packit bd1cd8
  const InterceptMode intercept_mode_;
Packit bd1cd8
  TestPartResultReporterInterface* old_reporter_;
Packit bd1cd8
  TestPartResultArray* const result_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// A helper class for implementing EXPECT_FATAL_FAILURE() and
Packit bd1cd8
// EXPECT_NONFATAL_FAILURE().  Its destructor verifies that the given
Packit bd1cd8
// TestPartResultArray contains exactly one failure that has the given
Packit bd1cd8
// type and contains the given substring.  If that's not the case, a
Packit bd1cd8
// non-fatal failure will be generated.
Packit bd1cd8
class GTEST_API_ SingleFailureChecker {
Packit bd1cd8
 public:
Packit bd1cd8
  // The constructor remembers the arguments.
Packit bd1cd8
  SingleFailureChecker(const TestPartResultArray* results,
Packit bd1cd8
                       TestPartResult::Type type,
Packit bd1cd8
                       const string& substr);
Packit bd1cd8
  ~SingleFailureChecker();
Packit bd1cd8
 private:
Packit bd1cd8
  const TestPartResultArray* const results_;
Packit bd1cd8
  const TestPartResult::Type type_;
Packit bd1cd8
  const string substr_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
// A set of macros for testing Google Test assertions or code that's expected
Packit bd1cd8
// to generate Google Test fatal failures.  It verifies that the given
Packit bd1cd8
// statement will cause exactly one fatal Google Test failure with 'substr'
Packit bd1cd8
// being part of the failure message.
Packit bd1cd8
//
Packit bd1cd8
// There are two different versions of this macro. EXPECT_FATAL_FAILURE only
Packit bd1cd8
// affects and considers failures generated in the current thread and
Packit bd1cd8
// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
Packit bd1cd8
//
Packit bd1cd8
// The verification of the assertion is done correctly even when the statement
Packit bd1cd8
// throws an exception or aborts the current function.
Packit bd1cd8
//
Packit bd1cd8
// Known restrictions:
Packit bd1cd8
//   - 'statement' cannot reference local non-static variables or
Packit bd1cd8
//     non-static members of the current object.
Packit bd1cd8
//   - 'statement' cannot return a value.
Packit bd1cd8
//   - You cannot stream a failure message to this macro.
Packit bd1cd8
//
Packit bd1cd8
// Note that even though the implementations of the following two
Packit bd1cd8
// macros are much alike, we cannot refactor them to use a common
Packit bd1cd8
// helper macro, due to some peculiarity in how the preprocessor
Packit bd1cd8
// works.  The AcceptsMacroThatExpandsToUnprotectedComma test in
Packit bd1cd8
// gtest_unittest.cc will fail to compile if we do that.
Packit bd1cd8
#define EXPECT_FATAL_FAILURE(statement, substr) \
Packit bd1cd8
  do { \
Packit bd1cd8
    class GTestExpectFatalFailureHelper {\
Packit bd1cd8
     public:\
Packit bd1cd8
      static void Execute() { statement; }\
Packit bd1cd8
    };\
Packit bd1cd8
    ::testing::TestPartResultArray gtest_failures;\
Packit bd1cd8
    ::testing::internal::SingleFailureChecker gtest_checker(\
Packit bd1cd8
        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
Packit bd1cd8
    {\
Packit bd1cd8
      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
Packit bd1cd8
          ::testing::ScopedFakeTestPartResultReporter:: \
Packit bd1cd8
          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
Packit bd1cd8
      GTestExpectFatalFailureHelper::Execute();\
Packit bd1cd8
    }\
Packit bd1cd8
  } while (::testing::internal::AlwaysFalse())
Packit bd1cd8
Packit bd1cd8
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
Packit bd1cd8
  do { \
Packit bd1cd8
    class GTestExpectFatalFailureHelper {\
Packit bd1cd8
     public:\
Packit bd1cd8
      static void Execute() { statement; }\
Packit bd1cd8
    };\
Packit bd1cd8
    ::testing::TestPartResultArray gtest_failures;\
Packit bd1cd8
    ::testing::internal::SingleFailureChecker gtest_checker(\
Packit bd1cd8
        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
Packit bd1cd8
    {\
Packit bd1cd8
      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
Packit bd1cd8
          ::testing::ScopedFakeTestPartResultReporter:: \
Packit bd1cd8
          INTERCEPT_ALL_THREADS, &gtest_failures);\
Packit bd1cd8
      GTestExpectFatalFailureHelper::Execute();\
Packit bd1cd8
    }\
Packit bd1cd8
  } while (::testing::internal::AlwaysFalse())
Packit bd1cd8
Packit bd1cd8
// A macro for testing Google Test assertions or code that's expected to
Packit bd1cd8
// generate Google Test non-fatal failures.  It asserts that the given
Packit bd1cd8
// statement will cause exactly one non-fatal Google Test failure with 'substr'
Packit bd1cd8
// being part of the failure message.
Packit bd1cd8
//
Packit bd1cd8
// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
Packit bd1cd8
// affects and considers failures generated in the current thread and
Packit bd1cd8
// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
Packit bd1cd8
//
Packit bd1cd8
// 'statement' is allowed to reference local variables and members of
Packit bd1cd8
// the current object.
Packit bd1cd8
//
Packit bd1cd8
// The verification of the assertion is done correctly even when the statement
Packit bd1cd8
// throws an exception or aborts the current function.
Packit bd1cd8
//
Packit bd1cd8
// Known restrictions:
Packit bd1cd8
//   - You cannot stream a failure message to this macro.
Packit bd1cd8
//
Packit bd1cd8
// Note that even though the implementations of the following two
Packit bd1cd8
// macros are much alike, we cannot refactor them to use a common
Packit bd1cd8
// helper macro, due to some peculiarity in how the preprocessor
Packit bd1cd8
// works.  If we do that, the code won't compile when the user gives
Packit bd1cd8
// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
Packit bd1cd8
// expands to code containing an unprotected comma.  The
Packit bd1cd8
// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
Packit bd1cd8
// catches that.
Packit bd1cd8
//
Packit bd1cd8
// For the same reason, we have to write
Packit bd1cd8
//   if (::testing::internal::AlwaysTrue()) { statement; }
Packit bd1cd8
// instead of
Packit bd1cd8
//   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
Packit bd1cd8
// to avoid an MSVC warning on unreachable code.
Packit bd1cd8
#define EXPECT_NONFATAL_FAILURE(statement, substr) \
Packit bd1cd8
  do {\
Packit bd1cd8
    ::testing::TestPartResultArray gtest_failures;\
Packit bd1cd8
    ::testing::internal::SingleFailureChecker gtest_checker(\
Packit bd1cd8
        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
Packit bd1cd8
        (substr));\
Packit bd1cd8
    {\
Packit bd1cd8
      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
Packit bd1cd8
          ::testing::ScopedFakeTestPartResultReporter:: \
Packit bd1cd8
          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
Packit bd1cd8
      if (::testing::internal::AlwaysTrue()) { statement; }\
Packit bd1cd8
    }\
Packit bd1cd8
  } while (::testing::internal::AlwaysFalse())
Packit bd1cd8
Packit bd1cd8
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
Packit bd1cd8
  do {\
Packit bd1cd8
    ::testing::TestPartResultArray gtest_failures;\
Packit bd1cd8
    ::testing::internal::SingleFailureChecker gtest_checker(\
Packit bd1cd8
        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
Packit bd1cd8
        (substr));\
Packit bd1cd8
    {\
Packit bd1cd8
      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
Packit bd1cd8
          ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
Packit bd1cd8
          &gtest_failures);\
Packit bd1cd8
      if (::testing::internal::AlwaysTrue()) { statement; }\
Packit bd1cd8
    }\
Packit bd1cd8
  } while (::testing::internal::AlwaysFalse())
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_INCLUDE_GTEST_GTEST_SPI_H_