Lines Matching defs:Error

1 //===----- unittests/ErrorTest.cpp - Error.h tests ------------------------===//
9 #include "llvm/Support/Error.h"
10 #include "llvm-c/Error.h"
15 #include "llvm/Testing/Support/Error.h"
88 static Error handleCustomError(const CustomError &CE) {
89 return Error::success();
94 static Error handleCustomErrorUP(std::unique_ptr<CustomError> CE) {
95 return Error::success();
102 TEST(Error, CheckedSuccess) {
103 Error E = Error::success();
104 EXPECT_FALSE(E) << "Unexpected error while testing Error 'Success'";
109 TEST(Error, UncheckedSuccess) {
110 EXPECT_DEATH({ Error E = Error::success(); },
111 "Program aborted due to an unhandled Error:")
112 << "Unchecked Error Succes value did not cause abort()";
117 void errAsOutParamHelper(Error &Err) {
120 Err = Error::success();
127 TEST(Error, ErrorAsOutParameterChecked) {
128 Error E = Error::success();
135 TEST(Error, ErrorAsOutParameterUnchecked) {
136 EXPECT_DEATH({ Error E = Error::success(); errAsOutParamHelper(E); },
137 "Program aborted due to an unhandled Error:")
146 TEST(Error, UncheckedError) {
148 Error E = make_error<CustomError>(42);
152 "Program aborted due to an unhandled Error:")
153 << "Unhandled Error failure value did not cause abort()";
157 // Check 'Error::isA<T>' method handling.
158 TEST(Error, IsAHandling) {
160 Error E = make_error<CustomError>(1);
161 Error F = make_error<CustomSubError>(1, 2);
162 Error G = Error::success();
176 TEST(Error, HandleCustomError) {
188 // Error (const Err&) mutable
190 // Error (Err&)
192 // Error (Err&) mutable
194 // Error (unique_ptr<Err>)
196 // Error (unique_ptr<Err>) mutable
198 TEST(Error, HandlerTypeDeduction) {
204 [](const CustomError &CE) mutable -> Error { return Error::success(); });
210 [](CustomError &CE) -> Error { return Error::success(); });
215 [](CustomError &CE) mutable -> Error { return Error::success(); });
221 [](std::unique_ptr<CustomError> CE) -> Error { return Error::success(); });
228 [](std::unique_ptr<CustomError> CE) mutable -> Error { return Error::success(); });
233 // Check that named handlers of type 'Error (const Err&)' work.
239 // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work.
242 // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work.
247 TEST(Error, HandleCustomErrorWithCustomBaseClass) {
262 TEST(Error, FirstHandlerOnly) {
274 EXPECT_EQ(CaughtErrorInfo, 42) << "Activated the wrong Error handler(s)";
275 EXPECT_EQ(CaughtErrorExtraInfo, 7) << "Activated the wrong Error handler(s)";
276 EXPECT_EQ(DummyInfo, 0) << "Activated the wrong Error handler(s)";
280 TEST(Error, HandlerShadowing) {
294 << "General Error handler did not shadow specific handler";
296 << "General Error handler did not shadow specific handler";
298 << "General Error handler did not shadow specific handler";
302 TEST(Error, CheckJoinErrors) {
306 Error E =
325 EXPECT_EQ(CustomErrorInfo1, 7) << "Failed handling compound Error.";
326 EXPECT_EQ(CustomErrorInfo2, 42) << "Failed handling compound Error.";
327 EXPECT_EQ(CustomErrorExtraInfo, 7) << "Failed handling compound Error.";
376 TEST(Error, ConsumeSuccess) {
377 Error E = Error::success();
381 TEST(Error, ConsumeError) {
382 Error E = make_error<CustomError>(7);
389 TEST(Error, FailureToHandle) {
400 << "Unhandled Error in handleAllErrors call did not cause an "
409 TEST(Error, FailureFromHandler) {
413 return Error(std::move(SE));
420 << " Error returned from handler in handleAllErrors call did not "
426 TEST(Error, CatchErrorFromHandler) {
429 Error E = handleErrors(
431 [&](std::unique_ptr<CustomError> CE) { return Error(std::move(CE)); });
437 << "Failed to handle Error returned from handleErrors.";
440 TEST(Error, StringError) {
453 TEST(Error, createStringError) {
477 ExitOnErr.setBanner("Error in tool:");
478 ExitOnErr.setExitCodeMapper([](const Error &E) {
485 ExitOnErr(Error::success());
495 ::testing::ExitedWithCode(1), "Error in tool:")
499 ::testing::ExitedWithCode(2), "Error in tool:")
504 TEST(Error, CantFailSuccess) {
505 cantFail(Error::success());
517 TEST(Error, CantFailDeath) {
523 << "cantFail(Error) did not cause an abort for failure value";
538 TEST(Error, CheckedExpectedInSuccessMode) {
546 TEST(Error, ExpectedWithReferenceType) {
556 // We expect this to blow up the same way Error would.
559 TEST(Error, UncheckedExpectedInSuccessModeDestruction) {
567 // We expect this to blow up the same way Error would.
570 TEST(Error, UncheckedExpectedInSuccessModeAccess) {
582 // We expect this to blow up the same way Error would.
585 TEST(Error, UncheckedExpectedInSuccessModeAssignment) {
597 TEST(Error, ExpectedInFailureMode) {
600 Error E = A.takeError();
609 TEST(Error, AccessExpectedInFailureMode) {
621 TEST(Error, UnhandledExpectedInFailureMode) {
629 TEST(Error, ExpectedCovariance) {
649 TEST(Error, HandleExpectedSuccess) {
668 TEST(Error, HandleExpectedUnhandledError) {
686 TEST(Error, HandleExpectedHandledError) {
701 TEST(Error, ErrorCodeConversions) {
705 << "std::error_code() should round-trip via Error conversions";
710 << "std::error_code error value should round-trip via Error "
738 TEST(Error, ErrorMessage) {
739 EXPECT_EQ(toString(Error::success()), "");
741 Error E0 = Error::success();
745 Error E1 = make_error<CustomError>(0);
749 Error E2 = make_error<CustomError>(0);
757 Error E3 = joinErrors(make_error<CustomError>(0), make_error<CustomError>(1));
764 TEST(Error, Stream) {
766 Error OK = Error::success();
774 Error E1 = make_error<CustomError>(0);
783 TEST(Error, SucceededMatcher) {
784 EXPECT_THAT_ERROR(Error::success(), Succeeded());
798 TEST(Error, FailedMatcher) {
800 EXPECT_NONFATAL_FAILURE(EXPECT_THAT_ERROR(Error::success(), Failed()),
805 EXPECT_THAT_ERROR(Error::success(), Failed<CustomError>()),
806 "Expected: failed with Error of given type\n Actual: succeeded");
809 "Error was not of given type");
823 "Expected: failed with Error of given type and the error is an object "
835 TEST(Error, HasValueMatcher) {
862 TEST(Error, FailedWithMessageMatcher) {
869 "Expected: failed with Error whose message has 1 element that is equal "
876 "Expected: failed with Error whose message has 1 element that is equal "
883 "Expected: failed with Error whose message has 2 elements where\n"
893 "Expected: failed with Error whose message has 1 element that is equal "
902 TEST(Error, C_API) {
903 EXPECT_THAT_ERROR(unwrap(wrap(Error::success())), Succeeded())
904 << "Failed to round-trip Error success value via C API";
907 << "Failed to round-trip Error failure value via C API";
932 LLVMCantFail(wrap(Error::success()));
935 TEST(Error, FileErrorTest) {
939 Error S = Error::success();
947 Error E1 = make_error<CustomError>(1);
948 Error FE1 = createFileError("file.bin", std::move(E1));
951 Error E2 = make_error<CustomError>(2);
952 Error FE2 = createFileError("file.bin", std::move(E2));
957 Error E3 = make_error<CustomError>(3);
958 Error FE3 = createFileError("file.bin", std::move(E3));
966 Error FE4 =
972 Error FE5 = createFileError("", make_error<CustomError>(5));
975 Error FE6 = createFileError("unused", make_error<CustomError>(6));
981 TEST(Error, FileErrorErrorCode) {
1044 return "Error 1.";
1046 return "Error 2.";
1059 TEST(Error, SubtypeStringErrorTest) {
1061 EXPECT_EQ(toString(std::move(E1)), "Error 1.");
1065 EXPECT_EQ(toString(std::move(E2)), "Error 1. Detailed information");
1069 EXPECT_EQ(F.message(), "Error 2.");
1075 EXPECT_EQ(toString(std::move(E4)), "Error 1. Detailed information\n"
1076 "Error 2.");
1079 static Error createAnyError() {
1101 TEST(Error, moveInto) {
1146 TEST(Error, FatalBadAllocErrorHandlersInteraction) {
1159 TEST(Error, BadAllocFatalErrorHandlersInteraction) {
1172 TEST(Error, ForwardToExpected) {
1175 "Some Error")
1176 : Error::success();