/firmware/tests/gtest-1.4.0/include/gtest/internal/gtest-death-test-internal.h

http://github.com/makerbot/G3Firmware · C Header · 274 lines · 118 code · 34 blank · 122 comment · 12 complexity · 872a3bb23bacfdb16ece9fe99c1cf1fa MD5 · raw 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. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
  37. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
  38. #include <gtest/internal/gtest-internal.h>
  39. namespace testing {
  40. namespace internal {
  41. GTEST_DECLARE_string_(internal_run_death_test);
  42. // Names of the flags (needed for parsing Google Test flags).
  43. const char kDeathTestStyleFlag[] = "death_test_style";
  44. const char kDeathTestUseFork[] = "death_test_use_fork";
  45. const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
  46. #if GTEST_HAS_DEATH_TEST
  47. // DeathTest is a class that hides much of the complexity of the
  48. // GTEST_DEATH_TEST_ macro. It is abstract; its static Create method
  49. // returns a concrete class that depends on the prevailing death test
  50. // style, as defined by the --gtest_death_test_style and/or
  51. // --gtest_internal_run_death_test flags.
  52. // In describing the results of death tests, these terms are used with
  53. // the corresponding definitions:
  54. //
  55. // exit status: The integer exit information in the format specified
  56. // by wait(2)
  57. // exit code: The integer code passed to exit(3), _exit(2), or
  58. // returned from main()
  59. class DeathTest {
  60. public:
  61. // Create returns false if there was an error determining the
  62. // appropriate action to take for the current death test; for example,
  63. // if the gtest_death_test_style flag is set to an invalid value.
  64. // The LastMessage method will return a more detailed message in that
  65. // case. Otherwise, the DeathTest pointer pointed to by the "test"
  66. // argument is set. If the death test should be skipped, the pointer
  67. // is set to NULL; otherwise, it is set to the address of a new concrete
  68. // DeathTest object that controls the execution of the current test.
  69. static bool Create(const char* statement, const RE* regex,
  70. const char* file, int line, DeathTest** test);
  71. DeathTest();
  72. virtual ~DeathTest() { }
  73. // A helper class that aborts a death test when it's deleted.
  74. class ReturnSentinel {
  75. public:
  76. explicit ReturnSentinel(DeathTest* test) : test_(test) { }
  77. ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
  78. private:
  79. DeathTest* const test_;
  80. GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);
  81. } GTEST_ATTRIBUTE_UNUSED_;
  82. // An enumeration of possible roles that may be taken when a death
  83. // test is encountered. EXECUTE means that the death test logic should
  84. // be executed immediately. OVERSEE means that the program should prepare
  85. // the appropriate environment for a child process to execute the death
  86. // test, then wait for it to complete.
  87. enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
  88. // An enumeration of the two reasons that a test might be aborted.
  89. enum AbortReason { TEST_ENCOUNTERED_RETURN_STATEMENT, TEST_DID_NOT_DIE };
  90. // Assumes one of the above roles.
  91. virtual TestRole AssumeRole() = 0;
  92. // Waits for the death test to finish and returns its status.
  93. virtual int Wait() = 0;
  94. // Returns true if the death test passed; that is, the test process
  95. // exited during the test, its exit status matches a user-supplied
  96. // predicate, and its stderr output matches a user-supplied regular
  97. // expression.
  98. // The user-supplied predicate may be a macro expression rather
  99. // than a function pointer or functor, or else Wait and Passed could
  100. // be combined.
  101. virtual bool Passed(bool exit_status_ok) = 0;
  102. // Signals that the death test did not die as expected.
  103. virtual void Abort(AbortReason reason) = 0;
  104. // Returns a human-readable outcome message regarding the outcome of
  105. // the last death test.
  106. static const char* LastMessage();
  107. static void set_last_death_test_message(const String& message);
  108. private:
  109. // A string containing a description of the outcome of the last death test.
  110. static String last_death_test_message_;
  111. GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest);
  112. };
  113. // Factory interface for death tests. May be mocked out for testing.
  114. class DeathTestFactory {
  115. public:
  116. virtual ~DeathTestFactory() { }
  117. virtual bool Create(const char* statement, const RE* regex,
  118. const char* file, int line, DeathTest** test) = 0;
  119. };
  120. // A concrete DeathTestFactory implementation for normal use.
  121. class DefaultDeathTestFactory : public DeathTestFactory {
  122. public:
  123. virtual bool Create(const char* statement, const RE* regex,
  124. const char* file, int line, DeathTest** test);
  125. };
  126. // Returns true if exit_status describes a process that was terminated
  127. // by a signal, or exited normally with a nonzero exit code.
  128. bool ExitedUnsuccessfully(int exit_status);
  129. // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
  130. // ASSERT_EXIT*, and EXPECT_EXIT*.
  131. #define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \
  132. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  133. if (::testing::internal::AlwaysTrue()) { \
  134. const ::testing::internal::RE& gtest_regex = (regex); \
  135. ::testing::internal::DeathTest* gtest_dt; \
  136. if (!::testing::internal::DeathTest::Create(#statement, &gtest_regex, \
  137. __FILE__, __LINE__, &gtest_dt)) { \
  138. goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
  139. } \
  140. if (gtest_dt != NULL) { \
  141. ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \
  142. gtest_dt_ptr(gtest_dt); \
  143. switch (gtest_dt->AssumeRole()) { \
  144. case ::testing::internal::DeathTest::OVERSEE_TEST: \
  145. if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
  146. goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
  147. } \
  148. break; \
  149. case ::testing::internal::DeathTest::EXECUTE_TEST: { \
  150. ::testing::internal::DeathTest::ReturnSentinel \
  151. gtest_sentinel(gtest_dt); \
  152. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
  153. gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
  154. break; \
  155. } \
  156. } \
  157. } \
  158. } else \
  159. GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \
  160. fail(::testing::internal::DeathTest::LastMessage())
  161. // The symbol "fail" here expands to something into which a message
  162. // can be streamed.
  163. // A class representing the parsed contents of the
  164. // --gtest_internal_run_death_test flag, as it existed when
  165. // RUN_ALL_TESTS was called.
  166. class InternalRunDeathTestFlag {
  167. public:
  168. InternalRunDeathTestFlag(const String& file,
  169. int line,
  170. int index,
  171. int write_fd)
  172. : file_(file), line_(line), index_(index), write_fd_(write_fd) {}
  173. ~InternalRunDeathTestFlag() {
  174. if (write_fd_ >= 0)
  175. posix::Close(write_fd_);
  176. }
  177. String file() const { return file_; }
  178. int line() const { return line_; }
  179. int index() const { return index_; }
  180. int write_fd() const { return write_fd_; }
  181. private:
  182. String file_;
  183. int line_;
  184. int index_;
  185. int write_fd_;
  186. GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag);
  187. };
  188. // Returns a newly created InternalRunDeathTestFlag object with fields
  189. // initialized from the GTEST_FLAG(internal_run_death_test) flag if
  190. // the flag is specified; otherwise returns NULL.
  191. InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
  192. #else // GTEST_HAS_DEATH_TEST
  193. // This macro is used for implementing macros such as
  194. // EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where
  195. // death tests are not supported. Those macros must compile on such systems
  196. // iff EXPECT_DEATH and ASSERT_DEATH compile with the same parameters on
  197. // systems that support death tests. This allows one to write such a macro
  198. // on a system that does not support death tests and be sure that it will
  199. // compile on a death-test supporting system.
  200. //
  201. // Parameters:
  202. // statement - A statement that a macro such as EXPECT_DEATH would test
  203. // for program termination. This macro has to make sure this
  204. // statement is compiled but not executed, to ensure that
  205. // EXPECT_DEATH_IF_SUPPORTED compiles with a certain
  206. // parameter iff EXPECT_DEATH compiles with it.
  207. // regex - A regex that a macro such as EXPECT_DEATH would use to test
  208. // the output of statement. This parameter has to be
  209. // compiled but not evaluated by this macro, to ensure that
  210. // this macro only accepts expressions that a macro such as
  211. // EXPECT_DEATH would accept.
  212. // terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED
  213. // and a return statement for ASSERT_DEATH_IF_SUPPORTED.
  214. // This ensures that ASSERT_DEATH_IF_SUPPORTED will not
  215. // compile inside functions where ASSERT_DEATH doesn't
  216. // compile.
  217. //
  218. // The branch that has an always false condition is used to ensure that
  219. // statement and regex are compiled (and thus syntactically correct) but
  220. // never executed. The unreachable code macro protects the terminator
  221. // statement from generating an 'unreachable code' warning in case
  222. // statement unconditionally returns or throws. The Message constructor at
  223. // the end allows the syntax of streaming additional messages into the
  224. // macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.
  225. #define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \
  226. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  227. if (::testing::internal::AlwaysTrue()) { \
  228. GTEST_LOG_(WARNING) \
  229. << "Death tests are not supported on this platform.\n" \
  230. << "Statement '" #statement "' cannot be verified."; \
  231. } else if (::testing::internal::AlwaysFalse()) { \
  232. ::testing::internal::RE::PartialMatch(".*", (regex)); \
  233. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
  234. terminator; \
  235. } else \
  236. ::testing::Message()
  237. #endif // GTEST_HAS_DEATH_TEST
  238. } // namespace internal
  239. } // namespace testing
  240. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_