Skip to content

Commit

Permalink
Change decomposition to use operator<< instead of operator->*, issue #14
Browse files Browse the repository at this point in the history


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,
  catchorg/Catch2@8cc1108 and issues mentioned there:
- Catch issue #359, catchorg/Catch2#359
- Catch issue #393, catchorg/Catch2#393
- Catch issue #247, catchorg/Catch2#247
  • Loading branch information
martinmoene committed Jul 22, 2015
1 parent 75a8c26 commit fc627ed
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <cstddef>

#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__
Expand Down Expand Up @@ -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__ )
Expand Down Expand Up @@ -699,7 +700,7 @@ struct expression_lhs
struct expression_decomposer
{
template <typename L>
expression_lhs<L const &> operator->* ( L const & operand )
expression_lhs<L const &> operator<< ( L const & operand )
{
return expression_lhs<L const &>( operand );
}
Expand Down
5 changes: 3 additions & 2 deletions lest_cpp03.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <ctime>

#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"
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -561,7 +562,7 @@ struct expression_lhs
struct expression_decomposer
{
template <typename L>
expression_lhs<L const &> operator->* ( L const & operand )
expression_lhs<L const &> operator<< ( L const & operand )
{
return expression_lhs<L const &>( operand );
}
Expand Down
5 changes: 3 additions & 2 deletions lest_decompose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <cstddef>

#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__
Expand Down Expand Up @@ -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__}

Expand Down Expand Up @@ -297,7 +298,7 @@ struct expression_lhs
struct expression_decomposer
{
template <typename L>
expression_lhs<L const &> operator->* ( L const & operand )
expression_lhs<L const &> operator<< ( L const & operand )
{
return expression_lhs<L const &>( operand );
}
Expand Down
18 changes: 18 additions & 0 deletions test/test_lest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ); } }};
Expand Down
18 changes: 18 additions & 0 deletions test/test_lest_cpp03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ); }
Expand Down
18 changes: 18 additions & 0 deletions test/test_lest_decompose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ); } }};
Expand Down

0 comments on commit fc627ed

Please sign in to comment.