googletest
googletest copied to clipboard
Extended exception handling
Hi, I think we need to make it possible to install your own exception handler. since it is always very inconvenient to write such code (not beautiful)
TEST(ATweet, RequiresUserNameToStartWithAtSign) {
try {
throw my_custom_exception();
}
catch(const InvalidUserException& expected) {
ASSERT_STREQ("notStartingWith@", expected.what());
}
}
maybe I can write the code myself - I just have to decide how to implement it. 2 options come to my mind
- pass the exception to a custom handler function, something like this(pseudo code)
static std::function<Result(const std::exception_ptr&)> handler = nullptr;
if(handler !=nullptr) {
try{
return HandleSehExceptionsInMethodIfSupported(object, method, location);
} catch (...) {
std::exception_ptr p = std::current_exception();
handler(p) ;
}
}else{
try {
return HandleSehExceptionsInMethodIfSupported(object, method, location);
} catch (const internal::GoogleTestFailureException&) { // NOLINT
throw;
} catch (const std::exception& e) { // NOLINT
internal::ReportFailureInUnknownLocation(
TestPartResult::kFatalFailure,
FormatCxxExceptionMessage(e.what(), location));
} catch (...) { // NOLINT
// internal::ReportFailureInUnknownLocation(
// TestPartResult::kFatalFailure,
// FormatCxxExceptionMessage(NULL, location));
}
return static_cast<Result>(0);
- I like it much less - create and insert define
this is not a bug - there is no other template (
Here is an example of how I would write this: https://github.com/google/googletest/blob/53495a2a7d6ba7e0691a7f3602e9a5324bba6e45/googlemock/test/gmock-matchers_test.cc#L8383-L8385
Does this solve your problem?
Here is an example of how I would write this: https://github.com/google/googletest/blob/53495a2a7d6ba7e0691a7f3602e9a5324bba6e45/googlemock/test/gmock-matchers_test.cc#L8383-L8385
Does this solve your problem?
I don't need to check if an exception will occur, and if an exception occurs that I did not expect, then I need to output information about that exception, but since my exception is not inherited from std :: runtime_error, I need my own catch ()
Thank you, I think I now understand what you are asking for. Unfortunately we aren't heavy users of exceptions ourselves. If you (or someone from the community) could come up with a patch we would consider accepting it.
One thing I would add is that we would not accept a patch that uses a custom handler injection point. I would try to find some other way to implement this.
We have the same need; our application uses two custom exception types (not derived from std::exception etc) and we need to be able to catch them whenever they are unexpectedly thrown from code under test and report their messages. The custom exception handler approach seems like a good, low-coupling way to extend gtest for these needs. I would request that you accept the patch from Andrysky.