I have the code below in my test code in many places:
//
// Make a function call while expecting an exception should be thrown
//
bool exceptionThrown = false;
try
{
expectNotEqual(someData, anotherData, methodName);
}
catch(std::logic_error&)
{
exceptionThrown = true;
}
if(!exceptionThrown)
throw std::logic_error(methodName+"exception not thrown");
It would be nice (more readable, concise) if I could encapsulate all that, and do something like:
exceptionShouldBeThrown(expectNotEqual(someData, anotherData, methodName));
I dont want to use macro …does anyone know how I could achieve the one-liner above with C++?
I know you say no macro’s, but why? They exist for generating code:
If you really don’t want to use macros, you need to make a functor to call:
Boost Bind helps here:
Of course anything that makes a functor works, like lambda’s, etc. But if Boost is available, just use their testing library: