/*
- * 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) \
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