From fc627edc6e260926b2405cac2170bc23c7150482 Mon Sep 17 00:00:00 2001 From: Martin Moene Date: Wed, 22 Jul 2015 15:31:32 +0200 Subject: [PATCH] Change decomposition to use operator<< instead of operator->*, issue #14 This enables the use of *, /, %, + and - at the LHS of expressions used in assertion macros, e.g. EXPECT( 1 + 2 == 3 ) See also discussion at - Catch commit: Use <= operator instead of ->* for decomposer, https://github.com/philsquared/Catch/commit/8cc1108f2b8aa215c94f1d6d3b93121d28ffd08a and issues mentioned there: - Catch issue #359, https://github.com/philsquared/Catch/issues/359 - Catch issue #393, https://github.com/philsquared/Catch/pull/393 - Catch issue #247, https://github.com/philsquared/Catch/pull/247 --- lest.hpp | 5 +++-- lest_cpp03.hpp | 5 +++-- lest_decompose.hpp | 5 +++-- test/test_lest.cpp | 18 ++++++++++++++++++ test/test_lest_cpp03.cpp | 18 ++++++++++++++++++ test/test_lest_decompose.cpp | 18 ++++++++++++++++++ 6 files changed, 63 insertions(+), 6 deletions(-) diff --git a/lest.hpp b/lest.hpp index da7600f..9f60bf3 100644 --- a/lest.hpp +++ b/lest.hpp @@ -32,6 +32,7 @@ #include #ifdef __clang__ +# pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses" # pragma clang diagnostic ignored "-Wunused-comparison" # pragma clang diagnostic ignored "-Wunused-value" #elif defined __GNUC__ @@ -211,7 +212,7 @@ #define lest_UNIQUE2( name, line ) lest_UNIQUE3( name, line ) #define lest_UNIQUE3( name, line ) name ## line -#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer()->* expr ) +#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer() << expr ) #define lest_FUNCTION lest_UNIQUE(__lest_function__ ) #define lest_REGISTRAR lest_UNIQUE(__lest_registrar__ ) @@ -699,7 +700,7 @@ struct expression_lhs struct expression_decomposer { template - expression_lhs operator->* ( L const & operand ) + expression_lhs operator<< ( L const & operand ) { return expression_lhs( operand ); } diff --git a/lest_cpp03.hpp b/lest_cpp03.hpp index acfab3e..9ed2ea3 100644 --- a/lest_cpp03.hpp +++ b/lest_cpp03.hpp @@ -28,6 +28,7 @@ #include #ifdef __clang__ +# pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses" # pragma clang diagnostic ignored "-Wunused-value" #elif defined __GNUC__ # pragma GCC diagnostic ignored "-Wunused-value" @@ -266,7 +267,7 @@ namespace lest } \ while ( lest::is_false() ) -#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer()->* expr ) +#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer() << expr ) #define lest_UNIQUE( name ) lest_UNIQUE2( name, __LINE__ ) #define lest_UNIQUE2( name, line ) lest_UNIQUE3( name, line ) @@ -561,7 +562,7 @@ struct expression_lhs struct expression_decomposer { template - expression_lhs operator->* ( L const & operand ) + expression_lhs operator<< ( L const & operand ) { return expression_lhs( operand ); } diff --git a/lest_decompose.hpp b/lest_decompose.hpp index a58b7a5..0fedc62 100644 --- a/lest_decompose.hpp +++ b/lest_decompose.hpp @@ -21,6 +21,7 @@ #include #ifdef __clang__ +# pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses" # pragma clang diagnostic ignored "-Wunused-comparison" # pragma clang diagnostic ignored "-Wunused-value" #elif defined __GNUC__ @@ -76,7 +77,7 @@ throw lest::expected{ lest_LOCATION, #expr, lest::of_type( #excpt ) }; \ } while ( lest::is_false() ) -#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer()->* expr ) +#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer() << expr ) #define lest_LOCATION lest::location{__FILE__, __LINE__} @@ -297,7 +298,7 @@ struct expression_lhs struct expression_decomposer { template - expression_lhs operator->* ( L const & operand ) + expression_lhs operator<< ( L const & operand ) { return expression_lhs( operand ); } diff --git a/test/test_lest.cpp b/test/test_lest.cpp index 9381407..69dea18 100644 --- a/test/test_lest.cpp +++ b/test/test_lest.cpp @@ -204,6 +204,24 @@ const lest::test specification[] = EXPECT_NOT( a > b ); }, + CASE( "Expect expression RHS can use *, / %, +, -" ) + { + EXPECT( 7 == 1 * 7 ); + EXPECT( 7 == 7 / 1 ); + EXPECT( 0 == 7 % 1 ); + EXPECT( 7 == 1 + 6 ); + EXPECT( 7 == 8 - 1 ); + }, + + CASE( "Expect expression LHS can use *, / %, +, -" ) + { + EXPECT( 1 * 7 == 7 ); + EXPECT( 7 / 1 == 7 ); + EXPECT( 7 % 1 == 0 ); + EXPECT( 1 + 6 == 7 ); + EXPECT( 8 - 1 == 7 ); + }, + CASE( "Function run() returns the right failure count" ) { test pass [] = {{ CASE( "P" ) { EXPECT( 1==1 ); } }}; diff --git a/test/test_lest_cpp03.cpp b/test/test_lest_cpp03.cpp index a48a403..8cb8066 100644 --- a/test/test_lest_cpp03.cpp +++ b/test/test_lest_cpp03.cpp @@ -202,6 +202,24 @@ CASE( "Expect succeeds for string comparation" ) EXPECT_NOT( a > b ); } +CASE( "Expect expression RHS can use *, / %, +, -" ) +{ + EXPECT( 7 == 1 * 7 ); + EXPECT( 7 == 7 / 1 ); + EXPECT( 0 == 7 % 1 ); + EXPECT( 7 == 1 + 6 ); + EXPECT( 7 == 8 - 1 ); +} + +CASE( "Expect expression LHS can use *, / %, +, -" ) +{ + EXPECT( 1 * 7 == 7 ); + EXPECT( 7 / 1 == 7 ); + EXPECT( 7 % 1 == 0 ); + EXPECT( 1 + 6 == 7 ); + EXPECT( 8 - 1 == 7 ); +} + CASE( "Function run() returns the right failure count" ) { struct f { static void pass(env & $) { EXPECT( 1==1 ); } diff --git a/test/test_lest_decompose.cpp b/test/test_lest_decompose.cpp index 35e9776..ea00e98 100644 --- a/test/test_lest_decompose.cpp +++ b/test/test_lest_decompose.cpp @@ -206,6 +206,24 @@ const lest::test specification[] = EXPECT( 1 == run( fail_6, os ) ); }, + CASE( "Expect expression RHS can use *, / %, +, -" ) + { + EXPECT( 7 == 1 * 7 ); + EXPECT( 7 == 7 / 1 ); + EXPECT( 0 == 7 % 1 ); + EXPECT( 7 == 1 + 6 ); + EXPECT( 7 == 8 - 1 ); + }, + + CASE( "Expect expression LHS can use *, / %, +, -" ) + { + EXPECT( 1 * 7 == 7 ); + EXPECT( 7 / 1 == 7 ); + EXPECT( 7 % 1 == 0 ); + EXPECT( 1 + 6 == 7 ); + EXPECT( 8 - 1 == 7 ); + }, + CASE( "Function run() returns the right failure count" ) { test pass [] = {{ CASE( "P" ) { EXPECT( 1==1 ); } }};