Blame googletest/test/gtest-death-test_ex_test.cc

Packit bd1cd8
// Copyright 2010, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: vladl@google.com (Vlad Losev)
Packit bd1cd8
//
Packit bd1cd8
// Tests that verify interaction of exceptions and death tests.
Packit bd1cd8
Packit bd1cd8
#include "gtest/gtest-death-test.h"
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_DEATH_TEST
Packit bd1cd8
Packit bd1cd8
# if GTEST_HAS_SEH
Packit bd1cd8
#  include <windows.h>          // For RaiseException().
Packit bd1cd8
# endif
Packit bd1cd8
Packit bd1cd8
# include "gtest/gtest-spi.h"
Packit bd1cd8
Packit bd1cd8
# if GTEST_HAS_EXCEPTIONS
Packit bd1cd8
Packit bd1cd8
#  include <exception>  // For std::exception.
Packit bd1cd8
Packit bd1cd8
// Tests that death tests report thrown exceptions as failures and that the
Packit bd1cd8
// exceptions do not escape death test macros.
Packit bd1cd8
TEST(CxxExceptionDeathTest, ExceptionIsFailure) {
Packit bd1cd8
  try {
Packit bd1cd8
    EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(throw 1, ""), "threw an exception");
Packit bd1cd8
  } catch (...) {  // NOLINT
Packit bd1cd8
    FAIL() << "An exception escaped a death test macro invocation "
Packit bd1cd8
           << "with catch_exceptions "
Packit bd1cd8
           << (testing::GTEST_FLAG(catch_exceptions) ? "enabled" : "disabled");
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
class TestException : public std::exception {
Packit bd1cd8
 public:
Packit bd1cd8
  virtual const char* what() const throw() { return "exceptional message"; }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
TEST(CxxExceptionDeathTest, PrintsMessageForStdExceptions) {
Packit bd1cd8
  // Verifies that the exception message is quoted in the failure text.
Packit bd1cd8
  EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(throw TestException(), ""),
Packit bd1cd8
                          "exceptional message");
Packit bd1cd8
  // Verifies that the location is mentioned in the failure text.
Packit bd1cd8
  EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(throw TestException(), ""),
Packit bd1cd8
                          "gtest-death-test_ex_test.cc");
Packit bd1cd8
}
Packit bd1cd8
# endif  // GTEST_HAS_EXCEPTIONS
Packit bd1cd8
Packit bd1cd8
# if GTEST_HAS_SEH
Packit bd1cd8
// Tests that enabling interception of SEH exceptions with the
Packit bd1cd8
// catch_exceptions flag does not interfere with SEH exceptions being
Packit bd1cd8
// treated as death by death tests.
Packit bd1cd8
TEST(SehExceptionDeasTest, CatchExceptionsDoesNotInterfere) {
Packit bd1cd8
  EXPECT_DEATH(RaiseException(42, 0x0, 0, NULL), "")
Packit bd1cd8
      << "with catch_exceptions "
Packit bd1cd8
      << (testing::GTEST_FLAG(catch_exceptions) ? "enabled" : "disabled");
Packit bd1cd8
}
Packit bd1cd8
# endif
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_HAS_DEATH_TEST
Packit bd1cd8
Packit bd1cd8
int main(int argc, char** argv) {
Packit bd1cd8
  testing::InitGoogleTest(&argc, argv);
Packit bd1cd8
  testing::GTEST_FLAG(catch_exceptions) = GTEST_ENABLE_CATCH_EXCEPTIONS_ != 0;
Packit bd1cd8
  return RUN_ALL_TESTS();
Packit bd1cd8
}