gtest-death-test.h
Go to the documentation of this file.
00001 // Copyright 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 //
00032 // The Google C++ Testing Framework (Google Test)
00033 //
00034 // This header file defines the public API for death tests.  It is
00035 // #included by gtest.h so a user doesn't need to include this
00036 // directly.
00037 
00038 #ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
00039 #define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
00040 
00041 #include "gtest/internal/gtest-death-test-internal.h"
00042 
00043 namespace testing {
00044 
00045 // This flag controls the style of death tests.  Valid values are "threadsafe",
00046 // meaning that the death test child process will re-execute the test binary
00047 // from the start, running only a single death test, or "fast",
00048 // meaning that the child process will execute the test logic immediately
00049 // after forking.
00050 GTEST_DECLARE_string_(death_test_style);
00051 
00052 #if GTEST_HAS_DEATH_TEST
00053 
00054 // The following macros are useful for writing death tests.
00055 
00056 // Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is
00057 // executed:
00058 //
00059 //   1. It generates a warning if there is more than one active
00060 //   thread.  This is because it's safe to fork() or clone() only
00061 //   when there is a single thread.
00062 //
00063 //   2. The parent process clone()s a sub-process and runs the death
00064 //   test in it; the sub-process exits with code 0 at the end of the
00065 //   death test, if it hasn't exited already.
00066 //
00067 //   3. The parent process waits for the sub-process to terminate.
00068 //
00069 //   4. The parent process checks the exit code and error message of
00070 //   the sub-process.
00071 //
00072 // Examples:
00073 //
00074 //   ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number");
00075 //   for (int i = 0; i < 5; i++) {
00076 //     EXPECT_DEATH(server.ProcessRequest(i),
00077 //                  "Invalid request .* in ProcessRequest()")
00078 //         << "Failed to die on request " << i);
00079 //   }
00080 //
00081 //   ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
00082 //
00083 //   bool KilledBySIGHUP(int exit_code) {
00084 //     return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;
00085 //   }
00086 //
00087 //   ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");
00088 //
00089 // On the regular expressions used in death tests:
00090 //
00091 //   On POSIX-compliant systems (*nix), we use the <regex.h> library,
00092 //   which uses the POSIX extended regex syntax.
00093 //
00094 //   On other platforms (e.g. Windows), we only support a simple regex
00095 //   syntax implemented as part of Google Test.  This limited
00096 //   implementation should be enough most of the time when writing
00097 //   death tests; though it lacks many features you can find in PCRE
00098 //   or POSIX extended regex syntax.  For example, we don't support
00099 //   union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and
00100 //   repetition count ("x{5,7}"), among others.
00101 //
00102 //   Below is the syntax that we do support.  We chose it to be a
00103 //   subset of both PCRE and POSIX extended regex, so it's easy to
00104 //   learn wherever you come from.  In the following: 'A' denotes a
00105 //   literal character, period (.), or a single \\ escape sequence;
00106 //   'x' and 'y' denote regular expressions; 'm' and 'n' are for
00107 //   natural numbers.
00108 //
00109 //     c     matches any literal character c
00110 //     \\d   matches any decimal digit
00111 //     \\D   matches any character that's not a decimal digit
00112 //     \\f   matches \f
00113 //     \\n   matches \n
00114 //     \\r   matches \r
00115 //     \\s   matches any ASCII whitespace, including \n
00116 //     \\S   matches any character that's not a whitespace
00117 //     \\t   matches \t
00118 //     \\v   matches \v
00119 //     \\w   matches any letter, _, or decimal digit
00120 //     \\W   matches any character that \\w doesn't match
00121 //     \\c   matches any literal character c, which must be a punctuation
00122 //     .     matches any single character except \n
00123 //     A?    matches 0 or 1 occurrences of A
00124 //     A*    matches 0 or many occurrences of A
00125 //     A+    matches 1 or many occurrences of A
00126 //     ^     matches the beginning of a string (not that of each line)
00127 //     $     matches the end of a string (not that of each line)
00128 //     xy    matches x followed by y
00129 //
00130 //   If you accidentally use PCRE or POSIX extended regex features
00131 //   not implemented by us, you will get a run-time failure.  In that
00132 //   case, please try to rewrite your regular expression within the
00133 //   above syntax.
00134 //
00135 //   This implementation is *not* meant to be as highly tuned or robust
00136 //   as a compiled regex library, but should perform well enough for a
00137 //   death test, which already incurs significant overhead by launching
00138 //   a child process.
00139 //
00140 // Known caveats:
00141 //
00142 //   A "threadsafe" style death test obtains the path to the test
00143 //   program from argv[0] and re-executes it in the sub-process.  For
00144 //   simplicity, the current implementation doesn't search the PATH
00145 //   when launching the sub-process.  This means that the user must
00146 //   invoke the test program via a path that contains at least one
00147 //   path separator (e.g. path/to/foo_test and
00148 //   /absolute/path/to/bar_test are fine, but foo_test is not).  This
00149 //   is rarely a problem as people usually don't put the test binary
00150 //   directory in PATH.
00151 //
00152 // TODO(wan@google.com): make thread-safe death tests search the PATH.
00153 
00154 // Asserts that a given statement causes the program to exit, with an
00155 // integer exit status that satisfies predicate, and emitting error output
00156 // that matches regex.
00157 # define ASSERT_EXIT(statement, predicate, regex) \
00158     GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_)
00159 
00160 // Like ASSERT_EXIT, but continues on to successive tests in the
00161 // test case, if any:
00162 # define EXPECT_EXIT(statement, predicate, regex) \
00163     GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_)
00164 
00165 // Asserts that a given statement causes the program to exit, either by
00166 // explicitly exiting with a nonzero exit code or being killed by a
00167 // signal, and emitting error output that matches regex.
00168 # define ASSERT_DEATH(statement, regex) \
00169     ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
00170 
00171 // Like ASSERT_DEATH, but continues on to successive tests in the
00172 // test case, if any:
00173 # define EXPECT_DEATH(statement, regex) \
00174     EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
00175 
00176 // Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
00177 
00178 // Tests that an exit code describes a normal exit with a given exit code.
00179 class GTEST_API_ ExitedWithCode {
00180  public:
00181   explicit ExitedWithCode(int exit_code);
00182   bool operator()(int exit_status) const;
00183  private:
00184   // No implementation - assignment is unsupported.
00185   void operator=(const ExitedWithCode& other);
00186 
00187   const int exit_code_;
00188 };
00189 
00190 # if !GTEST_OS_WINDOWS
00191 // Tests that an exit code describes an exit due to termination by a
00192 // given signal.
00193 class GTEST_API_ KilledBySignal {
00194  public:
00195   explicit KilledBySignal(int signum);
00196   bool operator()(int exit_status) const;
00197  private:
00198   const int signum_;
00199 };
00200 # endif  // !GTEST_OS_WINDOWS
00201 
00202 // EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.
00203 // The death testing framework causes this to have interesting semantics,
00204 // since the sideeffects of the call are only visible in opt mode, and not
00205 // in debug mode.
00206 //
00207 // In practice, this can be used to test functions that utilize the
00208 // LOG(DFATAL) macro using the following style:
00209 //
00210 // int DieInDebugOr12(int* sideeffect) {
00211 //   if (sideeffect) {
00212 //     *sideeffect = 12;
00213 //   }
00214 //   LOG(DFATAL) << "death";
00215 //   return 12;
00216 // }
00217 //
00218 // TEST(TestCase, TestDieOr12WorksInDgbAndOpt) {
00219 //   int sideeffect = 0;
00220 //   // Only asserts in dbg.
00221 //   EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");
00222 //
00223 // #ifdef NDEBUG
00224 //   // opt-mode has sideeffect visible.
00225 //   EXPECT_EQ(12, sideeffect);
00226 // #else
00227 //   // dbg-mode no visible sideeffect.
00228 //   EXPECT_EQ(0, sideeffect);
00229 // #endif
00230 // }
00231 //
00232 // This will assert that DieInDebugReturn12InOpt() crashes in debug
00233 // mode, usually due to a DCHECK or LOG(DFATAL), but returns the
00234 // appropriate fallback value (12 in this case) in opt mode. If you
00235 // need to test that a function has appropriate side-effects in opt
00236 // mode, include assertions against the side-effects.  A general
00237 // pattern for this is:
00238 //
00239 // EXPECT_DEBUG_DEATH({
00240 //   // Side-effects here will have an effect after this statement in
00241 //   // opt mode, but none in debug mode.
00242 //   EXPECT_EQ(12, DieInDebugOr12(&sideeffect));
00243 // }, "death");
00244 //
00245 # ifdef NDEBUG
00246 
00247 #  define EXPECT_DEBUG_DEATH(statement, regex) \
00248   do { statement; } while (::testing::internal::AlwaysFalse())
00249 
00250 #  define ASSERT_DEBUG_DEATH(statement, regex) \
00251   do { statement; } while (::testing::internal::AlwaysFalse())
00252 
00253 # else
00254 
00255 #  define EXPECT_DEBUG_DEATH(statement, regex) \
00256   EXPECT_DEATH(statement, regex)
00257 
00258 #  define ASSERT_DEBUG_DEATH(statement, regex) \
00259   ASSERT_DEATH(statement, regex)
00260 
00261 # endif  // NDEBUG for EXPECT_DEBUG_DEATH
00262 #endif  // GTEST_HAS_DEATH_TEST
00263 
00264 // EXPECT_DEATH_IF_SUPPORTED(statement, regex) and
00265 // ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if
00266 // death tests are supported; otherwise they just issue a warning.  This is
00267 // useful when you are combining death test assertions with normal test
00268 // assertions in one test.
00269 #if GTEST_HAS_DEATH_TEST
00270 # define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
00271     EXPECT_DEATH(statement, regex)
00272 # define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
00273     ASSERT_DEATH(statement, regex)
00274 #else
00275 # define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
00276     GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, )
00277 # define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
00278     GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return)
00279 #endif
00280 
00281 }  // namespace testing
00282 
00283 #endif  // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:24:39