1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Copyright 2005, Google Inc.
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// All rights reserved.
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Redistribution and use in source and binary forms, with or without
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// modification, are permitted provided that the following conditions are
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// met:
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     * Redistributions of source code must retain the above copyright
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// notice, this list of conditions and the following disclaimer.
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     * Redistributions in binary form must reproduce the above
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// copyright notice, this list of conditions and the following disclaimer
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// in the documentation and/or other materials provided with the
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// distribution.
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     * Neither the name of Google Inc. nor the names of its
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// contributors may be used to endorse or promote products derived from
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// this software without specific prior written permission.
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Author: wan@google.com (Zhanyong Wan)
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The Google C++ Testing Framework (Google Test)
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This header file defines the public API for death tests.  It is
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// #included by gtest.h so a user doesn't need to include this
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// directly.
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "gtest/internal/gtest-death-test-internal.h"
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace testing {
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This flag controls the style of death tests.  Valid values are "threadsafe",
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// meaning that the death test child process will re-execute the test binary
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// from the start, running only a single death test, or "fast",
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// meaning that the child process will execute the test logic immediately
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// after forking.
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGTEST_DECLARE_string_(death_test_style);
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_DEATH_TEST
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The following macros are useful for writing death tests.
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// executed:
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   1. It generates a warning if there is more than one active
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   thread.  This is because it's safe to fork() or clone() only
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   when there is a single thread.
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   2. The parent process clone()s a sub-process and runs the death
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   test in it; the sub-process exits with code 0 at the end of the
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   death test, if it hasn't exited already.
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   3. The parent process waits for the sub-process to terminate.
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   4. The parent process checks the exit code and error message of
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   the sub-process.
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Examples:
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number");
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   for (int i = 0; i < 5; i++) {
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     EXPECT_DEATH(server.ProcessRequest(i),
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                  "Invalid request .* in ProcessRequest()")
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//         << "Failed to die on request " << i);
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   }
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   bool KilledBySIGHUP(int exit_code) {
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   }
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// On the regular expressions used in death tests:
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   On POSIX-compliant systems (*nix), we use the <regex.h> library,
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   which uses the POSIX extended regex syntax.
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   On other platforms (e.g. Windows), we only support a simple regex
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   syntax implemented as part of Google Test.  This limited
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   implementation should be enough most of the time when writing
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   death tests; though it lacks many features you can find in PCRE
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   or POSIX extended regex syntax.  For example, we don't support
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   repetition count ("x{5,7}"), among others.
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   Below is the syntax that we do support.  We chose it to be a
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   subset of both PCRE and POSIX extended regex, so it's easy to
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   learn wherever you come from.  In the following: 'A' denotes a
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   literal character, period (.), or a single \\ escape sequence;
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   'x' and 'y' denote regular expressions; 'm' and 'n' are for
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   natural numbers.
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     c     matches any literal character c
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\d   matches any decimal digit
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\D   matches any character that's not a decimal digit
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\f   matches \f
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\n   matches \n
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\r   matches \r
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\s   matches any ASCII whitespace, including \n
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\S   matches any character that's not a whitespace
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\t   matches \t
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\v   matches \v
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\w   matches any letter, _, or decimal digit
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\W   matches any character that \\w doesn't match
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     \\c   matches any literal character c, which must be a punctuation
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     .     matches any single character except \n
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     A?    matches 0 or 1 occurrences of A
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     A*    matches 0 or many occurrences of A
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     A+    matches 1 or many occurrences of A
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     ^     matches the beginning of a string (not that of each line)
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     $     matches the end of a string (not that of each line)
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     xy    matches x followed by y
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   If you accidentally use PCRE or POSIX extended regex features
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   not implemented by us, you will get a run-time failure.  In that
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   case, please try to rewrite your regular expression within the
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   above syntax.
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   This implementation is *not* meant to be as highly tuned or robust
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   as a compiled regex library, but should perform well enough for a
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   death test, which already incurs significant overhead by launching
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   a child process.
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Known caveats:
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   A "threadsafe" style death test obtains the path to the test
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   program from argv[0] and re-executes it in the sub-process.  For
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   simplicity, the current implementation doesn't search the PATH
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   when launching the sub-process.  This means that the user must
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   invoke the test program via a path that contains at least one
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   path separator (e.g. path/to/foo_test and
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   /absolute/path/to/bar_test are fine, but foo_test is not).  This
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   is rarely a problem as people usually don't put the test binary
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   directory in PATH.
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// TODO(wan@google.com): make thread-safe death tests search the PATH.
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Asserts that a given statement causes the program to exit, with an
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// integer exit status that satisfies predicate, and emitting error output
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// that matches regex.
15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define ASSERT_EXIT(statement, predicate, regex) \
15819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_)
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Like ASSERT_EXIT, but continues on to successive tests in the
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// test case, if any:
16219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define EXPECT_EXIT(statement, predicate, regex) \
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_)
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Asserts that a given statement causes the program to exit, either by
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// explicitly exiting with a nonzero exit code or being killed by a
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// signal, and emitting error output that matches regex.
16819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define ASSERT_DEATH(statement, regex) \
16919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Like ASSERT_DEATH, but continues on to successive tests in the
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// test case, if any:
17319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define EXPECT_DEATH(statement, regex) \
17419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Tests that an exit code describes a normal exit with a given exit code.
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GTEST_API_ ExitedWithCode {
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public:
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  explicit ExitedWithCode(int exit_code);
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool operator()(int exit_status) const;
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private:
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // No implementation - assignment is unsupported.
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void operator=(const ExitedWithCode& other);
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const int exit_code_;
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
19019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if !GTEST_OS_WINDOWS
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Tests that an exit code describes an exit due to termination by a
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// given signal.
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GTEST_API_ KilledBySignal {
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public:
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  explicit KilledBySignal(int signum);
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool operator()(int exit_status) const;
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private:
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const int signum_;
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
20019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif  // !GTEST_OS_WINDOWS
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The death testing framework causes this to have interesting semantics,
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// since the sideeffects of the call are only visible in opt mode, and not
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// in debug mode.
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// In practice, this can be used to test functions that utilize the
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LOG(DFATAL) macro using the following style:
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// int DieInDebugOr12(int* sideeffect) {
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   if (sideeffect) {
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//     *sideeffect = 12;
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   }
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   LOG(DFATAL) << "death";
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   return 12;
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// }
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// TEST(TestCase, TestDieOr12WorksInDgbAndOpt) {
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   int sideeffect = 0;
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   // Only asserts in dbg.
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// #ifdef NDEBUG
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   // opt-mode has sideeffect visible.
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   EXPECT_EQ(12, sideeffect);
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// #else
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   // dbg-mode no visible sideeffect.
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   EXPECT_EQ(0, sideeffect);
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// #endif
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// }
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This will assert that DieInDebugReturn12InOpt() crashes in debug
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// mode, usually due to a DCHECK or LOG(DFATAL), but returns the
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// appropriate fallback value (12 in this case) in opt mode. If you
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// need to test that a function has appropriate side-effects in opt
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// mode, include assertions against the side-effects.  A general
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// pattern for this is:
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// EXPECT_DEBUG_DEATH({
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   // Side-effects here will have an effect after this statement in
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   // opt mode, but none in debug mode.
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//   EXPECT_EQ(12, DieInDebugOr12(&sideeffect));
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// }, "death");
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
24519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifdef NDEBUG
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#  define EXPECT_DEBUG_DEATH(statement, regex) \
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  do { statement; } while (::testing::internal::AlwaysFalse())
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
25019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#  define ASSERT_DEBUG_DEATH(statement, regex) \
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  do { statement; } while (::testing::internal::AlwaysFalse())
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
25319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
25519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#  define EXPECT_DEBUG_DEATH(statement, regex) \
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EXPECT_DEATH(statement, regex)
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
25819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#  define ASSERT_DEBUG_DEATH(statement, regex) \
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ASSERT_DEATH(statement, regex)
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
26119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif  // NDEBUG for EXPECT_DEBUG_DEATH
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif  // GTEST_HAS_DEATH_TEST
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// death tests are supported; otherwise they just issue a warning.  This is
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// useful when you are combining death test assertions with normal test
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// assertions in one test.
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_DEATH_TEST
27019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EXPECT_DEATH(statement, regex)
27219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ASSERT_DEATH(statement, regex)
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, )
27719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return)
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}  // namespace testing
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif  // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
284