11b362b15af34006e6a11974088a46d42b903418eJohann// Copyright 2007, Google Inc.
21b362b15af34006e6a11974088a46d42b903418eJohann// All rights reserved.
31b362b15af34006e6a11974088a46d42b903418eJohann//
41b362b15af34006e6a11974088a46d42b903418eJohann// Redistribution and use in source and binary forms, with or without
51b362b15af34006e6a11974088a46d42b903418eJohann// modification, are permitted provided that the following conditions are
61b362b15af34006e6a11974088a46d42b903418eJohann// met:
71b362b15af34006e6a11974088a46d42b903418eJohann//
81b362b15af34006e6a11974088a46d42b903418eJohann//     * Redistributions of source code must retain the above copyright
91b362b15af34006e6a11974088a46d42b903418eJohann// notice, this list of conditions and the following disclaimer.
101b362b15af34006e6a11974088a46d42b903418eJohann//     * Redistributions in binary form must reproduce the above
111b362b15af34006e6a11974088a46d42b903418eJohann// copyright notice, this list of conditions and the following disclaimer
121b362b15af34006e6a11974088a46d42b903418eJohann// in the documentation and/or other materials provided with the
131b362b15af34006e6a11974088a46d42b903418eJohann// distribution.
141b362b15af34006e6a11974088a46d42b903418eJohann//     * Neither the name of Google Inc. nor the names of its
151b362b15af34006e6a11974088a46d42b903418eJohann// contributors may be used to endorse or promote products derived from
161b362b15af34006e6a11974088a46d42b903418eJohann// this software without specific prior written permission.
171b362b15af34006e6a11974088a46d42b903418eJohann//
181b362b15af34006e6a11974088a46d42b903418eJohann// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191b362b15af34006e6a11974088a46d42b903418eJohann// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201b362b15af34006e6a11974088a46d42b903418eJohann// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211b362b15af34006e6a11974088a46d42b903418eJohann// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221b362b15af34006e6a11974088a46d42b903418eJohann// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231b362b15af34006e6a11974088a46d42b903418eJohann// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241b362b15af34006e6a11974088a46d42b903418eJohann// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251b362b15af34006e6a11974088a46d42b903418eJohann// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261b362b15af34006e6a11974088a46d42b903418eJohann// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271b362b15af34006e6a11974088a46d42b903418eJohann// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281b362b15af34006e6a11974088a46d42b903418eJohann// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291b362b15af34006e6a11974088a46d42b903418eJohann//
301b362b15af34006e6a11974088a46d42b903418eJohann// Author: wan@google.com (Zhanyong Wan)
311b362b15af34006e6a11974088a46d42b903418eJohann//
321b362b15af34006e6a11974088a46d42b903418eJohann// Utilities for testing Google Test itself and code that uses Google Test
331b362b15af34006e6a11974088a46d42b903418eJohann// (e.g. frameworks built on top of Google Test).
341b362b15af34006e6a11974088a46d42b903418eJohann
351b362b15af34006e6a11974088a46d42b903418eJohann#ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_
361b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_INCLUDE_GTEST_GTEST_SPI_H_
371b362b15af34006e6a11974088a46d42b903418eJohann
381b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/gtest.h"
391b362b15af34006e6a11974088a46d42b903418eJohann
401b362b15af34006e6a11974088a46d42b903418eJohannnamespace testing {
411b362b15af34006e6a11974088a46d42b903418eJohann
421b362b15af34006e6a11974088a46d42b903418eJohann// This helper class can be used to mock out Google Test failure reporting
431b362b15af34006e6a11974088a46d42b903418eJohann// so that we can test Google Test or code that builds on Google Test.
441b362b15af34006e6a11974088a46d42b903418eJohann//
451b362b15af34006e6a11974088a46d42b903418eJohann// An object of this class appends a TestPartResult object to the
461b362b15af34006e6a11974088a46d42b903418eJohann// TestPartResultArray object given in the constructor whenever a Google Test
471b362b15af34006e6a11974088a46d42b903418eJohann// failure is reported. It can either intercept only failures that are
481b362b15af34006e6a11974088a46d42b903418eJohann// generated in the same thread that created this object or it can intercept
491b362b15af34006e6a11974088a46d42b903418eJohann// all generated failures. The scope of this mock object can be controlled with
501b362b15af34006e6a11974088a46d42b903418eJohann// the second argument to the two arguments constructor.
511b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ ScopedFakeTestPartResultReporter
521b362b15af34006e6a11974088a46d42b903418eJohann    : public TestPartResultReporterInterface {
531b362b15af34006e6a11974088a46d42b903418eJohann public:
541b362b15af34006e6a11974088a46d42b903418eJohann  // The two possible mocking modes of this object.
551b362b15af34006e6a11974088a46d42b903418eJohann  enum InterceptMode {
561b362b15af34006e6a11974088a46d42b903418eJohann    INTERCEPT_ONLY_CURRENT_THREAD,  // Intercepts only thread local failures.
571b362b15af34006e6a11974088a46d42b903418eJohann    INTERCEPT_ALL_THREADS           // Intercepts all failures.
581b362b15af34006e6a11974088a46d42b903418eJohann  };
591b362b15af34006e6a11974088a46d42b903418eJohann
601b362b15af34006e6a11974088a46d42b903418eJohann  // The c'tor sets this object as the test part result reporter used
611b362b15af34006e6a11974088a46d42b903418eJohann  // by Google Test.  The 'result' parameter specifies where to report the
621b362b15af34006e6a11974088a46d42b903418eJohann  // results. This reporter will only catch failures generated in the current
631b362b15af34006e6a11974088a46d42b903418eJohann  // thread. DEPRECATED
641b362b15af34006e6a11974088a46d42b903418eJohann  explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
651b362b15af34006e6a11974088a46d42b903418eJohann
661b362b15af34006e6a11974088a46d42b903418eJohann  // Same as above, but you can choose the interception scope of this object.
671b362b15af34006e6a11974088a46d42b903418eJohann  ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
681b362b15af34006e6a11974088a46d42b903418eJohann                                   TestPartResultArray* result);
691b362b15af34006e6a11974088a46d42b903418eJohann
701b362b15af34006e6a11974088a46d42b903418eJohann  // The d'tor restores the previous test part result reporter.
711b362b15af34006e6a11974088a46d42b903418eJohann  virtual ~ScopedFakeTestPartResultReporter();
721b362b15af34006e6a11974088a46d42b903418eJohann
731b362b15af34006e6a11974088a46d42b903418eJohann  // Appends the TestPartResult object to the TestPartResultArray
741b362b15af34006e6a11974088a46d42b903418eJohann  // received in the constructor.
751b362b15af34006e6a11974088a46d42b903418eJohann  //
761b362b15af34006e6a11974088a46d42b903418eJohann  // This method is from the TestPartResultReporterInterface
771b362b15af34006e6a11974088a46d42b903418eJohann  // interface.
781b362b15af34006e6a11974088a46d42b903418eJohann  virtual void ReportTestPartResult(const TestPartResult& result);
791b362b15af34006e6a11974088a46d42b903418eJohann private:
801b362b15af34006e6a11974088a46d42b903418eJohann  void Init();
811b362b15af34006e6a11974088a46d42b903418eJohann
821b362b15af34006e6a11974088a46d42b903418eJohann  const InterceptMode intercept_mode_;
831b362b15af34006e6a11974088a46d42b903418eJohann  TestPartResultReporterInterface* old_reporter_;
841b362b15af34006e6a11974088a46d42b903418eJohann  TestPartResultArray* const result_;
851b362b15af34006e6a11974088a46d42b903418eJohann
861b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);
871b362b15af34006e6a11974088a46d42b903418eJohann};
881b362b15af34006e6a11974088a46d42b903418eJohann
891b362b15af34006e6a11974088a46d42b903418eJohannnamespace internal {
901b362b15af34006e6a11974088a46d42b903418eJohann
911b362b15af34006e6a11974088a46d42b903418eJohann// A helper class for implementing EXPECT_FATAL_FAILURE() and
921b362b15af34006e6a11974088a46d42b903418eJohann// EXPECT_NONFATAL_FAILURE().  Its destructor verifies that the given
931b362b15af34006e6a11974088a46d42b903418eJohann// TestPartResultArray contains exactly one failure that has the given
941b362b15af34006e6a11974088a46d42b903418eJohann// type and contains the given substring.  If that's not the case, a
951b362b15af34006e6a11974088a46d42b903418eJohann// non-fatal failure will be generated.
961b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ SingleFailureChecker {
971b362b15af34006e6a11974088a46d42b903418eJohann public:
981b362b15af34006e6a11974088a46d42b903418eJohann  // The constructor remembers the arguments.
991b362b15af34006e6a11974088a46d42b903418eJohann  SingleFailureChecker(const TestPartResultArray* results,
1001b362b15af34006e6a11974088a46d42b903418eJohann                       TestPartResult::Type type,
1011b362b15af34006e6a11974088a46d42b903418eJohann                       const string& substr);
1021b362b15af34006e6a11974088a46d42b903418eJohann  ~SingleFailureChecker();
1031b362b15af34006e6a11974088a46d42b903418eJohann private:
1041b362b15af34006e6a11974088a46d42b903418eJohann  const TestPartResultArray* const results_;
1051b362b15af34006e6a11974088a46d42b903418eJohann  const TestPartResult::Type type_;
1061b362b15af34006e6a11974088a46d42b903418eJohann  const string substr_;
1071b362b15af34006e6a11974088a46d42b903418eJohann
1081b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
1091b362b15af34006e6a11974088a46d42b903418eJohann};
1101b362b15af34006e6a11974088a46d42b903418eJohann
1111b362b15af34006e6a11974088a46d42b903418eJohann}  // namespace internal
1121b362b15af34006e6a11974088a46d42b903418eJohann
1131b362b15af34006e6a11974088a46d42b903418eJohann}  // namespace testing
1141b362b15af34006e6a11974088a46d42b903418eJohann
1151b362b15af34006e6a11974088a46d42b903418eJohann// A set of macros for testing Google Test assertions or code that's expected
1161b362b15af34006e6a11974088a46d42b903418eJohann// to generate Google Test fatal failures.  It verifies that the given
1171b362b15af34006e6a11974088a46d42b903418eJohann// statement will cause exactly one fatal Google Test failure with 'substr'
1181b362b15af34006e6a11974088a46d42b903418eJohann// being part of the failure message.
1191b362b15af34006e6a11974088a46d42b903418eJohann//
1201b362b15af34006e6a11974088a46d42b903418eJohann// There are two different versions of this macro. EXPECT_FATAL_FAILURE only
1211b362b15af34006e6a11974088a46d42b903418eJohann// affects and considers failures generated in the current thread and
1221b362b15af34006e6a11974088a46d42b903418eJohann// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
1231b362b15af34006e6a11974088a46d42b903418eJohann//
1241b362b15af34006e6a11974088a46d42b903418eJohann// The verification of the assertion is done correctly even when the statement
1251b362b15af34006e6a11974088a46d42b903418eJohann// throws an exception or aborts the current function.
1261b362b15af34006e6a11974088a46d42b903418eJohann//
1271b362b15af34006e6a11974088a46d42b903418eJohann// Known restrictions:
1281b362b15af34006e6a11974088a46d42b903418eJohann//   - 'statement' cannot reference local non-static variables or
1291b362b15af34006e6a11974088a46d42b903418eJohann//     non-static members of the current object.
1301b362b15af34006e6a11974088a46d42b903418eJohann//   - 'statement' cannot return a value.
1311b362b15af34006e6a11974088a46d42b903418eJohann//   - You cannot stream a failure message to this macro.
1321b362b15af34006e6a11974088a46d42b903418eJohann//
1331b362b15af34006e6a11974088a46d42b903418eJohann// Note that even though the implementations of the following two
1341b362b15af34006e6a11974088a46d42b903418eJohann// macros are much alike, we cannot refactor them to use a common
1351b362b15af34006e6a11974088a46d42b903418eJohann// helper macro, due to some peculiarity in how the preprocessor
1361b362b15af34006e6a11974088a46d42b903418eJohann// works.  The AcceptsMacroThatExpandsToUnprotectedComma test in
1371b362b15af34006e6a11974088a46d42b903418eJohann// gtest_unittest.cc will fail to compile if we do that.
1381b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_FATAL_FAILURE(statement, substr) \
1391b362b15af34006e6a11974088a46d42b903418eJohann  do { \
1401b362b15af34006e6a11974088a46d42b903418eJohann    class GTestExpectFatalFailureHelper {\
1411b362b15af34006e6a11974088a46d42b903418eJohann     public:\
1421b362b15af34006e6a11974088a46d42b903418eJohann      static void Execute() { statement; }\
1431b362b15af34006e6a11974088a46d42b903418eJohann    };\
1441b362b15af34006e6a11974088a46d42b903418eJohann    ::testing::TestPartResultArray gtest_failures;\
1451b362b15af34006e6a11974088a46d42b903418eJohann    ::testing::internal::SingleFailureChecker gtest_checker(\
1461b362b15af34006e6a11974088a46d42b903418eJohann        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
1471b362b15af34006e6a11974088a46d42b903418eJohann    {\
1481b362b15af34006e6a11974088a46d42b903418eJohann      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
1491b362b15af34006e6a11974088a46d42b903418eJohann          ::testing::ScopedFakeTestPartResultReporter:: \
1501b362b15af34006e6a11974088a46d42b903418eJohann          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
1511b362b15af34006e6a11974088a46d42b903418eJohann      GTestExpectFatalFailureHelper::Execute();\
1521b362b15af34006e6a11974088a46d42b903418eJohann    }\
1531b362b15af34006e6a11974088a46d42b903418eJohann  } while (::testing::internal::AlwaysFalse())
1541b362b15af34006e6a11974088a46d42b903418eJohann
1551b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
1561b362b15af34006e6a11974088a46d42b903418eJohann  do { \
1571b362b15af34006e6a11974088a46d42b903418eJohann    class GTestExpectFatalFailureHelper {\
1581b362b15af34006e6a11974088a46d42b903418eJohann     public:\
1591b362b15af34006e6a11974088a46d42b903418eJohann      static void Execute() { statement; }\
1601b362b15af34006e6a11974088a46d42b903418eJohann    };\
1611b362b15af34006e6a11974088a46d42b903418eJohann    ::testing::TestPartResultArray gtest_failures;\
1621b362b15af34006e6a11974088a46d42b903418eJohann    ::testing::internal::SingleFailureChecker gtest_checker(\
1631b362b15af34006e6a11974088a46d42b903418eJohann        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
1641b362b15af34006e6a11974088a46d42b903418eJohann    {\
1651b362b15af34006e6a11974088a46d42b903418eJohann      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
1661b362b15af34006e6a11974088a46d42b903418eJohann          ::testing::ScopedFakeTestPartResultReporter:: \
1671b362b15af34006e6a11974088a46d42b903418eJohann          INTERCEPT_ALL_THREADS, &gtest_failures);\
1681b362b15af34006e6a11974088a46d42b903418eJohann      GTestExpectFatalFailureHelper::Execute();\
1691b362b15af34006e6a11974088a46d42b903418eJohann    }\
1701b362b15af34006e6a11974088a46d42b903418eJohann  } while (::testing::internal::AlwaysFalse())
1711b362b15af34006e6a11974088a46d42b903418eJohann
1721b362b15af34006e6a11974088a46d42b903418eJohann// A macro for testing Google Test assertions or code that's expected to
1731b362b15af34006e6a11974088a46d42b903418eJohann// generate Google Test non-fatal failures.  It asserts that the given
1741b362b15af34006e6a11974088a46d42b903418eJohann// statement will cause exactly one non-fatal Google Test failure with 'substr'
1751b362b15af34006e6a11974088a46d42b903418eJohann// being part of the failure message.
1761b362b15af34006e6a11974088a46d42b903418eJohann//
1771b362b15af34006e6a11974088a46d42b903418eJohann// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
1781b362b15af34006e6a11974088a46d42b903418eJohann// affects and considers failures generated in the current thread and
1791b362b15af34006e6a11974088a46d42b903418eJohann// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
1801b362b15af34006e6a11974088a46d42b903418eJohann//
1811b362b15af34006e6a11974088a46d42b903418eJohann// 'statement' is allowed to reference local variables and members of
1821b362b15af34006e6a11974088a46d42b903418eJohann// the current object.
1831b362b15af34006e6a11974088a46d42b903418eJohann//
1841b362b15af34006e6a11974088a46d42b903418eJohann// The verification of the assertion is done correctly even when the statement
1851b362b15af34006e6a11974088a46d42b903418eJohann// throws an exception or aborts the current function.
1861b362b15af34006e6a11974088a46d42b903418eJohann//
1871b362b15af34006e6a11974088a46d42b903418eJohann// Known restrictions:
1881b362b15af34006e6a11974088a46d42b903418eJohann//   - You cannot stream a failure message to this macro.
1891b362b15af34006e6a11974088a46d42b903418eJohann//
1901b362b15af34006e6a11974088a46d42b903418eJohann// Note that even though the implementations of the following two
1911b362b15af34006e6a11974088a46d42b903418eJohann// macros are much alike, we cannot refactor them to use a common
1921b362b15af34006e6a11974088a46d42b903418eJohann// helper macro, due to some peculiarity in how the preprocessor
1931b362b15af34006e6a11974088a46d42b903418eJohann// works.  If we do that, the code won't compile when the user gives
1941b362b15af34006e6a11974088a46d42b903418eJohann// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
1951b362b15af34006e6a11974088a46d42b903418eJohann// expands to code containing an unprotected comma.  The
1961b362b15af34006e6a11974088a46d42b903418eJohann// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
1971b362b15af34006e6a11974088a46d42b903418eJohann// catches that.
1981b362b15af34006e6a11974088a46d42b903418eJohann//
1991b362b15af34006e6a11974088a46d42b903418eJohann// For the same reason, we have to write
2001b362b15af34006e6a11974088a46d42b903418eJohann//   if (::testing::internal::AlwaysTrue()) { statement; }
2011b362b15af34006e6a11974088a46d42b903418eJohann// instead of
2021b362b15af34006e6a11974088a46d42b903418eJohann//   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
2031b362b15af34006e6a11974088a46d42b903418eJohann// to avoid an MSVC warning on unreachable code.
2041b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_NONFATAL_FAILURE(statement, substr) \
2051b362b15af34006e6a11974088a46d42b903418eJohann  do {\
2061b362b15af34006e6a11974088a46d42b903418eJohann    ::testing::TestPartResultArray gtest_failures;\
2071b362b15af34006e6a11974088a46d42b903418eJohann    ::testing::internal::SingleFailureChecker gtest_checker(\
2081b362b15af34006e6a11974088a46d42b903418eJohann        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
2091b362b15af34006e6a11974088a46d42b903418eJohann        (substr));\
2101b362b15af34006e6a11974088a46d42b903418eJohann    {\
2111b362b15af34006e6a11974088a46d42b903418eJohann      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
2121b362b15af34006e6a11974088a46d42b903418eJohann          ::testing::ScopedFakeTestPartResultReporter:: \
2131b362b15af34006e6a11974088a46d42b903418eJohann          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
2141b362b15af34006e6a11974088a46d42b903418eJohann      if (::testing::internal::AlwaysTrue()) { statement; }\
2151b362b15af34006e6a11974088a46d42b903418eJohann    }\
2161b362b15af34006e6a11974088a46d42b903418eJohann  } while (::testing::internal::AlwaysFalse())
2171b362b15af34006e6a11974088a46d42b903418eJohann
2181b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
2191b362b15af34006e6a11974088a46d42b903418eJohann  do {\
2201b362b15af34006e6a11974088a46d42b903418eJohann    ::testing::TestPartResultArray gtest_failures;\
2211b362b15af34006e6a11974088a46d42b903418eJohann    ::testing::internal::SingleFailureChecker gtest_checker(\
2221b362b15af34006e6a11974088a46d42b903418eJohann        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
2231b362b15af34006e6a11974088a46d42b903418eJohann        (substr));\
2241b362b15af34006e6a11974088a46d42b903418eJohann    {\
2251b362b15af34006e6a11974088a46d42b903418eJohann      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
2261b362b15af34006e6a11974088a46d42b903418eJohann          ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS,\
2271b362b15af34006e6a11974088a46d42b903418eJohann          &gtest_failures);\
2281b362b15af34006e6a11974088a46d42b903418eJohann      if (::testing::internal::AlwaysTrue()) { statement; }\
2291b362b15af34006e6a11974088a46d42b903418eJohann    }\
2301b362b15af34006e6a11974088a46d42b903418eJohann  } while (::testing::internal::AlwaysFalse())
2311b362b15af34006e6a11974088a46d42b903418eJohann
2321b362b15af34006e6a11974088a46d42b903418eJohann#endif  // GTEST_INCLUDE_GTEST_GTEST_SPI_H_
233