You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
9.1 KiB

  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2010 Google Inc. All Rights Reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """Tests Google Test's exception catching behavior.
  31. This script invokes gtest_catch_exceptions_test_ and
  32. gtest_catch_exceptions_ex_test_ (programs written with
  33. Google Test) and verifies their output.
  34. """
  35. __author__ = 'vladl@google.com (Vlad Losev)'
  36. import os
  37. import gtest_test_utils
  38. # Constants.
  39. FLAG_PREFIX = '--gtest_'
  40. LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
  41. NO_CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions=0'
  42. FILTER_FLAG = FLAG_PREFIX + 'filter'
  43. # Path to the gtest_catch_exceptions_ex_test_ binary, compiled with
  44. # exceptions enabled.
  45. EX_EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  46. 'gtest_catch_exceptions_ex_test_')
  47. # Path to the gtest_catch_exceptions_test_ binary, compiled with
  48. # exceptions disabled.
  49. EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  50. 'gtest_catch_exceptions_no_ex_test_')
  51. TEST_LIST = gtest_test_utils.Subprocess([EXE_PATH, LIST_TESTS_FLAG]).output
  52. SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST
  53. if SUPPORTS_SEH_EXCEPTIONS:
  54. BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH]).output
  55. EX_BINARY_OUTPUT = gtest_test_utils.Subprocess([EX_EXE_PATH]).output
  56. # The tests.
  57. if SUPPORTS_SEH_EXCEPTIONS:
  58. # pylint:disable-msg=C6302
  59. class CatchSehExceptionsTest(gtest_test_utils.TestCase):
  60. """Tests exception-catching behavior."""
  61. def TestSehExceptions(self, test_output):
  62. self.assert_('SEH exception with code 0x2a thrown '
  63. 'in the test fixture\'s constructor'
  64. in test_output)
  65. self.assert_('SEH exception with code 0x2a thrown '
  66. 'in the test fixture\'s destructor'
  67. in test_output)
  68. self.assert_('SEH exception with code 0x2a thrown in SetUpTestCase()'
  69. in test_output)
  70. self.assert_('SEH exception with code 0x2a thrown in TearDownTestCase()'
  71. in test_output)
  72. self.assert_('SEH exception with code 0x2a thrown in SetUp()'
  73. in test_output)
  74. self.assert_('SEH exception with code 0x2a thrown in TearDown()'
  75. in test_output)
  76. self.assert_('SEH exception with code 0x2a thrown in the test body'
  77. in test_output)
  78. def testCatchesSehExceptionsWithCxxExceptionsEnabled(self):
  79. self.TestSehExceptions(EX_BINARY_OUTPUT)
  80. def testCatchesSehExceptionsWithCxxExceptionsDisabled(self):
  81. self.TestSehExceptions(BINARY_OUTPUT)
  82. class CatchCxxExceptionsTest(gtest_test_utils.TestCase):
  83. """Tests C++ exception-catching behavior.
  84. Tests in this test case verify that:
  85. * C++ exceptions are caught and logged as C++ (not SEH) exceptions
  86. * Exception thrown affect the remainder of the test work flow in the
  87. expected manner.
  88. """
  89. def testCatchesCxxExceptionsInFixtureConstructor(self):
  90. self.assert_('C++ exception with description '
  91. '"Standard C++ exception" thrown '
  92. 'in the test fixture\'s constructor'
  93. in EX_BINARY_OUTPUT)
  94. self.assert_('unexpected' not in EX_BINARY_OUTPUT,
  95. 'This failure belongs in this test only if '
  96. '"CxxExceptionInConstructorTest" (no quotes) '
  97. 'appears on the same line as words "called unexpectedly"')
  98. def testCatchesCxxExceptionsInFixtureDestructor(self):
  99. self.assert_('C++ exception with description '
  100. '"Standard C++ exception" thrown '
  101. 'in the test fixture\'s destructor'
  102. in EX_BINARY_OUTPUT)
  103. self.assert_('CxxExceptionInDestructorTest::TearDownTestCase() '
  104. 'called as expected.'
  105. in EX_BINARY_OUTPUT)
  106. def testCatchesCxxExceptionsInSetUpTestCase(self):
  107. self.assert_('C++ exception with description "Standard C++ exception"'
  108. ' thrown in SetUpTestCase()'
  109. in EX_BINARY_OUTPUT)
  110. self.assert_('CxxExceptionInConstructorTest::TearDownTestCase() '
  111. 'called as expected.'
  112. in EX_BINARY_OUTPUT)
  113. self.assert_('CxxExceptionInSetUpTestCaseTest constructor '
  114. 'called as expected.'
  115. in EX_BINARY_OUTPUT)
  116. self.assert_('CxxExceptionInSetUpTestCaseTest destructor '
  117. 'called as expected.'
  118. in EX_BINARY_OUTPUT)
  119. self.assert_('CxxExceptionInSetUpTestCaseTest::SetUp() '
  120. 'called as expected.'
  121. in EX_BINARY_OUTPUT)
  122. self.assert_('CxxExceptionInSetUpTestCaseTest::TearDown() '
  123. 'called as expected.'
  124. in EX_BINARY_OUTPUT)
  125. self.assert_('CxxExceptionInSetUpTestCaseTest test body '
  126. 'called as expected.'
  127. in EX_BINARY_OUTPUT)
  128. def testCatchesCxxExceptionsInTearDownTestCase(self):
  129. self.assert_('C++ exception with description "Standard C++ exception"'
  130. ' thrown in TearDownTestCase()'
  131. in EX_BINARY_OUTPUT)
  132. def testCatchesCxxExceptionsInSetUp(self):
  133. self.assert_('C++ exception with description "Standard C++ exception"'
  134. ' thrown in SetUp()'
  135. in EX_BINARY_OUTPUT)
  136. self.assert_('CxxExceptionInSetUpTest::TearDownTestCase() '
  137. 'called as expected.'
  138. in EX_BINARY_OUTPUT)
  139. self.assert_('CxxExceptionInSetUpTest destructor '
  140. 'called as expected.'
  141. in EX_BINARY_OUTPUT)
  142. self.assert_('CxxExceptionInSetUpTest::TearDown() '
  143. 'called as expected.'
  144. in EX_BINARY_OUTPUT)
  145. self.assert_('unexpected' not in EX_BINARY_OUTPUT,
  146. 'This failure belongs in this test only if '
  147. '"CxxExceptionInSetUpTest" (no quotes) '
  148. 'appears on the same line as words "called unexpectedly"')
  149. def testCatchesCxxExceptionsInTearDown(self):
  150. self.assert_('C++ exception with description "Standard C++ exception"'
  151. ' thrown in TearDown()'
  152. in EX_BINARY_OUTPUT)
  153. self.assert_('CxxExceptionInTearDownTest::TearDownTestCase() '
  154. 'called as expected.'
  155. in EX_BINARY_OUTPUT)
  156. self.assert_('CxxExceptionInTearDownTest destructor '
  157. 'called as expected.'
  158. in EX_BINARY_OUTPUT)
  159. def testCatchesCxxExceptionsInTestBody(self):
  160. self.assert_('C++ exception with description "Standard C++ exception"'
  161. ' thrown in the test body'
  162. in EX_BINARY_OUTPUT)
  163. self.assert_('CxxExceptionInTestBodyTest::TearDownTestCase() '
  164. 'called as expected.'
  165. in EX_BINARY_OUTPUT)
  166. self.assert_('CxxExceptionInTestBodyTest destructor '
  167. 'called as expected.'
  168. in EX_BINARY_OUTPUT)
  169. self.assert_('CxxExceptionInTestBodyTest::TearDown() '
  170. 'called as expected.'
  171. in EX_BINARY_OUTPUT)
  172. def testCatchesNonStdCxxExceptions(self):
  173. self.assert_('Unknown C++ exception thrown in the test body'
  174. in EX_BINARY_OUTPUT)
  175. def testUnhandledCxxExceptionsAbortTheProgram(self):
  176. # Filters out SEH exception tests on Windows. Unhandled SEH exceptions
  177. # cause tests to show pop-up windows there.
  178. FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*'
  179. # By default, Google Test doesn't catch the exceptions.
  180. uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess(
  181. [EX_EXE_PATH,
  182. NO_CATCH_EXCEPTIONS_FLAG,
  183. FITLER_OUT_SEH_TESTS_FLAG]).output
  184. self.assert_('Unhandled C++ exception terminating the program'
  185. in uncaught_exceptions_ex_binary_output)
  186. self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output)
  187. if __name__ == '__main__':
  188. gtest_test_utils.Main()