folly::Indestructible interface improvement
[folly.git] / folly / test / ExceptionTest.cpp
index a77725d9729095ec87470d763066cba918a56888..b378e91bc97a6a279cd3f9c63a5288203a3382e8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 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.
 #include <folly/Exception.h>
 
 #include <folly/experimental/TestUtil.h>
+#include <folly/portability/GTest.h>
 
 #include <cstdio>
 #include <memory>
 
-#include <glog/logging.h>
-#include <gtest/gtest.h>
-
 namespace folly { namespace test {
 
 #define EXPECT_SYSTEM_ERROR(statement, err, msg) \
@@ -92,4 +90,34 @@ TEST(ExceptionTest, Simple) {
                       EIO, "hello world");
 }
 
-}}  // namespaces
+TEST(ExceptionTest, makeSystemError) {
+  errno = ENOENT;
+  auto ex = makeSystemErrorExplicit(EDEADLK, "stuck");
+  EXPECT_EQ(EDEADLK, ex.code().value());
+  EXPECT_EQ(std::system_category(), ex.code().category());
+  EXPECT_TRUE(StringPiece{ex.what()}.contains("stuck"))
+      << "what() string missing input message: " << ex.what();
+
+  ex = makeSystemErrorExplicit(EDOM, 300, " is bigger than max=", 255);
+  EXPECT_EQ(EDOM, ex.code().value());
+  EXPECT_EQ(std::system_category(), ex.code().category());
+  EXPECT_TRUE(StringPiece{ex.what()}.contains("300 is bigger than max=255"))
+      << "what() string missing input message: " << ex.what();
+
+  errno = EINVAL;
+  ex = makeSystemError("bad argument ", 1234, ": bogus");
+  EXPECT_EQ(EINVAL, ex.code().value());
+  EXPECT_EQ(std::system_category(), ex.code().category());
+  EXPECT_TRUE(StringPiece{ex.what()}.contains("bad argument 1234: bogus"))
+      << "what() string missing input message: " << ex.what();
+
+  errno = 0;
+  ex = makeSystemError("unexpected success");
+  EXPECT_EQ(0, ex.code().value());
+  EXPECT_EQ(std::system_category(), ex.code().category());
+  EXPECT_TRUE(StringPiece{ex.what()}.contains("unexpected success"))
+      << "what() string missing input message: " << ex.what();
+}
+
+} // namespace test
+} // namespace folly