Blame nss/external_tests/google_test/gtest/test/gtest_catch_exceptions_test.py

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