#include <stdexcept>
-#include <folly/ExceptionWrapper.h>
#include <folly/Conv.h>
+#include <folly/ExceptionWrapper.h>
#include <folly/portability/GTest.h>
using namespace folly;
EXPECT_FALSE(eptr);
}
+TEST(ExceptionWrapper, from_exception_ptr_empty) {
+ auto ep = std::exception_ptr();
+ auto ew = exception_wrapper::from_exception_ptr(ep);
+ EXPECT_FALSE(bool(ew));
+}
+
+TEST(ExceptionWrapper, from_exception_ptr_exn) {
+ auto ep = std::make_exception_ptr(std::runtime_error("foo"));
+ auto ew = exception_wrapper::from_exception_ptr(ep);
+ EXPECT_TRUE(bool(ew));
+ EXPECT_EQ(ep, ew.to_exception_ptr());
+ EXPECT_TRUE(ew.is_compatible_with<std::runtime_error>());
+}
+
+TEST(ExceptionWrapper, from_exception_ptr_any) {
+ auto ep = std::make_exception_ptr<int>(12);
+ auto ew = exception_wrapper::from_exception_ptr(ep);
+ EXPECT_TRUE(bool(ew));
+ EXPECT_EQ(ep, ew.to_exception_ptr());
+ EXPECT_TRUE(ew.is_compatible_with<int>());
+}
+
TEST(ExceptionWrapper, with_exception_ptr_empty) {
auto ew = exception_wrapper(std::exception_ptr());
EXPECT_EQ(exception_wrapper::none(), ew.type());
EXPECT_EQ(typeid(std::runtime_error), ew.type());
EXPECT_NE(nullptr, ew.get_exception());
EXPECT_NE(nullptr, ew.get_exception<std::exception>());
+ EXPECT_STREQ("foo", ew.get_exception<std::exception>()->what());
EXPECT_EQ(nullptr, ew.get_exception<int>());
EXPECT_FALSE(ew.has_exception_ptr());
EXPECT_NE(nullptr, ew.to_exception_ptr());
EXPECT_EQ(typeid(std::runtime_error), ew.type());
EXPECT_NE(nullptr, ew.get_exception());
EXPECT_NE(nullptr, ew.get_exception<std::exception>());
+ EXPECT_STREQ("foo", ew.get_exception<std::exception>()->what());
EXPECT_EQ(nullptr, ew.get_exception<int>());
EXPECT_TRUE(ew.has_exception_ptr());
EXPECT_EQ(ep, ew.to_exception_ptr());
EXPECT_EQ(nullptr, ew.get_exception());
EXPECT_EQ(nullptr, ew.get_exception<std::exception>());
EXPECT_NE(nullptr, ew.get_exception<int>());
+ EXPECT_EQ(12, *ew.get_exception<int>());
EXPECT_TRUE(ew.has_exception_ptr());
EXPECT_EQ(ep, ew.to_exception_ptr());
EXPECT_TRUE(ew.has_exception_ptr());
EXPECT_EQ(nullptr, ew.get_exception());
EXPECT_EQ(nullptr, ew.get_exception<std::exception>());
EXPECT_NE(nullptr, ew.get_exception<int>());
- EXPECT_FALSE(ew.has_exception_ptr());
+ EXPECT_EQ(42, *ew.get_exception<int>());
+ EXPECT_TRUE(ew.has_exception_ptr());
EXPECT_EQ("int", ew.class_name());
EXPECT_EQ("int", ew.what());
EXPECT_NE(nullptr, ew.to_exception_ptr());
EXPECT_EQ(nullptr, ew.get_exception());
EXPECT_EQ(nullptr, ew.get_exception<std::exception>());
EXPECT_NE(nullptr, ew.get_exception<int>());
+ EXPECT_EQ(12, *ew.get_exception<int>());
EXPECT_EQ(ep, ew.to_exception_ptr());
EXPECT_EQ("<unknown exception>", ew.class_name()); // because concrete type is
// erased
namespace {
template <typename T>
T& r_to_l(T v) { return std::ref(v); }
-}
+} // namespace
TEST(ExceptionWrapper, with_exception_deduction_functor_lvalue) {
auto ew = make_exception_wrapper<std::runtime_error>("hi");
void testEW(const exception_wrapper& ew) {
EXPECT_THROW(ew.throw_exception(), TestException);
}
-} // namespace
+} // namespace
TEST(ExceptionWrapper, implicitConstruction) {
// Try with both lvalue and rvalue references
return exception_wrapper{std::current_exception(), e};
}
}
-}
+} // namespace
TEST(ExceptionWrapper, base_derived_non_std_exception_test) {
auto ew = testNonStdException();
struct BigNonStdError {
char data_[sizeof(exception_wrapper) + 1]{};
};
-}
+} // namespace
TEST(ExceptionWrapper, handle_std_exception) {
auto ep = std::make_exception_ptr(std::runtime_error{"hello world"});
bool handled = false;
auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) {
ew.handle(
- [](const std::logic_error&) { EXPECT_TRUE(false); },
+ [](const std::logic_error&) { ADD_FAILURE(); },
[&](const std::runtime_error&) { handled = true; },
- [](const std::exception&) { EXPECT_TRUE(false); },
- [](...) { EXPECT_TRUE(false); });
+ [](const std::exception&) { ADD_FAILURE(); },
+ [](...) { ADD_FAILURE(); });
};
expect_runtime_error_yes_catch_all(ew_eptr);
auto expect_runtime_error_no_catch_all = [&](const exception_wrapper& ew) {
ew.handle(
- [](const std::logic_error&) { EXPECT_TRUE(false); },
+ [](const std::logic_error&) { ADD_FAILURE(); },
[&](const std::runtime_error&) { handled = true; },
- [](const std::exception&) { EXPECT_TRUE(false); });
+ [](const std::exception&) { ADD_FAILURE(); });
};
expect_runtime_error_no_catch_all(ew_eptr);
auto expect_runtime_error_catch_non_std = [&](const exception_wrapper& ew) {
ew.handle(
- [](const std::logic_error&) { EXPECT_TRUE(false); },
+ [](const std::logic_error&) { ADD_FAILURE(); },
[&](const std::runtime_error&) { handled = true; },
- [](const std::exception&) { EXPECT_TRUE(false); },
- [](const int&) { EXPECT_TRUE(false); });
+ [](const std::exception&) { ADD_FAILURE(); },
+ [](const int&) { ADD_FAILURE(); });
};
expect_runtime_error_catch_non_std(ew_eptr);
// outer handler:
auto expect_runtime_error_rethrow = [&](const exception_wrapper& ew) {
ew.handle(
- [](const std::logic_error&) { EXPECT_TRUE(false); },
+ [](const std::logic_error&) { ADD_FAILURE(); },
[&](const std::runtime_error& e) {
handled = true;
throw e;
},
- [](const std::exception&) { EXPECT_TRUE(false); });
+ [](const std::exception&) { ADD_FAILURE(); });
};
EXPECT_THROW(expect_runtime_error_rethrow(ew_eptr), std::runtime_error);
bool handled = false;
auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) {
ew.handle(
- [](const std::logic_error&) { EXPECT_TRUE(false); },
- [](const std::runtime_error&) { EXPECT_TRUE(false); },
+ [](const std::logic_error&) { ADD_FAILURE(); },
+ [](const std::runtime_error&) { ADD_FAILURE(); },
[&](...) { handled = true; });
};
auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) {
ew.handle(
- [](const std::exception&) { EXPECT_TRUE(false); },
+ [](const std::exception&) { ADD_FAILURE(); },
[&](...) { handled = true; });
};
auto expect_int_no_catch_all = [&](const exception_wrapper& ew) {
ew.handle(
- [](const std::exception&) { EXPECT_TRUE(false); },
+ [](const std::exception&) { ADD_FAILURE(); },
[&](const int&) { handled = true; });
};
auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) {
ew.handle(
[&](const int&) { handled = true; },
- [](const std::exception&) { EXPECT_TRUE(false); });
+ [](const std::exception&) { ADD_FAILURE(); });
};
expect_int_no_catch_all_2(ew_eptr1);
auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) {
ew.handle(
- [](const std::exception&) { EXPECT_TRUE(false); },
+ [](const std::exception&) { ADD_FAILURE(); },
[&](...) { handled = true; });
};
auto expect_int_no_catch_all = [&](const exception_wrapper& ew) {
ew.handle(
- [](const std::exception&) { EXPECT_TRUE(false); },
+ [](const std::exception&) { ADD_FAILURE(); },
[&](const BigNonStdError&) { handled = true; });
};
auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) {
ew.handle(
[&](const BigNonStdError&) { handled = true; },
- [](const std::exception&) { EXPECT_TRUE(false); });
+ [](const std::exception&) { ADD_FAILURE(); });
};
expect_int_no_catch_all_2(ew_eptr1);
handled = true;
throw e;
},
- [](const BaseException&) { EXPECT_TRUE(false); }),
+ [](const BaseException&) { ADD_FAILURE(); }),
DerivedException);
EXPECT_TRUE(handled);
handled = false;
handled = true;
throw e;
},
- [](...) { EXPECT_TRUE(false); }),
+ [](...) { ADD_FAILURE(); }),
DerivedException);
EXPECT_TRUE(handled);
}