Add wrapper for getting X509_digest from a cert
[folly.git] / folly / test / ExceptionWrapperTest.cpp
index 07930ee0c2f098d28d6fd3edbfb7a09fec9dbedf..68dbc441c7643dbb7d8a56a25e51ebf0480311ac 100644 (file)
@@ -16,8 +16,8 @@
 
 #include <stdexcept>
 
-#include <folly/ExceptionWrapper.h>
 #include <folly/Conv.h>
+#include <folly/ExceptionWrapper.h>
 #include <folly/portability/GTest.h>
 
 using namespace folly;
@@ -29,17 +29,16 @@ class AbstractIntException : public std::exception {
 
 class IntException : public AbstractIntException {
  public:
-  explicit IntException(int i) : i_(i) {}
+  explicit IntException(int i) : i_(i), what_(to<std::string>("int == ", 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_;
+  std::string what_;
 };
 
 const static std::string kExceptionClassName =
@@ -210,6 +209,28 @@ TEST(ExceptionWrapper, get_or_make_exception_ptr_test) {
   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());
@@ -237,8 +258,8 @@ TEST(ExceptionWrapper, with_shared_ptr_test) {
   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_EQ(kRuntimeErrorClassName, ew.class_name());
+  EXPECT_EQ(kRuntimeErrorClassName + ": 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>());
@@ -269,8 +290,8 @@ TEST(ExceptionWrapper, with_exception_ptr_exn_test) {
   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_EQ(kRuntimeErrorClassName, ew.class_name());
+  EXPECT_EQ(kRuntimeErrorClassName + ": 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>());
@@ -413,7 +434,7 @@ TEST(ExceptionWrapper, with_exception_deduction_returning) {
 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");
@@ -459,7 +480,7 @@ class TestException : public std::exception { };
 void testEW(const exception_wrapper& ew) {
   EXPECT_THROW(ew.throw_exception(), TestException);
 }
-}  // namespace
+} // namespace
 
 TEST(ExceptionWrapper, implicitConstruction) {
   // Try with both lvalue and rvalue references
@@ -480,7 +501,7 @@ exception_wrapper testNonStdException() {
     return exception_wrapper{std::current_exception(), e};
   }
 }
-}
+} // namespace
 
 TEST(ExceptionWrapper, base_derived_non_std_exception_test) {
   auto ew = testNonStdException();
@@ -498,7 +519,7 @@ struct BigRuntimeError : std::runtime_error {
 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"});
@@ -509,10 +530,10 @@ TEST(ExceptionWrapper, handle_std_exception) {
   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);
@@ -527,9 +548,9 @@ TEST(ExceptionWrapper, handle_std_exception) {
 
   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);
@@ -544,10 +565,10 @@ TEST(ExceptionWrapper, handle_std_exception) {
 
   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);
@@ -564,12 +585,12 @@ TEST(ExceptionWrapper, handle_std_exception) {
   // 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);
@@ -590,8 +611,8 @@ TEST(ExceptionWrapper, handle_std_exception_unhandled) {
   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; });
   };
 
@@ -611,7 +632,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) {
 
   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; });
   };
 
@@ -627,7 +648,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) {
 
   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; });
   };
 
@@ -644,7 +665,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) {
   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);
@@ -666,7 +687,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) {
 
   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; });
   };
 
@@ -682,7 +703,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) {
 
   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; });
   };
 
@@ -699,7 +720,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) {
   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);
@@ -725,7 +746,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) {
             handled = true;
             throw e;
           },
-          [](const BaseException&) { EXPECT_TRUE(false); }),
+          [](const BaseException&) { ADD_FAILURE(); }),
       DerivedException);
   EXPECT_TRUE(handled);
   handled = false;
@@ -735,7 +756,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) {
             handled = true;
             throw e;
           },
-          [](...) { EXPECT_TRUE(false); }),
+          [](...) { ADD_FAILURE(); }),
       DerivedException);
   EXPECT_TRUE(handled);
 }
@@ -743,7 +764,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) {
 TEST(ExceptionWrapper, self_swap_test) {
   exception_wrapper ew(std::runtime_error("hello world"));
   folly::swap(ew, ew);
-  EXPECT_STREQ("std::runtime_error: hello world", ew.what().c_str());
+  EXPECT_EQ(kRuntimeErrorClassName + ": hello world", ew.what());
   auto& ew2 = ew;
   ew = std::move(ew2); // should not crash
 }