summaryrefslogtreecommitdiff
path: root/unittests/Support/ErrorTest.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2017-12-18 20:10:56 +0000
committerDimitry Andric <dim@FreeBSD.org>2017-12-18 20:10:56 +0000
commit044eb2f6afba375a914ac9d8024f8f5142bb912e (patch)
tree1475247dc9f9fe5be155ebd4c9069c75aadf8c20 /unittests/Support/ErrorTest.cpp
parenteb70dddbd77e120e5d490bd8fbe7ff3f8fa81c6b (diff)
Notes
Diffstat (limited to 'unittests/Support/ErrorTest.cpp')
-rw-r--r--unittests/Support/ErrorTest.cpp116
1 files changed, 111 insertions, 5 deletions
diff --git a/unittests/Support/ErrorTest.cpp b/unittests/Support/ErrorTest.cpp
index a762cf023f9c..2629e640f79c 100644
--- a/unittests/Support/ErrorTest.cpp
+++ b/unittests/Support/ErrorTest.cpp
@@ -12,6 +12,8 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Testing/Support/Error.h"
+#include "gtest/gtest-spi.h"
#include "gtest/gtest.h"
#include <memory>
@@ -386,7 +388,8 @@ TEST(Error, FailureToHandle) {
});
};
- EXPECT_DEATH(FailToHandle(), "Program aborted due to an unhandled Error:")
+ EXPECT_DEATH(FailToHandle(),
+ "Failure value returned from cantFail wrapped call")
<< "Unhandled Error in handleAllErrors call did not cause an "
"abort()";
}
@@ -405,7 +408,7 @@ TEST(Error, FailureFromHandler) {
};
EXPECT_DEATH(ReturnErrorFromHandler(),
- "Program aborted due to an unhandled Error:")
+ "Failure value returned from cantFail wrapped call")
<< " Error returned from handler in handleAllErrors call did not "
"cause abort()";
}
@@ -482,11 +485,12 @@ TEST(Error, CantFailSuccess) {
}
// Test that cantFail results in a crash if you pass it a failure value.
-#if LLVM_ENABLE_ABI_BREAKING_CHECKS
+#if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG)
TEST(Error, CantFailDeath) {
EXPECT_DEATH(
- cantFail(make_error<StringError>("foo", inconvertibleErrorCode())),
- "Failure value returned from cantFail wrapped call")
+ cantFail(make_error<StringError>("foo", inconvertibleErrorCode()),
+ "Cantfail call failed"),
+ "Cantfail call failed")
<< "cantFail(Error) did not cause an abort for failure value";
EXPECT_DEATH(
@@ -604,6 +608,59 @@ TEST(Error, ExpectedCovariance) {
(void)!!A2;
}
+// Test that handleExpected just returns success values.
+TEST(Error, HandleExpectedSuccess) {
+ auto ValOrErr =
+ handleExpected(Expected<int>(42),
+ []() { return Expected<int>(43); });
+ EXPECT_TRUE(!!ValOrErr)
+ << "handleExpected should have returned a success value here";
+ EXPECT_EQ(*ValOrErr, 42)
+ << "handleExpected should have returned the original success value here";
+}
+
+enum FooStrategy { Aggressive, Conservative };
+
+static Expected<int> foo(FooStrategy S) {
+ if (S == Aggressive)
+ return make_error<CustomError>(7);
+ return 42;
+}
+
+// Test that handleExpected invokes the error path if errors are not handled.
+TEST(Error, HandleExpectedUnhandledError) {
+ // foo(Aggressive) should return a CustomError which should pass through as
+ // there is no handler for CustomError.
+ auto ValOrErr =
+ handleExpected(
+ foo(Aggressive),
+ []() { return foo(Conservative); });
+
+ EXPECT_FALSE(!!ValOrErr)
+ << "handleExpected should have returned an error here";
+ auto Err = ValOrErr.takeError();
+ EXPECT_TRUE(Err.isA<CustomError>())
+ << "handleExpected should have returned the CustomError generated by "
+ "foo(Aggressive) here";
+ consumeError(std::move(Err));
+}
+
+// Test that handleExpected invokes the fallback path if errors are handled.
+TEST(Error, HandleExpectedHandledError) {
+ // foo(Aggressive) should return a CustomError which should handle triggering
+ // the fallback path.
+ auto ValOrErr =
+ handleExpected(
+ foo(Aggressive),
+ []() { return foo(Conservative); },
+ [](const CustomError&) { /* do nothing */ });
+
+ EXPECT_TRUE(!!ValOrErr)
+ << "handleExpected should have returned a success value here";
+ EXPECT_EQ(*ValOrErr, 42)
+ << "handleExpected returned the wrong success value";
+}
+
TEST(Error, ErrorCodeConversions) {
// Round-trip a success value to check that it converts correctly.
EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())),
@@ -659,4 +716,53 @@ TEST(Error, ErrorMessage) {
0);
}
+TEST(Error, ErrorMatchers) {
+ EXPECT_THAT_ERROR(Error::success(), Succeeded());
+ EXPECT_NONFATAL_FAILURE(
+ EXPECT_THAT_ERROR(make_error<CustomError>(0), Succeeded()),
+ "Expected: succeeded\n Actual: failed (CustomError { 0})");
+
+ EXPECT_THAT_ERROR(make_error<CustomError>(0), Failed());
+ EXPECT_NONFATAL_FAILURE(EXPECT_THAT_ERROR(Error::success(), Failed()),
+ "Expected: failed\n Actual: succeeded");
+
+ EXPECT_THAT_EXPECTED(Expected<int>(0), Succeeded());
+ EXPECT_NONFATAL_FAILURE(
+ EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(0)),
+ Succeeded()),
+ "Expected: succeeded\n Actual: failed (CustomError { 0})");
+
+ EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(0)), Failed());
+ EXPECT_NONFATAL_FAILURE(
+ EXPECT_THAT_EXPECTED(Expected<int>(0), Failed()),
+ "Expected: failed\n Actual: succeeded with value 0");
+
+ EXPECT_THAT_EXPECTED(Expected<int>(0), HasValue(0));
+ EXPECT_NONFATAL_FAILURE(
+ EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(0)),
+ HasValue(0)),
+ "Expected: succeeded with value (is equal to 0)\n"
+ " Actual: failed (CustomError { 0})");
+ EXPECT_NONFATAL_FAILURE(
+ EXPECT_THAT_EXPECTED(Expected<int>(1), HasValue(0)),
+ "Expected: succeeded with value (is equal to 0)\n"
+ " Actual: succeeded with value 1, (isn't equal to 0)");
+
+ EXPECT_THAT_EXPECTED(Expected<int &>(make_error<CustomError>(0)), Failed());
+ int a = 1;
+ EXPECT_THAT_EXPECTED(Expected<int &>(a), Succeeded());
+ EXPECT_THAT_EXPECTED(Expected<int &>(a), HasValue(testing::Eq(1)));
+
+ EXPECT_THAT_EXPECTED(Expected<int>(1), HasValue(testing::Gt(0)));
+ EXPECT_NONFATAL_FAILURE(
+ EXPECT_THAT_EXPECTED(Expected<int>(0), HasValue(testing::Gt(1))),
+ "Expected: succeeded with value (is > 1)\n"
+ " Actual: succeeded with value 0, (isn't > 1)");
+ EXPECT_NONFATAL_FAILURE(
+ EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(0)),
+ HasValue(testing::Gt(1))),
+ "Expected: succeeded with value (is > 1)\n"
+ " Actual: failed (CustomError { 0})");
+}
+
} // end anon namespace