Johan Nilsson - 2002-11-26

Hi,

I've found extensive use for macros dealing with exceptions, e.g.:

void testVector()
{
   std::vector<int> vec;
  CPPUNIT_ASSERT_EX("Succeded to get non-existing element", vec.at(0),
std::exception);

vec.push_back(2);
CPPUNIT_ASSERT_NOEX(vec.at(0));
CPPUNIT_ASSERT_EQUAL(2, vec.at(0));
}

Did I miss something, or does this already exist? Basic macro definitions
included at end of message.

Regards // Johan

---------------------

#ifndef CPPUNIT_ASSERT_EXCEPTION
#define CPPUNIT_ASSERT_EXCEPTION(ErrorMsg, Statement, ExpectedExceptionType)
\
{                 \
bool __exceptionCaught = false;  \
try                \
{                 \
     Statement;           \
}                 \
catch (ExpectedExceptionType&)  \
{                 \
  __exceptionCaught = true;    \
}                 \
CPPUNIT_ASSERT_MESSAGE(#ErrorMsg, __exceptionCaught); \
}
#define CPPUNIT_ASSERT_EX CPPUNIT_ASSERT_EXCEPTION
#endif // CPPUNIT_ASSERT_EXCEPTION

#ifndef CPPUNIT_ASSERT_NOEXCEPTION
#define CPPUNIT_ASSERT_NOEXCEPTION(expr)                 \
try                                   \
{                                    \
  expr;                                 \
}                                    \
catch(std::exception& e)                        \
{                                    \
  CPPUNIT_ASSERT_MESSAGE(#expr + std::string(", ") + e.what(), false); \
}                                    \
catch (...)                               \
{                                    \
  CPPUNIT_ASSERT_MESSAGE("Exception from '"###expr##"'", false);    \
}
#define CPPUNIT_ASSERT_NOEX CPPUNIT_ASSERT_NOEXCEPTION
#endif // CPPUNIT_ASSERT_NOEXCEPTION