catchorg / Catch2

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
https://discord.gg/4CWS9zD
Boost Software License 1.0
18.48k stars 3.02k forks source link

Simple if/else without block scope {} does not compile #72

Closed colonelsammy closed 12 years ago

colonelsammy commented 12 years ago

Example:

define CATCH_CONFIG_MAIN

include "catch.hpp"

TEST_CASE("bad if statement", "") { size_t x = 0;

if( x )
    REQUIRE(x > 0);
else
    REQUIRE(x == 0);

}

You need to do something like putting a do{ } while(0) around the macro body of INTERNAL_CATCH_TEST....e.g:

define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \

do {INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); }} while (0)
colonelsammy commented 12 years ago

Although note that if you use a 'while' loop then you'll get loads of this on MS compiler:

warning C4127: conditional expression is constant

colonelsammy commented 12 years ago

This patch works for me on msvc and mingw32 (based on code of 18th Feb):

diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp index d19f4c2..7bcb49f 100644 --- a/include/internal/catch_capture.hpp +++ b/include/internal/catch_capture.hpp @@ -683,7 +683,11 @@ inline bool isTrue }

/////////////////////////////////////////////////////////////////////////////// +#if defined(_MSC_VER)

define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \

philsquared commented 12 years ago

Hi Mal^H^H^HColonelSammy,

I thought I'd done this ages ago. Well I added it back in. I did it slightly differently, though. To avoid the msvc warning I use Catch::isTrue( false ), which seems to provide just the right levelofindirection(.com) to convert it to a runtime condition (I use it elsewhere for the same reason).

That said I've not had a chance to test it in msvc yet (other than on my CI server that tells me it builds ok - I should probably enable warnings as errors in that project).

colonelsammy commented 12 years ago

Hi Phil,

On 29/02/2012 08:51, Phil Nash wrote:

I thought I'd done this ages ago. Well I added it back in. I did it slightly differently, though. To avoid the msvc warning I use Catch::isTrue( false ), which seems to provide just the right levelofindirection(.com) to convert it to a runtime condition (I use it elsewhere for the same reason).

That said I've not had a chance to test it in msvc yet (other than on my CI server that tells me it builds ok - I should probably enable warnings as errors in that project). Excellent, thanks! I'll give it a try...

BTW, congrats on getting a keynote spot! Looking forward to it ;-)

regards, Malcolm

philsquared commented 12 years ago

Thanks!

On 1 Mar 2012, at 06:45, Malcolm Noyes wrote:

Hi Phil,

On 29/02/2012 08:51, Phil Nash wrote:

I thought I'd done this ages ago. Well I added it back in. I did it slightly differently, though. To avoid the msvc warning I use Catch::isTrue( false ), which seems to provide just the right levelofindirection(.com) to convert it to a runtime condition (I use it elsewhere for the same reason).

That said I've not had a chance to test it in msvc yet (other than on my CI server that tells me it builds ok - I should probably enable warnings as errors in that project). Excellent, thanks! I'll give it a try...

BTW, congrats on getting a keynote spot! Looking forward to it ;-)

regards, Malcolm


Reply to this email directly or view it on GitHub: https://github.com/philsquared/Catch/issues/72#issuecomment-4253292