CxxTest/cxxtest — Issue #28: Revise test macros to act like functions
Author: whart222
Created: 2012-02-04 18:03:39+00:00
State: open

The test macros (at least for TS_ASSERT_EQUALS) cannot be treated as single-line
statements. In particular, the following will not compile in g++:

if ( utilib::Serialization_Manager::Endian == 0xE4 )
TS_ASSERT_EQUALS(ss.str(), "type = int\n POD: 4: 5 0 0 0\n");
else if ( utilib::Serialization_Manager::Endian == 0x1B )
TS_ASSERT_EQUALS(ss.str(), "type = int\n POD: 4: 0 0 0 5\n");
else
TS_FAIL("Unrecognized endianness");

The problem is that the macro eventually expands to something like

if ( utilib::Serialization_Manager::Endian == 0xE4 )
try
{ /* test ss.str() == "type = int\n POD: 4: 5 0 0 0\n" */ }
catch
{ /* catch exceptions */ }
else if ( utilib::Serialization_Manager::Endian == 0x1B )
try
{ /* test ss.str() == "type = int\n POD: 4: 0 0 0 5\n" */ }
catch
{ /* catch exceptions */ }	else
TS_FAIL("Unrecognized endianness");

...which is obviously illegal. The simple solution is that all tests should be
guarded by:

if ( true ) {
/* insert original test code here, including try-catch blocks */
} else (void)(0)