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