Created
June 30, 2014 04:03
-
-
Save lshort/12a8a37dc7b74ed98946 to your computer and use it in GitHub Desktop.
final expect_exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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