Skip to content

Commit

Permalink
REQUIRE_THROWS_AS now catches exception by const&
Browse files Browse the repository at this point in the history
Prevents some warnings caused by catching complex types by value.

Closes #542
  • Loading branch information
horenmar committed Feb 9, 2017
1 parent 9952dda commit 73159ac
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion include/internal/catch_capture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "catch_tostring.h"
#include "catch_interfaces_runner.h"
#include "catch_compiler_capabilities.h"
#include "catch_type_traits.hpp"


///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -93,7 +94,7 @@
static_cast<void>(expr); \
__catchResult.captureResult( Catch::ResultWas::DidntThrowException ); \
} \
catch( exceptionType ) { \
catch( Catch::add_const<Catch::add_lvalue_reference<exceptionType>::type>::type ) { \
__catchResult.captureResult( Catch::ResultWas::Ok ); \
} \
catch( ... ) { \
Expand Down
47 changes: 47 additions & 0 deletions include/internal/catch_type_traits.hpp
Original file line number Diff line number Diff line change
@@ -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 <type_traits>
#endif


namespace Catch {

#if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS)

template <typename T>
using add_lvalue_reference = std::add_lvalue_reference<T>;

template <typename T>
using add_const = std::add_const<T>;

#else

template <typename T>
struct add_const {
typedef const T type;
};

template <typename T>
struct add_lvalue_reference {
typedef T& type;
};
template <typename T>
struct add_lvalue_reference<T&> {
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
18 changes: 18 additions & 0 deletions projects/SelfTest/CompilationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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&);
}

0 comments on commit 73159ac

Please sign in to comment.