Skip to content

Instantly share code, notes, and snippets.

@lshort
Created June 30, 2014 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lshort/12a8a37dc7b74ed98946 to your computer and use it in GitHub Desktop.
Save lshort/12a8a37dc7b74ed98946 to your computer and use it in GitHub Desktop.
final expect_exception
template<typename ExecLambda, typename OnThrowLambda, typename NoThrowLambda>
auto expect_exception( ExecLambda exec_lambda, bool expect_to_throw,
OnThrowLambda throw_lambda, NoThrowLambda no_throw_lambda,
decltype(throw_lambda()) expected_return_value)
{
bool threw;
decltype( exec_lambda() ) x;
try {
x = exec_lambda();
threw = false;
}
catch (...) {
threw = true;
}
if ( expect_to_throw != threw ) {
if (expect_to_throw)
throw "failed to catch expected exception";
else
throw "caught unexpect exception";
} else {
if (expect_to_throw) {
return throw_lambda();
} else {
auto rval = no_throw_lambda(x);
if ( rval != expected_return_value )
throw "Unexpected Return Value!!!";
else
return rval;
}
}
}
template<typename ExecLambda, typename ExpectThrowLambda,
typename OnThrowLambda, typename NoThrowLambda>
auto expect_exception_l( ExecLambda exec_lambda,
ExpectThrowLambda expect_throw,
OnThrowLambda throw_lambda,
NoThrowLambda no_throw_lambda,
decltype(throw_lambda()) expect)
{
return expect_exception(exec_lambda, expect_throw(),
throw_lambda, no_throw_lambda, expect);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment