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// Author: wan@google.com (Zhanyong Wan) 31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The Google C++ Testing Framework (Google Test) 33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This header file defines the public API for death tests. It is 35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// #included by gtest.h so a user doesn't need to include this 36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// directly. 37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ 39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ 40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "gtest/internal/gtest-death-test-internal.h" 42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace testing { 44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This flag controls the style of death tests. Valid values are "threadsafe", 46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// meaning that the death test child process will re-execute the test binary 47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// from the start, running only a single death test, or "fast", 48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// meaning that the child process will execute the test logic immediately 49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// after forking. 50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DECLARE_string_(death_test_style); 51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_DEATH_TEST 53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The following macros are useful for writing death tests. 55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is 57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// executed: 58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 1. It generates a warning if there is more than one active 60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// thread. This is because it's safe to fork() or clone() only 61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// when there is a single thread. 62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2. The parent process clone()s a sub-process and runs the death 64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// test in it; the sub-process exits with code 0 at the end of the 65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// death test, if it hasn't exited already. 66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3. The parent process waits for the sub-process to terminate. 68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4. The parent process checks the exit code and error message of 70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the sub-process. 71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Examples: 73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number"); 75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// for (int i = 0; i < 5; i++) { 76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// EXPECT_DEATH(server.ProcessRequest(i), 77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// "Invalid request .* in ProcessRequest()") 78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// << "Failed to die on request " << i); 79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// } 80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting"); 82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// bool KilledBySIGHUP(int exit_code) { 84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP; 85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// } 86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!"); 88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On the regular expressions used in death tests: 90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On POSIX-compliant systems (*nix), we use the <regex.h> library, 92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// which uses the POSIX extended regex syntax. 93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On other platforms (e.g. Windows), we only support a simple regex 95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// syntax implemented as part of Google Test. This limited 96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// implementation should be enough most of the time when writing 97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// death tests; though it lacks many features you can find in PCRE 98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// or POSIX extended regex syntax. For example, we don't support 99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and 100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// repetition count ("x{5,7}"), among others. 101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Below is the syntax that we do support. We chose it to be a 103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// subset of both PCRE and POSIX extended regex, so it's easy to 104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// learn wherever you come from. In the following: 'A' denotes a 105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// literal character, period (.), or a single \\ escape sequence; 106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 'x' and 'y' denote regular expressions; 'm' and 'n' are for 107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// natural numbers. 108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// c matches any literal character c 110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\d matches any decimal digit 111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\D matches any character that's not a decimal digit 112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\f matches \f 113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\n matches \n 114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\r matches \r 115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\s matches any ASCII whitespace, including \n 116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\S matches any character that's not a whitespace 117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\t matches \t 118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\v matches \v 119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\w matches any letter, _, or decimal digit 120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\W matches any character that \\w doesn't match 121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// \\c matches any literal character c, which must be a punctuation 122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// . matches any single character except \n 123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A? matches 0 or 1 occurrences of A 124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A* matches 0 or many occurrences of A 125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A+ matches 1 or many occurrences of A 126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// ^ matches the beginning of a string (not that of each line) 127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// $ matches the end of a string (not that of each line) 128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// xy matches x followed by y 129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If you accidentally use PCRE or POSIX extended regex features 131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// not implemented by us, you will get a run-time failure. In that 132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// case, please try to rewrite your regular expression within the 133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// above syntax. 134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This implementation is *not* meant to be as highly tuned or robust 136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// as a compiled regex library, but should perform well enough for a 137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// death test, which already incurs significant overhead by launching 138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// a child process. 139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Known caveats: 141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A "threadsafe" style death test obtains the path to the test 143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// program from argv[0] and re-executes it in the sub-process. For 144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// simplicity, the current implementation doesn't search the PATH 145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// when launching the sub-process. This means that the user must 146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// invoke the test program via a path that contains at least one 147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// path separator (e.g. path/to/foo_test and 148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// /absolute/path/to/bar_test are fine, but foo_test is not). This 149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// is rarely a problem as people usually don't put the test binary 150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// directory in PATH. 151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(wan@google.com): make thread-safe death tests search the PATH. 153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Asserts that a given statement causes the program to exit, with an 155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// integer exit status that satisfies predicate, and emitting error output 156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// that matches regex. 157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define ASSERT_EXIT(statement, predicate, regex) \ 158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_) 159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Like ASSERT_EXIT, but continues on to successive tests in the 161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// test case, if any: 162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define EXPECT_EXIT(statement, predicate, regex) \ 163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_) 164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Asserts that a given statement causes the program to exit, either by 166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// explicitly exiting with a nonzero exit code or being killed by a 167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// signal, and emitting error output that matches regex. 168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define ASSERT_DEATH(statement, regex) \ 169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex) 170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Like ASSERT_DEATH, but continues on to successive tests in the 172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// test case, if any: 173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define EXPECT_DEATH(statement, regex) \ 174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex) 175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*: 177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Tests that an exit code describes a normal exit with a given exit code. 179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass GTEST_API_ ExitedWithCode { 180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public: 181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org explicit ExitedWithCode(int exit_code); 182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool operator()(int exit_status) const; 183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org private: 184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // No implementation - assignment is unsupported. 185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void operator=(const ExitedWithCode& other); 186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int exit_code_; 188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# if !GTEST_OS_WINDOWS 191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Tests that an exit code describes an exit due to termination by a 192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// given signal. 193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass GTEST_API_ KilledBySignal { 194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public: 195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org explicit KilledBySignal(int signum); 196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool operator()(int exit_status) const; 197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org private: 198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int signum_; 199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# endif // !GTEST_OS_WINDOWS 201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode. 203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The death testing framework causes this to have interesting semantics, 204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// since the sideeffects of the call are only visible in opt mode, and not 205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// in debug mode. 206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// In practice, this can be used to test functions that utilize the 208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// LOG(DFATAL) macro using the following style: 209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// int DieInDebugOr12(int* sideeffect) { 211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// if (sideeffect) { 212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// *sideeffect = 12; 213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// } 214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// LOG(DFATAL) << "death"; 215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// return 12; 216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// } 217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TEST(TestCase, TestDieOr12WorksInDgbAndOpt) { 219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// int sideeffect = 0; 220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// // Only asserts in dbg. 221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death"); 222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// #ifdef NDEBUG 224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// // opt-mode has sideeffect visible. 225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// EXPECT_EQ(12, sideeffect); 226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// #else 227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// // dbg-mode no visible sideeffect. 228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// EXPECT_EQ(0, sideeffect); 229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// #endif 230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// } 231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This will assert that DieInDebugReturn12InOpt() crashes in debug 233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// mode, usually due to a DCHECK or LOG(DFATAL), but returns the 234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// appropriate fallback value (12 in this case) in opt mode. If you 235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// need to test that a function has appropriate side-effects in opt 236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// mode, include assertions against the side-effects. A general 237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// pattern for this is: 238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// EXPECT_DEBUG_DEATH({ 240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// // Side-effects here will have an effect after this statement in 241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// // opt mode, but none in debug mode. 242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// EXPECT_EQ(12, DieInDebugOr12(&sideeffect)); 243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// }, "death"); 244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# ifdef NDEBUG 246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define EXPECT_DEBUG_DEATH(statement, regex) \ 248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org do { statement; } while (::testing::internal::AlwaysFalse()) 249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define ASSERT_DEBUG_DEATH(statement, regex) \ 251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org do { statement; } while (::testing::internal::AlwaysFalse()) 252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# else 254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define EXPECT_DEBUG_DEATH(statement, regex) \ 256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org EXPECT_DEATH(statement, regex) 257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define ASSERT_DEBUG_DEATH(statement, regex) \ 259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ASSERT_DEATH(statement, regex) 260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# endif // NDEBUG for EXPECT_DEBUG_DEATH 262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_DEATH_TEST 263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and 265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if 266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// death tests are supported; otherwise they just issue a warning. This is 267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// useful when you are combining death test assertions with normal test 268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// assertions in one test. 269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_DEATH_TEST 270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org EXPECT_DEATH(statement, regex) 272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ 273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ASSERT_DEATH(statement, regex) 274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, ) 277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ 278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return) 279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace testing 282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ 284