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" 11f7f6d3e9SLang Hames #include "llvm/Support/Errc.h" 12f7f6d3e9SLang Hames #include "gtest/gtest.h" 13f7f6d3e9SLang Hames #include <memory> 14f7f6d3e9SLang Hames 15f7f6d3e9SLang Hames using namespace llvm; 16f7f6d3e9SLang Hames 17f7f6d3e9SLang Hames namespace { 18f7f6d3e9SLang Hames 19f7f6d3e9SLang Hames // Custom error class with a default base class and some random 'info' attached. 20f7f6d3e9SLang Hames class CustomError : public ErrorInfo<CustomError> { 21f7f6d3e9SLang Hames public: 22f7f6d3e9SLang Hames // Create an error with some info attached. 23f7f6d3e9SLang Hames CustomError(int Info) : Info(Info) {} 24f7f6d3e9SLang Hames 25f7f6d3e9SLang Hames // Get the info attached to this error. 26f7f6d3e9SLang Hames int getInfo() const { return Info; } 27f7f6d3e9SLang Hames 28f7f6d3e9SLang Hames // Log this error to a stream. 29f7f6d3e9SLang Hames void log(raw_ostream &OS) const override { 30f7f6d3e9SLang Hames OS << "CustomError { " << getInfo() << "}"; 31f7f6d3e9SLang Hames } 32f7f6d3e9SLang Hames 33f7f6d3e9SLang Hames protected: 34f7f6d3e9SLang Hames // This error is subclassed below, but we can't use inheriting constructors 35f7f6d3e9SLang Hames // yet, so we can't propagate the constructors through ErrorInfo. Instead 36f7f6d3e9SLang Hames // we have to have a default constructor and have the subclass initialize all 37f7f6d3e9SLang Hames // fields. 38f7f6d3e9SLang Hames CustomError() : Info(0) {} 39f7f6d3e9SLang Hames 40f7f6d3e9SLang Hames int Info; 41f7f6d3e9SLang Hames }; 42f7f6d3e9SLang Hames 43f7f6d3e9SLang Hames // Custom error class with a custom base class and some additional random 44f7f6d3e9SLang Hames // 'info'. 45f7f6d3e9SLang Hames class CustomSubError : public ErrorInfo<CustomSubError, CustomError> { 46f7f6d3e9SLang Hames public: 47f7f6d3e9SLang Hames // Create a sub-error with some info attached. 48f7f6d3e9SLang Hames CustomSubError(int Info, int ExtraInfo) : ExtraInfo(ExtraInfo) { 49f7f6d3e9SLang Hames this->Info = Info; 50f7f6d3e9SLang Hames } 51f7f6d3e9SLang Hames 52f7f6d3e9SLang Hames // Get the extra info attached to this error. 53f7f6d3e9SLang Hames int getExtraInfo() const { return ExtraInfo; } 54f7f6d3e9SLang Hames 55f7f6d3e9SLang Hames // Log this error to a stream. 56f7f6d3e9SLang Hames void log(raw_ostream &OS) const override { 57f7f6d3e9SLang Hames OS << "CustomSubError { " << getInfo() << ", " << getExtraInfo() << "}"; 58f7f6d3e9SLang Hames } 59f7f6d3e9SLang Hames 60f7f6d3e9SLang Hames protected: 61f7f6d3e9SLang Hames int ExtraInfo; 62f7f6d3e9SLang Hames }; 63f7f6d3e9SLang Hames 64f7f6d3e9SLang Hames static Error handleCustomError(const CustomError &CE) { return Error(); } 65f7f6d3e9SLang Hames 66f7f6d3e9SLang Hames static void handleCustomErrorVoid(const CustomError &CE) {} 67f7f6d3e9SLang Hames 68f7f6d3e9SLang Hames static Error handleCustomErrorUP(std::unique_ptr<CustomError> CE) { 69f7f6d3e9SLang Hames return Error(); 70f7f6d3e9SLang Hames } 71f7f6d3e9SLang Hames 72f7f6d3e9SLang Hames static void handleCustomErrorUPVoid(std::unique_ptr<CustomError> CE) {} 73f7f6d3e9SLang Hames 74*01864a2dSLang Hames // Test that success values implicitly convert to false, and don't cause crashes 75*01864a2dSLang Hames // once they've been implicitly converted. 76*01864a2dSLang Hames TEST(Error, CheckedSuccess) { 77*01864a2dSLang Hames Error E; 78*01864a2dSLang Hames EXPECT_FALSE(E) << "Unexpected error while testing Error 'Success'"; 79*01864a2dSLang Hames } 80*01864a2dSLang Hames 81*01864a2dSLang Hames // Test that unchecked succes values cause an abort. 82*01864a2dSLang Hames #ifndef NDEBUG 83*01864a2dSLang Hames TEST(Error, UncheckedSuccess) { 84*01864a2dSLang Hames EXPECT_DEATH({ Error E; }, "Program aborted due to an unhandled Error:") 85*01864a2dSLang Hames << "Unchecked Error Succes value did not cause abort()"; 86*01864a2dSLang Hames } 87*01864a2dSLang Hames #endif 88*01864a2dSLang Hames 89f7f6d3e9SLang Hames // Check that we abort on unhandled failure cases. (Force conversion to bool 90f7f6d3e9SLang Hames // to make sure that we don't accidentally treat checked errors as handled). 91f7f6d3e9SLang Hames // Test runs in debug mode only. 92f7f6d3e9SLang Hames #ifndef NDEBUG 93*01864a2dSLang Hames TEST(Error, UncheckedError) { 94f7f6d3e9SLang Hames auto DropUnhandledError = []() { 95f7f6d3e9SLang Hames Error E = make_error<CustomError>(42); 96f7f6d3e9SLang Hames (void)!E; 97f7f6d3e9SLang Hames }; 98f7f6d3e9SLang Hames EXPECT_DEATH(DropUnhandledError(), 99f7f6d3e9SLang Hames "Program aborted due to an unhandled Error:") 100f7f6d3e9SLang Hames << "Unhandled Error failure value did not cause abort()"; 101f7f6d3e9SLang Hames } 102f7f6d3e9SLang Hames #endif 103f7f6d3e9SLang Hames 104*01864a2dSLang Hames // Check 'Error::isA<T>' method handling. 105*01864a2dSLang Hames TEST(Error, IsAHandling) { 106f7f6d3e9SLang Hames // Check 'isA' handling. 107f7f6d3e9SLang Hames Error E = make_error<CustomError>(1); 108f7f6d3e9SLang Hames Error F = make_error<CustomSubError>(1, 2); 10901a3cf4dSLang Hames Error G = Error::success(); 110f7f6d3e9SLang Hames 111f7f6d3e9SLang Hames EXPECT_TRUE(E.isA<CustomError>()); 112f7f6d3e9SLang Hames EXPECT_FALSE(E.isA<CustomSubError>()); 113f7f6d3e9SLang Hames EXPECT_TRUE(F.isA<CustomError>()); 114f7f6d3e9SLang Hames EXPECT_TRUE(F.isA<CustomSubError>()); 11501a3cf4dSLang Hames EXPECT_FALSE(G.isA<CustomError>()); 116f7f6d3e9SLang Hames 117f7f6d3e9SLang Hames consumeError(std::move(E)); 118f7f6d3e9SLang Hames consumeError(std::move(F)); 11901a3cf4dSLang Hames consumeError(std::move(G)); 120f7f6d3e9SLang Hames } 121f7f6d3e9SLang Hames 122f7f6d3e9SLang Hames // Check that we can handle a custom error. 123*01864a2dSLang Hames TEST(Error, HandleCustomError) { 124f7f6d3e9SLang Hames int CaughtErrorInfo = 0; 125f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), [&](const CustomError &CE) { 126f7f6d3e9SLang Hames CaughtErrorInfo = CE.getInfo(); 127f7f6d3e9SLang Hames }); 128f7f6d3e9SLang Hames 129*01864a2dSLang Hames EXPECT_TRUE(CaughtErrorInfo == 42) << "Wrong result from CustomError handler"; 130f7f6d3e9SLang Hames } 131f7f6d3e9SLang Hames 132f7f6d3e9SLang Hames // Check that handler type deduction also works for handlers 133f7f6d3e9SLang Hames // of the following types: 134f7f6d3e9SLang Hames // void (const Err&) 135f7f6d3e9SLang Hames // Error (const Err&) mutable 136f7f6d3e9SLang Hames // void (const Err&) mutable 137f7f6d3e9SLang Hames // Error (Err&) 138f7f6d3e9SLang Hames // void (Err&) 139f7f6d3e9SLang Hames // Error (Err&) mutable 140f7f6d3e9SLang Hames // void (Err&) mutable 141f7f6d3e9SLang Hames // Error (unique_ptr<Err>) 142f7f6d3e9SLang Hames // void (unique_ptr<Err>) 143f7f6d3e9SLang Hames // Error (unique_ptr<Err>) mutable 144f7f6d3e9SLang Hames // void (unique_ptr<Err>) mutable 145*01864a2dSLang Hames TEST(Error, HandlerTypeDeduction) { 146f7f6d3e9SLang Hames 147f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), [](const CustomError &CE) {}); 148f7f6d3e9SLang Hames 149f7f6d3e9SLang Hames handleAllErrors( 150f7f6d3e9SLang Hames make_error<CustomError>(42), 151f7f6d3e9SLang Hames [](const CustomError &CE) mutable { return Error::success(); }); 152f7f6d3e9SLang Hames 153f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 154f7f6d3e9SLang Hames [](const CustomError &CE) mutable {}); 155f7f6d3e9SLang Hames 156f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 157f7f6d3e9SLang Hames [](CustomError &CE) { return Error::success(); }); 158f7f6d3e9SLang Hames 159f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) {}); 160f7f6d3e9SLang Hames 161f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 162f7f6d3e9SLang Hames [](CustomError &CE) mutable { return Error::success(); }); 163f7f6d3e9SLang Hames 164f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) mutable {}); 165f7f6d3e9SLang Hames 166f7f6d3e9SLang Hames handleAllErrors( 167f7f6d3e9SLang Hames make_error<CustomError>(42), 168f7f6d3e9SLang Hames [](std::unique_ptr<CustomError> CE) { return Error::success(); }); 169f7f6d3e9SLang Hames 170f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 171f7f6d3e9SLang Hames [](std::unique_ptr<CustomError> CE) {}); 172f7f6d3e9SLang Hames 173f7f6d3e9SLang Hames handleAllErrors( 174f7f6d3e9SLang Hames make_error<CustomError>(42), 175f7f6d3e9SLang Hames [](std::unique_ptr<CustomError> CE) mutable { return Error::success(); }); 176f7f6d3e9SLang Hames 177f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), 178f7f6d3e9SLang Hames [](std::unique_ptr<CustomError> CE) mutable {}); 179f7f6d3e9SLang Hames 180f7f6d3e9SLang Hames // Check that named handlers of type 'Error (const Err&)' work. 181f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), handleCustomError); 182f7f6d3e9SLang Hames 183f7f6d3e9SLang Hames // Check that named handlers of type 'void (const Err&)' work. 184f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), handleCustomErrorVoid); 185f7f6d3e9SLang Hames 186f7f6d3e9SLang Hames // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work. 187f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), handleCustomErrorUP); 188f7f6d3e9SLang Hames 189f7f6d3e9SLang Hames // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work. 190f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(42), handleCustomErrorUPVoid); 191*01864a2dSLang Hames } 192f7f6d3e9SLang Hames 193*01864a2dSLang Hames // Test that we can handle errors with custom base classes. 194*01864a2dSLang Hames TEST(Error, HandleCustomErrorWithCustomBaseClass) { 195f7f6d3e9SLang Hames int CaughtErrorInfo = 0; 196f7f6d3e9SLang Hames int CaughtErrorExtraInfo = 0; 197f7f6d3e9SLang Hames handleAllErrors(make_error<CustomSubError>(42, 7), 198f7f6d3e9SLang Hames [&](const CustomSubError &SE) { 199f7f6d3e9SLang Hames CaughtErrorInfo = SE.getInfo(); 200f7f6d3e9SLang Hames CaughtErrorExtraInfo = SE.getExtraInfo(); 201f7f6d3e9SLang Hames }); 202f7f6d3e9SLang Hames 203f7f6d3e9SLang Hames EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7) 204f7f6d3e9SLang Hames << "Wrong result from CustomSubError handler"; 205f7f6d3e9SLang Hames } 206f7f6d3e9SLang Hames 207f7f6d3e9SLang Hames // Check that we trigger only the first handler that applies. 208*01864a2dSLang Hames TEST(Error, FirstHandlerOnly) { 209f7f6d3e9SLang Hames int DummyInfo = 0; 210f7f6d3e9SLang Hames int CaughtErrorInfo = 0; 211f7f6d3e9SLang Hames int CaughtErrorExtraInfo = 0; 212f7f6d3e9SLang Hames 213f7f6d3e9SLang Hames handleAllErrors(make_error<CustomSubError>(42, 7), 214f7f6d3e9SLang Hames [&](const CustomSubError &SE) { 215f7f6d3e9SLang Hames CaughtErrorInfo = SE.getInfo(); 216f7f6d3e9SLang Hames CaughtErrorExtraInfo = SE.getExtraInfo(); 217f7f6d3e9SLang Hames }, 218f7f6d3e9SLang Hames [&](const CustomError &CE) { DummyInfo = CE.getInfo(); }); 219f7f6d3e9SLang Hames 220f7f6d3e9SLang Hames EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7 && 221f7f6d3e9SLang Hames DummyInfo == 0) 222f7f6d3e9SLang Hames << "Activated the wrong Error handler(s)"; 223f7f6d3e9SLang Hames } 224f7f6d3e9SLang Hames 225f7f6d3e9SLang Hames // Check that general handlers shadow specific ones. 226*01864a2dSLang Hames TEST(Error, HandlerShadowing) { 227f7f6d3e9SLang Hames int CaughtErrorInfo = 0; 228f7f6d3e9SLang Hames int DummyInfo = 0; 229f7f6d3e9SLang Hames int DummyExtraInfo = 0; 230f7f6d3e9SLang Hames 231f7f6d3e9SLang Hames handleAllErrors( 232f7f6d3e9SLang Hames make_error<CustomSubError>(42, 7), 233f7f6d3e9SLang Hames [&](const CustomError &CE) { CaughtErrorInfo = CE.getInfo(); }, 234f7f6d3e9SLang Hames [&](const CustomSubError &SE) { 235f7f6d3e9SLang Hames DummyInfo = SE.getInfo(); 236f7f6d3e9SLang Hames DummyExtraInfo = SE.getExtraInfo(); 237f7f6d3e9SLang Hames }); 238f7f6d3e9SLang Hames 239f7f6d3e9SLang Hames EXPECT_TRUE(CaughtErrorInfo = 42 && DummyInfo == 0 && DummyExtraInfo == 0) 240f7f6d3e9SLang Hames << "General Error handler did not shadow specific handler"; 241f7f6d3e9SLang Hames } 242f7f6d3e9SLang Hames 243*01864a2dSLang Hames // Test joinErrors. 244*01864a2dSLang Hames TEST(Error, CheckJoinErrors) { 245f7f6d3e9SLang Hames int CustomErrorInfo1 = 0; 246f7f6d3e9SLang Hames int CustomErrorInfo2 = 0; 247f7f6d3e9SLang Hames int CustomErrorExtraInfo = 0; 248*01864a2dSLang Hames Error E = 249*01864a2dSLang Hames joinErrors(make_error<CustomError>(7), make_error<CustomSubError>(42, 7)); 250f7f6d3e9SLang Hames 251f7f6d3e9SLang Hames handleAllErrors(std::move(E), 252f7f6d3e9SLang Hames [&](const CustomSubError &SE) { 253f7f6d3e9SLang Hames CustomErrorInfo2 = SE.getInfo(); 254f7f6d3e9SLang Hames CustomErrorExtraInfo = SE.getExtraInfo(); 255f7f6d3e9SLang Hames }, 256f7f6d3e9SLang Hames [&](const CustomError &CE) { 257f7f6d3e9SLang Hames // Assert that the CustomError instance above is handled 258f7f6d3e9SLang Hames // before the 259f7f6d3e9SLang Hames // CustomSubError - joinErrors should preserve error 260f7f6d3e9SLang Hames // ordering. 261f7f6d3e9SLang Hames EXPECT_EQ(CustomErrorInfo2, 0) 262f7f6d3e9SLang Hames << "CustomErrorInfo2 should be 0 here. " 263f7f6d3e9SLang Hames "joinErrors failed to preserve ordering.\n"; 264f7f6d3e9SLang Hames CustomErrorInfo1 = CE.getInfo(); 265f7f6d3e9SLang Hames }); 266f7f6d3e9SLang Hames 267f7f6d3e9SLang Hames EXPECT_TRUE(CustomErrorInfo1 == 7 && CustomErrorInfo2 == 42 && 268f7f6d3e9SLang Hames CustomErrorExtraInfo == 7) 269f7f6d3e9SLang Hames << "Failed handling compound Error."; 270f7f6d3e9SLang Hames } 271f7f6d3e9SLang Hames 272*01864a2dSLang Hames // Test that we can consume success values. 273*01864a2dSLang Hames TEST(Error, ConsumeSuccess) { 274f7f6d3e9SLang Hames Error E; 275f7f6d3e9SLang Hames consumeError(std::move(E)); 276f7f6d3e9SLang Hames } 277*01864a2dSLang Hames 278*01864a2dSLang Hames TEST(Error, ConsumeError) { 279f7f6d3e9SLang Hames Error E = make_error<CustomError>(7); 280f7f6d3e9SLang Hames consumeError(std::move(E)); 281f7f6d3e9SLang Hames } 282f7f6d3e9SLang Hames 283f7f6d3e9SLang Hames // Test that handleAllUnhandledErrors crashes if an error is not caught. 284f7f6d3e9SLang Hames // Test runs in debug mode only. 285f7f6d3e9SLang Hames #ifndef NDEBUG 286*01864a2dSLang Hames TEST(Error, FailureToHandle) { 287f7f6d3e9SLang Hames auto FailToHandle = []() { 288*01864a2dSLang Hames handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) { 289f7f6d3e9SLang Hames errs() << "This should never be called"; 290f7f6d3e9SLang Hames exit(1); 291f7f6d3e9SLang Hames }); 292f7f6d3e9SLang Hames }; 293f7f6d3e9SLang Hames 294f7f6d3e9SLang Hames EXPECT_DEATH(FailToHandle(), "Program aborted due to an unhandled Error:") 295f7f6d3e9SLang Hames << "Unhandled Error in handleAllErrors call did not cause an " 296f7f6d3e9SLang Hames "abort()"; 297f7f6d3e9SLang Hames } 298f7f6d3e9SLang Hames #endif 299f7f6d3e9SLang Hames 300f7f6d3e9SLang Hames // Test that handleAllUnhandledErrors crashes if an error is returned from a 301f7f6d3e9SLang Hames // handler. 302f7f6d3e9SLang Hames // Test runs in debug mode only. 303f7f6d3e9SLang Hames #ifndef NDEBUG 304*01864a2dSLang Hames TEST(Error, FailureFromHandler) { 305f7f6d3e9SLang Hames auto ReturnErrorFromHandler = []() { 306f7f6d3e9SLang Hames handleAllErrors(make_error<CustomError>(7), 307f7f6d3e9SLang Hames [&](std::unique_ptr<CustomSubError> SE) { 308f7f6d3e9SLang Hames return Error(std::move(SE)); 309f7f6d3e9SLang Hames }); 310f7f6d3e9SLang Hames }; 311f7f6d3e9SLang Hames 312f7f6d3e9SLang Hames EXPECT_DEATH(ReturnErrorFromHandler(), 313f7f6d3e9SLang Hames "Program aborted due to an unhandled Error:") 314f7f6d3e9SLang Hames << " Error returned from handler in handleAllErrors call did not " 315f7f6d3e9SLang Hames "cause abort()"; 316f7f6d3e9SLang Hames } 317f7f6d3e9SLang Hames #endif 318f7f6d3e9SLang Hames 319f7f6d3e9SLang Hames // Test that we can return values from handleErrors. 320*01864a2dSLang Hames TEST(Error, CatchErrorFromHandler) { 321f7f6d3e9SLang Hames int ErrorInfo = 0; 322f7f6d3e9SLang Hames 323f7f6d3e9SLang Hames Error E = handleErrors( 324f7f6d3e9SLang Hames make_error<CustomError>(7), 325f7f6d3e9SLang Hames [&](std::unique_ptr<CustomError> CE) { return Error(std::move(CE)); }); 326f7f6d3e9SLang Hames 327f7f6d3e9SLang Hames handleAllErrors(std::move(E), 328f7f6d3e9SLang Hames [&](const CustomError &CE) { ErrorInfo = CE.getInfo(); }); 329f7f6d3e9SLang Hames 330f7f6d3e9SLang Hames EXPECT_EQ(ErrorInfo, 7) 331f7f6d3e9SLang Hames << "Failed to handle Error returned from handleErrors."; 332f7f6d3e9SLang Hames } 3336935c2d3SLang Hames 334*01864a2dSLang Hames // Test that the ExitOnError utility works as expected. 335*01864a2dSLang Hames TEST(Error, ExitOnError) { 3366935c2d3SLang Hames ExitOnError ExitOnErr; 3376935c2d3SLang Hames ExitOnErr.setBanner("Error in tool:"); 338*01864a2dSLang Hames ExitOnErr.setExitCodeMapper([](const Error &E) { 3396935c2d3SLang Hames if (E.isA<CustomSubError>()) 3406935c2d3SLang Hames return 2; 3416935c2d3SLang Hames return 1; 3426935c2d3SLang Hames }); 3436935c2d3SLang Hames 3446935c2d3SLang Hames // Make sure we don't bail on success. 3456935c2d3SLang Hames ExitOnErr(Error::success()); 3466935c2d3SLang Hames EXPECT_EQ(ExitOnErr(Expected<int>(7)), 7) 3476935c2d3SLang Hames << "exitOnError returned an invalid value for Expected"; 3486935c2d3SLang Hames 3496935c2d3SLang Hames // Exit tests. 3506935c2d3SLang Hames EXPECT_EXIT(ExitOnErr(make_error<CustomError>(7)), 3516935c2d3SLang Hames ::testing::ExitedWithCode(1), "Error in tool:") 3526935c2d3SLang Hames << "exitOnError returned an unexpected error result"; 3536935c2d3SLang Hames 3546935c2d3SLang Hames EXPECT_EXIT(ExitOnErr(Expected<int>(make_error<CustomSubError>(0, 0))), 3556935c2d3SLang Hames ::testing::ExitedWithCode(2), "Error in tool:") 3566935c2d3SLang Hames << "exitOnError returned an unexpected error result"; 3576935c2d3SLang Hames } 358f7f6d3e9SLang Hames 359*01864a2dSLang Hames // Test Expected<T> in success mode. 360*01864a2dSLang Hames TEST(Error, ExpectedInSuccessMode) { 361f7f6d3e9SLang Hames Expected<int> A = 7; 362*01864a2dSLang Hames EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'"; 363f7f6d3e9SLang Hames EXPECT_EQ(*A, 7) << "Incorrect Expected non-error value"; 364f7f6d3e9SLang Hames } 365f7f6d3e9SLang Hames 366*01864a2dSLang Hames // Test Expected<T> in failure mode. 367*01864a2dSLang Hames TEST(Error, ExpectedInFailureMode) { 368f7f6d3e9SLang Hames Expected<int> A = make_error<CustomError>(42); 369f7f6d3e9SLang Hames EXPECT_FALSE(!!A) << "Expected with error value doesn't convert to 'false'"; 370f7f6d3e9SLang Hames Error E = A.takeError(); 371f7f6d3e9SLang Hames EXPECT_TRUE(E.isA<CustomError>()) << "Incorrect Expected error value"; 372f7f6d3e9SLang Hames consumeError(std::move(E)); 373f7f6d3e9SLang Hames } 374f7f6d3e9SLang Hames 375f7f6d3e9SLang Hames // Check that an Expected instance with an error value doesn't allow access to 376f7f6d3e9SLang Hames // operator*. 377f7f6d3e9SLang Hames // Test runs in debug mode only. 378f7f6d3e9SLang Hames #ifndef NDEBUG 379*01864a2dSLang Hames TEST(Error, AccessExpectedInFailureMode) { 380f7f6d3e9SLang Hames Expected<int> A = make_error<CustomError>(42); 3819fb8e1b2SLang Hames EXPECT_DEATH(*A, "!HasError && \"Cannot get value when an error exists!\"") 382f7f6d3e9SLang Hames << "Incorrect Expected error value"; 383f7f6d3e9SLang Hames consumeError(A.takeError()); 384f7f6d3e9SLang Hames } 385f7f6d3e9SLang Hames #endif 386f7f6d3e9SLang Hames 387f7f6d3e9SLang Hames // Check that an Expected instance with an error triggers an abort if 388f7f6d3e9SLang Hames // unhandled. 389f7f6d3e9SLang Hames // Test runs in debug mode only. 390f7f6d3e9SLang Hames #ifndef NDEBUG 391*01864a2dSLang Hames TEST(Error, UnhandledExpectedInFailureMode) { 392f7f6d3e9SLang Hames EXPECT_DEATH({ Expected<int> A = make_error<CustomError>(42); }, 393f7f6d3e9SLang Hames "Program aborted due to an unhandled Error:") 394f7f6d3e9SLang Hames << "Unchecked Expected<T> failure value did not cause an abort()"; 395*01864a2dSLang Hames } 396f7f6d3e9SLang Hames #endif 397f7f6d3e9SLang Hames 398f7f6d3e9SLang Hames // Test covariance of Expected. 399*01864a2dSLang Hames TEST(Error, ExpectedCovariance) { 400f7f6d3e9SLang Hames class B {}; 401f7f6d3e9SLang Hames class D : public B {}; 402f7f6d3e9SLang Hames 403f7f6d3e9SLang Hames Expected<B *> A1(Expected<D *>(nullptr)); 404f7f6d3e9SLang Hames A1 = Expected<D *>(nullptr); 405f7f6d3e9SLang Hames 406f7f6d3e9SLang Hames Expected<std::unique_ptr<B>> A2(Expected<std::unique_ptr<D>>(nullptr)); 407f7f6d3e9SLang Hames A2 = Expected<std::unique_ptr<D>>(nullptr); 408f7f6d3e9SLang Hames } 409f7f6d3e9SLang Hames 410f7f6d3e9SLang Hames TEST(Error, ECError) { 411f7f6d3e9SLang Hames // Round-trip a success value to check that it converts correctly. 412f7f6d3e9SLang Hames EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())), 413f7f6d3e9SLang Hames std::error_code()) 414f7f6d3e9SLang Hames << "std::error_code() should round-trip via Error conversions"; 415f7f6d3e9SLang Hames 416f7f6d3e9SLang Hames // Round-trip an error value to check that it converts correctly. 417f7f6d3e9SLang Hames EXPECT_EQ(errorToErrorCode(errorCodeToError(errc::invalid_argument)), 418f7f6d3e9SLang Hames errc::invalid_argument) 419f7f6d3e9SLang Hames << "std::error_code error value should round-trip via Error " 420f7f6d3e9SLang Hames "conversions"; 421f7f6d3e9SLang Hames } 422f7f6d3e9SLang Hames 423f7f6d3e9SLang Hames } // end anon namespace 424