From 044eb2f6afba375a914ac9d8024f8f5142bb912e Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Mon, 18 Dec 2017 20:10:56 +0000 Subject: Vendor import of llvm trunk r321017: https://llvm.org/svn/llvm-project/llvm/trunk@321017 --- unittests/Support/ErrorTest.cpp | 116 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 5 deletions(-) (limited to 'unittests/Support/ErrorTest.cpp') 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 @@ -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("foo", inconvertibleErrorCode())), - "Failure value returned from cantFail wrapped call") + cantFail(make_error("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(42), + []() { return Expected(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 foo(FooStrategy S) { + if (S == Aggressive) + return make_error(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()) + << "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(0), Succeeded()), + "Expected: succeeded\n Actual: failed (CustomError { 0})"); + + EXPECT_THAT_ERROR(make_error(0), Failed()); + EXPECT_NONFATAL_FAILURE(EXPECT_THAT_ERROR(Error::success(), Failed()), + "Expected: failed\n Actual: succeeded"); + + EXPECT_THAT_EXPECTED(Expected(0), Succeeded()); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected(make_error(0)), + Succeeded()), + "Expected: succeeded\n Actual: failed (CustomError { 0})"); + + EXPECT_THAT_EXPECTED(Expected(make_error(0)), Failed()); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected(0), Failed()), + "Expected: failed\n Actual: succeeded with value 0"); + + EXPECT_THAT_EXPECTED(Expected(0), HasValue(0)); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected(make_error(0)), + HasValue(0)), + "Expected: succeeded with value (is equal to 0)\n" + " Actual: failed (CustomError { 0})"); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected(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(make_error(0)), Failed()); + int a = 1; + EXPECT_THAT_EXPECTED(Expected(a), Succeeded()); + EXPECT_THAT_EXPECTED(Expected(a), HasValue(testing::Eq(1))); + + EXPECT_THAT_EXPECTED(Expected(1), HasValue(testing::Gt(0))); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected(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(make_error(0)), + HasValue(testing::Gt(1))), + "Expected: succeeded with value (is > 1)\n" + " Actual: failed (CustomError { 0})"); +} + } // end anon namespace -- cgit v1.2.3