2022-01-29 00:03:43 +01:00
|
|
|
|
|
|
|
// Copyright Catch2 Authors
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
2022-10-28 11:22:53 +02:00
|
|
|
// (See accompanying file LICENSE.txt or copy at
|
2022-01-29 00:03:43 +01:00
|
|
|
// https://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2020-01-20 23:24:04 +01:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2020-05-19 21:16:33 +02:00
|
|
|
#include <catch2/catch_translate_exception.hpp>
|
2020-03-01 19:59:18 +01:00
|
|
|
#include <catch2/matchers/catch_matchers_string.hpp>
|
2010-11-10 00:24:00 +01:00
|
|
|
|
|
|
|
#include <string>
|
2011-01-07 11:22:24 +01:00
|
|
|
#include <stdexcept>
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2017-03-06 10:52:21 +01:00
|
|
|
#ifdef _MSC_VER
|
2018-03-07 10:08:35 +01:00
|
|
|
#pragma warning(disable:4702) // Unreachable code -- unconditional throws and so on
|
2017-03-06 10:52:21 +01:00
|
|
|
#endif
|
2017-09-07 16:51:33 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wweak-vtables"
|
2017-11-21 21:39:40 +01:00
|
|
|
#pragma clang diagnostic ignored "-Wmissing-noreturn"
|
2018-09-01 22:34:29 +02:00
|
|
|
#pragma clang diagnostic ignored "-Wunreachable-code"
|
2017-09-07 16:51:33 +02:00
|
|
|
#endif
|
2017-03-06 10:52:21 +01:00
|
|
|
|
2021-06-20 16:25:57 +02:00
|
|
|
namespace {
|
2017-11-15 08:48:21 +01:00
|
|
|
|
2021-06-20 16:25:57 +02:00
|
|
|
int thisThrows() {
|
|
|
|
throw std::domain_error("expected exception");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-11-15 08:48:21 +01:00
|
|
|
|
2021-06-20 16:25:57 +02:00
|
|
|
int thisDoesntThrow() {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-15 08:48:21 +01:00
|
|
|
|
2021-06-20 16:25:57 +02:00
|
|
|
class CustomException {
|
|
|
|
public:
|
|
|
|
explicit CustomException(const std::string& msg)
|
|
|
|
: m_msg(msg) {}
|
2017-11-15 08:48:21 +01:00
|
|
|
|
2022-05-02 12:13:29 +02:00
|
|
|
std::string const& getMessage() const {
|
2021-06-20 16:25:57 +02:00
|
|
|
return m_msg;
|
|
|
|
}
|
2017-11-15 08:48:21 +01:00
|
|
|
|
2021-06-20 16:25:57 +02:00
|
|
|
private:
|
|
|
|
std::string m_msg;
|
|
|
|
};
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2021-06-20 16:25:57 +02:00
|
|
|
class CustomStdException : public std::exception {
|
|
|
|
public:
|
|
|
|
explicit CustomStdException(const std::string& msg)
|
|
|
|
: m_msg(msg) {}
|
2022-05-02 12:13:29 +02:00
|
|
|
~CustomStdException() noexcept override = default;
|
2017-11-15 08:48:21 +01:00
|
|
|
|
2022-05-02 12:13:29 +02:00
|
|
|
CustomStdException( CustomStdException const& ) = default;
|
|
|
|
CustomStdException& operator=( CustomStdException const& ) = default;
|
|
|
|
|
|
|
|
std::string const& getMessage() const {
|
2021-06-20 16:25:57 +02:00
|
|
|
return m_msg;
|
|
|
|
}
|
2017-11-15 08:48:21 +01:00
|
|
|
|
2021-06-20 16:25:57 +02:00
|
|
|
private:
|
|
|
|
std::string m_msg;
|
|
|
|
};
|
2017-11-15 08:48:21 +01:00
|
|
|
|
2021-06-20 16:25:57 +02:00
|
|
|
[[noreturn]] void throwCustom() {
|
|
|
|
throw CustomException("custom exception - not std");
|
|
|
|
}
|
2017-11-15 08:48:21 +01:00
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "When checked exceptions are thrown they can be expected or unexpected", "[!throws]" ) {
|
2010-12-14 10:00:09 +01:00
|
|
|
REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
|
|
|
|
REQUIRE_NOTHROW( thisDoesntThrow() );
|
|
|
|
REQUIRE_THROWS( thisThrows() );
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "Expected exceptions that don't throw or unexpected exceptions fail the test", "[.][failing][!throws]" ) {
|
2010-11-10 00:24:00 +01:00
|
|
|
CHECK_THROWS_AS( thisThrows(), std::string );
|
|
|
|
CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
|
|
|
|
CHECK_NOTHROW( thisThrows() );
|
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "When unchecked exceptions are thrown directly they are always failures", "[.][failing][!throws]" ) {
|
2017-11-21 21:39:40 +01:00
|
|
|
throw std::domain_error( "unexpected exception" );
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "An unchecked exception reports the line of the last assertion", "[.][failing][!throws]" ) {
|
2012-11-17 18:22:37 +01:00
|
|
|
CHECK( 1 == 1 );
|
2017-11-21 21:39:40 +01:00
|
|
|
throw std::domain_error( "unexpected exception" );
|
2012-11-17 18:22:37 +01:00
|
|
|
}
|
2014-05-01 20:07:02 +02:00
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "When unchecked exceptions are thrown from sections they are always failures", "[.][failing][!throws]" ) {
|
|
|
|
SECTION( "section name" ) {
|
2017-11-21 21:39:40 +01:00
|
|
|
throw std::domain_error("unexpected exception");
|
2013-02-19 20:45:09 +01:00
|
|
|
}
|
|
|
|
}
|
2012-11-17 18:22:37 +01:00
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "When unchecked exceptions are thrown from functions they are always failures", "[.][failing][!throws]" ) {
|
2013-04-20 22:04:32 +02:00
|
|
|
CHECK( thisThrows() == 0 );
|
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "When unchecked exceptions are thrown during a REQUIRE the test should abort fail", "[.][failing][!throws]" ) {
|
2014-04-12 20:07:24 +02:00
|
|
|
REQUIRE( thisThrows() == 0 );
|
|
|
|
FAIL( "This should never happen" );
|
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "When unchecked exceptions are thrown during a CHECK the test should continue", "[.][failing][!throws]" ) {
|
2017-01-23 18:56:41 +01:00
|
|
|
try {
|
|
|
|
CHECK(thisThrows() == 0);
|
|
|
|
}
|
|
|
|
catch(...) {
|
|
|
|
FAIL( "This should never happen" );
|
|
|
|
}
|
2014-04-12 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "When unchecked exceptions are thrown, but caught, they do not affect the test", "[!throws]" ) {
|
|
|
|
try {
|
2010-11-10 00:24:00 +01:00
|
|
|
throw std::domain_error( "unexpected exception" );
|
|
|
|
}
|
2017-11-15 08:48:21 +01:00
|
|
|
catch(...) {}
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2015-11-18 09:39:21 +01:00
|
|
|
|
2020-05-19 21:23:35 +02:00
|
|
|
CATCH_TRANSLATE_EXCEPTION( CustomException const& ex ) {
|
2011-04-20 20:09:41 +02:00
|
|
|
return ex.getMessage();
|
|
|
|
}
|
2011-04-20 16:40:40 +02:00
|
|
|
|
2020-05-19 21:23:35 +02:00
|
|
|
CATCH_TRANSLATE_EXCEPTION( CustomStdException const& ex ) {
|
2015-11-18 09:39:21 +01:00
|
|
|
return ex.getMessage();
|
|
|
|
}
|
|
|
|
|
2020-05-19 21:23:35 +02:00
|
|
|
CATCH_TRANSLATE_EXCEPTION( double const& ex ) {
|
2017-05-02 23:51:03 +02:00
|
|
|
return Catch::Detail::stringify( ex );
|
2011-04-20 16:40:40 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" ) {
|
2017-11-21 21:39:40 +01:00
|
|
|
throw CustomException( "custom exception" );
|
2013-04-23 21:52:49 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE("Custom std-exceptions can be custom translated", "[.][failing][!throws]" ) {
|
2022-05-02 12:13:29 +02:00
|
|
|
throw CustomStdException( "custom std exception" );
|
2015-11-18 09:39:21 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "Custom exceptions can be translated when testing for nothrow", "[.][failing][!throws]" ) {
|
2013-04-23 21:52:49 +02:00
|
|
|
REQUIRE_NOTHROW( throwCustom() );
|
2011-04-20 20:09:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "Custom exceptions can be translated when testing for throwing as something else", "[.][failing][!throws]" ) {
|
2013-04-23 21:52:49 +02:00
|
|
|
REQUIRE_THROWS_AS( throwCustom(), std::exception );
|
2011-04-20 20:09:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-15 08:48:21 +01:00
|
|
|
TEST_CASE( "Unexpected exceptions can be translated", "[.][failing][!throws]" ) {
|
2017-11-21 21:39:40 +01:00
|
|
|
throw double( 3.14 );
|
2011-04-20 20:09:41 +02:00
|
|
|
}
|
2011-04-21 20:43:55 +02:00
|
|
|
|
2018-02-15 19:58:49 +01:00
|
|
|
TEST_CASE("Thrown string literals are translated", "[.][failing][!throws]") {
|
|
|
|
throw "For some reason someone is throwing a string literal!";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("thrown std::strings are translated", "[.][failing][!throws]") {
|
|
|
|
throw std::string{ "Why would you throw a std::string?" };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-23 18:56:41 +01:00
|
|
|
TEST_CASE( "Exception messages can be tested for", "[!throws]" ) {
|
2015-07-16 00:02:25 +02:00
|
|
|
using namespace Catch::Matchers;
|
2015-07-13 16:03:04 +02:00
|
|
|
SECTION( "exact match" )
|
|
|
|
REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
|
|
|
|
SECTION( "different case" )
|
2015-07-16 00:02:25 +02:00
|
|
|
REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) );
|
2015-07-13 16:03:04 +02:00
|
|
|
SECTION( "wildcarded" ) {
|
2015-07-16 00:02:25 +02:00
|
|
|
REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) );
|
|
|
|
REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) );
|
2021-09-23 23:28:59 +02:00
|
|
|
REQUIRE_THROWS_WITH( thisThrows(), ContainsSubstring( "except" ) );
|
|
|
|
REQUIRE_THROWS_WITH( thisThrows(), ContainsSubstring( "exCept", Catch::CaseSensitive::No ) );
|
2015-07-13 16:03:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:56:41 +01:00
|
|
|
TEST_CASE( "Mismatching exception messages failing the test", "[.][failing][!throws]" ) {
|
2015-07-13 07:34:41 +02:00
|
|
|
REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
|
|
|
|
REQUIRE_THROWS_WITH( thisThrows(), "should fail" );
|
2015-07-16 00:02:25 +02:00
|
|
|
REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
|
2015-07-13 07:34:41 +02:00
|
|
|
}
|
2017-03-31 00:06:35 +02:00
|
|
|
|
2017-08-11 11:38:29 +02:00
|
|
|
TEST_CASE( "#748 - captures with unexpected exceptions", "[.][failing][!throws][!shouldfail]" ) {
|
2017-03-31 00:06:35 +02:00
|
|
|
int answer = 42;
|
2017-04-04 11:31:13 +02:00
|
|
|
CAPTURE( answer );
|
2017-03-31 00:06:35 +02:00
|
|
|
// the message should be printed on the first two sections but not on the third
|
|
|
|
SECTION( "outside assertions" ) {
|
|
|
|
thisThrows();
|
|
|
|
}
|
|
|
|
SECTION( "inside REQUIRE_NOTHROW" ) {
|
2017-04-04 11:31:13 +02:00
|
|
|
REQUIRE_NOTHROW( thisThrows() );
|
2017-03-31 00:06:35 +02:00
|
|
|
}
|
|
|
|
SECTION( "inside REQUIRE_THROWS" ) {
|
2017-04-04 11:31:13 +02:00
|
|
|
REQUIRE_THROWS( thisThrows() );
|
2017-03-31 00:06:35 +02:00
|
|
|
}
|
2017-04-04 11:31:13 +02:00
|
|
|
}
|
2017-09-07 16:51:33 +02:00
|
|
|
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|