Blame projects/SelfTest/UsageTests/Exception.tests.cpp

rpm-build a7f80b
/*
rpm-build a7f80b
 *  Created by Phil on 09/11/2010.
rpm-build a7f80b
 *  Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
rpm-build a7f80b
 *
rpm-build a7f80b
 *  Distributed under the Boost Software License, Version 1.0. (See accompanying
rpm-build a7f80b
 *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
rpm-build a7f80b
 */
rpm-build a7f80b
rpm-build a7f80b
#include "catch.hpp"
rpm-build a7f80b
rpm-build a7f80b
#include <string>
rpm-build a7f80b
#include <stdexcept>
rpm-build a7f80b
rpm-build a7f80b
#ifdef _MSC_VER
rpm-build a7f80b
#pragma warning(disable:4702) // Unreachable code -- unconditional throws and so on
rpm-build a7f80b
#endif
rpm-build a7f80b
#ifdef __clang__
rpm-build a7f80b
#pragma clang diagnostic push
rpm-build a7f80b
#pragma clang diagnostic ignored "-Wweak-vtables"
rpm-build a7f80b
#pragma clang diagnostic ignored "-Wmissing-noreturn"
rpm-build a7f80b
#endif
rpm-build a7f80b
rpm-build a7f80b
namespace { namespace ExceptionTests {
rpm-build a7f80b
rpm-build a7f80b
#ifndef EXCEPTION_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
rpm-build a7f80b
#define EXCEPTION_TEST_HELPERS_INCLUDED
rpm-build a7f80b
rpm-build a7f80b
int thisThrows() {
rpm-build a7f80b
    throw std::domain_error( "expected exception" );
rpm-build a7f80b
    return 1;
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
int thisDoesntThrow() {
rpm-build a7f80b
    return 0;
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
class CustomException {
rpm-build a7f80b
public:
rpm-build a7f80b
    explicit CustomException( const std::string& msg )
rpm-build a7f80b
    : m_msg( msg )
rpm-build a7f80b
    {}
rpm-build a7f80b
rpm-build a7f80b
    std::string getMessage() const {
rpm-build a7f80b
        return m_msg;
rpm-build a7f80b
    }
rpm-build a7f80b
rpm-build a7f80b
private:
rpm-build a7f80b
    std::string m_msg;
rpm-build a7f80b
};
rpm-build a7f80b
rpm-build a7f80b
class CustomStdException : public std::exception {
rpm-build a7f80b
public:
rpm-build a7f80b
    explicit CustomStdException( const std::string& msg )
rpm-build a7f80b
    : m_msg( msg )
rpm-build a7f80b
    {}
rpm-build a7f80b
    ~CustomStdException() noexcept override {}
rpm-build a7f80b
rpm-build a7f80b
    std::string getMessage() const {
rpm-build a7f80b
        return m_msg;
rpm-build a7f80b
    }
rpm-build a7f80b
rpm-build a7f80b
private:
rpm-build a7f80b
    std::string m_msg;
rpm-build a7f80b
};
rpm-build a7f80b
rpm-build a7f80b
[[noreturn]] void throwCustom() {
rpm-build a7f80b
    throw CustomException( "custom exception - not std" );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
#endif
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "When checked exceptions are thrown they can be expected or unexpected", "[!throws]" ) {
rpm-build a7f80b
    REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
rpm-build a7f80b
    REQUIRE_NOTHROW( thisDoesntThrow() );
rpm-build a7f80b
    REQUIRE_THROWS( thisThrows() );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "Expected exceptions that don't throw or unexpected exceptions fail the test", "[.][failing][!throws]" ) {
rpm-build a7f80b
    CHECK_THROWS_AS( thisThrows(), std::string );
rpm-build a7f80b
    CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
rpm-build a7f80b
    CHECK_NOTHROW( thisThrows() );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "When unchecked exceptions are thrown directly they are always failures", "[.][failing][!throws]" ) {
rpm-build a7f80b
    throw std::domain_error( "unexpected exception" );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "An unchecked exception reports the line of the last assertion", "[.][failing][!throws]" ) {
rpm-build a7f80b
    CHECK( 1 == 1 );
rpm-build a7f80b
    throw std::domain_error( "unexpected exception" );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "When unchecked exceptions are thrown from sections they are always failures", "[.][failing][!throws]" ) {
rpm-build a7f80b
    SECTION( "section name" ) {
rpm-build a7f80b
        throw std::domain_error("unexpected exception");
rpm-build a7f80b
    }
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "When unchecked exceptions are thrown from functions they are always failures", "[.][failing][!throws]" ) {
rpm-build a7f80b
    CHECK( thisThrows() == 0 );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "When unchecked exceptions are thrown during a REQUIRE the test should abort fail", "[.][failing][!throws]" ) {
rpm-build a7f80b
    REQUIRE( thisThrows() == 0 );
rpm-build a7f80b
    FAIL( "This should never happen" );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "When unchecked exceptions are thrown during a CHECK the test should continue", "[.][failing][!throws]" ) {
rpm-build a7f80b
    try {
rpm-build a7f80b
        CHECK(thisThrows() == 0);
rpm-build a7f80b
    }
rpm-build a7f80b
    catch(...) {
rpm-build a7f80b
        FAIL( "This should never happen" );
rpm-build a7f80b
    }
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "When unchecked exceptions are thrown, but caught, they do not affect the test", "[!throws]" ) {
rpm-build a7f80b
    try {
rpm-build a7f80b
        throw std::domain_error( "unexpected exception" );
rpm-build a7f80b
    }
rpm-build a7f80b
    catch(...) {}
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
rpm-build a7f80b
CATCH_TRANSLATE_EXCEPTION( CustomException& ex ) {
rpm-build a7f80b
    return ex.getMessage();
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
CATCH_TRANSLATE_EXCEPTION( CustomStdException& ex ) {
rpm-build a7f80b
    return ex.getMessage();
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
CATCH_TRANSLATE_EXCEPTION( double& ex ) {
rpm-build a7f80b
    return Catch::Detail::stringify( ex );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" ) {
rpm-build a7f80b
    throw CustomException( "custom exception" );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE("Custom std-exceptions can be custom translated", "[.][failing][!throws]" ) {
rpm-build a7f80b
    throw CustomException( "custom std exception" );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "Custom exceptions can be translated when testing for nothrow", "[.][failing][!throws]" ) {
rpm-build a7f80b
    REQUIRE_NOTHROW( throwCustom() );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "Custom exceptions can be translated when testing for throwing as something else", "[.][failing][!throws]" ) {
rpm-build a7f80b
    REQUIRE_THROWS_AS( throwCustom(), std::exception );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "Unexpected exceptions can be translated", "[.][failing][!throws]"  ) {
rpm-build a7f80b
    throw double( 3.14 );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE("Thrown string literals are translated", "[.][failing][!throws]") {
rpm-build a7f80b
    throw "For some reason someone is throwing a string literal!";
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE("thrown std::strings are translated", "[.][failing][!throws]") {
rpm-build a7f80b
    throw std::string{ "Why would you throw a std::string?" };
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
rpm-build a7f80b
#ifndef CATCH_CONFIG_DISABLE_MATCHERS
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "Exception messages can be tested for", "[!throws]" ) {
rpm-build a7f80b
    using namespace Catch::Matchers;
rpm-build a7f80b
    SECTION( "exact match" )
rpm-build a7f80b
        REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
rpm-build a7f80b
    SECTION( "different case" )
rpm-build a7f80b
    REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) );
rpm-build a7f80b
    SECTION( "wildcarded" ) {
rpm-build a7f80b
        REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) );
rpm-build a7f80b
        REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) );
rpm-build a7f80b
        REQUIRE_THROWS_WITH( thisThrows(), Contains( "except" ) );
rpm-build a7f80b
        REQUIRE_THROWS_WITH( thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) );
rpm-build a7f80b
    }
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
#endif
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "Mismatching exception messages failing the test", "[.][failing][!throws]" ) {
rpm-build a7f80b
    REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
rpm-build a7f80b
    REQUIRE_THROWS_WITH( thisThrows(), "should fail" );
rpm-build a7f80b
    REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE( "#748 - captures with unexpected exceptions", "[.][failing][!throws][!shouldfail]" ) {
rpm-build a7f80b
    int answer = 42;
rpm-build a7f80b
    CAPTURE( answer );
rpm-build a7f80b
    // the message should be printed on the first two sections but not on the third
rpm-build a7f80b
    SECTION( "outside assertions" ) {
rpm-build a7f80b
        thisThrows();
rpm-build a7f80b
    }
rpm-build a7f80b
    SECTION( "inside REQUIRE_NOTHROW" ) {
rpm-build a7f80b
        REQUIRE_NOTHROW( thisThrows() );
rpm-build a7f80b
    }
rpm-build a7f80b
    SECTION( "inside REQUIRE_THROWS" ) {
rpm-build a7f80b
        REQUIRE_THROWS( thisThrows() );
rpm-build a7f80b
    }
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
}} // namespace ExceptionTests
rpm-build a7f80b
rpm-build a7f80b
#ifdef __clang__
rpm-build a7f80b
#pragma clang diagnostic pop
rpm-build a7f80b
#endif