From 060ed99f0c79f0280aab523adac4d698f31a3d69 Mon Sep 17 00:00:00 2001 From: Konstantin Baumann Date: Thu, 31 Jan 2013 12:25:57 +0100 Subject: [PATCH] enhanced support for C++11 style nullptr with these changes you can now write the following tests: std::exception_ptr ex; CATCH_REQUIRE(ex == nullptr); and std::unique_ptr ptr; CATCH_REQUIRE(ptr.get() == nullptr); The issue was that you cannot cast a std::nullptr_t to a non-const reference. --- README | 10 ++++----- include/internal/catch_evaluate.hpp | 31 +++++++++++++++++++------ include/internal/catch_version.hpp | 2 +- single_include/catch.hpp | 35 +++++++++++++++++++++-------- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/README b/README index b7a90aa4..42812e92 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -CATCH v0.9 build 16 (integration branch) +CATCH v0.9 build 18 (integration branch) --------------------------------------------- CATCH is an automated test framework for C, C++ and Objective-C. @@ -6,9 +6,9 @@ CATCH is an automated test framework for C, C++ and Objective-C. This branch may contain code that is experimental or not yet fully tested. The latest stable version can be found on the Master branch. -For documentation see the wiki at: +For documentation see the wiki at: https://github.com/philsquared/Catch/wiki -Issues and bugs can be raised at: +Issues and bugs can be raised at: https://github.com/philsquared/Catch/issues -For discussion or questions please use: - https://groups.google.com/forum/?fromgroups#!forum/catch-forum \ No newline at end of file +For discussion or questions please use: + https://groups.google.com/forum/?fromgroups#!forum/catch-forum diff --git a/include/internal/catch_evaluate.hpp b/include/internal/catch_evaluate.hpp index 7e9d57d0..9d805786 100644 --- a/include/internal/catch_evaluate.hpp +++ b/include/internal/catch_evaluate.hpp @@ -28,6 +28,13 @@ namespace Internal { template<> struct OperatorTraits { static const char* getName(){ return "<="; } }; template<> struct OperatorTraits{ static const char* getName(){ return ">="; } }; + template + inline T& catch_const_cast(const T& t) { return const_cast(t); } + +#ifdef CATCH_CONFIG_CPP11_NULLPTR + inline std::nullptr_t catch_const_cast(std::nullptr_t) { return nullptr; } +#endif // CATCH_CONFIG_CPP11_NULLPTR + // So the compare overloads can be operator agnostic we convey the operator as a template // enum, which is used to specialise an Evaluator for doing the comparison. template @@ -36,37 +43,37 @@ namespace Internal { template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs) { - return const_cast( lhs ) == const_cast( rhs ); + return catch_const_cast( lhs ) == catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) != const_cast( rhs ); + return catch_const_cast( lhs ) != catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) < const_cast( rhs ); + return catch_const_cast( lhs ) < catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) > const_cast( rhs ); + return catch_const_cast( lhs ) > catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) >= const_cast( rhs ); + return catch_const_cast( lhs ) >= catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) <= const_cast( rhs ); + return catch_const_cast( lhs ) <= catch_const_cast( rhs ); } }; @@ -143,7 +150,17 @@ namespace Internal { template bool compare( T* lhs, int rhs ) { return Evaluator::evaluate( lhs, reinterpret_cast( rhs ) ); } - + +#ifdef CATCH_CONFIG_CPP11_NULLPTR + // pointer to nullptr_t (when comparing against nullptr) + template bool compare( std::nullptr_t lhs, T* rhs ) { + return Evaluator::evaluate( reinterpret_cast( lhs ), rhs ); + } + template bool compare( T* lhs, std::nullptr_t rhs ) { + return Evaluator::evaluate( lhs, reinterpret_cast( rhs ) ); + } +#endif // CATCH_CONFIG_CPP11_NULLPTR + } // end of namespace Internal } // end of namespace Catch diff --git a/include/internal/catch_version.hpp b/include/internal/catch_version.hpp index 6989a932..380ffcb4 100644 --- a/include/internal/catch_version.hpp +++ b/include/internal/catch_version.hpp @@ -13,7 +13,7 @@ namespace Catch { // These numbers are maintained by a script - Version libraryVersion( 0, 9, 16, "integration" ); + Version libraryVersion( 0, 9, 18, "integration" ); } #endif // TWOBLUECUBES_CATCH_VERSION_HPP_INCLUDED diff --git a/single_include/catch.hpp b/single_include/catch.hpp index bac4dd72..602f6fad 100644 --- a/single_include/catch.hpp +++ b/single_include/catch.hpp @@ -1,6 +1,6 @@ /* - * CATCH v0.9 build 16 (integration branch) - * Generated: 2013-01-26 20:18:07.076275 + * CATCH v0.9 build 17 (integration branch) + * Generated: 2013-01-31 12:14:41.781000 * ---------------------------------------------------------- * This file has been merged from multiple headers. Please don't edit it directly * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. @@ -758,6 +758,13 @@ namespace Internal { template<> struct OperatorTraits { static const char* getName(){ return "<="; } }; template<> struct OperatorTraits{ static const char* getName(){ return ">="; } }; + template + inline T& catch_const_cast(const T& t) { return const_cast(t); } + +#ifdef CATCH_CONFIG_CPP11_NULLPTR + inline std::nullptr_t catch_const_cast(std::nullptr_t) { return nullptr; } +#endif // CATCH_CONFIG_CPP11_NULLPTR + // So the compare overloads can be operator agnostic we convey the operator as a template // enum, which is used to specialise an Evaluator for doing the comparison. template @@ -766,37 +773,37 @@ namespace Internal { template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs) { - return const_cast( lhs ) == const_cast( rhs ); + return catch_const_cast( lhs ) == catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) != const_cast( rhs ); + return catch_const_cast( lhs ) != catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) < const_cast( rhs ); + return catch_const_cast( lhs ) < catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) > const_cast( rhs ); + return catch_const_cast( lhs ) > catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) >= const_cast( rhs ); + return catch_const_cast( lhs ) >= catch_const_cast( rhs ); } }; template struct Evaluator { static bool evaluate( const T1& lhs, const T2& rhs ) { - return const_cast( lhs ) <= const_cast( rhs ); + return catch_const_cast( lhs ) <= catch_const_cast( rhs ); } }; @@ -874,6 +881,16 @@ namespace Internal { return Evaluator::evaluate( lhs, reinterpret_cast( rhs ) ); } +#ifdef CATCH_CONFIG_CPP11_NULLPTR + // pointer to nullptr_t (when comparing against nullptr) + template bool compare( std::nullptr_t lhs, T* rhs ) { + return Evaluator::evaluate( reinterpret_cast( lhs ), rhs ); + } + template bool compare( T* lhs, std::nullptr_t rhs ) { + return Evaluator::evaluate( lhs, reinterpret_cast( rhs ) ); + } +#endif // CATCH_CONFIG_CPP11_NULLPTR + } // end of namespace Internal } // end of namespace Catch @@ -5777,7 +5794,7 @@ namespace Catch { namespace Catch { // These numbers are maintained by a script - Version libraryVersion( 0, 9, 16, "integration" ); + Version libraryVersion( 0, 9, 17, "integration" ); } // #included from: catch_line_wrap.hpp