/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include <gtest/gtest.h>
#include <stdexcept>
+
#include <folly/ExceptionWrapper.h>
#include <folly/Conv.h>
+#include <folly/portability/GTest.h>
using namespace folly;
+class AbstractIntException : public std::exception {
+ public:
+ virtual int getInt() const = 0;
+};
+
+class IntException : public AbstractIntException {
+ public:
+ explicit IntException(int i) : i_(i) {}
+
+ int getInt() const override { return i_; }
+ const char* what() const noexcept override {
+ what_ = folly::to<std::string>("int == ", i_);
+ return what_.c_str();
+ }
+
+ private:
+ int i_;
+ mutable std::string what_;
+};
+
+const static std::string kExceptionClassName =
+ demangle(typeid(std::exception)).toStdString();
+const static std::string kRuntimeErrorClassName =
+ demangle(typeid(std::runtime_error)).toStdString();
+const static std::string kIntExceptionClassName =
+ demangle(typeid(IntException)).toStdString();
+const static std::string kIntClassName = demangle(typeid(int)).toStdString();
+
// Tests that when we call throwException, the proper type is thrown (derived)
TEST(ExceptionWrapper, throw_test) {
std::runtime_error e("payload");
try {
container[0].throwException();
- } catch (std::runtime_error& e) {
+ } catch (std::runtime_error& err) {
std::string expected = "payload";
- std::string actual = e.what();
+ std::string actual = err.what();
EXPECT_EQ(expected, actual);
}
}
EXPECT_EQ(ew.class_name(), "");
ew = make_exception_wrapper<std::runtime_error>("payload");
EXPECT_TRUE(bool(ew));
- EXPECT_EQ(ew.what(), "std::runtime_error: payload");
- EXPECT_EQ(ew.class_name(), "std::runtime_error");
+ EXPECT_EQ(ew.what(), kRuntimeErrorClassName + ": payload");
+ EXPECT_EQ(ew.class_name(), kRuntimeErrorClassName);
}
TEST(ExceptionWrapper, equals) {
});
EXPECT_TRUE(bool(ew));
EXPECT_TRUE(ew.getCopied());
- EXPECT_EQ(ew.what(), "std::runtime_error: payload");
- EXPECT_EQ(ew.class_name(), "std::runtime_error");
+ EXPECT_EQ(ew.what(), kRuntimeErrorClassName + ": payload");
+ EXPECT_EQ(ew.class_name(), kRuntimeErrorClassName);
auto rep = ew.is_compatible_with<std::runtime_error>();
EXPECT_TRUE(rep);
throw std::exception();
});
EXPECT_TRUE(bool(ew3));
- EXPECT_EQ(ew3.what(), "std::exception: std::exception");
- EXPECT_EQ(ew3.class_name(), "std::exception");
+ EXPECT_EQ(ew3.what(), kExceptionClassName + ": std::exception");
+ EXPECT_EQ(ew3.class_name(), kExceptionClassName);
rep = ew3.is_compatible_with<std::runtime_error>();
EXPECT_FALSE(rep);
std::exception);
}
-class AbstractIntException : public std::exception {
-public:
- virtual int getInt() const = 0;
-};
-
-class IntException : public AbstractIntException {
-public:
- explicit IntException(int i)
- : i_(i) {}
-
- virtual int getInt() const { return i_; }
- virtual const char* what() const noexcept override {
- what_ = folly::to<std::string>("int == ", i_);
- return what_.c_str();
- }
-
-private:
- int i_;
- mutable std::string what_;
-};
-
TEST(ExceptionWrapper, with_exception_test) {
int expected = 23;
throw IntException(expected);
});
EXPECT_TRUE(bool(ew));
- EXPECT_EQ(ew.what(), "IntException: int == 23");
- EXPECT_EQ(ew.class_name(), "IntException");
- ew.with_exception<IntException>([&](const IntException& ie) {
- EXPECT_EQ(ie.getInt(), expected);
- });
+ EXPECT_EQ(ew.what(), kIntExceptionClassName + ": int == 23");
+ EXPECT_EQ(ew.class_name(), kIntExceptionClassName);
+ EXPECT_TRUE(ew.with_exception(
+ [&](const IntException& ie) { EXPECT_EQ(ie.getInt(), expected); }));
// I can try_and_catch a non-copyable base class. This will use
// std::exception_ptr internally.
throw IntException(expected);
});
EXPECT_TRUE(bool(ew2));
- EXPECT_EQ(ew2.what(), "IntException: int == 23");
- EXPECT_EQ(ew2.class_name(), "IntException");
- ew2.with_exception<AbstractIntException>([&](AbstractIntException& ie) {
- EXPECT_EQ(ie.getInt(), expected);
- EXPECT_EQ(typeid(ie), typeid(IntException));
- });
+ EXPECT_EQ(ew2.what(), kIntExceptionClassName + ": int == 23");
+ EXPECT_EQ(ew2.class_name(), kIntExceptionClassName);
+ EXPECT_TRUE(ew2.with_exception([&](AbstractIntException& ie) {
+ EXPECT_EQ(ie.getInt(), expected);
+ EXPECT_TRUE(dynamic_cast<IntException*>(&ie));
+ }));
// Test with const this. If this compiles and does not crash due to
// infinite loop when it runs, it succeeds.
const exception_wrapper& cew = ew;
- cew.with_exception<IntException>([&](const IntException& ie) {
- SUCCEED();
- });
+ EXPECT_TRUE(
+ cew.with_exception([&](const IntException& /* ie */) { SUCCEED(); }));
+ // Test with empty ew.
+ exception_wrapper empty_ew;
+ EXPECT_FALSE(
+ empty_ew.with_exception([&](const std::exception& /* ie */) { FAIL(); }));
+
+ // Testing with const exception_wrapper; sanity check first:
+ EXPECT_FALSE(cew.with_exception([&](const std::runtime_error&) {}));
+ EXPECT_FALSE(cew.with_exception([&](const int&) {}));
// This won't even compile. You can't use a function which takes a
// non-const reference with a const exception_wrapper.
-/*
- cew.with_exception<IntException>([&](IntException& ie) {
- SUCCEED();
- });
-*/
+ /*
+ EXPECT_FALSE(cew.with_exception([&](std::runtime_error&) {}));
+ EXPECT_FALSE(cew.with_exception([&](int&) {}));
+ */
+}
+
+TEST(ExceptionWrapper, get_or_make_exception_ptr_test) {
+ int expected = 23;
+
+ // This works, and doesn't slice.
+ exception_wrapper ew = try_and_catch<std::exception, std::runtime_error>(
+ [=]() { throw IntException(expected); });
+ std::exception_ptr eptr = ew.to_exception_ptr();
+ EXPECT_THROW(std::rethrow_exception(eptr), IntException);
+
+ // I can try_and_catch a non-copyable base class. This will use
+ // std::exception_ptr internally.
+ exception_wrapper ew2 = try_and_catch<AbstractIntException>(
+ [=]() { throw IntException(expected); });
+ eptr = ew2.to_exception_ptr();
+ EXPECT_THROW(std::rethrow_exception(eptr), IntException);
+
+ // Test with const this.
+ const exception_wrapper& cew = ew;
+ eptr = cew.to_exception_ptr();
+ EXPECT_THROW(std::rethrow_exception(eptr), IntException);
+
+ // Test with empty ew.
+ exception_wrapper empty_ew;
+ eptr = empty_ew.to_exception_ptr();
+ EXPECT_FALSE(eptr);
+}
+
+TEST(ExceptionWrapper, with_exception_deduction) {
+ auto ew = make_exception_wrapper<std::runtime_error>("hi");
+ EXPECT_TRUE(ew.with_exception([](std::runtime_error&) {}));
+ EXPECT_TRUE(ew.with_exception([](std::exception&) {}));
+ EXPECT_FALSE(ew.with_exception([](std::logic_error&) {}));
+}
+
+TEST(ExceptionWrapper, with_exception_deduction_exn_const) {
+ auto ew = make_exception_wrapper<std::runtime_error>("hi");
+ EXPECT_TRUE(ew.with_exception([](const std::runtime_error&) {}));
+ EXPECT_TRUE(ew.with_exception([](const std::exception&) {}));
+ EXPECT_FALSE(ew.with_exception([](const std::logic_error&) {}));
+}
+
+TEST(ExceptionWrapper, with_exception_deduction_wrap_const_exn_const) {
+ const auto cew = make_exception_wrapper<std::runtime_error>("hi");
+ EXPECT_TRUE(cew.with_exception([](const std::runtime_error&) {}));
+ EXPECT_TRUE(cew.with_exception([](const std::exception&) {}));
+ EXPECT_FALSE(cew.with_exception([](const std::logic_error&) {}));
+}
+
+TEST(ExceptionWrapper, with_exception_deduction_returning) {
+ auto ew = make_exception_wrapper<std::runtime_error>("hi");
+ EXPECT_TRUE(ew.with_exception([](std::runtime_error&) { return 3; }));
+ EXPECT_TRUE(ew.with_exception([](std::exception&) { return "hello"; }));
+ EXPECT_FALSE(ew.with_exception([](std::logic_error&) { return nullptr; }));
+}
+
+namespace {
+template <typename T>
+T& r_to_l(T v) { return std::ref(v); }
+}
+
+TEST(ExceptionWrapper, with_exception_deduction_functor_lvalue) {
+ auto ew = make_exception_wrapper<std::runtime_error>("hi");
+ EXPECT_TRUE(ew.with_exception(r_to_l([](std::runtime_error&) {})));
+ EXPECT_TRUE(ew.with_exception(r_to_l([](std::exception&) {})));
+ EXPECT_FALSE(ew.with_exception(r_to_l([](std::logic_error&) {})));
}
TEST(ExceptionWrapper, non_std_exception_test) {
});
EXPECT_TRUE(bool(ew));
EXPECT_FALSE(ew.is_compatible_with<std::exception>());
- EXPECT_EQ(ew.what(), "int");
- EXPECT_EQ(ew.class_name(), "int");
+ EXPECT_TRUE(ew.is_compatible_with<int>());
+ EXPECT_EQ(ew.what(), kIntClassName);
+ EXPECT_EQ(ew.class_name(), kIntClassName);
// non-std::exception types are supported, but the only way to
// access their value is to explicity rethrow and catch it.
try {
ew.throwException();
- } catch (int& i) {
+ } catch /* nolint */ (int& i) {
EXPECT_EQ(i, expected);
}
}
+
+
+TEST(ExceptionWrapper, exceptionStr) {
+ auto ew = make_exception_wrapper<std::runtime_error>("argh");
+ EXPECT_EQ(kRuntimeErrorClassName + ": argh", exceptionStr(ew));
+}
+
+TEST(ExceptionWrapper, throwException_noException) {
+ exception_wrapper ew;
+ ASSERT_DEATH(ew.throwException(), "empty folly::exception_wrapper");
+}
+
+namespace {
+class TestException : public std::exception { };
+void testEW(const exception_wrapper& ew) {
+ EXPECT_THROW(ew.throwException(), TestException);
+}
+} // namespace
+
+TEST(ExceptionWrapper, implicitConstruction) {
+ // Try with both lvalue and rvalue references
+ TestException e;
+ testEW(e);
+ testEW(TestException());
+}