1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2005, Google Inc. 2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// All rights reserved. 3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without 5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are 6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met: 7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions of source code must retain the above copyright 9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer. 10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions in binary form must reproduce the above 11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer 12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the 13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution. 14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Neither the name of Google Inc. nor the names of its 15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from 16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission. 17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee) 31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The Google C++ Testing Framework (Google Test) 33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This header file defines internal utilities needed for implementing 35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// death tests. They are subject to change without notice. 36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ 38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ 39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/internal/gtest-internal.h> 41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace testing { 43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace internal { 44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleGTEST_DECLARE_string_(internal_run_death_test); 46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Names of the flags (needed for parsing Google Test flags). 48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kDeathTestStyleFlag[] = "death_test_style"; 49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kDeathTestUseFork[] = "death_test_use_fork"; 50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kInternalRunDeathTestFlag[] = "internal_run_death_test"; 51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 52fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_HAS_DEATH_TEST 53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DeathTest is a class that hides much of the complexity of the 55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// GTEST_DEATH_TEST_ macro. It is abstract; its static Create method 56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// returns a concrete class that depends on the prevailing death test 57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// style, as defined by the --gtest_death_test_style and/or 58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// --gtest_internal_run_death_test flags. 59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// In describing the results of death tests, these terms are used with 61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// the corresponding definitions: 62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// exit status: The integer exit information in the format specified 64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// by wait(2) 65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// exit code: The integer code passed to exit(3), _exit(2), or 66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// returned from main() 67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass DeathTest { 68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public: 69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Create returns false if there was an error determining the 70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // appropriate action to take for the current death test; for example, 71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // if the gtest_death_test_style flag is set to an invalid value. 72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // The LastMessage method will return a more detailed message in that 73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // case. Otherwise, the DeathTest pointer pointed to by the "test" 74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // argument is set. If the death test should be skipped, the pointer 75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // is set to NULL; otherwise, it is set to the address of a new concrete 76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // DeathTest object that controls the execution of the current test. 77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville static bool Create(const char* statement, const RE* regex, 78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const char* file, int line, DeathTest** test); 79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville DeathTest(); 80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville virtual ~DeathTest() { } 81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // A helper class that aborts a death test when it's deleted. 83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville class ReturnSentinel { 84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public: 85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville explicit ReturnSentinel(DeathTest* test) : test_(test) { } 86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); } 87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private: 88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville DeathTest* const test_; 89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel); 90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } GTEST_ATTRIBUTE_UNUSED_; 91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // An enumeration of possible roles that may be taken when a death 93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // test is encountered. EXECUTE means that the death test logic should 94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // be executed immediately. OVERSEE means that the program should prepare 95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // the appropriate environment for a child process to execute the death 96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // test, then wait for it to complete. 97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville enum TestRole { OVERSEE_TEST, EXECUTE_TEST }; 98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // An enumeration of the two reasons that a test might be aborted. 100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville enum AbortReason { TEST_ENCOUNTERED_RETURN_STATEMENT, TEST_DID_NOT_DIE }; 101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Assumes one of the above roles. 103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville virtual TestRole AssumeRole() = 0; 104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Waits for the death test to finish and returns its status. 106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville virtual int Wait() = 0; 107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Returns true if the death test passed; that is, the test process 109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // exited during the test, its exit status matches a user-supplied 110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // predicate, and its stderr output matches a user-supplied regular 111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // expression. 112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // The user-supplied predicate may be a macro expression rather 113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // than a function pointer or functor, or else Wait and Passed could 114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // be combined. 115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville virtual bool Passed(bool exit_status_ok) = 0; 116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Signals that the death test did not die as expected. 118fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville virtual void Abort(AbortReason reason) = 0; 119fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Returns a human-readable outcome message regarding the outcome of 121fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // the last death test. 122fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville static const char* LastMessage(); 123fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 124fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville static void set_last_death_test_message(const String& message); 125fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 126fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private: 127fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // A string containing a description of the outcome of the last death test. 128fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville static String last_death_test_message_; 129fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 130fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest); 131fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}; 132fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 133fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Factory interface for death tests. May be mocked out for testing. 134fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass DeathTestFactory { 135fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public: 136fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville virtual ~DeathTestFactory() { } 137fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville virtual bool Create(const char* statement, const RE* regex, 138fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const char* file, int line, DeathTest** test) = 0; 139fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}; 140fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 141fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A concrete DeathTestFactory implementation for normal use. 142fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass DefaultDeathTestFactory : public DeathTestFactory { 143fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public: 144fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville virtual bool Create(const char* statement, const RE* regex, 145fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const char* file, int line, DeathTest** test); 146fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}; 147fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 148fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns true if exit_status describes a process that was terminated 149fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// by a signal, or exited normally with a nonzero exit code. 150fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillebool ExitedUnsuccessfully(int exit_status); 151fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 152fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*, 153fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// ASSERT_EXIT*, and EXPECT_EXIT*. 154fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \ 155fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 156d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if (::testing::internal::AlwaysTrue()) { \ 157fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const ::testing::internal::RE& gtest_regex = (regex); \ 158fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville ::testing::internal::DeathTest* gtest_dt; \ 159fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville if (!::testing::internal::DeathTest::Create(#statement, >est_regex, \ 160fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville __FILE__, __LINE__, >est_dt)) { \ 161fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ 162fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } \ 163fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville if (gtest_dt != NULL) { \ 164fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \ 165fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville gtest_dt_ptr(gtest_dt); \ 166fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville switch (gtest_dt->AssumeRole()) { \ 167fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville case ::testing::internal::DeathTest::OVERSEE_TEST: \ 168fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \ 169fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ 170fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } \ 171fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville break; \ 172fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville case ::testing::internal::DeathTest::EXECUTE_TEST: { \ 173fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville ::testing::internal::DeathTest::ReturnSentinel \ 174fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville gtest_sentinel(gtest_dt); \ 175d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 176fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \ 177fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville break; \ 178fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } \ 179fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } \ 180fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } \ 181fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } else \ 182fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \ 183fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville fail(::testing::internal::DeathTest::LastMessage()) 184fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The symbol "fail" here expands to something into which a message 185fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// can be streamed. 186fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 187fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A class representing the parsed contents of the 188fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// --gtest_internal_run_death_test flag, as it existed when 189fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// RUN_ALL_TESTS was called. 190fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass InternalRunDeathTestFlag { 191fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public: 192fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville InternalRunDeathTestFlag(const String& file, 193fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville int line, 194fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville int index, 195fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville int write_fd) 196fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville : file_(file), line_(line), index_(index), write_fd_(write_fd) {} 197fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 198fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville ~InternalRunDeathTestFlag() { 199fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville if (write_fd_ >= 0) 200fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville posix::Close(write_fd_); 201fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 202fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 203fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville String file() const { return file_; } 204fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville int line() const { return line_; } 205fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville int index() const { return index_; } 206fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville int write_fd() const { return write_fd_; } 207fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 208fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private: 209fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville String file_; 210fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville int line_; 211fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville int index_; 212fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville int write_fd_; 213fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 214fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag); 215fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}; 216fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 217fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns a newly created InternalRunDeathTestFlag object with fields 218fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// initialized from the GTEST_FLAG(internal_run_death_test) flag if 219fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// the flag is specified; otherwise returns NULL. 220fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleInternalRunDeathTestFlag* ParseInternalRunDeathTestFlag(); 221fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 222d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville#else // GTEST_HAS_DEATH_TEST 223d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 224d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// This macro is used for implementing macros such as 225d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where 226d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// death tests are not supported. Those macros must compile on such systems 227d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// iff EXPECT_DEATH and ASSERT_DEATH compile with the same parameters on 228d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// systems that support death tests. This allows one to write such a macro 229d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// on a system that does not support death tests and be sure that it will 230d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// compile on a death-test supporting system. 231d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// 232d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Parameters: 233d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// statement - A statement that a macro such as EXPECT_DEATH would test 234d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// for program termination. This macro has to make sure this 235d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// statement is compiled but not executed, to ensure that 236d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// EXPECT_DEATH_IF_SUPPORTED compiles with a certain 237d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// parameter iff EXPECT_DEATH compiles with it. 238d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// regex - A regex that a macro such as EXPECT_DEATH would use to test 239d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// the output of statement. This parameter has to be 240d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// compiled but not evaluated by this macro, to ensure that 241d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// this macro only accepts expressions that a macro such as 242d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// EXPECT_DEATH would accept. 243d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED 244d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// and a return statement for ASSERT_DEATH_IF_SUPPORTED. 245d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// This ensures that ASSERT_DEATH_IF_SUPPORTED will not 246d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// compile inside functions where ASSERT_DEATH doesn't 247d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// compile. 248d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// 249d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// The branch that has an always false condition is used to ensure that 250d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// statement and regex are compiled (and thus syntactically correct) but 251d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// never executed. The unreachable code macro protects the terminator 252d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// statement from generating an 'unreachable code' warning in case 253d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// statement unconditionally returns or throws. The Message constructor at 254d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// the end allows the syntax of streaming additional messages into the 255d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH. 256d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville#define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \ 257d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 258d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville if (::testing::internal::AlwaysTrue()) { \ 259d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GTEST_LOG_(WARNING) \ 260d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville << "Death tests are not supported on this platform.\n" \ 261d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville << "Statement '" #statement "' cannot be verified."; \ 262d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville } else if (::testing::internal::AlwaysFalse()) { \ 263d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville ::testing::internal::RE::PartialMatch(".*", (regex)); \ 264d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 265d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville terminator; \ 266d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville } else \ 267d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville ::testing::Message() 268d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville 269fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif // GTEST_HAS_DEATH_TEST 270fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 271fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace internal 272fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} // namespace testing 273fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 274fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ 275