From 73159ace3d84b3cf01a126d6edefde196cceb621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Thu, 9 Feb 2017 12:57:01 +0100 Subject: [PATCH] REQUIRE_THROWS_AS now catches exception by const& Prevents some warnings caused by catching complex types by value. Closes #542 --- CMakeLists.txt | 1 + include/internal/catch_capture.hpp | 3 +- include/internal/catch_type_traits.hpp | 47 ++++++++++++++++++++++++++ projects/SelfTest/CompilationTests.cpp | 18 ++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 include/internal/catch_type_traits.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6fe4e4e2..e9a00c2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,6 +195,7 @@ set(INTERNAL_HEADERS ${HEADER_DIR}/internal/catch_tostring.h ${HEADER_DIR}/internal/catch_tostring.hpp ${HEADER_DIR}/internal/catch_totals.hpp + ${HEADER_DIR}/internal/catch_type_traits.hpp ${HEADER_DIR}/internal/catch_version.h ${HEADER_DIR}/internal/catch_version.hpp ${HEADER_DIR}/internal/catch_wildcard_pattern.hpp diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp index ae10f58a..8abc536b 100644 --- a/include/internal/catch_capture.hpp +++ b/include/internal/catch_capture.hpp @@ -16,6 +16,7 @@ #include "catch_tostring.h" #include "catch_interfaces_runner.h" #include "catch_compiler_capabilities.h" +#include "catch_type_traits.hpp" /////////////////////////////////////////////////////////////////////////////// @@ -93,7 +94,7 @@ static_cast(expr); \ __catchResult.captureResult( Catch::ResultWas::DidntThrowException ); \ } \ - catch( exceptionType ) { \ + catch( Catch::add_const::type>::type ) { \ __catchResult.captureResult( Catch::ResultWas::Ok ); \ } \ catch( ... ) { \ diff --git a/include/internal/catch_type_traits.hpp b/include/internal/catch_type_traits.hpp new file mode 100644 index 00000000..9be89162 --- /dev/null +++ b/include/internal/catch_type_traits.hpp @@ -0,0 +1,47 @@ +/* + * Created by Martin on 08/02/2017. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ +#ifndef TWOBLUECUBES_CATCH_TYPE_TRAITS_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_TYPE_TRAITS_HPP_INCLUDED + +#if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) +#include +#endif + + +namespace Catch { + +#if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) + + template + using add_lvalue_reference = std::add_lvalue_reference; + + template + using add_const = std::add_const; + +#else + + template + struct add_const { + typedef const T type; + }; + + template + struct add_lvalue_reference { + typedef T& type; + }; + template + struct add_lvalue_reference { + typedef T& type; + }; + // No && overload, because that is C++11, in which case we have + // proper type_traits implementation from the standard library + +#endif + +} + +#endif // TWOBLUECUBES_CATCH_TYPE_TRAITS_HPP_INCLUDED diff --git a/projects/SelfTest/CompilationTests.cpp b/projects/SelfTest/CompilationTests.cpp index f12372bf..635c62c8 100644 --- a/projects/SelfTest/CompilationTests.cpp +++ b/projects/SelfTest/CompilationTests.cpp @@ -22,3 +22,21 @@ TEST_CASE("#809") { foo f; f.i = 42; REQUIRE(42 == f); } + +// ------------------------------------------------------------------ +// REQUIRE_THROWS_AS was changed to catch exceptions by const& +// using type traits. This means that this should compile cleanly + +// Provides indirection to prevent unreachable-code warnings +void throws_int(bool b) { + if (b) { + throw 1; + } +} + +TEST_CASE("#542") { + CHECK_THROWS_AS(throws_int(true), int); + CHECK_THROWS_AS(throws_int(true), int&); + CHECK_THROWS_AS(throws_int(true), const int); + CHECK_THROWS_AS(throws_int(true), const int&); +}