|
|
|||
File indexing completed on 2026-11-19 10:45:44
0001 // Copyright 2005, Google Inc. 0002 // All rights reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 0030 // The Google C++ Testing and Mocking Framework (Google Test) 0031 // 0032 // This header file defines the public API for death tests. It is 0033 // #included by gtest.h so a user doesn't need to include this 0034 // directly. 0035 0036 // IWYU pragma: private, include "gtest/gtest.h" 0037 // IWYU pragma: friend gtest/.* 0038 // IWYU pragma: friend gmock/.* 0039 0040 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ 0041 #define GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ 0042 0043 #include "gtest/internal/gtest-death-test-internal.h" 0044 0045 // This flag controls the style of death tests. Valid values are "threadsafe", 0046 // meaning that the death test child process will re-execute the test binary 0047 // from the start, running only a single death test, or "fast", 0048 // meaning that the child process will execute the test logic immediately 0049 // after forking. 0050 GTEST_DECLARE_string_(death_test_style); 0051 0052 namespace testing { 0053 0054 #ifdef GTEST_HAS_DEATH_TEST 0055 0056 namespace internal { 0057 0058 // Returns a Boolean value indicating whether the caller is currently 0059 // executing in the context of the death test child process. Tools such as 0060 // Valgrind heap checkers may need this to modify their behavior in death 0061 // tests. IMPORTANT: This is an internal utility. Using it may break the 0062 // implementation of death tests. User code MUST NOT use it. 0063 GTEST_API_ bool InDeathTestChild(); 0064 0065 } // namespace internal 0066 0067 // The following macros are useful for writing death tests. 0068 0069 // Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is 0070 // executed: 0071 // 0072 // 1. It generates a warning if there is more than one active 0073 // thread. This is because it's safe to fork() or clone() only 0074 // when there is a single thread. 0075 // 0076 // 2. The parent process clone()s a sub-process and runs the death 0077 // test in it; the sub-process exits with code 0 at the end of the 0078 // death test, if it hasn't exited already. 0079 // 0080 // 3. The parent process waits for the sub-process to terminate. 0081 // 0082 // 4. The parent process checks the exit code and error message of 0083 // the sub-process. 0084 // 0085 // Examples: 0086 // 0087 // ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number"); 0088 // for (int i = 0; i < 5; i++) { 0089 // EXPECT_DEATH(server.ProcessRequest(i), 0090 // "Invalid request .* in ProcessRequest()") 0091 // << "Failed to die on request " << i; 0092 // } 0093 // 0094 // ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting"); 0095 // 0096 // bool KilledBySIGHUP(int exit_code) { 0097 // return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP; 0098 // } 0099 // 0100 // ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!"); 0101 // 0102 // The final parameter to each of these macros is a matcher applied to any data 0103 // the sub-process wrote to stderr. For compatibility with existing tests, a 0104 // bare string is interpreted as a regular expression matcher. 0105 // 0106 // On the regular expressions used in death tests: 0107 // 0108 // On POSIX-compliant systems (*nix), we use the <regex.h> library, 0109 // which uses the POSIX extended regex syntax. 0110 // 0111 // On other platforms (e.g. Windows or Mac), we only support a simple regex 0112 // syntax implemented as part of Google Test. This limited 0113 // implementation should be enough most of the time when writing 0114 // death tests; though it lacks many features you can find in PCRE 0115 // or POSIX extended regex syntax. For example, we don't support 0116 // union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and 0117 // repetition count ("x{5,7}"), among others. 0118 // 0119 // Below is the syntax that we do support. We chose it to be a 0120 // subset of both PCRE and POSIX extended regex, so it's easy to 0121 // learn wherever you come from. In the following: 'A' denotes a 0122 // literal character, period (.), or a single \\ escape sequence; 0123 // 'x' and 'y' denote regular expressions; 'm' and 'n' are for 0124 // natural numbers. 0125 // 0126 // c matches any literal character c 0127 // \\d matches any decimal digit 0128 // \\D matches any character that's not a decimal digit 0129 // \\f matches \f 0130 // \\n matches \n 0131 // \\r matches \r 0132 // \\s matches any ASCII whitespace, including \n 0133 // \\S matches any character that's not a whitespace 0134 // \\t matches \t 0135 // \\v matches \v 0136 // \\w matches any letter, _, or decimal digit 0137 // \\W matches any character that \\w doesn't match 0138 // \\c matches any literal character c, which must be a punctuation 0139 // . matches any single character except \n 0140 // A? matches 0 or 1 occurrences of A 0141 // A* matches 0 or many occurrences of A 0142 // A+ matches 1 or many occurrences of A 0143 // ^ matches the beginning of a string (not that of each line) 0144 // $ matches the end of a string (not that of each line) 0145 // xy matches x followed by y 0146 // 0147 // If you accidentally use PCRE or POSIX extended regex features 0148 // not implemented by us, you will get a run-time failure. In that 0149 // case, please try to rewrite your regular expression within the 0150 // above syntax. 0151 // 0152 // This implementation is *not* meant to be as highly tuned or robust 0153 // as a compiled regex library, but should perform well enough for a 0154 // death test, which already incurs significant overhead by launching 0155 // a child process. 0156 // 0157 // Known caveats: 0158 // 0159 // A "threadsafe" style death test obtains the path to the test 0160 // program from argv[0] and re-executes it in the sub-process. For 0161 // simplicity, the current implementation doesn't search the PATH 0162 // when launching the sub-process. This means that the user must 0163 // invoke the test program via a path that contains at least one 0164 // path separator (e.g. path/to/foo_test and 0165 // /absolute/path/to/bar_test are fine, but foo_test is not). This 0166 // is rarely a problem as people usually don't put the test binary 0167 // directory in PATH. 0168 // 0169 0170 // Asserts that a given `statement` causes the program to exit, with an 0171 // integer exit status that satisfies `predicate`, and emitting error output 0172 // that matches `matcher`. 0173 #define ASSERT_EXIT(statement, predicate, matcher) \ 0174 GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_FATAL_FAILURE_) 0175 0176 // Like `ASSERT_EXIT`, but continues on to successive tests in the 0177 // test suite, if any: 0178 #define EXPECT_EXIT(statement, predicate, matcher) \ 0179 GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_NONFATAL_FAILURE_) 0180 0181 // Asserts that a given `statement` causes the program to exit, either by 0182 // explicitly exiting with a nonzero exit code or being killed by a 0183 // signal, and emitting error output that matches `matcher`. 0184 #define ASSERT_DEATH(statement, matcher) \ 0185 ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher) 0186 0187 // Like `ASSERT_DEATH`, but continues on to successive tests in the 0188 // test suite, if any: 0189 #define EXPECT_DEATH(statement, matcher) \ 0190 EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher) 0191 0192 // Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*: 0193 0194 // Tests that an exit code describes a normal exit with a given exit code. 0195 class GTEST_API_ [[nodiscard]] ExitedWithCode { 0196 public: 0197 explicit ExitedWithCode(int exit_code); 0198 ExitedWithCode(const ExitedWithCode&) = default; 0199 void operator=(const ExitedWithCode& other) = delete; 0200 bool operator()(int exit_status) const; 0201 0202 private: 0203 const int exit_code_; 0204 }; 0205 0206 #if !defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_FUCHSIA) 0207 // Tests that an exit code describes an exit due to termination by a 0208 // given signal. 0209 class GTEST_API_ [[nodiscard]] KilledBySignal { 0210 public: 0211 explicit KilledBySignal(int signum); 0212 bool operator()(int exit_status) const; 0213 0214 private: 0215 const int signum_; 0216 }; 0217 #endif // !GTEST_OS_WINDOWS 0218 0219 // EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode. 0220 // The death testing framework causes this to have interesting semantics, 0221 // since the sideeffects of the call are only visible in opt mode, and not 0222 // in debug mode. 0223 // 0224 // In practice, this can be used to test functions that utilize the 0225 // LOG(DFATAL) macro using the following style: 0226 // 0227 // int DieInDebugOr12(int* sideeffect) { 0228 // if (sideeffect) { 0229 // *sideeffect = 12; 0230 // } 0231 // LOG(DFATAL) << "death"; 0232 // return 12; 0233 // } 0234 // 0235 // TEST(TestSuite, TestDieOr12WorksInDgbAndOpt) { 0236 // int sideeffect = 0; 0237 // // Only asserts in dbg. 0238 // EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death"); 0239 // 0240 // #ifdef NDEBUG 0241 // // opt-mode has sideeffect visible. 0242 // EXPECT_EQ(12, sideeffect); 0243 // #else 0244 // // dbg-mode no visible sideeffect. 0245 // EXPECT_EQ(0, sideeffect); 0246 // #endif 0247 // } 0248 // 0249 // This will assert that DieInDebugReturn12InOpt() crashes in debug 0250 // mode, usually due to a DCHECK or LOG(DFATAL), but returns the 0251 // appropriate fallback value (12 in this case) in opt mode. If you 0252 // need to test that a function has appropriate side-effects in opt 0253 // mode, include assertions against the side-effects. A general 0254 // pattern for this is: 0255 // 0256 // EXPECT_DEBUG_DEATH({ 0257 // // Side-effects here will have an effect after this statement in 0258 // // opt mode, but none in debug mode. 0259 // EXPECT_EQ(12, DieInDebugOr12(&sideeffect)); 0260 // }, "death"); 0261 // 0262 #ifdef NDEBUG 0263 0264 #define EXPECT_DEBUG_DEATH(statement, regex) \ 0265 GTEST_EXECUTE_STATEMENT_(statement, regex) 0266 0267 #define ASSERT_DEBUG_DEATH(statement, regex) \ 0268 GTEST_EXECUTE_STATEMENT_(statement, regex) 0269 0270 #else 0271 0272 #define EXPECT_DEBUG_DEATH(statement, regex) EXPECT_DEATH(statement, regex) 0273 0274 #define ASSERT_DEBUG_DEATH(statement, regex) ASSERT_DEATH(statement, regex) 0275 0276 #endif // NDEBUG for EXPECT_DEBUG_DEATH 0277 #endif // GTEST_HAS_DEATH_TEST 0278 0279 // This macro is used for implementing macros such as 0280 // EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where 0281 // death tests are not supported. Those macros must compile on such systems 0282 // if and only if EXPECT_DEATH and ASSERT_DEATH compile with the same parameters 0283 // on systems that support death tests. This allows one to write such a macro on 0284 // a system that does not support death tests and be sure that it will compile 0285 // on a death-test supporting system. It is exposed publicly so that systems 0286 // that have death-tests with stricter requirements than GTEST_HAS_DEATH_TEST 0287 // can write their own equivalent of EXPECT_DEATH_IF_SUPPORTED and 0288 // ASSERT_DEATH_IF_SUPPORTED. 0289 // 0290 // Parameters: 0291 // statement - A statement that a macro such as EXPECT_DEATH would test 0292 // for program termination. This macro has to make sure this 0293 // statement is compiled but not executed, to ensure that 0294 // EXPECT_DEATH_IF_SUPPORTED compiles with a certain 0295 // parameter if and only if EXPECT_DEATH compiles with it. 0296 // regex_or_matcher - A regex that a macro such as EXPECT_DEATH would use 0297 // to test the output of statement. This parameter has to be 0298 // compiled but not evaluated by this macro, to ensure that 0299 // this macro only accepts expressions that a macro such as 0300 // EXPECT_DEATH would accept. 0301 // terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED 0302 // and a return statement for ASSERT_DEATH_IF_SUPPORTED. 0303 // This ensures that ASSERT_DEATH_IF_SUPPORTED will not 0304 // compile inside functions where ASSERT_DEATH doesn't 0305 // compile. 0306 // 0307 // The branch that has an always false condition is used to ensure that 0308 // statement and regex are compiled (and thus syntactically correct) but 0309 // never executed. The unreachable code macro protects the terminator 0310 // statement from generating an 'unreachable code' warning in case 0311 // statement unconditionally returns or throws. The Message constructor at 0312 // the end allows the syntax of streaming additional messages into the 0313 // macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH. 0314 #define GTEST_UNSUPPORTED_DEATH_TEST(statement, regex_or_matcher, terminator) \ 0315 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 0316 if (::testing::internal::AlwaysTrue()) { \ 0317 GTEST_LOG_(WARNING) << "Death tests are not supported on this platform.\n" \ 0318 << "Statement '" #statement "' cannot be verified."; \ 0319 } else if (::testing::internal::AlwaysFalse()) { \ 0320 (void)::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \ 0321 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 0322 terminator; \ 0323 } else \ 0324 ::testing::Message() 0325 0326 // EXPECT_DEATH_IF_SUPPORTED(statement, regex) and 0327 // ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if 0328 // death tests are supported; otherwise they just issue a warning. This is 0329 // useful when you are combining death test assertions with normal test 0330 // assertions in one test. 0331 #ifdef GTEST_HAS_DEATH_TEST 0332 #define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 0333 EXPECT_DEATH(statement, regex) 0334 #define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ 0335 ASSERT_DEATH(statement, regex) 0336 #else 0337 #define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 0338 GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, ) 0339 #define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ 0340 GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, return) 0341 #endif 0342 0343 } // namespace testing 0344 0345 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|