gmock/gtest/include/gtest/gtest-death-test.h
Go to the documentation of this file.
1 // Copyright 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: wan@google.com (Zhanyong Wan)
31 //
32 // The Google C++ Testing Framework (Google Test)
33 //
34 // This header file defines the public API for death tests. It is
35 // #included by gtest.h so a user doesn't need to include this
36 // directly.
37 
38 #ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
39 #define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
40 
41 #include "gtest/internal/gtest-death-test-internal.h"
42 
43 namespace testing
44 {
45 
46 // This flag controls the style of death tests. Valid values are "threadsafe",
47 // meaning that the death test child process will re-execute the test binary
48 // from the start, running only a single death test, or "fast",
49 // meaning that the child process will execute the test logic immediately
50 // after forking.
51 GTEST_DECLARE_string_(death_test_style);
52 
53 #if GTEST_HAS_DEATH_TEST
54 
55 namespace internal
56 {
57 
58 // Returns a Boolean value indicating whether the caller is currently
59 // executing in the context of the death test child process. Tools such as
60 // Valgrind heap checkers may need this to modify their behavior in death
61 // tests. IMPORTANT: This is an internal utility. Using it may break the
62 // implementation of death tests. User code MUST NOT use it.
63 GTEST_API_ bool InDeathTestChild();
64 
65 } // namespace internal
66 
67 // The following macros are useful for writing death tests.
68 
69 // Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is
70 // executed:
71 //
72 // 1. It generates a warning if there is more than one active
73 // thread. This is because it's safe to fork() or clone() only
74 // when there is a single thread.
75 //
76 // 2. The parent process clone()s a sub-process and runs the death
77 // test in it; the sub-process exits with code 0 at the end of the
78 // death test, if it hasn't exited already.
79 //
80 // 3. The parent process waits for the sub-process to terminate.
81 //
82 // 4. The parent process checks the exit code and error message of
83 // the sub-process.
84 //
85 // Examples:
86 //
87 // ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number");
88 // for (int i = 0; i < 5; i++) {
89 // EXPECT_DEATH(server.ProcessRequest(i),
90 // "Invalid request .* in ProcessRequest()")
91 // << "Failed to die on request " << i;
92 // }
93 //
94 // ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
95 //
96 // bool KilledBySIGHUP(int exit_code) {
97 // return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;
98 // }
99 //
100 // ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");
101 //
102 // On the regular expressions used in death tests:
103 //
104 // On POSIX-compliant systems (*nix), we use the <regex.h> library,
105 // which uses the POSIX extended regex syntax.
106 //
107 // On other platforms (e.g. Windows), we only support a simple regex
108 // syntax implemented as part of Google Test. This limited
109 // implementation should be enough most of the time when writing
110 // death tests; though it lacks many features you can find in PCRE
111 // or POSIX extended regex syntax. For example, we don't support
112 // union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and
113 // repetition count ("x{5,7}"), among others.
114 //
115 // Below is the syntax that we do support. We chose it to be a
116 // subset of both PCRE and POSIX extended regex, so it's easy to
117 // learn wherever you come from. In the following: 'A' denotes a
118 // literal character, period (.), or a single \\ escape sequence;
119 // 'x' and 'y' denote regular expressions; 'm' and 'n' are for
120 // natural numbers.
121 //
122 // c matches any literal character c
123 // \\d matches any decimal digit
124 // \\D matches any character that's not a decimal digit
125 // \\f matches \f
126 // \\n matches \n
127 // \\r matches \r
128 // \\s matches any ASCII whitespace, including \n
129 // \\S matches any character that's not a whitespace
130 // \\t matches \t
131 // \\v matches \v
132 // \\w matches any letter, _, or decimal digit
133 // \\W matches any character that \\w doesn't match
134 // \\c matches any literal character c, which must be a punctuation
135 // . matches any single character except \n
136 // A? matches 0 or 1 occurrences of A
137 // A* matches 0 or many occurrences of A
138 // A+ matches 1 or many occurrences of A
139 // ^ matches the beginning of a string (not that of each line)
140 // $ matches the end of a string (not that of each line)
141 // xy matches x followed by y
142 //
143 // If you accidentally use PCRE or POSIX extended regex features
144 // not implemented by us, you will get a run-time failure. In that
145 // case, please try to rewrite your regular expression within the
146 // above syntax.
147 //
148 // This implementation is *not* meant to be as highly tuned or robust
149 // as a compiled regex library, but should perform well enough for a
150 // death test, which already incurs significant overhead by launching
151 // a child process.
152 //
153 // Known caveats:
154 //
155 // A "threadsafe" style death test obtains the path to the test
156 // program from argv[0] and re-executes it in the sub-process. For
157 // simplicity, the current implementation doesn't search the PATH
158 // when launching the sub-process. This means that the user must
159 // invoke the test program via a path that contains at least one
160 // path separator (e.g. path/to/foo_test and
161 // /absolute/path/to/bar_test are fine, but foo_test is not). This
162 // is rarely a problem as people usually don't put the test binary
163 // directory in PATH.
164 //
165 // TODO(wan@google.com): make thread-safe death tests search the PATH.
166 
167 // Asserts that a given statement causes the program to exit, with an
168 // integer exit status that satisfies predicate, and emitting error output
169 // that matches regex.
170 # define ASSERT_EXIT(statement, predicate, regex) \
171  GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_)
172 
173 // Like ASSERT_EXIT, but continues on to successive tests in the
174 // test case, if any:
175 # define EXPECT_EXIT(statement, predicate, regex) \
176  GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_)
177 
178 // Asserts that a given statement causes the program to exit, either by
179 // explicitly exiting with a nonzero exit code or being killed by a
180 // signal, and emitting error output that matches regex.
181 # define ASSERT_DEATH(statement, regex) \
182  ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
183 
184 // Like ASSERT_DEATH, but continues on to successive tests in the
185 // test case, if any:
186 # define EXPECT_DEATH(statement, regex) \
187  EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
188 
189 // Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
190 
191 // Tests that an exit code describes a normal exit with a given exit code.
192 class GTEST_API_ ExitedWithCode
193 {
194 public:
195  explicit ExitedWithCode(int exit_code);
196  bool operator()(int exit_status) const;
197 private:
198  // No implementation - assignment is unsupported.
199  void operator=(const ExitedWithCode & other);
200 
201  const int exit_code_;
202 };
203 
204 # if !GTEST_OS_WINDOWS
205 // Tests that an exit code describes an exit due to termination by a
206 // given signal.
207 class GTEST_API_ KilledBySignal
208 {
209 public:
210  explicit KilledBySignal(int signum);
211  bool operator()(int exit_status) const;
212 private:
213  const int signum_;
214 };
215 # endif // !GTEST_OS_WINDOWS
216 
217 // EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.
218 // The death testing framework causes this to have interesting semantics,
219 // since the sideeffects of the call are only visible in opt mode, and not
220 // in debug mode.
221 //
222 // In practice, this can be used to test functions that utilize the
223 // LOG(DFATAL) macro using the following style:
224 //
225 // int DieInDebugOr12(int* sideeffect) {
226 // if (sideeffect) {
227 // *sideeffect = 12;
228 // }
229 // LOG(DFATAL) << "death";
230 // return 12;
231 // }
232 //
233 // TEST(TestCase, TestDieOr12WorksInDgbAndOpt) {
234 // int sideeffect = 0;
235 // // Only asserts in dbg.
236 // EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");
237 //
238 // #ifdef NDEBUG
239 // // opt-mode has sideeffect visible.
240 // EXPECT_EQ(12, sideeffect);
241 // #else
242 // // dbg-mode no visible sideeffect.
243 // EXPECT_EQ(0, sideeffect);
244 // #endif
245 // }
246 //
247 // This will assert that DieInDebugReturn12InOpt() crashes in debug
248 // mode, usually due to a DCHECK or LOG(DFATAL), but returns the
249 // appropriate fallback value (12 in this case) in opt mode. If you
250 // need to test that a function has appropriate side-effects in opt
251 // mode, include assertions against the side-effects. A general
252 // pattern for this is:
253 //
254 // EXPECT_DEBUG_DEATH({
255 // // Side-effects here will have an effect after this statement in
256 // // opt mode, but none in debug mode.
257 // EXPECT_EQ(12, DieInDebugOr12(&sideeffect));
258 // }, "death");
259 //
260 # ifdef NDEBUG
261 
262 # define EXPECT_DEBUG_DEATH(statement, regex) \
263  GTEST_EXECUTE_STATEMENT_(statement, regex)
264 
265 # define ASSERT_DEBUG_DEATH(statement, regex) \
266  GTEST_EXECUTE_STATEMENT_(statement, regex)
267 
268 # else
269 
270 # define EXPECT_DEBUG_DEATH(statement, regex) \
271  EXPECT_DEATH(statement, regex)
272 
273 # define ASSERT_DEBUG_DEATH(statement, regex) \
274  ASSERT_DEATH(statement, regex)
275 
276 # endif // NDEBUG for EXPECT_DEBUG_DEATH
277 #endif // GTEST_HAS_DEATH_TEST
278 
279 // EXPECT_DEATH_IF_SUPPORTED(statement, regex) and
280 // ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if
281 // death tests are supported; otherwise they just issue a warning. This is
282 // useful when you are combining death test assertions with normal test
283 // assertions in one test.
284 #if GTEST_HAS_DEATH_TEST
285 # define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
286  EXPECT_DEATH(statement, regex)
287 # define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
288  ASSERT_DEATH(statement, regex)
289 #else
290 # define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
291  GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, )
292 # define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
293  GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return)
294 #endif
295 
296 } // namespace testing
297 
298 #endif // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
#define GTEST_API_
GTEST_DECLARE_string_(death_test_style)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:12