gtest-death-test-internal.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 // Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
00031 //
00032 // The Google C++ Testing Framework (Google Test)
00033 //
00034 // This header file defines internal utilities needed for implementing
00035 // death tests.  They are subject to change without notice.
00036 
00037 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
00038 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
00039 
00040 #include "gtest/internal/gtest-internal.h"
00041 
00042 #include <stdio.h>
00043 
00044 namespace testing {
00045 namespace internal {
00046 
00047 GTEST_DECLARE_string_(internal_run_death_test);
00048 
00049 // Names of the flags (needed for parsing Google Test flags).
00050 const char kDeathTestStyleFlag[] = "death_test_style";
00051 const char kDeathTestUseFork[] = "death_test_use_fork";
00052 const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
00053 
00054 #if GTEST_HAS_DEATH_TEST
00055 
00056 // DeathTest is a class that hides much of the complexity of the
00057 // GTEST_DEATH_TEST_ macro.  It is abstract; its static Create method
00058 // returns a concrete class that depends on the prevailing death test
00059 // style, as defined by the --gtest_death_test_style and/or
00060 // --gtest_internal_run_death_test flags.
00061 
00062 // In describing the results of death tests, these terms are used with
00063 // the corresponding definitions:
00064 //
00065 // exit status:  The integer exit information in the format specified
00066 //               by wait(2)
00067 // exit code:    The integer code passed to exit(3), _exit(2), or
00068 //               returned from main()
00069 class GTEST_API_ DeathTest {
00070  public:
00071   // Create returns false if there was an error determining the
00072   // appropriate action to take for the current death test; for example,
00073   // if the gtest_death_test_style flag is set to an invalid value.
00074   // The LastMessage method will return a more detailed message in that
00075   // case.  Otherwise, the DeathTest pointer pointed to by the "test"
00076   // argument is set.  If the death test should be skipped, the pointer
00077   // is set to NULL; otherwise, it is set to the address of a new concrete
00078   // DeathTest object that controls the execution of the current test.
00079   static bool Create(const char* statement, const RE* regex,
00080                      const char* file, int line, DeathTest** test);
00081   DeathTest();
00082   virtual ~DeathTest() { }
00083 
00084   // A helper class that aborts a death test when it's deleted.
00085   class ReturnSentinel {
00086    public:
00087     explicit ReturnSentinel(DeathTest* test) : test_(test) { }
00088     ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
00089    private:
00090     DeathTest* const test_;
00091     GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);
00092   } GTEST_ATTRIBUTE_UNUSED_;
00093 
00094   // An enumeration of possible roles that may be taken when a death
00095   // test is encountered.  EXECUTE means that the death test logic should
00096   // be executed immediately.  OVERSEE means that the program should prepare
00097   // the appropriate environment for a child process to execute the death
00098   // test, then wait for it to complete.
00099   enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
00100 
00101   // An enumeration of the three reasons that a test might be aborted.
00102   enum AbortReason {
00103     TEST_ENCOUNTERED_RETURN_STATEMENT,
00104     TEST_THREW_EXCEPTION,
00105     TEST_DID_NOT_DIE
00106   };
00107 
00108   // Assumes one of the above roles.
00109   virtual TestRole AssumeRole() = 0;
00110 
00111   // Waits for the death test to finish and returns its status.
00112   virtual int Wait() = 0;
00113 
00114   // Returns true if the death test passed; that is, the test process
00115   // exited during the test, its exit status matches a user-supplied
00116   // predicate, and its stderr output matches a user-supplied regular
00117   // expression.
00118   // The user-supplied predicate may be a macro expression rather
00119   // than a function pointer or functor, or else Wait and Passed could
00120   // be combined.
00121   virtual bool Passed(bool exit_status_ok) = 0;
00122 
00123   // Signals that the death test did not die as expected.
00124   virtual void Abort(AbortReason reason) = 0;
00125 
00126   // Returns a human-readable outcome message regarding the outcome of
00127   // the last death test.
00128   static const char* LastMessage();
00129 
00130   static void set_last_death_test_message(const std::string& message);
00131 
00132  private:
00133   // A string containing a description of the outcome of the last death test.
00134   static std::string last_death_test_message_;
00135 
00136   GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest);
00137 };
00138 
00139 // Factory interface for death tests.  May be mocked out for testing.
00140 class DeathTestFactory {
00141  public:
00142   virtual ~DeathTestFactory() { }
00143   virtual bool Create(const char* statement, const RE* regex,
00144                       const char* file, int line, DeathTest** test) = 0;
00145 };
00146 
00147 // A concrete DeathTestFactory implementation for normal use.
00148 class DefaultDeathTestFactory : public DeathTestFactory {
00149  public:
00150   virtual bool Create(const char* statement, const RE* regex,
00151                       const char* file, int line, DeathTest** test);
00152 };
00153 
00154 // Returns true if exit_status describes a process that was terminated
00155 // by a signal, or exited normally with a nonzero exit code.
00156 GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
00157 
00158 // Traps C++ exceptions escaping statement and reports them as test
00159 // failures. Note that trapping SEH exceptions is not implemented here.
00160 # if GTEST_HAS_EXCEPTIONS
00161 #  define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
00162   try { \
00163     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
00164   } catch (const ::std::exception& gtest_exception) { \
00165     fprintf(\
00166         stderr, \
00167         "\n%s: Caught std::exception-derived exception escaping the " \
00168         "death test statement. Exception message: %s\n", \
00169         ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \
00170         gtest_exception.what()); \
00171     fflush(stderr); \
00172     death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
00173   } catch (...) { \
00174     death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
00175   }
00176 
00177 # else
00178 #  define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
00179   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
00180 
00181 # endif
00182 
00183 // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
00184 // ASSERT_EXIT*, and EXPECT_EXIT*.
00185 # define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \
00186   GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
00187   if (::testing::internal::AlwaysTrue()) { \
00188     const ::testing::internal::RE& gtest_regex = (regex); \
00189     ::testing::internal::DeathTest* gtest_dt; \
00190     if (!::testing::internal::DeathTest::Create(#statement, &gtest_regex, \
00191         __FILE__, __LINE__, &gtest_dt)) { \
00192       goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
00193     } \
00194     if (gtest_dt != NULL) { \
00195       ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \
00196           gtest_dt_ptr(gtest_dt); \
00197       switch (gtest_dt->AssumeRole()) { \
00198         case ::testing::internal::DeathTest::OVERSEE_TEST: \
00199           if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
00200             goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
00201           } \
00202           break; \
00203         case ::testing::internal::DeathTest::EXECUTE_TEST: { \
00204           ::testing::internal::DeathTest::ReturnSentinel \
00205               gtest_sentinel(gtest_dt); \
00206           GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \
00207           gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
00208           break; \
00209         } \
00210         default: \
00211           break; \
00212       } \
00213     } \
00214   } else \
00215     GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \
00216       fail(::testing::internal::DeathTest::LastMessage())
00217 // The symbol "fail" here expands to something into which a message
00218 // can be streamed.
00219 
00220 // This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in
00221 // NDEBUG mode. In this case we need the statements to be executed, the regex is
00222 // ignored, and the macro must accept a streamed message even though the message
00223 // is never printed.
00224 # define GTEST_EXECUTE_STATEMENT_(statement, regex) \
00225   GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
00226   if (::testing::internal::AlwaysTrue()) { \
00227      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
00228   } else \
00229     ::testing::Message()
00230 
00231 // A class representing the parsed contents of the
00232 // --gtest_internal_run_death_test flag, as it existed when
00233 // RUN_ALL_TESTS was called.
00234 class InternalRunDeathTestFlag {
00235  public:
00236   InternalRunDeathTestFlag(const std::string& a_file,
00237                            int a_line,
00238                            int an_index,
00239                            int a_write_fd)
00240       : file_(a_file), line_(a_line), index_(an_index),
00241         write_fd_(a_write_fd) {}
00242 
00243   ~InternalRunDeathTestFlag() {
00244     if (write_fd_ >= 0)
00245       posix::Close(write_fd_);
00246   }
00247 
00248   const std::string& file() const { return file_; }
00249   int line() const { return line_; }
00250   int index() const { return index_; }
00251   int write_fd() const { return write_fd_; }
00252 
00253  private:
00254   std::string file_;
00255   int line_;
00256   int index_;
00257   int write_fd_;
00258 
00259   GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag);
00260 };
00261 
00262 // Returns a newly created InternalRunDeathTestFlag object with fields
00263 // initialized from the GTEST_FLAG(internal_run_death_test) flag if
00264 // the flag is specified; otherwise returns NULL.
00265 InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
00266 
00267 #else  // GTEST_HAS_DEATH_TEST
00268 
00269 // This macro is used for implementing macros such as
00270 // EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where
00271 // death tests are not supported. Those macros must compile on such systems
00272 // iff EXPECT_DEATH and ASSERT_DEATH compile with the same parameters on
00273 // systems that support death tests. This allows one to write such a macro
00274 // on a system that does not support death tests and be sure that it will
00275 // compile on a death-test supporting system.
00276 //
00277 // Parameters:
00278 //   statement -  A statement that a macro such as EXPECT_DEATH would test
00279 //                for program termination. This macro has to make sure this
00280 //                statement is compiled but not executed, to ensure that
00281 //                EXPECT_DEATH_IF_SUPPORTED compiles with a certain
00282 //                parameter iff EXPECT_DEATH compiles with it.
00283 //   regex     -  A regex that a macro such as EXPECT_DEATH would use to test
00284 //                the output of statement.  This parameter has to be
00285 //                compiled but not evaluated by this macro, to ensure that
00286 //                this macro only accepts expressions that a macro such as
00287 //                EXPECT_DEATH would accept.
00288 //   terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED
00289 //                and a return statement for ASSERT_DEATH_IF_SUPPORTED.
00290 //                This ensures that ASSERT_DEATH_IF_SUPPORTED will not
00291 //                compile inside functions where ASSERT_DEATH doesn't
00292 //                compile.
00293 //
00294 //  The branch that has an always false condition is used to ensure that
00295 //  statement and regex are compiled (and thus syntactically correct) but
00296 //  never executed. The unreachable code macro protects the terminator
00297 //  statement from generating an 'unreachable code' warning in case
00298 //  statement unconditionally returns or throws. The Message constructor at
00299 //  the end allows the syntax of streaming additional messages into the
00300 //  macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.
00301 # define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \
00302     GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
00303     if (::testing::internal::AlwaysTrue()) { \
00304       GTEST_LOG_(WARNING) \
00305           << "Death tests are not supported on this platform.\n" \
00306           << "Statement '" #statement "' cannot be verified."; \
00307     } else if (::testing::internal::AlwaysFalse()) { \
00308       ::testing::internal::RE::PartialMatch(".*", (regex)); \
00309       GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
00310       terminator; \
00311     } else \
00312       ::testing::Message()
00313 
00314 #endif  // GTEST_HAS_DEATH_TEST
00315 
00316 }  // namespace internal
00317 }  // namespace testing
00318 
00319 #endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:03