11cb0ef41Sopenharmony_ci// Copyright 2005, Google Inc.
21cb0ef41Sopenharmony_ci// All rights reserved.
31cb0ef41Sopenharmony_ci//
41cb0ef41Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
51cb0ef41Sopenharmony_ci// modification, are permitted provided that the following conditions are
61cb0ef41Sopenharmony_ci// met:
71cb0ef41Sopenharmony_ci//
81cb0ef41Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
91cb0ef41Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
101cb0ef41Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
111cb0ef41Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
121cb0ef41Sopenharmony_ci// in the documentation and/or other materials provided with the
131cb0ef41Sopenharmony_ci// distribution.
141cb0ef41Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
151cb0ef41Sopenharmony_ci// contributors may be used to endorse or promote products derived from
161cb0ef41Sopenharmony_ci// this software without specific prior written permission.
171cb0ef41Sopenharmony_ci//
181cb0ef41Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191cb0ef41Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201cb0ef41Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211cb0ef41Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221cb0ef41Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231cb0ef41Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241cb0ef41Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251cb0ef41Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261cb0ef41Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271cb0ef41Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281cb0ef41Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci// The Google C++ Testing and Mocking Framework (Google Test)
311cb0ef41Sopenharmony_ci//
321cb0ef41Sopenharmony_ci// This header file defines the public API for death tests.  It is
331cb0ef41Sopenharmony_ci// #included by gtest.h so a user doesn't need to include this
341cb0ef41Sopenharmony_ci// directly.
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// IWYU pragma: private, include "gtest/gtest.h"
371cb0ef41Sopenharmony_ci// IWYU pragma: friend gtest/.*
381cb0ef41Sopenharmony_ci// IWYU pragma: friend gmock/.*
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
411cb0ef41Sopenharmony_ci#define GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci#include "gtest/internal/gtest-death-test-internal.h"
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci// This flag controls the style of death tests.  Valid values are "threadsafe",
461cb0ef41Sopenharmony_ci// meaning that the death test child process will re-execute the test binary
471cb0ef41Sopenharmony_ci// from the start, running only a single death test, or "fast",
481cb0ef41Sopenharmony_ci// meaning that the child process will execute the test logic immediately
491cb0ef41Sopenharmony_ci// after forking.
501cb0ef41Sopenharmony_ciGTEST_DECLARE_string_(death_test_style);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cinamespace testing {
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci#ifdef GTEST_HAS_DEATH_TEST
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cinamespace internal {
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci// Returns a Boolean value indicating whether the caller is currently
591cb0ef41Sopenharmony_ci// executing in the context of the death test child process.  Tools such as
601cb0ef41Sopenharmony_ci// Valgrind heap checkers may need this to modify their behavior in death
611cb0ef41Sopenharmony_ci// tests.  IMPORTANT: This is an internal utility.  Using it may break the
621cb0ef41Sopenharmony_ci// implementation of death tests.  User code MUST NOT use it.
631cb0ef41Sopenharmony_ciGTEST_API_ bool InDeathTestChild();
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci}  // namespace internal
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci// The following macros are useful for writing death tests.
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is
701cb0ef41Sopenharmony_ci// executed:
711cb0ef41Sopenharmony_ci//
721cb0ef41Sopenharmony_ci//   1. It generates a warning if there is more than one active
731cb0ef41Sopenharmony_ci//   thread.  This is because it's safe to fork() or clone() only
741cb0ef41Sopenharmony_ci//   when there is a single thread.
751cb0ef41Sopenharmony_ci//
761cb0ef41Sopenharmony_ci//   2. The parent process clone()s a sub-process and runs the death
771cb0ef41Sopenharmony_ci//   test in it; the sub-process exits with code 0 at the end of the
781cb0ef41Sopenharmony_ci//   death test, if it hasn't exited already.
791cb0ef41Sopenharmony_ci//
801cb0ef41Sopenharmony_ci//   3. The parent process waits for the sub-process to terminate.
811cb0ef41Sopenharmony_ci//
821cb0ef41Sopenharmony_ci//   4. The parent process checks the exit code and error message of
831cb0ef41Sopenharmony_ci//   the sub-process.
841cb0ef41Sopenharmony_ci//
851cb0ef41Sopenharmony_ci// Examples:
861cb0ef41Sopenharmony_ci//
871cb0ef41Sopenharmony_ci//   ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number");
881cb0ef41Sopenharmony_ci//   for (int i = 0; i < 5; i++) {
891cb0ef41Sopenharmony_ci//     EXPECT_DEATH(server.ProcessRequest(i),
901cb0ef41Sopenharmony_ci//                  "Invalid request .* in ProcessRequest()")
911cb0ef41Sopenharmony_ci//                  << "Failed to die on request " << i;
921cb0ef41Sopenharmony_ci//   }
931cb0ef41Sopenharmony_ci//
941cb0ef41Sopenharmony_ci//   ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
951cb0ef41Sopenharmony_ci//
961cb0ef41Sopenharmony_ci//   bool KilledBySIGHUP(int exit_code) {
971cb0ef41Sopenharmony_ci//     return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;
981cb0ef41Sopenharmony_ci//   }
991cb0ef41Sopenharmony_ci//
1001cb0ef41Sopenharmony_ci//   ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");
1011cb0ef41Sopenharmony_ci//
1021cb0ef41Sopenharmony_ci// The final parameter to each of these macros is a matcher applied to any data
1031cb0ef41Sopenharmony_ci// the sub-process wrote to stderr.  For compatibility with existing tests, a
1041cb0ef41Sopenharmony_ci// bare string is interpreted as a regular expression matcher.
1051cb0ef41Sopenharmony_ci//
1061cb0ef41Sopenharmony_ci// On the regular expressions used in death tests:
1071cb0ef41Sopenharmony_ci//
1081cb0ef41Sopenharmony_ci//   On POSIX-compliant systems (*nix), we use the <regex.h> library,
1091cb0ef41Sopenharmony_ci//   which uses the POSIX extended regex syntax.
1101cb0ef41Sopenharmony_ci//
1111cb0ef41Sopenharmony_ci//   On other platforms (e.g. Windows or Mac), we only support a simple regex
1121cb0ef41Sopenharmony_ci//   syntax implemented as part of Google Test.  This limited
1131cb0ef41Sopenharmony_ci//   implementation should be enough most of the time when writing
1141cb0ef41Sopenharmony_ci//   death tests; though it lacks many features you can find in PCRE
1151cb0ef41Sopenharmony_ci//   or POSIX extended regex syntax.  For example, we don't support
1161cb0ef41Sopenharmony_ci//   union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and
1171cb0ef41Sopenharmony_ci//   repetition count ("x{5,7}"), among others.
1181cb0ef41Sopenharmony_ci//
1191cb0ef41Sopenharmony_ci//   Below is the syntax that we do support.  We chose it to be a
1201cb0ef41Sopenharmony_ci//   subset of both PCRE and POSIX extended regex, so it's easy to
1211cb0ef41Sopenharmony_ci//   learn wherever you come from.  In the following: 'A' denotes a
1221cb0ef41Sopenharmony_ci//   literal character, period (.), or a single \\ escape sequence;
1231cb0ef41Sopenharmony_ci//   'x' and 'y' denote regular expressions; 'm' and 'n' are for
1241cb0ef41Sopenharmony_ci//   natural numbers.
1251cb0ef41Sopenharmony_ci//
1261cb0ef41Sopenharmony_ci//     c     matches any literal character c
1271cb0ef41Sopenharmony_ci//     \\d   matches any decimal digit
1281cb0ef41Sopenharmony_ci//     \\D   matches any character that's not a decimal digit
1291cb0ef41Sopenharmony_ci//     \\f   matches \f
1301cb0ef41Sopenharmony_ci//     \\n   matches \n
1311cb0ef41Sopenharmony_ci//     \\r   matches \r
1321cb0ef41Sopenharmony_ci//     \\s   matches any ASCII whitespace, including \n
1331cb0ef41Sopenharmony_ci//     \\S   matches any character that's not a whitespace
1341cb0ef41Sopenharmony_ci//     \\t   matches \t
1351cb0ef41Sopenharmony_ci//     \\v   matches \v
1361cb0ef41Sopenharmony_ci//     \\w   matches any letter, _, or decimal digit
1371cb0ef41Sopenharmony_ci//     \\W   matches any character that \\w doesn't match
1381cb0ef41Sopenharmony_ci//     \\c   matches any literal character c, which must be a punctuation
1391cb0ef41Sopenharmony_ci//     .     matches any single character except \n
1401cb0ef41Sopenharmony_ci//     A?    matches 0 or 1 occurrences of A
1411cb0ef41Sopenharmony_ci//     A*    matches 0 or many occurrences of A
1421cb0ef41Sopenharmony_ci//     A+    matches 1 or many occurrences of A
1431cb0ef41Sopenharmony_ci//     ^     matches the beginning of a string (not that of each line)
1441cb0ef41Sopenharmony_ci//     $     matches the end of a string (not that of each line)
1451cb0ef41Sopenharmony_ci//     xy    matches x followed by y
1461cb0ef41Sopenharmony_ci//
1471cb0ef41Sopenharmony_ci//   If you accidentally use PCRE or POSIX extended regex features
1481cb0ef41Sopenharmony_ci//   not implemented by us, you will get a run-time failure.  In that
1491cb0ef41Sopenharmony_ci//   case, please try to rewrite your regular expression within the
1501cb0ef41Sopenharmony_ci//   above syntax.
1511cb0ef41Sopenharmony_ci//
1521cb0ef41Sopenharmony_ci//   This implementation is *not* meant to be as highly tuned or robust
1531cb0ef41Sopenharmony_ci//   as a compiled regex library, but should perform well enough for a
1541cb0ef41Sopenharmony_ci//   death test, which already incurs significant overhead by launching
1551cb0ef41Sopenharmony_ci//   a child process.
1561cb0ef41Sopenharmony_ci//
1571cb0ef41Sopenharmony_ci// Known caveats:
1581cb0ef41Sopenharmony_ci//
1591cb0ef41Sopenharmony_ci//   A "threadsafe" style death test obtains the path to the test
1601cb0ef41Sopenharmony_ci//   program from argv[0] and re-executes it in the sub-process.  For
1611cb0ef41Sopenharmony_ci//   simplicity, the current implementation doesn't search the PATH
1621cb0ef41Sopenharmony_ci//   when launching the sub-process.  This means that the user must
1631cb0ef41Sopenharmony_ci//   invoke the test program via a path that contains at least one
1641cb0ef41Sopenharmony_ci//   path separator (e.g. path/to/foo_test and
1651cb0ef41Sopenharmony_ci//   /absolute/path/to/bar_test are fine, but foo_test is not).  This
1661cb0ef41Sopenharmony_ci//   is rarely a problem as people usually don't put the test binary
1671cb0ef41Sopenharmony_ci//   directory in PATH.
1681cb0ef41Sopenharmony_ci//
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci// Asserts that a given `statement` causes the program to exit, with an
1711cb0ef41Sopenharmony_ci// integer exit status that satisfies `predicate`, and emitting error output
1721cb0ef41Sopenharmony_ci// that matches `matcher`.
1731cb0ef41Sopenharmony_ci#define ASSERT_EXIT(statement, predicate, matcher) \
1741cb0ef41Sopenharmony_ci  GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_FATAL_FAILURE_)
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci// Like `ASSERT_EXIT`, but continues on to successive tests in the
1771cb0ef41Sopenharmony_ci// test suite, if any:
1781cb0ef41Sopenharmony_ci#define EXPECT_EXIT(statement, predicate, matcher) \
1791cb0ef41Sopenharmony_ci  GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_NONFATAL_FAILURE_)
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci// Asserts that a given `statement` causes the program to exit, either by
1821cb0ef41Sopenharmony_ci// explicitly exiting with a nonzero exit code or being killed by a
1831cb0ef41Sopenharmony_ci// signal, and emitting error output that matches `matcher`.
1841cb0ef41Sopenharmony_ci#define ASSERT_DEATH(statement, matcher) \
1851cb0ef41Sopenharmony_ci  ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher)
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci// Like `ASSERT_DEATH`, but continues on to successive tests in the
1881cb0ef41Sopenharmony_ci// test suite, if any:
1891cb0ef41Sopenharmony_ci#define EXPECT_DEATH(statement, matcher) \
1901cb0ef41Sopenharmony_ci  EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher)
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci// Tests that an exit code describes a normal exit with a given exit code.
1951cb0ef41Sopenharmony_ciclass GTEST_API_ ExitedWithCode {
1961cb0ef41Sopenharmony_ci public:
1971cb0ef41Sopenharmony_ci  explicit ExitedWithCode(int exit_code);
1981cb0ef41Sopenharmony_ci  ExitedWithCode(const ExitedWithCode&) = default;
1991cb0ef41Sopenharmony_ci  void operator=(const ExitedWithCode& other) = delete;
2001cb0ef41Sopenharmony_ci  bool operator()(int exit_status) const;
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci private:
2031cb0ef41Sopenharmony_ci  const int exit_code_;
2041cb0ef41Sopenharmony_ci};
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci#if !defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_FUCHSIA)
2071cb0ef41Sopenharmony_ci// Tests that an exit code describes an exit due to termination by a
2081cb0ef41Sopenharmony_ci// given signal.
2091cb0ef41Sopenharmony_ciclass GTEST_API_ KilledBySignal {
2101cb0ef41Sopenharmony_ci public:
2111cb0ef41Sopenharmony_ci  explicit KilledBySignal(int signum);
2121cb0ef41Sopenharmony_ci  bool operator()(int exit_status) const;
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci private:
2151cb0ef41Sopenharmony_ci  const int signum_;
2161cb0ef41Sopenharmony_ci};
2171cb0ef41Sopenharmony_ci#endif  // !GTEST_OS_WINDOWS
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.
2201cb0ef41Sopenharmony_ci// The death testing framework causes this to have interesting semantics,
2211cb0ef41Sopenharmony_ci// since the sideeffects of the call are only visible in opt mode, and not
2221cb0ef41Sopenharmony_ci// in debug mode.
2231cb0ef41Sopenharmony_ci//
2241cb0ef41Sopenharmony_ci// In practice, this can be used to test functions that utilize the
2251cb0ef41Sopenharmony_ci// LOG(DFATAL) macro using the following style:
2261cb0ef41Sopenharmony_ci//
2271cb0ef41Sopenharmony_ci// int DieInDebugOr12(int* sideeffect) {
2281cb0ef41Sopenharmony_ci//   if (sideeffect) {
2291cb0ef41Sopenharmony_ci//     *sideeffect = 12;
2301cb0ef41Sopenharmony_ci//   }
2311cb0ef41Sopenharmony_ci//   LOG(DFATAL) << "death";
2321cb0ef41Sopenharmony_ci//   return 12;
2331cb0ef41Sopenharmony_ci// }
2341cb0ef41Sopenharmony_ci//
2351cb0ef41Sopenharmony_ci// TEST(TestSuite, TestDieOr12WorksInDgbAndOpt) {
2361cb0ef41Sopenharmony_ci//   int sideeffect = 0;
2371cb0ef41Sopenharmony_ci//   // Only asserts in dbg.
2381cb0ef41Sopenharmony_ci//   EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");
2391cb0ef41Sopenharmony_ci//
2401cb0ef41Sopenharmony_ci// #ifdef NDEBUG
2411cb0ef41Sopenharmony_ci//   // opt-mode has sideeffect visible.
2421cb0ef41Sopenharmony_ci//   EXPECT_EQ(12, sideeffect);
2431cb0ef41Sopenharmony_ci// #else
2441cb0ef41Sopenharmony_ci//   // dbg-mode no visible sideeffect.
2451cb0ef41Sopenharmony_ci//   EXPECT_EQ(0, sideeffect);
2461cb0ef41Sopenharmony_ci// #endif
2471cb0ef41Sopenharmony_ci// }
2481cb0ef41Sopenharmony_ci//
2491cb0ef41Sopenharmony_ci// This will assert that DieInDebugReturn12InOpt() crashes in debug
2501cb0ef41Sopenharmony_ci// mode, usually due to a DCHECK or LOG(DFATAL), but returns the
2511cb0ef41Sopenharmony_ci// appropriate fallback value (12 in this case) in opt mode. If you
2521cb0ef41Sopenharmony_ci// need to test that a function has appropriate side-effects in opt
2531cb0ef41Sopenharmony_ci// mode, include assertions against the side-effects.  A general
2541cb0ef41Sopenharmony_ci// pattern for this is:
2551cb0ef41Sopenharmony_ci//
2561cb0ef41Sopenharmony_ci// EXPECT_DEBUG_DEATH({
2571cb0ef41Sopenharmony_ci//   // Side-effects here will have an effect after this statement in
2581cb0ef41Sopenharmony_ci//   // opt mode, but none in debug mode.
2591cb0ef41Sopenharmony_ci//   EXPECT_EQ(12, DieInDebugOr12(&sideeffect));
2601cb0ef41Sopenharmony_ci// }, "death");
2611cb0ef41Sopenharmony_ci//
2621cb0ef41Sopenharmony_ci#ifdef NDEBUG
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ci#define EXPECT_DEBUG_DEATH(statement, regex) \
2651cb0ef41Sopenharmony_ci  GTEST_EXECUTE_STATEMENT_(statement, regex)
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci#define ASSERT_DEBUG_DEATH(statement, regex) \
2681cb0ef41Sopenharmony_ci  GTEST_EXECUTE_STATEMENT_(statement, regex)
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci#else
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci#define EXPECT_DEBUG_DEATH(statement, regex) EXPECT_DEATH(statement, regex)
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ci#define ASSERT_DEBUG_DEATH(statement, regex) ASSERT_DEATH(statement, regex)
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci#endif  // NDEBUG for EXPECT_DEBUG_DEATH
2771cb0ef41Sopenharmony_ci#endif  // GTEST_HAS_DEATH_TEST
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ci// This macro is used for implementing macros such as
2801cb0ef41Sopenharmony_ci// EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where
2811cb0ef41Sopenharmony_ci// death tests are not supported. Those macros must compile on such systems
2821cb0ef41Sopenharmony_ci// if and only if EXPECT_DEATH and ASSERT_DEATH compile with the same parameters
2831cb0ef41Sopenharmony_ci// on systems that support death tests. This allows one to write such a macro on
2841cb0ef41Sopenharmony_ci// a system that does not support death tests and be sure that it will compile
2851cb0ef41Sopenharmony_ci// on a death-test supporting system. It is exposed publicly so that systems
2861cb0ef41Sopenharmony_ci// that have death-tests with stricter requirements than GTEST_HAS_DEATH_TEST
2871cb0ef41Sopenharmony_ci// can write their own equivalent of EXPECT_DEATH_IF_SUPPORTED and
2881cb0ef41Sopenharmony_ci// ASSERT_DEATH_IF_SUPPORTED.
2891cb0ef41Sopenharmony_ci//
2901cb0ef41Sopenharmony_ci// Parameters:
2911cb0ef41Sopenharmony_ci//   statement -  A statement that a macro such as EXPECT_DEATH would test
2921cb0ef41Sopenharmony_ci//                for program termination. This macro has to make sure this
2931cb0ef41Sopenharmony_ci//                statement is compiled but not executed, to ensure that
2941cb0ef41Sopenharmony_ci//                EXPECT_DEATH_IF_SUPPORTED compiles with a certain
2951cb0ef41Sopenharmony_ci//                parameter if and only if EXPECT_DEATH compiles with it.
2961cb0ef41Sopenharmony_ci//   regex     -  A regex that a macro such as EXPECT_DEATH would use to test
2971cb0ef41Sopenharmony_ci//                the output of statement.  This parameter has to be
2981cb0ef41Sopenharmony_ci//                compiled but not evaluated by this macro, to ensure that
2991cb0ef41Sopenharmony_ci//                this macro only accepts expressions that a macro such as
3001cb0ef41Sopenharmony_ci//                EXPECT_DEATH would accept.
3011cb0ef41Sopenharmony_ci//   terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED
3021cb0ef41Sopenharmony_ci//                and a return statement for ASSERT_DEATH_IF_SUPPORTED.
3031cb0ef41Sopenharmony_ci//                This ensures that ASSERT_DEATH_IF_SUPPORTED will not
3041cb0ef41Sopenharmony_ci//                compile inside functions where ASSERT_DEATH doesn't
3051cb0ef41Sopenharmony_ci//                compile.
3061cb0ef41Sopenharmony_ci//
3071cb0ef41Sopenharmony_ci//  The branch that has an always false condition is used to ensure that
3081cb0ef41Sopenharmony_ci//  statement and regex are compiled (and thus syntactically correct) but
3091cb0ef41Sopenharmony_ci//  never executed. The unreachable code macro protects the terminator
3101cb0ef41Sopenharmony_ci//  statement from generating an 'unreachable code' warning in case
3111cb0ef41Sopenharmony_ci//  statement unconditionally returns or throws. The Message constructor at
3121cb0ef41Sopenharmony_ci//  the end allows the syntax of streaming additional messages into the
3131cb0ef41Sopenharmony_ci//  macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.
3141cb0ef41Sopenharmony_ci#define GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, terminator)             \
3151cb0ef41Sopenharmony_ci  GTEST_AMBIGUOUS_ELSE_BLOCKER_                                                \
3161cb0ef41Sopenharmony_ci  if (::testing::internal::AlwaysTrue()) {                                     \
3171cb0ef41Sopenharmony_ci    GTEST_LOG_(WARNING) << "Death tests are not supported on this platform.\n" \
3181cb0ef41Sopenharmony_ci                        << "Statement '" #statement "' cannot be verified.";   \
3191cb0ef41Sopenharmony_ci  } else if (::testing::internal::AlwaysFalse()) {                             \
3201cb0ef41Sopenharmony_ci    ::testing::internal::RE::PartialMatch(".*", (regex));                      \
3211cb0ef41Sopenharmony_ci    GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement);                 \
3221cb0ef41Sopenharmony_ci    terminator;                                                                \
3231cb0ef41Sopenharmony_ci  } else                                                                       \
3241cb0ef41Sopenharmony_ci    ::testing::Message()
3251cb0ef41Sopenharmony_ci
3261cb0ef41Sopenharmony_ci// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and
3271cb0ef41Sopenharmony_ci// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if
3281cb0ef41Sopenharmony_ci// death tests are supported; otherwise they just issue a warning.  This is
3291cb0ef41Sopenharmony_ci// useful when you are combining death test assertions with normal test
3301cb0ef41Sopenharmony_ci// assertions in one test.
3311cb0ef41Sopenharmony_ci#ifdef GTEST_HAS_DEATH_TEST
3321cb0ef41Sopenharmony_ci#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
3331cb0ef41Sopenharmony_ci  EXPECT_DEATH(statement, regex)
3341cb0ef41Sopenharmony_ci#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
3351cb0ef41Sopenharmony_ci  ASSERT_DEATH(statement, regex)
3361cb0ef41Sopenharmony_ci#else
3371cb0ef41Sopenharmony_ci#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
3381cb0ef41Sopenharmony_ci  GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, )
3391cb0ef41Sopenharmony_ci#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
3401cb0ef41Sopenharmony_ci  GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, return)
3411cb0ef41Sopenharmony_ci#endif
3421cb0ef41Sopenharmony_ci
3431cb0ef41Sopenharmony_ci}  // namespace testing
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ci#endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
346