gmock/gtest/include/gtest/internal/gtest-death-test-internal.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 // Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
31 //
32 // The Google C++ Testing Framework (Google Test)
33 //
34 // This header file defines internal utilities needed for implementing
35 // death tests. They are subject to change without notice.
36 
37 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
38 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
39 
40 #include "gtest/internal/gtest-internal.h"
41 
42 #include <stdio.h>
43 
44 namespace testing
45 {
46 namespace internal
47 {
48 
49 GTEST_DECLARE_string_(internal_run_death_test);
50 
51 // Names of the flags (needed for parsing Google Test flags).
52 const char kDeathTestStyleFlag[] = "death_test_style";
53 const char kDeathTestUseFork[] = "death_test_use_fork";
54 const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
55 
56 #if GTEST_HAS_DEATH_TEST
57 
58 // DeathTest is a class that hides much of the complexity of the
59 // GTEST_DEATH_TEST_ macro. It is abstract; its static Create method
60 // returns a concrete class that depends on the prevailing death test
61 // style, as defined by the --gtest_death_test_style and/or
62 // --gtest_internal_run_death_test flags.
63 
64 // In describing the results of death tests, these terms are used with
65 // the corresponding definitions:
66 //
67 // exit status: The integer exit information in the format specified
68 // by wait(2)
69 // exit code: The integer code passed to exit(3), _exit(2), or
70 // returned from main()
71 class GTEST_API_ DeathTest
72 {
73 public:
74  // Create returns false if there was an error determining the
75  // appropriate action to take for the current death test; for example,
76  // if the gtest_death_test_style flag is set to an invalid value.
77  // The LastMessage method will return a more detailed message in that
78  // case. Otherwise, the DeathTest pointer pointed to by the "test"
79  // argument is set. If the death test should be skipped, the pointer
80  // is set to NULL; otherwise, it is set to the address of a new concrete
81  // DeathTest object that controls the execution of the current test.
82  static bool Create(const char * statement, const RE * regex,
83  const char * file, int line, DeathTest ** test);
84  DeathTest();
85  virtual ~DeathTest() { }
86 
87  // A helper class that aborts a death test when it's deleted.
88  class ReturnSentinel
89  {
90  public:
91  explicit ReturnSentinel(DeathTest * test) : test_(test) { }
92  ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
93  private:
94  DeathTest * const test_;
95  GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);
97 
98  // An enumeration of possible roles that may be taken when a death
99  // test is encountered. EXECUTE means that the death test logic should
100  // be executed immediately. OVERSEE means that the program should prepare
101  // the appropriate environment for a child process to execute the death
102  // test, then wait for it to complete.
103  enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
104 
105  // An enumeration of the three reasons that a test might be aborted.
106  enum AbortReason
107  {
108  TEST_ENCOUNTERED_RETURN_STATEMENT,
109  TEST_THREW_EXCEPTION,
110  TEST_DID_NOT_DIE
111  };
112 
113  // Assumes one of the above roles.
114  virtual TestRole AssumeRole() = 0;
115 
116  // Waits for the death test to finish and returns its status.
117  virtual int Wait() = 0;
118 
119  // Returns true if the death test passed; that is, the test process
120  // exited during the test, its exit status matches a user-supplied
121  // predicate, and its stderr output matches a user-supplied regular
122  // expression.
123  // The user-supplied predicate may be a macro expression rather
124  // than a function pointer or functor, or else Wait and Passed could
125  // be combined.
126  virtual bool Passed(bool exit_status_ok) = 0;
127 
128  // Signals that the death test did not die as expected.
129  virtual void Abort(AbortReason reason) = 0;
130 
131  // Returns a human-readable outcome message regarding the outcome of
132  // the last death test.
133  static const char * LastMessage();
134 
135  static void set_last_death_test_message(const std::string & message);
136 
137 private:
138  // A string containing a description of the outcome of the last death test.
139  static std::string last_death_test_message_;
140 
142 };
143 
144 // Factory interface for death tests. May be mocked out for testing.
145 class DeathTestFactory
146 {
147 public:
148  virtual ~DeathTestFactory() { }
149  virtual bool Create(const char * statement, const RE * regex,
150  const char * file, int line, DeathTest ** test) = 0;
151 };
152 
153 // A concrete DeathTestFactory implementation for normal use.
154 class DefaultDeathTestFactory : public DeathTestFactory
155 {
156 public:
157  virtual bool Create(const char * statement, const RE * regex,
158  const char * file, int line, DeathTest ** test);
159 };
160 
161 // Returns true if exit_status describes a process that was terminated
162 // by a signal, or exited normally with a nonzero exit code.
163 GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
164 
165 // Traps C++ exceptions escaping statement and reports them as test
166 // failures. Note that trapping SEH exceptions is not implemented here.
167 # if GTEST_HAS_EXCEPTIONS
168 # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
169  try { \
170  GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
171  } catch (const ::std::exception& gtest_exception) { \
172  fprintf(\
173  stderr, \
174  "\n%s: Caught std::exception-derived exception escaping the " \
175  "death test statement. Exception message: %s\n", \
176  ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \
177  gtest_exception.what()); \
178  fflush(stderr); \
179  death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
180  } catch (...) { \
181  death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
182  }
183 
184 # else
185 # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
186  GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
187 
188 # endif
189 
190 // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
191 // ASSERT_EXIT*, and EXPECT_EXIT*.
192 # define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \
193  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
194  if (::testing::internal::AlwaysTrue()) { \
195  const ::testing::internal::RE& gtest_regex = (regex); \
196  ::testing::internal::DeathTest* gtest_dt; \
197  if (!::testing::internal::DeathTest::Create(#statement, &gtest_regex, \
198  __FILE__, __LINE__, &gtest_dt)) { \
199  goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
200  } \
201  if (gtest_dt != NULL) { \
202  ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \
203  gtest_dt_ptr(gtest_dt); \
204  switch (gtest_dt->AssumeRole()) { \
205  case ::testing::internal::DeathTest::OVERSEE_TEST: \
206  if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
207  goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
208  } \
209  break; \
210  case ::testing::internal::DeathTest::EXECUTE_TEST: { \
211  ::testing::internal::DeathTest::ReturnSentinel \
212  gtest_sentinel(gtest_dt); \
213  GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \
214  gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
215  break; \
216  } \
217  default: \
218  break; \
219  } \
220  } \
221  } else \
222  GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \
223  fail(::testing::internal::DeathTest::LastMessage())
224 // The symbol "fail" here expands to something into which a message
225 // can be streamed.
226 
227 // This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in
228 // NDEBUG mode. In this case we need the statements to be executed, the regex is
229 // ignored, and the macro must accept a streamed message even though the message
230 // is never printed.
231 # define GTEST_EXECUTE_STATEMENT_(statement, regex) \
232  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
233  if (::testing::internal::AlwaysTrue()) { \
234  GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
235  } else \
236  ::testing::Message()
237 
238 // A class representing the parsed contents of the
239 // --gtest_internal_run_death_test flag, as it existed when
240 // RUN_ALL_TESTS was called.
241 class InternalRunDeathTestFlag
242 {
243 public:
244  InternalRunDeathTestFlag(const std::string & a_file,
245  int a_line,
246  int an_index,
247  int a_write_fd)
248  : file_(a_file), line_(a_line), index_(an_index),
249  write_fd_(a_write_fd) {}
250 
251  ~InternalRunDeathTestFlag()
252  {
253  if (write_fd_ >= 0)
254  { posix::Close(write_fd_); }
255  }
256 
257  const std::string & file() const { return file_; }
258  int line() const { return line_; }
259  int index() const { return index_; }
260  int write_fd() const { return write_fd_; }
261 
262 private:
263  std::string file_;
264  int line_;
265  int index_;
266  int write_fd_;
267 
268  GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag);
269 };
270 
271 // Returns a newly created InternalRunDeathTestFlag object with fields
272 // initialized from the GTEST_FLAG(internal_run_death_test) flag if
273 // the flag is specified; otherwise returns NULL.
274 InternalRunDeathTestFlag * ParseInternalRunDeathTestFlag();
275 
276 #else // GTEST_HAS_DEATH_TEST
277 
278 // This macro is used for implementing macros such as
279 // EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where
280 // death tests are not supported. Those macros must compile on such systems
281 // iff EXPECT_DEATH and ASSERT_DEATH compile with the same parameters on
282 // systems that support death tests. This allows one to write such a macro
283 // on a system that does not support death tests and be sure that it will
284 // compile on a death-test supporting system.
285 //
286 // Parameters:
287 // statement - A statement that a macro such as EXPECT_DEATH would test
288 // for program termination. This macro has to make sure this
289 // statement is compiled but not executed, to ensure that
290 // EXPECT_DEATH_IF_SUPPORTED compiles with a certain
291 // parameter iff EXPECT_DEATH compiles with it.
292 // regex - A regex that a macro such as EXPECT_DEATH would use to test
293 // the output of statement. This parameter has to be
294 // compiled but not evaluated by this macro, to ensure that
295 // this macro only accepts expressions that a macro such as
296 // EXPECT_DEATH would accept.
297 // terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED
298 // and a return statement for ASSERT_DEATH_IF_SUPPORTED.
299 // This ensures that ASSERT_DEATH_IF_SUPPORTED will not
300 // compile inside functions where ASSERT_DEATH doesn't
301 // compile.
302 //
303 // The branch that has an always false condition is used to ensure that
304 // statement and regex are compiled (and thus syntactically correct) but
305 // never executed. The unreachable code macro protects the terminator
306 // statement from generating an 'unreachable code' warning in case
307 // statement unconditionally returns or throws. The Message constructor at
308 // the end allows the syntax of streaming additional messages into the
309 // macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.
310 # define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \
311  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
312  if (::testing::internal::AlwaysTrue()) { \
313  GTEST_LOG_(WARNING) \
314  << "Death tests are not supported on this platform.\n" \
315  << "Statement '" #statement "' cannot be verified."; \
316  } else if (::testing::internal::AlwaysFalse()) { \
317  ::testing::internal::RE::PartialMatch(".*", (regex)); \
318  GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
319  terminator; \
320  } else \
321  ::testing::Message()
322 
323 #endif // GTEST_HAS_DEATH_TEST
324 
325 } // namespace internal
326 } // namespace testing
327 
328 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define GTEST_API_
class testing::internal::GTestFlagSaver GTEST_ATTRIBUTE_UNUSED_
Definition: test.py:1
message
Definition: server.py:50
GTEST_DECLARE_string_(internal_run_death_test)


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