* limitations under the License.
*/
-#include <folly/Memory.h>
#include <folly/Try.h>
+
+#include <glog/logging.h>
+
+#include <folly/Memory.h>
#include <folly/portability/GTest.h>
using namespace folly;
auto result = makeTryWith(func);
EXPECT_TRUE(result.hasException<std::runtime_error>());
}
+
+template <typename E>
+static E* get_exception(std::exception_ptr eptr) {
+ try {
+ std::rethrow_exception(eptr);
+ } catch (E& e) {
+ return &e;
+ } catch (...) {
+ return nullptr;
+ }
+}
+
+TEST(Try, tryGetExceptionObject) {
+ auto epexn = std::make_exception_ptr(std::range_error("oops"));
+ auto epnum = std::make_exception_ptr(17);
+
+ auto exn = CHECK_NOTNULL(get_exception<std::range_error>(epexn));
+ auto num = CHECK_NOTNULL(get_exception<int>(epnum));
+
+ {
+ auto t = Try<bool>(true);
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<bool>(exception_wrapper(epexn, *exn));
+ EXPECT_EQ(exn, t.tryGetExceptionObject());
+ EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<bool>(exception_wrapper(epnum, *num));
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(num, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<void>();
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<void>(exception_wrapper(epexn, *exn));
+ EXPECT_EQ(exn, t.tryGetExceptionObject());
+ EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto t = Try<void>(exception_wrapper(epnum, *num));
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(num, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<bool>(true);
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<bool>(exception_wrapper(epexn, *exn));
+ EXPECT_EQ(exn, t.tryGetExceptionObject());
+ EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<bool>(exception_wrapper(epnum, *num));
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(num, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<void>();
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<void>(exception_wrapper(epexn, *exn));
+ EXPECT_EQ(exn, t.tryGetExceptionObject());
+ EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
+ }
+
+ {
+ auto const t = Try<void>(exception_wrapper(epnum, *num));
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject());
+ EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
+ EXPECT_EQ(num, t.tryGetExceptionObject<int>());
+ }
+}