1f7f6d3e9SLang Hames //===----- unittests/ErrorTest.cpp - Error.h tests ------------------------===// 2f7f6d3e9SLang Hames // 3f7f6d3e9SLang Hames // The LLVM Compiler Infrastructure 4f7f6d3e9SLang Hames // 5f7f6d3e9SLang Hames // This file is distributed under the University of Illinois Open Source 6f7f6d3e9SLang Hames // License. See LICENSE.TXT for details. 7f7f6d3e9SLang Hames // 8f7f6d3e9SLang Hames //===----------------------------------------------------------------------===// 9f7f6d3e9SLang Hames 10f7f6d3e9SLang Hames #include "llvm/Support/Error.h" 11c5e0bbd7SLang Hames 12c5e0bbd7SLang Hames #include "llvm/ADT/Twine.h" 13f7f6d3e9SLang Hames #include "llvm/Support/Errc.h" 14e7aad357SLang Hames #include "llvm/Support/ErrorHandling.h" 15f7f6d3e9SLang Hames #include "gtest/gtest.h" 16f7f6d3e9SLang Hames #include <memory> 17f7f6d3e9SLang Hames 18f7f6d3e9SLang Hames using namespace llvm; 19f7f6d3e9SLang Hames 20f7f6d3e9SLang Hames namespace { 21f7f6d3e9SLang Hames 22f7f6d3e9SLang Hames // Custom error class with a default base class and some random 'info' attached. 23f7f6d3e9SLang Hames class CustomError : public ErrorInfo<CustomError> { 24f7f6d3e9SLang Hames public: 25f7f6d3e9SLang Hames // Create an error with some info attached. 26f7f6d3e9SLang Hames CustomError(int Info) : Info(Info) {} 27f7f6d3e9SLang Hames 28f7f6d3e9SLang Hames // Get the info attached to this error. 29f7f6d3e9SLang Hames int getInfo() const { return Info; } 30f7f6d3e9SLang Hames 31f7f6d3e9SLang Hames // Log this error to a stream. 32f7f6d3e9SLang Hames void log(raw_ostream &OS) const override { 33f7f6d3e9SLang Hames OS << "CustomError { " << getInfo() << "}"; 34f7f6d3e9SLang Hames } 35f7f6d3e9SLang Hames 36e7aad357SLang Hames std::error_code convertToErrorCode() const override { 37e7aad357SLang Hames llvm_unreachable("CustomError doesn't support ECError conversion"); 38e7aad357SLang Hames } 39e7aad357SLang Hames 40a15b76b3SReid Kleckner // Used by ErrorInfo::classID. 41a15b76b3SReid Kleckner static char ID; 42a15b76b3SReid Kleckner 43f7f6d3e9SLang Hames protected: 44f7f6d3e9SLang Hames // This error is subclassed below, but we can't use inheriting constructors 45f7f6d3e9SLang Hames // yet, so we can't propagate the constructors through ErrorInfo. Instead 46f7f6d3e9SLang Hames // we have to have a default constructor and have the subclass initialize all 47f7f6d3e9SLang Hames // fields. 48f7f6d3e9SLang Hames CustomError() : Info(0) {} 49f7f6d3e9SLang Hames 50f7f6d3e9SLang Hames int Info; 51f7f6d3e9SLang Hames }; 52f7f6d3e9SLang Hames 53a15b76b3SReid Kleckner char CustomError::ID = 0; 54a15b76b3SReid Kleckner 55f7f6d3e9SLang Hames // Custom error class with a custom base class and some additional random 56f7f6d3e9SLang Hames // 'info'. 57f7f6d3e9SLang Hames class CustomSubError : public ErrorInfo<CustomSubError, CustomError> { 58f7f6d3e9SLang Hames public: 59f7f6d3e9SLang Hames // Create a sub-error with some info attached. 60f7f6d3e9SLang Hames CustomSubError(int Info, int ExtraInfo) : ExtraInfo(ExtraInfo) { 61f7f6d3e9SLang Hames this->Info = Info; 62f7f6d3e9SLang Hames } 63f7f6d3e9SLang Hames 64f7f6d3e9SLang Hames // Get the extra info attached to this error. 65f7f6d3e9SLang Hames int getExtraInfo() const { return ExtraInfo; } 66f7f6d3e9SLang Hames 67f7f6d3e9SLang Hames // Log this error to a stream. 68f7f6d3e9SLang Hames void log(raw_ostream &OS) const override { 69f7f6d3e9SLang Hames OS << "CustomSubError { " << getInfo() << ", " << getExtraInfo() << "}"; 70f7f6d3e9SLang Hames } 71f7f6d3e9SLang Hames 72e7aad357SLang Hames std::error_code convertToErrorCode() const override { 73e7aad357SLang Hames llvm_unreachable("CustomSubError doesn't support ECError conversion"); 74e7aad357SLang Hames } 75e7aad357SLang Hames 76a15b76b3SReid Kleckner // Used by ErrorInfo::classID. 77a15b76b3SReid Kleckner static char ID; 78a15b76b3SReid Kleckner 79f7f6d3e9SLang Hames protected: 80f7f6d3e9SLang Hames int ExtraInfo; 81f7f6d3e9SLang Hames }; 82f7f6d3e9SLang Hames 83a15b76b3SReid Kleckner char CustomSubError::ID = 0; 84a15b76b3SReid Kleckner 85*41af4309SMehdi Amini static Error handleCustomError(const CustomError &CE) { 86*41af4309SMehdi Amini return Error::success(); 87*41af4309SMehdi Amini } 88f7f6d3e9SLang Hames 89f7f6d3e9SLang Hames static void handleCustomErrorVoid(const CustomError &CE) {} 90f7f6d3e9SLang Hames 91f7f6d3e9SLang Hames static Error handleCustomErrorUP(std::unique_ptr<CustomError> CE) { 92*41af4309SMehdi Amini return Error::success(); 93f7f6d3e9SLang Hames } 94f7f6d3e9SLang Hames 95f7f6d3e9SLang Hames static void handleCustomErrorUPVoid(std::unique_ptr<CustomError> CE) {} 96f7f6d3e9SLang Hames 9701864a2dSLang Hames // Test that success values implicitly convert to false, and don't cause crashes 9801864a2dSLang Hames // once they've been implicitly converted. 9901864a2dSLang Hames TEST(Error, CheckedSuccess) { 10001864a2dSLang Hames Error E; 10101864a2dSLang Hames EXPECT_FALSE(E) << "Unexpected error while testing Error 'Success'"; 10201864a2dSLang Hames } 10301864a2dSLang Hames 10401864a2dSLang Hames // Test that unchecked succes values cause an abort. 10501864a2dSLang Hames #ifndef NDEBUG 10601864a2dSLang Hames TEST(Error, UncheckedSuccess) { 10701864a2dSLang Hames EXPECT_DEATH({ Error E; }, "Program aborted due to an unhandled Error:") 10801864a2dSLang Hames << "Unchecked Error Succes value did not cause abort()"; 10901864a2dSLang Hames } 11001864a2dSLang Hames #endif 11101864a2dSLang Hames 112d1af8fceSLang Hames // ErrorAsOutParameter tester. 113d1af8fceSLang Hames void errAsOutParamHelper(Error &Err) { 1145e51a2e3SLang Hames ErrorAsOutParameter ErrAsOutParam(&Err); 115d1af8fceSLang Hames // Verify that checked flag is raised - assignment should not crash. 116d1af8fceSLang Hames Err = Error::success(); 117d1af8fceSLang Hames // Raise the checked bit manually - caller should still have to test the 118d1af8fceSLang Hames // error. 119d1af8fceSLang Hames (void)!!Err; 120d0ac31a7SLang Hames } 121d0ac31a7SLang Hames 122d1af8fceSLang Hames // Test that ErrorAsOutParameter sets the checked flag on construction. 123d1af8fceSLang Hames TEST(Error, ErrorAsOutParameterChecked) { 124d1af8fceSLang Hames Error E; 125d1af8fceSLang Hames errAsOutParamHelper(E); 126d1af8fceSLang Hames (void)!!E; 127d1af8fceSLang Hames } 128d1af8fceSLang Hames 129d1af8fceSLang Hames // Test that ErrorAsOutParameter clears the checked flag on destruction. 130d1af8fceSLang Hames #ifndef NDEBUG 131d1af8fceSLang Hames TEST(Error, ErrorAsOutParameterUnchecked) { 132d1af8fceSLang Hames EXPECT_DEATH({ Error E; errAsOutParamHelper(E); }, 133d1af8fceSLang Hames "Program aborted due to an unhandled Error:") 134d1af8fceSLang Hames << "ErrorAsOutParameter did not clear the checked flag on destruction."; 135d1af8fceSLang Hames } 136d1af8fceSLang Hames #endif 137d1af8fceSLang Hames 138f7f6d3e9SLang Hames // Check that we abort on unhandled failure cases. (Force conversion to bool 139f7f6d3e9SLang Hames // to make sure that we don't accidentally treat checked errors as handled). 140f7f6d3e9SLang Hames // Test runs in debug mode only. 141f7f6d3e9SLang Hames #ifndef NDEBUG 14201864a2dSLang Hames TEST(Error, UncheckedError) { 143f7f6d3e9SLang Hames auto DropUnhandledError = []() { 144f7f6d3e9SLang Hames Error E = make_error<CustomError>(42); 145f7f6d3e9SLang Hames (void)!E; 146f7f6d3e9SLang Hames }; 147f7f6d3e9SLang Hames EXPECT_DEATH(DropUnhandledError(), 148f7f6d3e9SLang Hames "Program aborted due to an unhandled Error:") 149f7f6d3e9SLang Hames << "Unhandled Error failure value did not cause abort()"; 150f7f6d3e9SLang Hames } 151f7f6d3e9SLang Hames #endif 152f7f6d3e9SLang Hames 15301864a2dSLang Hames // Check 'Error::isA<T>' method handling. 15401864a2dSLang Hames TEST(Error, IsAHandling) { 155f7f6d3e9SLang Hames // Check 'isA' handling. 156f7f6d3e9SLang Hames Error E = make_error<CustomError>(1); 157f7f6d3e9SLang Hames Error F = make_error<CustomSubError>(1, 2); 15801a3cf4dSLang Hames Error G = Error::success(); 159f7f6d3e9SLang Hames 160f7f6d3e9SLang Hames EXPECT_TRUE(E.isA<CustomError>()); 161f7f6d3e9SLang Hames EXPECT_FALSE(E.isA<CustomSubError>()); 162f7f6d3e9SLang Hames EXPECT_TRUE(F.isA<CustomError>()); 163f7f6d3e9SLang Hames EXPECT_TRUE(F.isA<CustomSubError>()); 16401a3cf4dSLang Hames EXPECT_FALSE(G.isA<CustomError>()); 165f7f6d3e9SLang Hames 166f7f6d3e9SLang Hames consumeError(std::move(E)); 167f7f6d3e9SLang Hames consumeError(std::move(F)); 16801a3cf4dSLang Hames consumeError(std::move(G)); 169f7f6d3e9SLang Hames } 170f7f6d3e9SLang Hames 171f7f6d3e9SLang Hames // Check that we can handle a custom error. 17201864a2dSLang Hames TEST(Error, HandleCustomError) { 173f7f6d3e9SLang Hames int CaughtErrorInfo = 0; 174f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), [&](const CustomError &CE) { 175f7f6d3e9SLang Hames CaughtErrorInfo = CE.getInfo(); 176f7f6d3e9SLang Hames }); 177f7f6d3e9SLang Hames 17801864a2dSLang Hames EXPECT_TRUE(CaughtErrorInfo == 42) << "Wrong result from CustomError handler"; 179f7f6d3e9SLang Hames } 180f7f6d3e9SLang Hames 181f7f6d3e9SLang Hames // Check that handler type deduction also works for handlers 182f7f6d3e9SLang Hames // of the following types: 183f7f6d3e9SLang Hames // void (const Err&) 184f7f6d3e9SLang Hames // Error (const Err&) mutable 185f7f6d3e9SLang Hames // void (const Err&) mutable 186f7f6d3e9SLang Hames // Error (Err&) 187f7f6d3e9SLang Hames // void (Err&) 188f7f6d3e9SLang Hames // Error (Err&) mutable 189f7f6d3e9SLang Hames // void (Err&) mutable 190f7f6d3e9SLang Hames // Error (unique_ptr<Err>) 191f7f6d3e9SLang Hames // void (unique_ptr<Err>) 192f7f6d3e9SLang Hames // Error (unique_ptr<Err>) mutable 193f7f6d3e9SLang Hames // void (unique_ptr<Err>) mutable 19401864a2dSLang Hames TEST(Error, HandlerTypeDeduction) { 195f7f6d3e9SLang Hames 196f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), [](const CustomError &CE) {}); 197f7f6d3e9SLang Hames 198f7f6d3e9SLang Hames handleAllErrors( 199f7f6d3e9SLang Hames make_error<CustomError>(42), 200f7f6d3e9SLang Hames [](const CustomError &CE) mutable { return Error::success(); }); 201f7f6d3e9SLang Hames 202f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 203f7f6d3e9SLang Hames [](const CustomError &CE) mutable {}); 204f7f6d3e9SLang Hames 205f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 206f7f6d3e9SLang Hames [](CustomError &CE) { return Error::success(); }); 207f7f6d3e9SLang Hames 208f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) {}); 209f7f6d3e9SLang Hames 210f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 211f7f6d3e9SLang Hames [](CustomError &CE) mutable { return Error::success(); }); 212f7f6d3e9SLang Hames 213f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) mutable {}); 214f7f6d3e9SLang Hames 215f7f6d3e9SLang Hames handleAllErrors( 216f7f6d3e9SLang Hames make_error<CustomError>(42), 217f7f6d3e9SLang Hames [](std::unique_ptr<CustomError> CE) { return Error::success(); }); 218f7f6d3e9SLang Hames 219f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 220f7f6d3e9SLang Hames [](std::unique_ptr<CustomError> CE) {}); 221f7f6d3e9SLang Hames 222f7f6d3e9SLang Hames handleAllErrors( 223f7f6d3e9SLang Hames make_error<CustomError>(42), 224f7f6d3e9SLang Hames [](std::unique_ptr<CustomError> CE) mutable { return Error::success(); }); 225f7f6d3e9SLang Hames 226f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 227f7f6d3e9SLang Hames [](std::unique_ptr<CustomError> CE) mutable {}); 228f7f6d3e9SLang Hames 229f7f6d3e9SLang Hames // Check that named handlers of type 'Error (const Err&)' work. 230f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), handleCustomError); 231f7f6d3e9SLang Hames 232f7f6d3e9SLang Hames // Check that named handlers of type 'void (const Err&)' work. 233f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), handleCustomErrorVoid); 234f7f6d3e9SLang Hames 235f7f6d3e9SLang Hames // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work. 236f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), handleCustomErrorUP); 237f7f6d3e9SLang Hames 238f7f6d3e9SLang Hames // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work. 239f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), handleCustomErrorUPVoid); 24001864a2dSLang Hames } 241f7f6d3e9SLang Hames 24201864a2dSLang Hames // Test that we can handle errors with custom base classes. 24301864a2dSLang Hames TEST(Error, HandleCustomErrorWithCustomBaseClass) { 244f7f6d3e9SLang Hames int CaughtErrorInfo = 0; 245f7f6d3e9SLang Hames int CaughtErrorExtraInfo = 0; 246f7f6d3e9SLang Hames handleAllErrors(make_error<CustomSubError>(42, 7), 247f7f6d3e9SLang Hames [&](const CustomSubError &SE) { 248f7f6d3e9SLang Hames CaughtErrorInfo = SE.getInfo(); 249f7f6d3e9SLang Hames CaughtErrorExtraInfo = SE.getExtraInfo(); 250f7f6d3e9SLang Hames }); 251f7f6d3e9SLang Hames 252f7f6d3e9SLang Hames EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7) 253f7f6d3e9SLang Hames << "Wrong result from CustomSubError handler"; 254f7f6d3e9SLang Hames } 255f7f6d3e9SLang Hames 256f7f6d3e9SLang Hames // Check that we trigger only the first handler that applies. 25701864a2dSLang Hames TEST(Error, FirstHandlerOnly) { 258f7f6d3e9SLang Hames int DummyInfo = 0; 259f7f6d3e9SLang Hames int CaughtErrorInfo = 0; 260f7f6d3e9SLang Hames int CaughtErrorExtraInfo = 0; 261f7f6d3e9SLang Hames 262f7f6d3e9SLang Hames handleAllErrors(make_error<CustomSubError>(42, 7), 263f7f6d3e9SLang Hames [&](const CustomSubError &SE) { 264f7f6d3e9SLang Hames CaughtErrorInfo = SE.getInfo(); 265f7f6d3e9SLang Hames CaughtErrorExtraInfo = SE.getExtraInfo(); 266f7f6d3e9SLang Hames }, 267f7f6d3e9SLang Hames [&](const CustomError &CE) { DummyInfo = CE.getInfo(); }); 268f7f6d3e9SLang Hames 269f7f6d3e9SLang Hames EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7 && 270f7f6d3e9SLang Hames DummyInfo == 0) 271f7f6d3e9SLang Hames << "Activated the wrong Error handler(s)"; 272f7f6d3e9SLang Hames } 273f7f6d3e9SLang Hames 274f7f6d3e9SLang Hames // Check that general handlers shadow specific ones. 27501864a2dSLang Hames TEST(Error, HandlerShadowing) { 276f7f6d3e9SLang Hames int CaughtErrorInfo = 0; 277f7f6d3e9SLang Hames int DummyInfo = 0; 278f7f6d3e9SLang Hames int DummyExtraInfo = 0; 279f7f6d3e9SLang Hames 280f7f6d3e9SLang Hames handleAllErrors( 281f7f6d3e9SLang Hames make_error<CustomSubError>(42, 7), 282f7f6d3e9SLang Hames [&](const CustomError &CE) { CaughtErrorInfo = CE.getInfo(); }, 283f7f6d3e9SLang Hames [&](const CustomSubError &SE) { 284f7f6d3e9SLang Hames DummyInfo = SE.getInfo(); 285f7f6d3e9SLang Hames DummyExtraInfo = SE.getExtraInfo(); 286f7f6d3e9SLang Hames }); 287f7f6d3e9SLang Hames 288b2cef64bSNAKAMURA Takumi EXPECT_TRUE(CaughtErrorInfo == 42 && DummyInfo == 0 && DummyExtraInfo == 0) 289f7f6d3e9SLang Hames << "General Error handler did not shadow specific handler"; 290f7f6d3e9SLang Hames } 291f7f6d3e9SLang Hames 29201864a2dSLang Hames // Test joinErrors. 29301864a2dSLang Hames TEST(Error, CheckJoinErrors) { 294f7f6d3e9SLang Hames int CustomErrorInfo1 = 0; 295f7f6d3e9SLang Hames int CustomErrorInfo2 = 0; 296f7f6d3e9SLang Hames int CustomErrorExtraInfo = 0; 29701864a2dSLang Hames Error E = 29801864a2dSLang Hames joinErrors(make_error<CustomError>(7), make_error<CustomSubError>(42, 7)); 299f7f6d3e9SLang Hames 300f7f6d3e9SLang Hames handleAllErrors(std::move(E), 301f7f6d3e9SLang Hames [&](const CustomSubError &SE) { 302f7f6d3e9SLang Hames CustomErrorInfo2 = SE.getInfo(); 303f7f6d3e9SLang Hames CustomErrorExtraInfo = SE.getExtraInfo(); 304f7f6d3e9SLang Hames }, 305f7f6d3e9SLang Hames [&](const CustomError &CE) { 306f7f6d3e9SLang Hames // Assert that the CustomError instance above is handled 307f7f6d3e9SLang Hames // before the 308f7f6d3e9SLang Hames // CustomSubError - joinErrors should preserve error 309f7f6d3e9SLang Hames // ordering. 310f7f6d3e9SLang Hames EXPECT_EQ(CustomErrorInfo2, 0) 311f7f6d3e9SLang Hames << "CustomErrorInfo2 should be 0 here. " 312f7f6d3e9SLang Hames "joinErrors failed to preserve ordering.\n"; 313f7f6d3e9SLang Hames CustomErrorInfo1 = CE.getInfo(); 314f7f6d3e9SLang Hames }); 315f7f6d3e9SLang Hames 316f7f6d3e9SLang Hames EXPECT_TRUE(CustomErrorInfo1 == 7 && CustomErrorInfo2 == 42 && 317f7f6d3e9SLang Hames CustomErrorExtraInfo == 7) 318f7f6d3e9SLang Hames << "Failed handling compound Error."; 319759b30edSLang Hames 320759b30edSLang Hames // Test appending a single item to a list. 321759b30edSLang Hames { 322759b30edSLang Hames int Sum = 0; 323759b30edSLang Hames handleAllErrors( 324759b30edSLang Hames joinErrors( 325759b30edSLang Hames joinErrors(make_error<CustomError>(7), 326759b30edSLang Hames make_error<CustomError>(7)), 327759b30edSLang Hames make_error<CustomError>(7)), 328759b30edSLang Hames [&](const CustomError &CE) { 329759b30edSLang Hames Sum += CE.getInfo(); 330759b30edSLang Hames }); 331759b30edSLang Hames EXPECT_EQ(Sum, 21) << "Failed to correctly append error to error list."; 332759b30edSLang Hames } 333759b30edSLang Hames 334759b30edSLang Hames // Test prepending a single item to a list. 335759b30edSLang Hames { 336759b30edSLang Hames int Sum = 0; 337759b30edSLang Hames handleAllErrors( 338759b30edSLang Hames joinErrors( 339759b30edSLang Hames make_error<CustomError>(7), 340759b30edSLang Hames joinErrors(make_error<CustomError>(7), 341759b30edSLang Hames make_error<CustomError>(7))), 342759b30edSLang Hames [&](const CustomError &CE) { 343759b30edSLang Hames Sum += CE.getInfo(); 344759b30edSLang Hames }); 345759b30edSLang Hames EXPECT_EQ(Sum, 21) << "Failed to correctly prepend error to error list."; 346759b30edSLang Hames } 347759b30edSLang Hames 348759b30edSLang Hames // Test concatenating two error lists. 349759b30edSLang Hames { 350759b30edSLang Hames int Sum = 0; 351759b30edSLang Hames handleAllErrors( 352759b30edSLang Hames joinErrors( 353759b30edSLang Hames joinErrors( 354759b30edSLang Hames make_error<CustomError>(7), 355759b30edSLang Hames make_error<CustomError>(7)), 356759b30edSLang Hames joinErrors( 357759b30edSLang Hames make_error<CustomError>(7), 358759b30edSLang Hames make_error<CustomError>(7))), 359759b30edSLang Hames [&](const CustomError &CE) { 360759b30edSLang Hames Sum += CE.getInfo(); 361759b30edSLang Hames }); 362759b30edSLang Hames EXPECT_EQ(Sum, 28) << "Failed to correctly concatenate erorr lists."; 363759b30edSLang Hames } 364f7f6d3e9SLang Hames } 365f7f6d3e9SLang Hames 36601864a2dSLang Hames // Test that we can consume success values. 36701864a2dSLang Hames TEST(Error, ConsumeSuccess) { 368f7f6d3e9SLang Hames Error E; 369f7f6d3e9SLang Hames consumeError(std::move(E)); 370f7f6d3e9SLang Hames } 37101864a2dSLang Hames 37201864a2dSLang Hames TEST(Error, ConsumeError) { 373f7f6d3e9SLang Hames Error E = make_error<CustomError>(7); 374f7f6d3e9SLang Hames consumeError(std::move(E)); 375f7f6d3e9SLang Hames } 376f7f6d3e9SLang Hames 377f7f6d3e9SLang Hames // Test that handleAllUnhandledErrors crashes if an error is not caught. 378f7f6d3e9SLang Hames // Test runs in debug mode only. 379f7f6d3e9SLang Hames #ifndef NDEBUG 38001864a2dSLang Hames TEST(Error, FailureToHandle) { 381f7f6d3e9SLang Hames auto FailToHandle = []() { 38201864a2dSLang Hames handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) { 383f7f6d3e9SLang Hames errs() << "This should never be called"; 384f7f6d3e9SLang Hames exit(1); 385f7f6d3e9SLang Hames }); 386f7f6d3e9SLang Hames }; 387f7f6d3e9SLang Hames 388f7f6d3e9SLang Hames EXPECT_DEATH(FailToHandle(), "Program aborted due to an unhandled Error:") 389f7f6d3e9SLang Hames << "Unhandled Error in handleAllErrors call did not cause an " 390f7f6d3e9SLang Hames "abort()"; 391f7f6d3e9SLang Hames } 392f7f6d3e9SLang Hames #endif 393f7f6d3e9SLang Hames 394f7f6d3e9SLang Hames // Test that handleAllUnhandledErrors crashes if an error is returned from a 395f7f6d3e9SLang Hames // handler. 396f7f6d3e9SLang Hames // Test runs in debug mode only. 397f7f6d3e9SLang Hames #ifndef NDEBUG 39801864a2dSLang Hames TEST(Error, FailureFromHandler) { 399f7f6d3e9SLang Hames auto ReturnErrorFromHandler = []() { 400f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(7), 401f7f6d3e9SLang Hames [&](std::unique_ptr<CustomSubError> SE) { 402f7f6d3e9SLang Hames return Error(std::move(SE)); 403f7f6d3e9SLang Hames }); 404f7f6d3e9SLang Hames }; 405f7f6d3e9SLang Hames 406f7f6d3e9SLang Hames EXPECT_DEATH(ReturnErrorFromHandler(), 407f7f6d3e9SLang Hames "Program aborted due to an unhandled Error:") 408f7f6d3e9SLang Hames << " Error returned from handler in handleAllErrors call did not " 409f7f6d3e9SLang Hames "cause abort()"; 410f7f6d3e9SLang Hames } 411f7f6d3e9SLang Hames #endif 412f7f6d3e9SLang Hames 413f7f6d3e9SLang Hames // Test that we can return values from handleErrors. 41401864a2dSLang Hames TEST(Error, CatchErrorFromHandler) { 415f7f6d3e9SLang Hames int ErrorInfo = 0; 416f7f6d3e9SLang Hames 417f7f6d3e9SLang Hames Error E = handleErrors( 418f7f6d3e9SLang Hames make_error<CustomError>(7), 419f7f6d3e9SLang Hames [&](std::unique_ptr<CustomError> CE) { return Error(std::move(CE)); }); 420f7f6d3e9SLang Hames 421f7f6d3e9SLang Hames handleAllErrors(std::move(E), 422f7f6d3e9SLang Hames [&](const CustomError &CE) { ErrorInfo = CE.getInfo(); }); 423f7f6d3e9SLang Hames 424f7f6d3e9SLang Hames EXPECT_EQ(ErrorInfo, 7) 425f7f6d3e9SLang Hames << "Failed to handle Error returned from handleErrors."; 426f7f6d3e9SLang Hames } 4276935c2d3SLang Hames 428c5e0bbd7SLang Hames TEST(Error, StringError) { 429c5e0bbd7SLang Hames std::string Msg; 430c5e0bbd7SLang Hames raw_string_ostream S(Msg); 431c5e0bbd7SLang Hames logAllUnhandledErrors(make_error<StringError>("foo" + Twine(42), 432bd8e9542SLang Hames inconvertibleErrorCode()), 433c5e0bbd7SLang Hames S, ""); 434c5e0bbd7SLang Hames EXPECT_EQ(S.str(), "foo42\n") << "Unexpected StringError log result"; 435c5e0bbd7SLang Hames 436c5e0bbd7SLang Hames auto EC = 437c5e0bbd7SLang Hames errorToErrorCode(make_error<StringError>("", errc::invalid_argument)); 438c5e0bbd7SLang Hames EXPECT_EQ(EC, errc::invalid_argument) 439c5e0bbd7SLang Hames << "Failed to convert StringError to error_code."; 440c5e0bbd7SLang Hames } 441c5e0bbd7SLang Hames 44201864a2dSLang Hames // Test that the ExitOnError utility works as expected. 44301864a2dSLang Hames TEST(Error, ExitOnError) { 4446935c2d3SLang Hames ExitOnError ExitOnErr; 4456935c2d3SLang Hames ExitOnErr.setBanner("Error in tool:"); 44601864a2dSLang Hames ExitOnErr.setExitCodeMapper([](const Error &E) { 4476935c2d3SLang Hames if (E.isA<CustomSubError>()) 4486935c2d3SLang Hames return 2; 4496935c2d3SLang Hames return 1; 4506935c2d3SLang Hames }); 4516935c2d3SLang Hames 4526935c2d3SLang Hames // Make sure we don't bail on success. 4536935c2d3SLang Hames ExitOnErr(Error::success()); 4546935c2d3SLang Hames EXPECT_EQ(ExitOnErr(Expected<int>(7)), 7) 4556935c2d3SLang Hames << "exitOnError returned an invalid value for Expected"; 4566935c2d3SLang Hames 457285639f8SLang Hames int A = 7; 458285639f8SLang Hames int &B = ExitOnErr(Expected<int&>(A)); 459285639f8SLang Hames EXPECT_EQ(&A, &B) << "ExitOnError failed to propagate reference"; 460285639f8SLang Hames 4616935c2d3SLang Hames // Exit tests. 4626935c2d3SLang Hames EXPECT_EXIT(ExitOnErr(make_error<CustomError>(7)), 4636935c2d3SLang Hames ::testing::ExitedWithCode(1), "Error in tool:") 4646935c2d3SLang Hames << "exitOnError returned an unexpected error result"; 4656935c2d3SLang Hames 4666935c2d3SLang Hames EXPECT_EXIT(ExitOnErr(Expected<int>(make_error<CustomSubError>(0, 0))), 4676935c2d3SLang Hames ::testing::ExitedWithCode(2), "Error in tool:") 4686935c2d3SLang Hames << "exitOnError returned an unexpected error result"; 4696935c2d3SLang Hames } 470f7f6d3e9SLang Hames 471580ca237SLang Hames // Test Checked Expected<T> in success mode. 472580ca237SLang Hames TEST(Error, CheckedExpectedInSuccessMode) { 473f7f6d3e9SLang Hames Expected<int> A = 7; 47401864a2dSLang Hames EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'"; 475580ca237SLang Hames // Access is safe in second test, since we checked the error in the first. 476f7f6d3e9SLang Hames EXPECT_EQ(*A, 7) << "Incorrect Expected non-error value"; 477f7f6d3e9SLang Hames } 478f7f6d3e9SLang Hames 479285639f8SLang Hames // Test Expected with reference type. 480285639f8SLang Hames TEST(Error, ExpectedWithReferenceType) { 481285639f8SLang Hames int A = 7; 482285639f8SLang Hames Expected<int&> B = A; 483285639f8SLang Hames // 'Check' B. 484285639f8SLang Hames (void)!!B; 485285639f8SLang Hames int &C = *B; 486285639f8SLang Hames EXPECT_EQ(&A, &C) << "Expected failed to propagate reference"; 487285639f8SLang Hames } 488285639f8SLang Hames 489580ca237SLang Hames // Test Unchecked Expected<T> in success mode. 490580ca237SLang Hames // We expect this to blow up the same way Error would. 491580ca237SLang Hames // Test runs in debug mode only. 492580ca237SLang Hames #ifndef NDEBUG 493580ca237SLang Hames TEST(Error, UncheckedExpectedInSuccessModeDestruction) { 494580ca237SLang Hames EXPECT_DEATH({ Expected<int> A = 7; }, 495580ca237SLang Hames "Expected<T> must be checked before access or destruction.") 496580ca237SLang Hames << "Unchecekd Expected<T> success value did not cause an abort()."; 497580ca237SLang Hames } 498580ca237SLang Hames #endif 499580ca237SLang Hames 500580ca237SLang Hames // Test Unchecked Expected<T> in success mode. 501580ca237SLang Hames // We expect this to blow up the same way Error would. 502580ca237SLang Hames // Test runs in debug mode only. 503580ca237SLang Hames #ifndef NDEBUG 504580ca237SLang Hames TEST(Error, UncheckedExpectedInSuccessModeAccess) { 505580ca237SLang Hames EXPECT_DEATH({ Expected<int> A = 7; *A; }, 506580ca237SLang Hames "Expected<T> must be checked before access or destruction.") 507580ca237SLang Hames << "Unchecekd Expected<T> success value did not cause an abort()."; 508580ca237SLang Hames } 509580ca237SLang Hames #endif 510580ca237SLang Hames 511580ca237SLang Hames // Test Unchecked Expected<T> in success mode. 512580ca237SLang Hames // We expect this to blow up the same way Error would. 513580ca237SLang Hames // Test runs in debug mode only. 514580ca237SLang Hames #ifndef NDEBUG 515580ca237SLang Hames TEST(Error, UncheckedExpectedInSuccessModeAssignment) { 516580ca237SLang Hames EXPECT_DEATH({ Expected<int> A = 7; A = 7; }, 517580ca237SLang Hames "Expected<T> must be checked before access or destruction.") 518580ca237SLang Hames << "Unchecekd Expected<T> success value did not cause an abort()."; 519580ca237SLang Hames } 520580ca237SLang Hames #endif 521580ca237SLang Hames 52201864a2dSLang Hames // Test Expected<T> in failure mode. 52301864a2dSLang Hames TEST(Error, ExpectedInFailureMode) { 524f7f6d3e9SLang Hames Expected<int> A = make_error<CustomError>(42); 525f7f6d3e9SLang Hames EXPECT_FALSE(!!A) << "Expected with error value doesn't convert to 'false'"; 526f7f6d3e9SLang Hames Error E = A.takeError(); 527f7f6d3e9SLang Hames EXPECT_TRUE(E.isA<CustomError>()) << "Incorrect Expected error value"; 528f7f6d3e9SLang Hames consumeError(std::move(E)); 529f7f6d3e9SLang Hames } 530f7f6d3e9SLang Hames 531f7f6d3e9SLang Hames // Check that an Expected instance with an error value doesn't allow access to 532f7f6d3e9SLang Hames // operator*. 533f7f6d3e9SLang Hames // Test runs in debug mode only. 534f7f6d3e9SLang Hames #ifndef NDEBUG 53501864a2dSLang Hames TEST(Error, AccessExpectedInFailureMode) { 536f7f6d3e9SLang Hames Expected<int> A = make_error<CustomError>(42); 537580ca237SLang Hames EXPECT_DEATH(*A, "Expected<T> must be checked before access or destruction.") 538f7f6d3e9SLang Hames << "Incorrect Expected error value"; 539f7f6d3e9SLang Hames consumeError(A.takeError()); 540f7f6d3e9SLang Hames } 541f7f6d3e9SLang Hames #endif 542f7f6d3e9SLang Hames 543f7f6d3e9SLang Hames // Check that an Expected instance with an error triggers an abort if 544f7f6d3e9SLang Hames // unhandled. 545f7f6d3e9SLang Hames // Test runs in debug mode only. 546f7f6d3e9SLang Hames #ifndef NDEBUG 54701864a2dSLang Hames TEST(Error, UnhandledExpectedInFailureMode) { 548f7f6d3e9SLang Hames EXPECT_DEATH({ Expected<int> A = make_error<CustomError>(42); }, 549580ca237SLang Hames "Expected<T> must be checked before access or destruction.") 550f7f6d3e9SLang Hames << "Unchecked Expected<T> failure value did not cause an abort()"; 55101864a2dSLang Hames } 552f7f6d3e9SLang Hames #endif 553f7f6d3e9SLang Hames 554f7f6d3e9SLang Hames // Test covariance of Expected. 55501864a2dSLang Hames TEST(Error, ExpectedCovariance) { 556f7f6d3e9SLang Hames class B {}; 557f7f6d3e9SLang Hames class D : public B {}; 558f7f6d3e9SLang Hames 559f7f6d3e9SLang Hames Expected<B *> A1(Expected<D *>(nullptr)); 560580ca237SLang Hames // Check A1 by converting to bool before assigning to it. 561580ca237SLang Hames (void)!!A1; 562f7f6d3e9SLang Hames A1 = Expected<D *>(nullptr); 563580ca237SLang Hames // Check A1 again before destruction. 564580ca237SLang Hames (void)!!A1; 565f7f6d3e9SLang Hames 566f7f6d3e9SLang Hames Expected<std::unique_ptr<B>> A2(Expected<std::unique_ptr<D>>(nullptr)); 567580ca237SLang Hames // Check A2 by converting to bool before assigning to it. 568580ca237SLang Hames (void)!!A2; 569f7f6d3e9SLang Hames A2 = Expected<std::unique_ptr<D>>(nullptr); 570580ca237SLang Hames // Check A2 again before destruction. 571580ca237SLang Hames (void)!!A2; 572f7f6d3e9SLang Hames } 573f7f6d3e9SLang Hames 574d21a535bSLang Hames TEST(Error, ErrorCodeConversions) { 575f7f6d3e9SLang Hames // Round-trip a success value to check that it converts correctly. 576f7f6d3e9SLang Hames EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())), 577f7f6d3e9SLang Hames std::error_code()) 578f7f6d3e9SLang Hames << "std::error_code() should round-trip via Error conversions"; 579f7f6d3e9SLang Hames 580f7f6d3e9SLang Hames // Round-trip an error value to check that it converts correctly. 581f7f6d3e9SLang Hames EXPECT_EQ(errorToErrorCode(errorCodeToError(errc::invalid_argument)), 582f7f6d3e9SLang Hames errc::invalid_argument) 583f7f6d3e9SLang Hames << "std::error_code error value should round-trip via Error " 584f7f6d3e9SLang Hames "conversions"; 585d21a535bSLang Hames 586d21a535bSLang Hames // Round-trip a success value through ErrorOr/Expected to check that it 587d21a535bSLang Hames // converts correctly. 588d21a535bSLang Hames { 589d21a535bSLang Hames auto Orig = ErrorOr<int>(42); 590d21a535bSLang Hames auto RoundTripped = 591d21a535bSLang Hames expectedToErrorOr(errorOrToExpected(ErrorOr<int>(42))); 592d21a535bSLang Hames EXPECT_EQ(*Orig, *RoundTripped) 593d21a535bSLang Hames << "ErrorOr<T> success value should round-trip via Expected<T> " 594d21a535bSLang Hames "conversions."; 595d21a535bSLang Hames } 596d21a535bSLang Hames 597d21a535bSLang Hames // Round-trip a failure value through ErrorOr/Expected to check that it 598d21a535bSLang Hames // converts correctly. 599d21a535bSLang Hames { 600d21a535bSLang Hames auto Orig = ErrorOr<int>(errc::invalid_argument); 601d21a535bSLang Hames auto RoundTripped = 602d21a535bSLang Hames expectedToErrorOr( 603d21a535bSLang Hames errorOrToExpected(ErrorOr<int>(errc::invalid_argument))); 604d21a535bSLang Hames EXPECT_EQ(Orig.getError(), RoundTripped.getError()) 605d21a535bSLang Hames << "ErrorOr<T> failure value should round-trip via Expected<T> " 606d21a535bSLang Hames "conversions."; 607d21a535bSLang Hames } 608f7f6d3e9SLang Hames } 609f7f6d3e9SLang Hames 61027370a09SVedant Kumar // Test that error messages work. 61127370a09SVedant Kumar TEST(Error, ErrorMessage) { 61227370a09SVedant Kumar EXPECT_EQ(toString(Error::success()).compare(""), 0); 61327370a09SVedant Kumar 61427370a09SVedant Kumar Error E1 = make_error<CustomError>(0); 61527370a09SVedant Kumar EXPECT_EQ(toString(std::move(E1)).compare("CustomError { 0}"), 0); 61627370a09SVedant Kumar 61727370a09SVedant Kumar Error E2 = make_error<CustomError>(0); 61827370a09SVedant Kumar handleAllErrors(std::move(E2), [](const CustomError &CE) { 61927370a09SVedant Kumar EXPECT_EQ(CE.message().compare("CustomError { 0}"), 0); 62027370a09SVedant Kumar }); 62127370a09SVedant Kumar 62227370a09SVedant Kumar Error E3 = joinErrors(make_error<CustomError>(0), make_error<CustomError>(1)); 62327370a09SVedant Kumar EXPECT_EQ(toString(std::move(E3)) 62427370a09SVedant Kumar .compare("CustomError { 0}\n" 62527370a09SVedant Kumar "CustomError { 1}"), 62627370a09SVedant Kumar 0); 62727370a09SVedant Kumar } 62827370a09SVedant Kumar 629f7f6d3e9SLang Hames } // end anon namespace 630