1848b8605Smrg// Copyright 2007, Google Inc. 2848b8605Smrg// All rights reserved. 3848b8605Smrg// 4848b8605Smrg// Redistribution and use in source and binary forms, with or without 5848b8605Smrg// modification, are permitted provided that the following conditions are 6848b8605Smrg// met: 7848b8605Smrg// 8848b8605Smrg// * Redistributions of source code must retain the above copyright 9848b8605Smrg// notice, this list of conditions and the following disclaimer. 10848b8605Smrg// * Redistributions in binary form must reproduce the above 11848b8605Smrg// copyright notice, this list of conditions and the following disclaimer 12848b8605Smrg// in the documentation and/or other materials provided with the 13848b8605Smrg// distribution. 14848b8605Smrg// * Neither the name of Google Inc. nor the names of its 15848b8605Smrg// contributors may be used to endorse or promote products derived from 16848b8605Smrg// this software without specific prior written permission. 17848b8605Smrg// 18848b8605Smrg// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19848b8605Smrg// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20848b8605Smrg// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21848b8605Smrg// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22848b8605Smrg// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23848b8605Smrg// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24848b8605Smrg// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25848b8605Smrg// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26848b8605Smrg// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27848b8605Smrg// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28848b8605Smrg// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29848b8605Smrg// 30848b8605Smrg// Author: wan@google.com (Zhanyong Wan) 31848b8605Smrg// 32848b8605Smrg// Utilities for testing Google Test itself and code that uses Google Test 33848b8605Smrg// (e.g. frameworks built on top of Google Test). 34848b8605Smrg 35848b8605Smrg#ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_ 36848b8605Smrg#define GTEST_INCLUDE_GTEST_GTEST_SPI_H_ 37848b8605Smrg 38848b8605Smrg#include "gtest/gtest.h" 39848b8605Smrg 40848b8605Smrgnamespace testing { 41848b8605Smrg 42848b8605Smrg// This helper class can be used to mock out Google Test failure reporting 43848b8605Smrg// so that we can test Google Test or code that builds on Google Test. 44848b8605Smrg// 45848b8605Smrg// An object of this class appends a TestPartResult object to the 46848b8605Smrg// TestPartResultArray object given in the constructor whenever a Google Test 47848b8605Smrg// failure is reported. It can either intercept only failures that are 48848b8605Smrg// generated in the same thread that created this object or it can intercept 49848b8605Smrg// all generated failures. The scope of this mock object can be controlled with 50848b8605Smrg// the second argument to the two arguments constructor. 51848b8605Smrgclass GTEST_API_ ScopedFakeTestPartResultReporter 52848b8605Smrg : public TestPartResultReporterInterface { 53848b8605Smrg public: 54848b8605Smrg // The two possible mocking modes of this object. 55848b8605Smrg enum InterceptMode { 56848b8605Smrg INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures. 57848b8605Smrg INTERCEPT_ALL_THREADS // Intercepts all failures. 58848b8605Smrg }; 59848b8605Smrg 60848b8605Smrg // The c'tor sets this object as the test part result reporter used 61848b8605Smrg // by Google Test. The 'result' parameter specifies where to report the 62848b8605Smrg // results. This reporter will only catch failures generated in the current 63848b8605Smrg // thread. DEPRECATED 64848b8605Smrg explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result); 65848b8605Smrg 66848b8605Smrg // Same as above, but you can choose the interception scope of this object. 67848b8605Smrg ScopedFakeTestPartResultReporter(InterceptMode intercept_mode, 68848b8605Smrg TestPartResultArray* result); 69848b8605Smrg 70848b8605Smrg // The d'tor restores the previous test part result reporter. 71848b8605Smrg virtual ~ScopedFakeTestPartResultReporter(); 72848b8605Smrg 73848b8605Smrg // Appends the TestPartResult object to the TestPartResultArray 74848b8605Smrg // received in the constructor. 75848b8605Smrg // 76848b8605Smrg // This method is from the TestPartResultReporterInterface 77848b8605Smrg // interface. 78848b8605Smrg virtual void ReportTestPartResult(const TestPartResult& result); 79848b8605Smrg private: 80848b8605Smrg void Init(); 81848b8605Smrg 82848b8605Smrg const InterceptMode intercept_mode_; 83848b8605Smrg TestPartResultReporterInterface* old_reporter_; 84848b8605Smrg TestPartResultArray* const result_; 85848b8605Smrg 86848b8605Smrg GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter); 87848b8605Smrg}; 88848b8605Smrg 89848b8605Smrgnamespace internal { 90848b8605Smrg 91848b8605Smrg// A helper class for implementing EXPECT_FATAL_FAILURE() and 92848b8605Smrg// EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given 93848b8605Smrg// TestPartResultArray contains exactly one failure that has the given 94848b8605Smrg// type and contains the given substring. If that's not the case, a 95848b8605Smrg// non-fatal failure will be generated. 96848b8605Smrgclass GTEST_API_ SingleFailureChecker { 97848b8605Smrg public: 98848b8605Smrg // The constructor remembers the arguments. 99848b8605Smrg SingleFailureChecker(const TestPartResultArray* results, 100848b8605Smrg TestPartResult::Type type, 101848b8605Smrg const string& substr); 102848b8605Smrg ~SingleFailureChecker(); 103848b8605Smrg private: 104848b8605Smrg const TestPartResultArray* const results_; 105848b8605Smrg const TestPartResult::Type type_; 106848b8605Smrg const string substr_; 107848b8605Smrg 108848b8605Smrg GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker); 109848b8605Smrg}; 110848b8605Smrg 111848b8605Smrg} // namespace internal 112848b8605Smrg 113848b8605Smrg} // namespace testing 114848b8605Smrg 115848b8605Smrg// A set of macros for testing Google Test assertions or code that's expected 116848b8605Smrg// to generate Google Test fatal failures. It verifies that the given 117848b8605Smrg// statement will cause exactly one fatal Google Test failure with 'substr' 118848b8605Smrg// being part of the failure message. 119848b8605Smrg// 120848b8605Smrg// There are two different versions of this macro. EXPECT_FATAL_FAILURE only 121848b8605Smrg// affects and considers failures generated in the current thread and 122848b8605Smrg// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 123848b8605Smrg// 124848b8605Smrg// The verification of the assertion is done correctly even when the statement 125848b8605Smrg// throws an exception or aborts the current function. 126848b8605Smrg// 127848b8605Smrg// Known restrictions: 128848b8605Smrg// - 'statement' cannot reference local non-static variables or 129848b8605Smrg// non-static members of the current object. 130848b8605Smrg// - 'statement' cannot return a value. 131848b8605Smrg// - You cannot stream a failure message to this macro. 132848b8605Smrg// 133848b8605Smrg// Note that even though the implementations of the following two 134848b8605Smrg// macros are much alike, we cannot refactor them to use a common 135848b8605Smrg// helper macro, due to some peculiarity in how the preprocessor 136848b8605Smrg// works. The AcceptsMacroThatExpandsToUnprotectedComma test in 137848b8605Smrg// gtest_unittest.cc will fail to compile if we do that. 138848b8605Smrg#define EXPECT_FATAL_FAILURE(statement, substr) \ 139848b8605Smrg do { \ 140848b8605Smrg class GTestExpectFatalFailureHelper {\ 141848b8605Smrg public:\ 142848b8605Smrg static void Execute() { statement; }\ 143848b8605Smrg };\ 144848b8605Smrg ::testing::TestPartResultArray gtest_failures;\ 145848b8605Smrg ::testing::internal::SingleFailureChecker gtest_checker(\ 146848b8605Smrg >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ 147848b8605Smrg {\ 148848b8605Smrg ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 149848b8605Smrg ::testing::ScopedFakeTestPartResultReporter:: \ 150848b8605Smrg INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ 151848b8605Smrg GTestExpectFatalFailureHelper::Execute();\ 152848b8605Smrg }\ 153848b8605Smrg } while (::testing::internal::AlwaysFalse()) 154848b8605Smrg 155848b8605Smrg#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 156848b8605Smrg do { \ 157848b8605Smrg class GTestExpectFatalFailureHelper {\ 158848b8605Smrg public:\ 159848b8605Smrg static void Execute() { statement; }\ 160848b8605Smrg };\ 161848b8605Smrg ::testing::TestPartResultArray gtest_failures;\ 162848b8605Smrg ::testing::internal::SingleFailureChecker gtest_checker(\ 163848b8605Smrg >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ 164848b8605Smrg {\ 165848b8605Smrg ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 166848b8605Smrg ::testing::ScopedFakeTestPartResultReporter:: \ 167848b8605Smrg INTERCEPT_ALL_THREADS, >est_failures);\ 168848b8605Smrg GTestExpectFatalFailureHelper::Execute();\ 169848b8605Smrg }\ 170848b8605Smrg } while (::testing::internal::AlwaysFalse()) 171848b8605Smrg 172848b8605Smrg// A macro for testing Google Test assertions or code that's expected to 173848b8605Smrg// generate Google Test non-fatal failures. It asserts that the given 174848b8605Smrg// statement will cause exactly one non-fatal Google Test failure with 'substr' 175848b8605Smrg// being part of the failure message. 176848b8605Smrg// 177848b8605Smrg// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only 178848b8605Smrg// affects and considers failures generated in the current thread and 179848b8605Smrg// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. 180848b8605Smrg// 181848b8605Smrg// 'statement' is allowed to reference local variables and members of 182848b8605Smrg// the current object. 183848b8605Smrg// 184848b8605Smrg// The verification of the assertion is done correctly even when the statement 185848b8605Smrg// throws an exception or aborts the current function. 186848b8605Smrg// 187848b8605Smrg// Known restrictions: 188848b8605Smrg// - You cannot stream a failure message to this macro. 189848b8605Smrg// 190848b8605Smrg// Note that even though the implementations of the following two 191848b8605Smrg// macros are much alike, we cannot refactor them to use a common 192848b8605Smrg// helper macro, due to some peculiarity in how the preprocessor 193848b8605Smrg// works. If we do that, the code won't compile when the user gives 194848b8605Smrg// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that 195848b8605Smrg// expands to code containing an unprotected comma. The 196848b8605Smrg// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc 197848b8605Smrg// catches that. 198848b8605Smrg// 199848b8605Smrg// For the same reason, we have to write 200848b8605Smrg// if (::testing::internal::AlwaysTrue()) { statement; } 201848b8605Smrg// instead of 202848b8605Smrg// GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) 203848b8605Smrg// to avoid an MSVC warning on unreachable code. 204848b8605Smrg#define EXPECT_NONFATAL_FAILURE(statement, substr) \ 205848b8605Smrg do {\ 206848b8605Smrg ::testing::TestPartResultArray gtest_failures;\ 207848b8605Smrg ::testing::internal::SingleFailureChecker gtest_checker(\ 208848b8605Smrg >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 209848b8605Smrg (substr));\ 210848b8605Smrg {\ 211848b8605Smrg ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 212848b8605Smrg ::testing::ScopedFakeTestPartResultReporter:: \ 213848b8605Smrg INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ 214848b8605Smrg if (::testing::internal::AlwaysTrue()) { statement; }\ 215848b8605Smrg }\ 216848b8605Smrg } while (::testing::internal::AlwaysFalse()) 217848b8605Smrg 218848b8605Smrg#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ 219848b8605Smrg do {\ 220848b8605Smrg ::testing::TestPartResultArray gtest_failures;\ 221848b8605Smrg ::testing::internal::SingleFailureChecker gtest_checker(\ 222848b8605Smrg >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ 223848b8605Smrg (substr));\ 224848b8605Smrg {\ 225848b8605Smrg ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ 226848b8605Smrg ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \ 227848b8605Smrg >est_failures);\ 228848b8605Smrg if (::testing::internal::AlwaysTrue()) { statement; }\ 229848b8605Smrg }\ 230848b8605Smrg } while (::testing::internal::AlwaysFalse()) 231848b8605Smrg 232848b8605Smrg#endif // GTEST_INCLUDE_GTEST_GTEST_SPI_H_ 233