protobuf/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py
Go to the documentation of this file.
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 
31 """Tests Google Test's exception catching behavior.
32 
33 This script invokes googletest-catch-exceptions-test_ and
34 googletest-catch-exceptions-ex-test_ (programs written with
35 Google Test) and verifies their output.
36 """
37 
38 import gtest_test_utils
39 
40 # Constants.
41 FLAG_PREFIX = '--gtest_'
42 LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
43 NO_CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions=0'
44 FILTER_FLAG = FLAG_PREFIX + 'filter'
45 
46 # Path to the googletest-catch-exceptions-ex-test_ binary, compiled with
47 # exceptions enabled.
49  'googletest-catch-exceptions-ex-test_')
50 
51 # Path to the googletest-catch-exceptions-test_ binary, compiled with
52 # exceptions disabled.
54  'googletest-catch-exceptions-no-ex-test_')
55 
56 environ = gtest_test_utils.environ
57 SetEnvVar = gtest_test_utils.SetEnvVar
58 
59 # Tests in this file run a Google-Test-based test program and expect it
60 # to terminate prematurely. Therefore they are incompatible with
61 # the premature-exit-file protocol by design. Unset the
62 # premature-exit filepath to prevent Google Test from creating
63 # the file.
64 SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
65 
66 TEST_LIST = gtest_test_utils.Subprocess(
67  [EXE_PATH, LIST_TESTS_FLAG], env=environ).output
68 
69 SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST
70 
71 if SUPPORTS_SEH_EXCEPTIONS:
72  BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH], env=environ).output
73 
74 EX_BINARY_OUTPUT = gtest_test_utils.Subprocess(
75  [EX_EXE_PATH], env=environ).output
76 
77 
78 # The tests.
79 if SUPPORTS_SEH_EXCEPTIONS:
80  # pylint:disable-msg=C6302
81  class CatchSehExceptionsTest(gtest_test_utils.TestCase):
82  """Tests exception-catching behavior."""
83 
84 
85  def TestSehExceptions(self, test_output):
86  self.assert_('SEH exception with code 0x2a thrown '
87  'in the test fixture\'s constructor'
88  in test_output)
89  self.assert_('SEH exception with code 0x2a thrown '
90  'in the test fixture\'s destructor'
91  in test_output)
92  self.assert_('SEH exception with code 0x2a thrown in SetUpTestSuite()'
93  in test_output)
94  self.assert_('SEH exception with code 0x2a thrown in TearDownTestSuite()'
95  in test_output)
96  self.assert_('SEH exception with code 0x2a thrown in SetUp()'
97  in test_output)
98  self.assert_('SEH exception with code 0x2a thrown in TearDown()'
99  in test_output)
100  self.assert_('SEH exception with code 0x2a thrown in the test body'
101  in test_output)
102 
104  self.TestSehExceptions(EX_BINARY_OUTPUT)
105 
107  self.TestSehExceptions(BINARY_OUTPUT)
108 
109 
111  """Tests C++ exception-catching behavior.
112 
113  Tests in this test case verify that:
114  * C++ exceptions are caught and logged as C++ (not SEH) exceptions
115  * Exception thrown affect the remainder of the test work flow in the
116  expected manner.
117  """
118 
120  self.assert_('C++ exception with description '
121  '"Standard C++ exception" thrown '
122  'in the test fixture\'s constructor'
123  in EX_BINARY_OUTPUT)
124  self.assert_('unexpected' not in EX_BINARY_OUTPUT,
125  'This failure belongs in this test only if '
126  '"CxxExceptionInConstructorTest" (no quotes) '
127  'appears on the same line as words "called unexpectedly"')
128 
129  if ('CxxExceptionInDestructorTest.ThrowsExceptionInDestructor' in
130  EX_BINARY_OUTPUT):
131 
133  self.assert_('C++ exception with description '
134  '"Standard C++ exception" thrown '
135  'in the test fixture\'s destructor'
136  in EX_BINARY_OUTPUT)
137  self.assert_('CxxExceptionInDestructorTest::TearDownTestSuite() '
138  'called as expected.'
139  in EX_BINARY_OUTPUT)
140 
142  self.assert_('C++ exception with description "Standard C++ exception"'
143  ' thrown in SetUpTestSuite()' in EX_BINARY_OUTPUT)
144  self.assert_('CxxExceptionInConstructorTest::TearDownTestSuite() '
145  'called as expected.'
146  in EX_BINARY_OUTPUT)
147  self.assert_('CxxExceptionInSetUpTestSuiteTest constructor '
148  'called as expected.'
149  in EX_BINARY_OUTPUT)
150  self.assert_('CxxExceptionInSetUpTestSuiteTest destructor '
151  'called as expected.'
152  in EX_BINARY_OUTPUT)
153  self.assert_('CxxExceptionInSetUpTestSuiteTest::SetUp() '
154  'called as expected.'
155  in EX_BINARY_OUTPUT)
156  self.assert_('CxxExceptionInSetUpTestSuiteTest::TearDown() '
157  'called as expected.'
158  in EX_BINARY_OUTPUT)
159  self.assert_('CxxExceptionInSetUpTestSuiteTest test body '
160  'called as expected.'
161  in EX_BINARY_OUTPUT)
162 
164  self.assert_('C++ exception with description "Standard C++ exception"'
165  ' thrown in TearDownTestSuite()' in EX_BINARY_OUTPUT)
166 
168  self.assert_('C++ exception with description "Standard C++ exception"'
169  ' thrown in SetUp()'
170  in EX_BINARY_OUTPUT)
171  self.assert_('CxxExceptionInSetUpTest::TearDownTestSuite() '
172  'called as expected.'
173  in EX_BINARY_OUTPUT)
174  self.assert_('CxxExceptionInSetUpTest destructor '
175  'called as expected.'
176  in EX_BINARY_OUTPUT)
177  self.assert_('CxxExceptionInSetUpTest::TearDown() '
178  'called as expected.'
179  in EX_BINARY_OUTPUT)
180  self.assert_('unexpected' not in EX_BINARY_OUTPUT,
181  'This failure belongs in this test only if '
182  '"CxxExceptionInSetUpTest" (no quotes) '
183  'appears on the same line as words "called unexpectedly"')
184 
186  self.assert_('C++ exception with description "Standard C++ exception"'
187  ' thrown in TearDown()'
188  in EX_BINARY_OUTPUT)
189  self.assert_('CxxExceptionInTearDownTest::TearDownTestSuite() '
190  'called as expected.'
191  in EX_BINARY_OUTPUT)
192  self.assert_('CxxExceptionInTearDownTest destructor '
193  'called as expected.'
194  in EX_BINARY_OUTPUT)
195 
197  self.assert_('C++ exception with description "Standard C++ exception"'
198  ' thrown in the test body'
199  in EX_BINARY_OUTPUT)
200  self.assert_('CxxExceptionInTestBodyTest::TearDownTestSuite() '
201  'called as expected.'
202  in EX_BINARY_OUTPUT)
203  self.assert_('CxxExceptionInTestBodyTest destructor '
204  'called as expected.'
205  in EX_BINARY_OUTPUT)
206  self.assert_('CxxExceptionInTestBodyTest::TearDown() '
207  'called as expected.'
208  in EX_BINARY_OUTPUT)
209 
211  self.assert_('Unknown C++ exception thrown in the test body'
212  in EX_BINARY_OUTPUT)
213 
215  # Filters out SEH exception tests on Windows. Unhandled SEH exceptions
216  # cause tests to show pop-up windows there.
217  FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*'
218  # By default, Google Test doesn't catch the exceptions.
219  uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess(
220  [EX_EXE_PATH,
221  NO_CATCH_EXCEPTIONS_FLAG,
222  FITLER_OUT_SEH_TESTS_FLAG],
223  env=environ).output
224 
225  self.assert_('Unhandled C++ exception terminating the program'
226  in uncaught_exceptions_ex_binary_output)
227  self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output)
228 
229 
230 if __name__ == '__main__':
googletest-catch-exceptions-test.CatchSehExceptionsTest.testCatchesSehExceptionsWithCxxExceptionsDisabled
def testCatchesSehExceptionsWithCxxExceptionsDisabled(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:106
googletest-catch-exceptions-test.CatchCxxExceptionsTest.testCatchesCxxExceptionsInSetUpTestCase
def testCatchesCxxExceptionsInSetUpTestCase(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:143
googletest-catch-exceptions-test.CatchCxxExceptionsTest.testCatchesCxxExceptionsInTearDownTestCase
def testCatchesCxxExceptionsInTearDownTestCase(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:166
googletest-catch-exceptions-test.CatchCxxExceptionsTest.testCatchesCxxExceptionsInSetUp
def testCatchesCxxExceptionsInSetUp(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:171
gtest_test_utils.Subprocess
Definition: bloaty/third_party/googletest/googletest/test/gtest_test_utils.py:202
googletest-catch-exceptions-test.CatchSehExceptionsTest.TestSehExceptions
def TestSehExceptions(self, test_output)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:85
googletest-catch-exceptions-test.CatchCxxExceptionsTest.testCatchesCxxExceptionsInFixtureDestructor
def testCatchesCxxExceptionsInFixtureDestructor(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:133
gtest_test_utils.GetTestExecutablePath
def GetTestExecutablePath(executable_name, build_dir=None)
Definition: bloaty/third_party/googletest/googletest/test/gtest_test_utils.py:151
googletest-catch-exceptions-test.CatchCxxExceptionsTest.testCatchesCxxExceptionsInTestBody
def testCatchesCxxExceptionsInTestBody(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:200
googletest-catch-exceptions-test.CatchCxxExceptionsTest.testCatchesCxxExceptionsInTearDown
def testCatchesCxxExceptionsInTearDown(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:189
googletest-catch-exceptions-test.CatchCxxExceptionsTest.testCatchesCxxExceptionsInFixtureConstructor
def testCatchesCxxExceptionsInFixtureConstructor(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:119
googletest-catch-exceptions-test.CatchCxxExceptionsTest.testUnhandledCxxExceptionsAbortTheProgram
def testUnhandledCxxExceptionsAbortTheProgram(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:219
googletest-catch-exceptions-test.SetEnvVar
SetEnvVar
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:57
googletest-catch-exceptions-test.CatchCxxExceptionsTest.testCatchesNonStdCxxExceptions
def testCatchesNonStdCxxExceptions(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:214
googletest-catch-exceptions-test.CatchCxxExceptionsTest
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:110
gtest_test_utils.Main
def Main()
Definition: bloaty/third_party/googletest/googletest/test/gtest_test_utils.py:301
googletest-catch-exceptions-test.CatchSehExceptionsTest.testCatchesSehExceptionsWithCxxExceptionsEnabled
def testCatchesSehExceptionsWithCxxExceptionsEnabled(self)
Definition: bloaty/third_party/googletest/googletest/test/googletest-catch-exceptions-test.py:103
gtest_test_utils.TestCase
TestCase
Definition: bloaty/third_party/googletest/googletest/test/gtest_test_utils.py:74


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:39