+TEST(ExceptionWrapper, with_exception_ptr_empty) {
+ auto ew = exception_wrapper(std::exception_ptr());
+ EXPECT_EQ(exception_wrapper::none(), ew.type());
+ EXPECT_FALSE(bool(ew));
+ EXPECT_EQ(nullptr, ew.get_exception());
+ EXPECT_EQ(nullptr, ew.get_object<std::exception>());
+ EXPECT_EQ(nullptr, ew.get_object<int>());
+ EXPECT_FALSE(ew.has_exception_ptr());
+ EXPECT_EQ(nullptr, ew.to_exception_ptr());
+ EXPECT_FALSE(ew.has_exception_ptr());
+ EXPECT_EQ("", ew.class_name());
+ EXPECT_EQ("", ew.what());
+ EXPECT_FALSE(ew.is_compatible_with<std::exception>());
+ EXPECT_FALSE(ew.is_compatible_with<int>());
+ EXPECT_DEATH(ew.throw_exception(), "empty folly::exception_wrapper");
+}
+
+TEST(ExceptionWrapper, with_shared_ptr_test) {
+ auto ew = exception_wrapper(std::runtime_error("foo"));
+ EXPECT_TRUE(bool(ew));
+ EXPECT_EQ(typeid(std::runtime_error), ew.type());
+ EXPECT_NE(nullptr, ew.get_exception());
+ EXPECT_NE(nullptr, ew.get_object<std::exception>());
+ EXPECT_EQ(nullptr, ew.get_object<int>());
+ EXPECT_FALSE(ew.has_exception_ptr());
+ EXPECT_NE(nullptr, ew.to_exception_ptr());
+ EXPECT_TRUE(ew.has_exception_ptr());
+ EXPECT_EQ("std::runtime_error", ew.class_name());
+ EXPECT_EQ("std::runtime_error: foo", ew.what());
+ EXPECT_TRUE(ew.is_compatible_with<std::exception>());
+ EXPECT_TRUE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_FALSE(ew.is_compatible_with<int>());
+ EXPECT_THROW(ew.throw_exception(), std::runtime_error);
+
+ exception_wrapper(std::move(ew));
+ EXPECT_FALSE(bool(ew));
+ EXPECT_EQ(exception_wrapper::none(), ew.type());
+ EXPECT_EQ(nullptr, ew.get_exception());
+ EXPECT_EQ(nullptr, ew.get_object<std::exception>());
+ EXPECT_EQ(nullptr, ew.get_object<int>());
+ EXPECT_EQ(nullptr, ew.to_exception_ptr());
+ EXPECT_EQ("", ew.class_name());
+ EXPECT_EQ("", ew.what());
+ EXPECT_FALSE(ew.is_compatible_with<std::exception>());
+ EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_FALSE(ew.is_compatible_with<int>());
+}
+
+TEST(ExceptionWrapper, with_exception_ptr_exn_test) {
+ auto ep = std::make_exception_ptr(std::runtime_error("foo"));
+ auto ew = exception_wrapper(ep, from_eptr<std::runtime_error>(ep));
+ EXPECT_TRUE(bool(ew));
+ EXPECT_EQ(typeid(std::runtime_error), ew.type());
+ EXPECT_NE(nullptr, ew.get_exception());
+ EXPECT_NE(nullptr, ew.get_object<std::exception>());
+ EXPECT_EQ(nullptr, ew.get_object<int>());
+ EXPECT_TRUE(ew.has_exception_ptr());
+ EXPECT_EQ(ep, ew.to_exception_ptr());
+ EXPECT_TRUE(ew.has_exception_ptr());
+ EXPECT_EQ("std::runtime_error", ew.class_name());
+ EXPECT_EQ("std::runtime_error: foo", ew.what());
+ EXPECT_TRUE(ew.is_compatible_with<std::exception>());
+ EXPECT_TRUE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_FALSE(ew.is_compatible_with<int>());
+ EXPECT_THROW(ew.throw_exception(), std::runtime_error);
+
+ exception_wrapper(std::move(ew));
+ EXPECT_FALSE(bool(ew));
+ EXPECT_EQ(exception_wrapper::none(), ew.type());
+ EXPECT_EQ(nullptr, ew.get_exception());
+ EXPECT_EQ(nullptr, ew.get_object<std::exception>());
+ EXPECT_EQ(nullptr, ew.get_object<int>());
+ EXPECT_EQ(nullptr, ew.to_exception_ptr());
+ EXPECT_EQ("", ew.class_name());
+ EXPECT_EQ("", ew.what());
+ EXPECT_FALSE(ew.is_compatible_with<std::exception>());
+ EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_FALSE(ew.is_compatible_with<int>());
+}
+
+TEST(ExceptionWrapper, with_exception_ptr_any_test) {
+ auto ep = std::make_exception_ptr<int>(12);
+ auto ew = exception_wrapper(ep, from_eptr<int>(ep));
+ EXPECT_TRUE(bool(ew));
+ EXPECT_EQ(nullptr, ew.get_exception());
+ EXPECT_EQ(nullptr, ew.get_object<std::exception>());
+ EXPECT_NE(nullptr, ew.get_object<int>());
+ EXPECT_TRUE(ew.has_exception_ptr());
+ EXPECT_EQ(ep, ew.to_exception_ptr());
+ EXPECT_TRUE(ew.has_exception_ptr());
+ EXPECT_EQ("int", ew.class_name());
+ EXPECT_EQ("int", ew.what());
+ EXPECT_FALSE(ew.is_compatible_with<std::exception>());
+ EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_TRUE(ew.is_compatible_with<int>());
+ EXPECT_THROW(ew.throw_exception(), int);
+
+ exception_wrapper(std::move(ew));
+ EXPECT_FALSE(bool(ew));
+ EXPECT_EQ(nullptr, ew.get_exception());
+ EXPECT_EQ(nullptr, ew.get_object<std::exception>());
+ EXPECT_EQ(nullptr, ew.get_object<int>());
+ EXPECT_EQ(nullptr, ew.to_exception_ptr());
+ EXPECT_FALSE(ew.has_exception_ptr());
+ EXPECT_EQ("", ew.class_name());
+ EXPECT_EQ("", ew.what());
+ EXPECT_FALSE(ew.is_compatible_with<std::exception>());
+ EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_FALSE(ew.is_compatible_with<int>());
+}
+
+TEST(ExceptionWrapper, with_non_std_exception_test) {
+ auto ew = exception_wrapper(folly::in_place, 42);
+ EXPECT_TRUE(bool(ew));
+ EXPECT_EQ(nullptr, ew.get_exception());
+ EXPECT_EQ(nullptr, ew.get_object<std::exception>());
+ EXPECT_NE(nullptr, ew.get_object<int>());
+ EXPECT_FALSE(ew.has_exception_ptr());
+ EXPECT_EQ("int", ew.class_name());
+ EXPECT_EQ("int", ew.what());
+ EXPECT_NE(nullptr, ew.to_exception_ptr());
+ EXPECT_TRUE(ew.has_exception_ptr());
+ EXPECT_EQ("int", ew.class_name());
+ EXPECT_EQ("int", ew.what());
+ EXPECT_FALSE(ew.is_compatible_with<std::exception>());
+ EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_TRUE(ew.is_compatible_with<int>());
+ EXPECT_THROW(ew.throw_exception(), int);
+
+ exception_wrapper(std::move(ew));
+ EXPECT_FALSE(bool(ew));
+ EXPECT_EQ(nullptr, ew.get_exception());
+ EXPECT_EQ(nullptr, ew.get_object<std::exception>());
+ EXPECT_EQ(nullptr, ew.get_object<int>());
+ EXPECT_EQ(nullptr, ew.to_exception_ptr());
+ EXPECT_FALSE(ew.has_exception_ptr());
+ EXPECT_EQ("", ew.class_name());
+ EXPECT_EQ("", ew.what());
+ EXPECT_FALSE(ew.is_compatible_with<std::exception>());
+ EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_FALSE(ew.is_compatible_with<int>());
+}
+
+TEST(ExceptionWrapper, with_exception_ptr_any_nil_test) {
+ auto ep = std::make_exception_ptr<int>(12);
+ auto ew = exception_wrapper(ep); // concrete type is erased
+ EXPECT_TRUE(bool(ew));
+ EXPECT_EQ(nullptr, ew.get_exception());
+ EXPECT_EQ(nullptr, ew.get_object<std::exception>());
+ EXPECT_NE(nullptr, ew.get_object<int>());
+ EXPECT_EQ(ep, ew.to_exception_ptr());
+ EXPECT_EQ("<unknown exception>", ew.class_name()); // because concrete type is
+ // erased
+ EXPECT_EQ("<unknown exception>", ew.what());
+ EXPECT_FALSE(ew.is_compatible_with<std::exception>());
+ EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_TRUE(ew.is_compatible_with<int>());
+ EXPECT_THROW(ew.throw_exception(), int);
+
+ exception_wrapper(std::move(ew));
+ EXPECT_FALSE(bool(ew));
+ EXPECT_EQ(nullptr, ew.get_exception());
+ EXPECT_EQ(nullptr, ew.get_object<std::exception>());
+ EXPECT_EQ(nullptr, ew.get_object<int>());
+ EXPECT_EQ(nullptr, ew.to_exception_ptr());
+ EXPECT_EQ("", ew.class_name());
+ EXPECT_EQ("", ew.what());
+ EXPECT_FALSE(ew.is_compatible_with<std::exception>());
+ EXPECT_FALSE(ew.is_compatible_with<std::runtime_error>());
+ EXPECT_FALSE(ew.is_compatible_with<int>());
+}
+