michael@0: #!/usr/bin/env python michael@0: # michael@0: # Copyright 2010 Google Inc. All Rights Reserved. michael@0: # michael@0: # Redistribution and use in source and binary forms, with or without michael@0: # modification, are permitted provided that the following conditions are michael@0: # met: michael@0: # michael@0: # * Redistributions of source code must retain the above copyright michael@0: # notice, this list of conditions and the following disclaimer. michael@0: # * Redistributions in binary form must reproduce the above michael@0: # copyright notice, this list of conditions and the following disclaimer michael@0: # in the documentation and/or other materials provided with the michael@0: # distribution. michael@0: # * Neither the name of Google Inc. nor the names of its michael@0: # contributors may be used to endorse or promote products derived from michael@0: # this software without specific prior written permission. michael@0: # michael@0: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: """Tests Google Test's exception catching behavior. michael@0: michael@0: This script invokes gtest_catch_exceptions_test_ and michael@0: gtest_catch_exceptions_ex_test_ (programs written with michael@0: Google Test) and verifies their output. michael@0: """ michael@0: michael@0: __author__ = 'vladl@google.com (Vlad Losev)' michael@0: michael@0: import os michael@0: michael@0: import gtest_test_utils michael@0: michael@0: # Constants. michael@0: FLAG_PREFIX = '--gtest_' michael@0: LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' michael@0: NO_CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions=0' michael@0: FILTER_FLAG = FLAG_PREFIX + 'filter' michael@0: michael@0: # Path to the gtest_catch_exceptions_ex_test_ binary, compiled with michael@0: # exceptions enabled. michael@0: EX_EXE_PATH = gtest_test_utils.GetTestExecutablePath( michael@0: 'gtest_catch_exceptions_ex_test_') michael@0: michael@0: # Path to the gtest_catch_exceptions_test_ binary, compiled with michael@0: # exceptions disabled. michael@0: EXE_PATH = gtest_test_utils.GetTestExecutablePath( michael@0: 'gtest_catch_exceptions_no_ex_test_') michael@0: michael@0: TEST_LIST = gtest_test_utils.Subprocess([EXE_PATH, LIST_TESTS_FLAG]).output michael@0: michael@0: SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST michael@0: michael@0: if SUPPORTS_SEH_EXCEPTIONS: michael@0: BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH]).output michael@0: michael@0: EX_BINARY_OUTPUT = gtest_test_utils.Subprocess([EX_EXE_PATH]).output michael@0: michael@0: # The tests. michael@0: if SUPPORTS_SEH_EXCEPTIONS: michael@0: # pylint:disable-msg=C6302 michael@0: class CatchSehExceptionsTest(gtest_test_utils.TestCase): michael@0: """Tests exception-catching behavior.""" michael@0: michael@0: michael@0: def TestSehExceptions(self, test_output): michael@0: self.assert_('SEH exception with code 0x2a thrown ' michael@0: 'in the test fixture\'s constructor' michael@0: in test_output) michael@0: self.assert_('SEH exception with code 0x2a thrown ' michael@0: 'in the test fixture\'s destructor' michael@0: in test_output) michael@0: self.assert_('SEH exception with code 0x2a thrown in SetUpTestCase()' michael@0: in test_output) michael@0: self.assert_('SEH exception with code 0x2a thrown in TearDownTestCase()' michael@0: in test_output) michael@0: self.assert_('SEH exception with code 0x2a thrown in SetUp()' michael@0: in test_output) michael@0: self.assert_('SEH exception with code 0x2a thrown in TearDown()' michael@0: in test_output) michael@0: self.assert_('SEH exception with code 0x2a thrown in the test body' michael@0: in test_output) michael@0: michael@0: def testCatchesSehExceptionsWithCxxExceptionsEnabled(self): michael@0: self.TestSehExceptions(EX_BINARY_OUTPUT) michael@0: michael@0: def testCatchesSehExceptionsWithCxxExceptionsDisabled(self): michael@0: self.TestSehExceptions(BINARY_OUTPUT) michael@0: michael@0: michael@0: class CatchCxxExceptionsTest(gtest_test_utils.TestCase): michael@0: """Tests C++ exception-catching behavior. michael@0: michael@0: Tests in this test case verify that: michael@0: * C++ exceptions are caught and logged as C++ (not SEH) exceptions michael@0: * Exception thrown affect the remainder of the test work flow in the michael@0: expected manner. michael@0: """ michael@0: michael@0: def testCatchesCxxExceptionsInFixtureConstructor(self): michael@0: self.assert_('C++ exception with description ' michael@0: '"Standard C++ exception" thrown ' michael@0: 'in the test fixture\'s constructor' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('unexpected' not in EX_BINARY_OUTPUT, michael@0: 'This failure belongs in this test only if ' michael@0: '"CxxExceptionInConstructorTest" (no quotes) ' michael@0: 'appears on the same line as words "called unexpectedly"') michael@0: michael@0: if ('CxxExceptionInDestructorTest.ThrowsExceptionInDestructor' in michael@0: EX_BINARY_OUTPUT): michael@0: michael@0: def testCatchesCxxExceptionsInFixtureDestructor(self): michael@0: self.assert_('C++ exception with description ' michael@0: '"Standard C++ exception" thrown ' michael@0: 'in the test fixture\'s destructor' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInDestructorTest::TearDownTestCase() ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: michael@0: def testCatchesCxxExceptionsInSetUpTestCase(self): michael@0: self.assert_('C++ exception with description "Standard C++ exception"' michael@0: ' thrown in SetUpTestCase()' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInConstructorTest::TearDownTestCase() ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInSetUpTestCaseTest constructor ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInSetUpTestCaseTest destructor ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInSetUpTestCaseTest::SetUp() ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInSetUpTestCaseTest::TearDown() ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInSetUpTestCaseTest test body ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: michael@0: def testCatchesCxxExceptionsInTearDownTestCase(self): michael@0: self.assert_('C++ exception with description "Standard C++ exception"' michael@0: ' thrown in TearDownTestCase()' michael@0: in EX_BINARY_OUTPUT) michael@0: michael@0: def testCatchesCxxExceptionsInSetUp(self): michael@0: self.assert_('C++ exception with description "Standard C++ exception"' michael@0: ' thrown in SetUp()' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInSetUpTest::TearDownTestCase() ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInSetUpTest destructor ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInSetUpTest::TearDown() ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('unexpected' not in EX_BINARY_OUTPUT, michael@0: 'This failure belongs in this test only if ' michael@0: '"CxxExceptionInSetUpTest" (no quotes) ' michael@0: 'appears on the same line as words "called unexpectedly"') michael@0: michael@0: def testCatchesCxxExceptionsInTearDown(self): michael@0: self.assert_('C++ exception with description "Standard C++ exception"' michael@0: ' thrown in TearDown()' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInTearDownTest::TearDownTestCase() ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInTearDownTest destructor ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: michael@0: def testCatchesCxxExceptionsInTestBody(self): michael@0: self.assert_('C++ exception with description "Standard C++ exception"' michael@0: ' thrown in the test body' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInTestBodyTest::TearDownTestCase() ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInTestBodyTest destructor ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: self.assert_('CxxExceptionInTestBodyTest::TearDown() ' michael@0: 'called as expected.' michael@0: in EX_BINARY_OUTPUT) michael@0: michael@0: def testCatchesNonStdCxxExceptions(self): michael@0: self.assert_('Unknown C++ exception thrown in the test body' michael@0: in EX_BINARY_OUTPUT) michael@0: michael@0: def testUnhandledCxxExceptionsAbortTheProgram(self): michael@0: # Filters out SEH exception tests on Windows. Unhandled SEH exceptions michael@0: # cause tests to show pop-up windows there. michael@0: FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*' michael@0: # By default, Google Test doesn't catch the exceptions. michael@0: uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess( michael@0: [EX_EXE_PATH, michael@0: NO_CATCH_EXCEPTIONS_FLAG, michael@0: FITLER_OUT_SEH_TESTS_FLAG]).output michael@0: michael@0: self.assert_('Unhandled C++ exception terminating the program' michael@0: in uncaught_exceptions_ex_binary_output) michael@0: self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: gtest_test_utils.Main()