jeudi 24 décembre 2015

noexcept, assertion handler and unit testing

I'm working on a test framework similar to LEST in which I provide a macro that checks that a given expression triggers a runtime assertion coming from BOOST_ASSERT macro. This is done by using BOOST_ASSERT_HANDLER to make the assertion be turned into a assertion_failure exception and have the macro catch it and report success in catching it.

Problem is that we have a bunch of functions beign tested of the general shape of :

float foo(int x) noexcept
{
   BOOST_ASSERT(x > 1);
   return x/(x-1.f);
}

This noexcept specification is properly set as -- except during unit testing, the function is indeed noexcept. During testing of course, everything blows as the BOOST_ASSERT now throws.

An idea to fix this was to make the ASSERT fill up a global unique_ptr to a flag sayign that assertions were triggered. However, it fails as the flow of control still run the bad code.

What coudl be the most elegant solution to this issue which involves the least amount of changes on the tested code ? Is using exception and using set_terminate a valid solution ?

Aucun commentaire:

Enregistrer un commentaire