113481Sgiacomo.travaglini@arm.com// Copyright 2007, Google Inc.
213481Sgiacomo.travaglini@arm.com// All rights reserved.
313481Sgiacomo.travaglini@arm.com//
413481Sgiacomo.travaglini@arm.com// Redistribution and use in source and binary forms, with or without
513481Sgiacomo.travaglini@arm.com// modification, are permitted provided that the following conditions are
613481Sgiacomo.travaglini@arm.com// met:
713481Sgiacomo.travaglini@arm.com//
813481Sgiacomo.travaglini@arm.com//     * Redistributions of source code must retain the above copyright
913481Sgiacomo.travaglini@arm.com// notice, this list of conditions and the following disclaimer.
1013481Sgiacomo.travaglini@arm.com//     * Redistributions in binary form must reproduce the above
1113481Sgiacomo.travaglini@arm.com// copyright notice, this list of conditions and the following disclaimer
1213481Sgiacomo.travaglini@arm.com// in the documentation and/or other materials provided with the
1313481Sgiacomo.travaglini@arm.com// distribution.
1413481Sgiacomo.travaglini@arm.com//     * Neither the name of Google Inc. nor the names of its
1513481Sgiacomo.travaglini@arm.com// contributors may be used to endorse or promote products derived from
1613481Sgiacomo.travaglini@arm.com// this software without specific prior written permission.
1713481Sgiacomo.travaglini@arm.com//
1813481Sgiacomo.travaglini@arm.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1913481Sgiacomo.travaglini@arm.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2013481Sgiacomo.travaglini@arm.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2113481Sgiacomo.travaglini@arm.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2213481Sgiacomo.travaglini@arm.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2313481Sgiacomo.travaglini@arm.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2413481Sgiacomo.travaglini@arm.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2513481Sgiacomo.travaglini@arm.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2613481Sgiacomo.travaglini@arm.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2713481Sgiacomo.travaglini@arm.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2813481Sgiacomo.travaglini@arm.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2913481Sgiacomo.travaglini@arm.com//
3013481Sgiacomo.travaglini@arm.com// Author: wan@google.com (Zhanyong Wan)
3113481Sgiacomo.travaglini@arm.com//
3213481Sgiacomo.travaglini@arm.com// Utilities for testing Google Test itself and code that uses Google Test
3313481Sgiacomo.travaglini@arm.com// (e.g. frameworks built on top of Google Test).
3413481Sgiacomo.travaglini@arm.com
3513481Sgiacomo.travaglini@arm.com#ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_
3613481Sgiacomo.travaglini@arm.com#define GTEST_INCLUDE_GTEST_GTEST_SPI_H_
3713481Sgiacomo.travaglini@arm.com
3813481Sgiacomo.travaglini@arm.com#include "gtest/gtest.h"
3913481Sgiacomo.travaglini@arm.com
4013481Sgiacomo.travaglini@arm.comnamespace testing {
4113481Sgiacomo.travaglini@arm.com
4213481Sgiacomo.travaglini@arm.com// This helper class can be used to mock out Google Test failure reporting
4313481Sgiacomo.travaglini@arm.com// so that we can test Google Test or code that builds on Google Test.
4413481Sgiacomo.travaglini@arm.com//
4513481Sgiacomo.travaglini@arm.com// An object of this class appends a TestPartResult object to the
4613481Sgiacomo.travaglini@arm.com// TestPartResultArray object given in the constructor whenever a Google Test
4713481Sgiacomo.travaglini@arm.com// failure is reported. It can either intercept only failures that are
4813481Sgiacomo.travaglini@arm.com// generated in the same thread that created this object or it can intercept
4913481Sgiacomo.travaglini@arm.com// all generated failures. The scope of this mock object can be controlled with
5013481Sgiacomo.travaglini@arm.com// the second argument to the two arguments constructor.
5113481Sgiacomo.travaglini@arm.comclass GTEST_API_ ScopedFakeTestPartResultReporter
5213481Sgiacomo.travaglini@arm.com    : public TestPartResultReporterInterface {
5313481Sgiacomo.travaglini@arm.com public:
5413481Sgiacomo.travaglini@arm.com  // The two possible mocking modes of this object.
5513481Sgiacomo.travaglini@arm.com  enum InterceptMode {
5613481Sgiacomo.travaglini@arm.com    INTERCEPT_ONLY_CURRENT_THREAD,  // Intercepts only thread local failures.
5713481Sgiacomo.travaglini@arm.com    INTERCEPT_ALL_THREADS           // Intercepts all failures.
5813481Sgiacomo.travaglini@arm.com  };
5913481Sgiacomo.travaglini@arm.com
6013481Sgiacomo.travaglini@arm.com  // The c'tor sets this object as the test part result reporter used
6113481Sgiacomo.travaglini@arm.com  // by Google Test.  The 'result' parameter specifies where to report the
6213481Sgiacomo.travaglini@arm.com  // results. This reporter will only catch failures generated in the current
6313481Sgiacomo.travaglini@arm.com  // thread. DEPRECATED
6413481Sgiacomo.travaglini@arm.com  explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
6513481Sgiacomo.travaglini@arm.com
6613481Sgiacomo.travaglini@arm.com  // Same as above, but you can choose the interception scope of this object.
6713481Sgiacomo.travaglini@arm.com  ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
6813481Sgiacomo.travaglini@arm.com                                   TestPartResultArray* result);
6913481Sgiacomo.travaglini@arm.com
7013481Sgiacomo.travaglini@arm.com  // The d'tor restores the previous test part result reporter.
7113481Sgiacomo.travaglini@arm.com  virtual ~ScopedFakeTestPartResultReporter();
7213481Sgiacomo.travaglini@arm.com
7313481Sgiacomo.travaglini@arm.com  // Appends the TestPartResult object to the TestPartResultArray
7413481Sgiacomo.travaglini@arm.com  // received in the constructor.
7513481Sgiacomo.travaglini@arm.com  //
7613481Sgiacomo.travaglini@arm.com  // This method is from the TestPartResultReporterInterface
7713481Sgiacomo.travaglini@arm.com  // interface.
7813481Sgiacomo.travaglini@arm.com  virtual void ReportTestPartResult(const TestPartResult& result);
7913481Sgiacomo.travaglini@arm.com private:
8013481Sgiacomo.travaglini@arm.com  void Init();
8113481Sgiacomo.travaglini@arm.com
8213481Sgiacomo.travaglini@arm.com  const InterceptMode intercept_mode_;
8313481Sgiacomo.travaglini@arm.com  TestPartResultReporterInterface* old_reporter_;
8413481Sgiacomo.travaglini@arm.com  TestPartResultArray* const result_;
8513481Sgiacomo.travaglini@arm.com
8613481Sgiacomo.travaglini@arm.com  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);
8713481Sgiacomo.travaglini@arm.com};
8813481Sgiacomo.travaglini@arm.com
8913481Sgiacomo.travaglini@arm.comnamespace internal {
9013481Sgiacomo.travaglini@arm.com
9113481Sgiacomo.travaglini@arm.com// A helper class for implementing EXPECT_FATAL_FAILURE() and
9213481Sgiacomo.travaglini@arm.com// EXPECT_NONFATAL_FAILURE().  Its destructor verifies that the given
9313481Sgiacomo.travaglini@arm.com// TestPartResultArray contains exactly one failure that has the given
9413481Sgiacomo.travaglini@arm.com// type and contains the given substring.  If that's not the case, a
9513481Sgiacomo.travaglini@arm.com// non-fatal failure will be generated.
9613481Sgiacomo.travaglini@arm.comclass GTEST_API_ SingleFailureChecker {
9713481Sgiacomo.travaglini@arm.com public:
9813481Sgiacomo.travaglini@arm.com  // The constructor remembers the arguments.
9913481Sgiacomo.travaglini@arm.com  SingleFailureChecker(const TestPartResultArray* results,
10013481Sgiacomo.travaglini@arm.com                       TestPartResult::Type type,
10113481Sgiacomo.travaglini@arm.com                       const string& substr);
10213481Sgiacomo.travaglini@arm.com  ~SingleFailureChecker();
10313481Sgiacomo.travaglini@arm.com private:
10413481Sgiacomo.travaglini@arm.com  const TestPartResultArray* const results_;
10513481Sgiacomo.travaglini@arm.com  const TestPartResult::Type type_;
10613481Sgiacomo.travaglini@arm.com  const string substr_;
10713481Sgiacomo.travaglini@arm.com
10813481Sgiacomo.travaglini@arm.com  GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
10913481Sgiacomo.travaglini@arm.com};
11013481Sgiacomo.travaglini@arm.com
11113481Sgiacomo.travaglini@arm.com}  // namespace internal
11213481Sgiacomo.travaglini@arm.com
11313481Sgiacomo.travaglini@arm.com}  // namespace testing
11413481Sgiacomo.travaglini@arm.com
11513481Sgiacomo.travaglini@arm.com// A set of macros for testing Google Test assertions or code that's expected
11613481Sgiacomo.travaglini@arm.com// to generate Google Test fatal failures.  It verifies that the given
11713481Sgiacomo.travaglini@arm.com// statement will cause exactly one fatal Google Test failure with 'substr'
11813481Sgiacomo.travaglini@arm.com// being part of the failure message.
11913481Sgiacomo.travaglini@arm.com//
12013481Sgiacomo.travaglini@arm.com// There are two different versions of this macro. EXPECT_FATAL_FAILURE only
12113481Sgiacomo.travaglini@arm.com// affects and considers failures generated in the current thread and
12213481Sgiacomo.travaglini@arm.com// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
12313481Sgiacomo.travaglini@arm.com//
12413481Sgiacomo.travaglini@arm.com// The verification of the assertion is done correctly even when the statement
12513481Sgiacomo.travaglini@arm.com// throws an exception or aborts the current function.
12613481Sgiacomo.travaglini@arm.com//
12713481Sgiacomo.travaglini@arm.com// Known restrictions:
12813481Sgiacomo.travaglini@arm.com//   - 'statement' cannot reference local non-static variables or
12913481Sgiacomo.travaglini@arm.com//     non-static members of the current object.
13013481Sgiacomo.travaglini@arm.com//   - 'statement' cannot return a value.
13113481Sgiacomo.travaglini@arm.com//   - You cannot stream a failure message to this macro.
13213481Sgiacomo.travaglini@arm.com//
13313481Sgiacomo.travaglini@arm.com// Note that even though the implementations of the following two
13413481Sgiacomo.travaglini@arm.com// macros are much alike, we cannot refactor them to use a common
13513481Sgiacomo.travaglini@arm.com// helper macro, due to some peculiarity in how the preprocessor
13613481Sgiacomo.travaglini@arm.com// works.  The AcceptsMacroThatExpandsToUnprotectedComma test in
13713481Sgiacomo.travaglini@arm.com// gtest_unittest.cc will fail to compile if we do that.
13813481Sgiacomo.travaglini@arm.com#define EXPECT_FATAL_FAILURE(statement, substr) \
13913481Sgiacomo.travaglini@arm.com  do { \
14013481Sgiacomo.travaglini@arm.com    class GTestExpectFatalFailureHelper {\
14113481Sgiacomo.travaglini@arm.com     public:\
14213481Sgiacomo.travaglini@arm.com      static void Execute() { statement; }\
14313481Sgiacomo.travaglini@arm.com    };\
14413481Sgiacomo.travaglini@arm.com    ::testing::TestPartResultArray gtest_failures;\
14513481Sgiacomo.travaglini@arm.com    ::testing::internal::SingleFailureChecker gtest_checker(\
14613481Sgiacomo.travaglini@arm.com        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
14713481Sgiacomo.travaglini@arm.com    {\
14813481Sgiacomo.travaglini@arm.com      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
14913481Sgiacomo.travaglini@arm.com          ::testing::ScopedFakeTestPartResultReporter:: \
15013481Sgiacomo.travaglini@arm.com          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
15113481Sgiacomo.travaglini@arm.com      GTestExpectFatalFailureHelper::Execute();\
15213481Sgiacomo.travaglini@arm.com    }\
15313481Sgiacomo.travaglini@arm.com  } while (::testing::internal::AlwaysFalse())
15413481Sgiacomo.travaglini@arm.com
15513481Sgiacomo.travaglini@arm.com#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
15613481Sgiacomo.travaglini@arm.com  do { \
15713481Sgiacomo.travaglini@arm.com    class GTestExpectFatalFailureHelper {\
15813481Sgiacomo.travaglini@arm.com     public:\
15913481Sgiacomo.travaglini@arm.com      static void Execute() { statement; }\
16013481Sgiacomo.travaglini@arm.com    };\
16113481Sgiacomo.travaglini@arm.com    ::testing::TestPartResultArray gtest_failures;\
16213481Sgiacomo.travaglini@arm.com    ::testing::internal::SingleFailureChecker gtest_checker(\
16313481Sgiacomo.travaglini@arm.com        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
16413481Sgiacomo.travaglini@arm.com    {\
16513481Sgiacomo.travaglini@arm.com      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
16613481Sgiacomo.travaglini@arm.com          ::testing::ScopedFakeTestPartResultReporter:: \
16713481Sgiacomo.travaglini@arm.com          INTERCEPT_ALL_THREADS, &gtest_failures);\
16813481Sgiacomo.travaglini@arm.com      GTestExpectFatalFailureHelper::Execute();\
16913481Sgiacomo.travaglini@arm.com    }\
17013481Sgiacomo.travaglini@arm.com  } while (::testing::internal::AlwaysFalse())
17113481Sgiacomo.travaglini@arm.com
17213481Sgiacomo.travaglini@arm.com// A macro for testing Google Test assertions or code that's expected to
17313481Sgiacomo.travaglini@arm.com// generate Google Test non-fatal failures.  It asserts that the given
17413481Sgiacomo.travaglini@arm.com// statement will cause exactly one non-fatal Google Test failure with 'substr'
17513481Sgiacomo.travaglini@arm.com// being part of the failure message.
17613481Sgiacomo.travaglini@arm.com//
17713481Sgiacomo.travaglini@arm.com// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
17813481Sgiacomo.travaglini@arm.com// affects and considers failures generated in the current thread and
17913481Sgiacomo.travaglini@arm.com// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
18013481Sgiacomo.travaglini@arm.com//
18113481Sgiacomo.travaglini@arm.com// 'statement' is allowed to reference local variables and members of
18213481Sgiacomo.travaglini@arm.com// the current object.
18313481Sgiacomo.travaglini@arm.com//
18413481Sgiacomo.travaglini@arm.com// The verification of the assertion is done correctly even when the statement
18513481Sgiacomo.travaglini@arm.com// throws an exception or aborts the current function.
18613481Sgiacomo.travaglini@arm.com//
18713481Sgiacomo.travaglini@arm.com// Known restrictions:
18813481Sgiacomo.travaglini@arm.com//   - You cannot stream a failure message to this macro.
18913481Sgiacomo.travaglini@arm.com//
19013481Sgiacomo.travaglini@arm.com// Note that even though the implementations of the following two
19113481Sgiacomo.travaglini@arm.com// macros are much alike, we cannot refactor them to use a common
19213481Sgiacomo.travaglini@arm.com// helper macro, due to some peculiarity in how the preprocessor
19313481Sgiacomo.travaglini@arm.com// works.  If we do that, the code won't compile when the user gives
19413481Sgiacomo.travaglini@arm.com// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
19513481Sgiacomo.travaglini@arm.com// expands to code containing an unprotected comma.  The
19613481Sgiacomo.travaglini@arm.com// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
19713481Sgiacomo.travaglini@arm.com// catches that.
19813481Sgiacomo.travaglini@arm.com//
19913481Sgiacomo.travaglini@arm.com// For the same reason, we have to write
20013481Sgiacomo.travaglini@arm.com//   if (::testing::internal::AlwaysTrue()) { statement; }
20113481Sgiacomo.travaglini@arm.com// instead of
20213481Sgiacomo.travaglini@arm.com//   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
20313481Sgiacomo.travaglini@arm.com// to avoid an MSVC warning on unreachable code.
20413481Sgiacomo.travaglini@arm.com#define EXPECT_NONFATAL_FAILURE(statement, substr) \
20513481Sgiacomo.travaglini@arm.com  do {\
20613481Sgiacomo.travaglini@arm.com    ::testing::TestPartResultArray gtest_failures;\
20713481Sgiacomo.travaglini@arm.com    ::testing::internal::SingleFailureChecker gtest_checker(\
20813481Sgiacomo.travaglini@arm.com        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
20913481Sgiacomo.travaglini@arm.com        (substr));\
21013481Sgiacomo.travaglini@arm.com    {\
21113481Sgiacomo.travaglini@arm.com      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
21213481Sgiacomo.travaglini@arm.com          ::testing::ScopedFakeTestPartResultReporter:: \
21313481Sgiacomo.travaglini@arm.com          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
21413481Sgiacomo.travaglini@arm.com      if (::testing::internal::AlwaysTrue()) { statement; }\
21513481Sgiacomo.travaglini@arm.com    }\
21613481Sgiacomo.travaglini@arm.com  } while (::testing::internal::AlwaysFalse())
21713481Sgiacomo.travaglini@arm.com
21813481Sgiacomo.travaglini@arm.com#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
21913481Sgiacomo.travaglini@arm.com  do {\
22013481Sgiacomo.travaglini@arm.com    ::testing::TestPartResultArray gtest_failures;\
22113481Sgiacomo.travaglini@arm.com    ::testing::internal::SingleFailureChecker gtest_checker(\
22213481Sgiacomo.travaglini@arm.com        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
22313481Sgiacomo.travaglini@arm.com        (substr));\
22413481Sgiacomo.travaglini@arm.com    {\
22513481Sgiacomo.travaglini@arm.com      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
22613481Sgiacomo.travaglini@arm.com          ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
22713481Sgiacomo.travaglini@arm.com          &gtest_failures);\
22813481Sgiacomo.travaglini@arm.com      if (::testing::internal::AlwaysTrue()) { statement; }\
22913481Sgiacomo.travaglini@arm.com    }\
23013481Sgiacomo.travaglini@arm.com  } while (::testing::internal::AlwaysFalse())
23113481Sgiacomo.travaglini@arm.com
23213481Sgiacomo.travaglini@arm.com#endif  // GTEST_INCLUDE_GTEST_GTEST_SPI_H_
233