1 //===----- unittests/ErrorTest.cpp - Error.h tests ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/Error.h" 11 12 #include "llvm/ADT/Twine.h" 13 #include "llvm/Support/Errc.h" 14 #include "llvm/Support/ErrorHandling.h" 15 #include "gtest/gtest.h" 16 #include <memory> 17 18 using namespace llvm; 19 20 namespace { 21 22 // Custom error class with a default base class and some random 'info' attached. 23 class CustomError : public ErrorInfo<CustomError> { 24 public: 25 // Create an error with some info attached. 26 CustomError(int Info) : Info(Info) {} 27 28 // Get the info attached to this error. 29 int getInfo() const { return Info; } 30 31 // Log this error to a stream. 32 void log(raw_ostream &OS) const override { 33 OS << "CustomError { " << getInfo() << "}"; 34 } 35 36 std::error_code convertToErrorCode() const override { 37 llvm_unreachable("CustomError doesn't support ECError conversion"); 38 } 39 40 // Used by ErrorInfo::classID. 41 static char ID; 42 43 protected: 44 // This error is subclassed below, but we can't use inheriting constructors 45 // yet, so we can't propagate the constructors through ErrorInfo. Instead 46 // we have to have a default constructor and have the subclass initialize all 47 // fields. 48 CustomError() : Info(0) {} 49 50 int Info; 51 }; 52 53 char CustomError::ID = 0; 54 55 // Custom error class with a custom base class and some additional random 56 // 'info'. 57 class CustomSubError : public ErrorInfo<CustomSubError, CustomError> { 58 public: 59 // Create a sub-error with some info attached. 60 CustomSubError(int Info, int ExtraInfo) : ExtraInfo(ExtraInfo) { 61 this->Info = Info; 62 } 63 64 // Get the extra info attached to this error. 65 int getExtraInfo() const { return ExtraInfo; } 66 67 // Log this error to a stream. 68 void log(raw_ostream &OS) const override { 69 OS << "CustomSubError { " << getInfo() << ", " << getExtraInfo() << "}"; 70 } 71 72 std::error_code convertToErrorCode() const override { 73 llvm_unreachable("CustomSubError doesn't support ECError conversion"); 74 } 75 76 // Used by ErrorInfo::classID. 77 static char ID; 78 79 protected: 80 int ExtraInfo; 81 }; 82 83 char CustomSubError::ID = 0; 84 85 static Error handleCustomError(const CustomError &CE) { 86 return Error::success(); 87 } 88 89 static void handleCustomErrorVoid(const CustomError &CE) {} 90 91 static Error handleCustomErrorUP(std::unique_ptr<CustomError> CE) { 92 return Error::success(); 93 } 94 95 static void handleCustomErrorUPVoid(std::unique_ptr<CustomError> CE) {} 96 97 // Test that success values implicitly convert to false, and don't cause crashes 98 // once they've been implicitly converted. 99 TEST(Error, CheckedSuccess) { 100 Error E = Error::success(); 101 EXPECT_FALSE(E) << "Unexpected error while testing Error 'Success'"; 102 } 103 104 // Test that unchecked succes values cause an abort. 105 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 106 TEST(Error, UncheckedSuccess) { 107 EXPECT_DEATH({ Error E = Error::success(); }, 108 "Program aborted due to an unhandled Error:") 109 << "Unchecked Error Succes value did not cause abort()"; 110 } 111 #endif 112 113 // ErrorAsOutParameter tester. 114 void errAsOutParamHelper(Error &Err) { 115 ErrorAsOutParameter ErrAsOutParam(&Err); 116 // Verify that checked flag is raised - assignment should not crash. 117 Err = Error::success(); 118 // Raise the checked bit manually - caller should still have to test the 119 // error. 120 (void)!!Err; 121 } 122 123 // Test that ErrorAsOutParameter sets the checked flag on construction. 124 TEST(Error, ErrorAsOutParameterChecked) { 125 Error E = Error::success(); 126 errAsOutParamHelper(E); 127 (void)!!E; 128 } 129 130 // Test that ErrorAsOutParameter clears the checked flag on destruction. 131 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 132 TEST(Error, ErrorAsOutParameterUnchecked) { 133 EXPECT_DEATH({ Error E = Error::success(); errAsOutParamHelper(E); }, 134 "Program aborted due to an unhandled Error:") 135 << "ErrorAsOutParameter did not clear the checked flag on destruction."; 136 } 137 #endif 138 139 // Check that we abort on unhandled failure cases. (Force conversion to bool 140 // to make sure that we don't accidentally treat checked errors as handled). 141 // Test runs in debug mode only. 142 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 143 TEST(Error, UncheckedError) { 144 auto DropUnhandledError = []() { 145 Error E = make_error<CustomError>(42); 146 (void)!E; 147 }; 148 EXPECT_DEATH(DropUnhandledError(), 149 "Program aborted due to an unhandled Error:") 150 << "Unhandled Error failure value did not cause abort()"; 151 } 152 #endif 153 154 // Check 'Error::isA<T>' method handling. 155 TEST(Error, IsAHandling) { 156 // Check 'isA' handling. 157 Error E = make_error<CustomError>(1); 158 Error F = make_error<CustomSubError>(1, 2); 159 Error G = Error::success(); 160 161 EXPECT_TRUE(E.isA<CustomError>()); 162 EXPECT_FALSE(E.isA<CustomSubError>()); 163 EXPECT_TRUE(F.isA<CustomError>()); 164 EXPECT_TRUE(F.isA<CustomSubError>()); 165 EXPECT_FALSE(G.isA<CustomError>()); 166 167 consumeError(std::move(E)); 168 consumeError(std::move(F)); 169 consumeError(std::move(G)); 170 } 171 172 // Check that we can handle a custom error. 173 TEST(Error, HandleCustomError) { 174 int CaughtErrorInfo = 0; 175 handleAllErrors(make_error<CustomError>(42), [&](const CustomError &CE) { 176 CaughtErrorInfo = CE.getInfo(); 177 }); 178 179 EXPECT_TRUE(CaughtErrorInfo == 42) << "Wrong result from CustomError handler"; 180 } 181 182 // Check that handler type deduction also works for handlers 183 // of the following types: 184 // void (const Err&) 185 // Error (const Err&) mutable 186 // void (const Err&) mutable 187 // Error (Err&) 188 // void (Err&) 189 // Error (Err&) mutable 190 // void (Err&) mutable 191 // Error (unique_ptr<Err>) 192 // void (unique_ptr<Err>) 193 // Error (unique_ptr<Err>) mutable 194 // void (unique_ptr<Err>) mutable 195 TEST(Error, HandlerTypeDeduction) { 196 197 handleAllErrors(make_error<CustomError>(42), [](const CustomError &CE) {}); 198 199 handleAllErrors( 200 make_error<CustomError>(42), 201 [](const CustomError &CE) mutable -> Error { return Error::success(); }); 202 203 handleAllErrors(make_error<CustomError>(42), 204 [](const CustomError &CE) mutable {}); 205 206 handleAllErrors(make_error<CustomError>(42), 207 [](CustomError &CE) -> Error { return Error::success(); }); 208 209 handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) {}); 210 211 handleAllErrors(make_error<CustomError>(42), 212 [](CustomError &CE) mutable -> Error { return Error::success(); }); 213 214 handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) mutable {}); 215 216 handleAllErrors( 217 make_error<CustomError>(42), 218 [](std::unique_ptr<CustomError> CE) -> Error { return Error::success(); }); 219 220 handleAllErrors(make_error<CustomError>(42), 221 [](std::unique_ptr<CustomError> CE) {}); 222 223 handleAllErrors( 224 make_error<CustomError>(42), 225 [](std::unique_ptr<CustomError> CE) mutable -> Error { return Error::success(); }); 226 227 handleAllErrors(make_error<CustomError>(42), 228 [](std::unique_ptr<CustomError> CE) mutable {}); 229 230 // Check that named handlers of type 'Error (const Err&)' work. 231 handleAllErrors(make_error<CustomError>(42), handleCustomError); 232 233 // Check that named handlers of type 'void (const Err&)' work. 234 handleAllErrors(make_error<CustomError>(42), handleCustomErrorVoid); 235 236 // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work. 237 handleAllErrors(make_error<CustomError>(42), handleCustomErrorUP); 238 239 // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work. 240 handleAllErrors(make_error<CustomError>(42), handleCustomErrorUPVoid); 241 } 242 243 // Test that we can handle errors with custom base classes. 244 TEST(Error, HandleCustomErrorWithCustomBaseClass) { 245 int CaughtErrorInfo = 0; 246 int CaughtErrorExtraInfo = 0; 247 handleAllErrors(make_error<CustomSubError>(42, 7), 248 [&](const CustomSubError &SE) { 249 CaughtErrorInfo = SE.getInfo(); 250 CaughtErrorExtraInfo = SE.getExtraInfo(); 251 }); 252 253 EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7) 254 << "Wrong result from CustomSubError handler"; 255 } 256 257 // Check that we trigger only the first handler that applies. 258 TEST(Error, FirstHandlerOnly) { 259 int DummyInfo = 0; 260 int CaughtErrorInfo = 0; 261 int CaughtErrorExtraInfo = 0; 262 263 handleAllErrors(make_error<CustomSubError>(42, 7), 264 [&](const CustomSubError &SE) { 265 CaughtErrorInfo = SE.getInfo(); 266 CaughtErrorExtraInfo = SE.getExtraInfo(); 267 }, 268 [&](const CustomError &CE) { DummyInfo = CE.getInfo(); }); 269 270 EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7 && 271 DummyInfo == 0) 272 << "Activated the wrong Error handler(s)"; 273 } 274 275 // Check that general handlers shadow specific ones. 276 TEST(Error, HandlerShadowing) { 277 int CaughtErrorInfo = 0; 278 int DummyInfo = 0; 279 int DummyExtraInfo = 0; 280 281 handleAllErrors( 282 make_error<CustomSubError>(42, 7), 283 [&](const CustomError &CE) { CaughtErrorInfo = CE.getInfo(); }, 284 [&](const CustomSubError &SE) { 285 DummyInfo = SE.getInfo(); 286 DummyExtraInfo = SE.getExtraInfo(); 287 }); 288 289 EXPECT_TRUE(CaughtErrorInfo == 42 && DummyInfo == 0 && DummyExtraInfo == 0) 290 << "General Error handler did not shadow specific handler"; 291 } 292 293 // Test joinErrors. 294 TEST(Error, CheckJoinErrors) { 295 int CustomErrorInfo1 = 0; 296 int CustomErrorInfo2 = 0; 297 int CustomErrorExtraInfo = 0; 298 Error E = 299 joinErrors(make_error<CustomError>(7), make_error<CustomSubError>(42, 7)); 300 301 handleAllErrors(std::move(E), 302 [&](const CustomSubError &SE) { 303 CustomErrorInfo2 = SE.getInfo(); 304 CustomErrorExtraInfo = SE.getExtraInfo(); 305 }, 306 [&](const CustomError &CE) { 307 // Assert that the CustomError instance above is handled 308 // before the 309 // CustomSubError - joinErrors should preserve error 310 // ordering. 311 EXPECT_EQ(CustomErrorInfo2, 0) 312 << "CustomErrorInfo2 should be 0 here. " 313 "joinErrors failed to preserve ordering.\n"; 314 CustomErrorInfo1 = CE.getInfo(); 315 }); 316 317 EXPECT_TRUE(CustomErrorInfo1 == 7 && CustomErrorInfo2 == 42 && 318 CustomErrorExtraInfo == 7) 319 << "Failed handling compound Error."; 320 321 // Test appending a single item to a list. 322 { 323 int Sum = 0; 324 handleAllErrors( 325 joinErrors( 326 joinErrors(make_error<CustomError>(7), 327 make_error<CustomError>(7)), 328 make_error<CustomError>(7)), 329 [&](const CustomError &CE) { 330 Sum += CE.getInfo(); 331 }); 332 EXPECT_EQ(Sum, 21) << "Failed to correctly append error to error list."; 333 } 334 335 // Test prepending a single item to a list. 336 { 337 int Sum = 0; 338 handleAllErrors( 339 joinErrors( 340 make_error<CustomError>(7), 341 joinErrors(make_error<CustomError>(7), 342 make_error<CustomError>(7))), 343 [&](const CustomError &CE) { 344 Sum += CE.getInfo(); 345 }); 346 EXPECT_EQ(Sum, 21) << "Failed to correctly prepend error to error list."; 347 } 348 349 // Test concatenating two error lists. 350 { 351 int Sum = 0; 352 handleAllErrors( 353 joinErrors( 354 joinErrors( 355 make_error<CustomError>(7), 356 make_error<CustomError>(7)), 357 joinErrors( 358 make_error<CustomError>(7), 359 make_error<CustomError>(7))), 360 [&](const CustomError &CE) { 361 Sum += CE.getInfo(); 362 }); 363 EXPECT_EQ(Sum, 28) << "Failed to correctly concatenate erorr lists."; 364 } 365 } 366 367 // Test that we can consume success values. 368 TEST(Error, ConsumeSuccess) { 369 Error E = Error::success(); 370 consumeError(std::move(E)); 371 } 372 373 TEST(Error, ConsumeError) { 374 Error E = make_error<CustomError>(7); 375 consumeError(std::move(E)); 376 } 377 378 // Test that handleAllUnhandledErrors crashes if an error is not caught. 379 // Test runs in debug mode only. 380 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 381 TEST(Error, FailureToHandle) { 382 auto FailToHandle = []() { 383 handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) { 384 errs() << "This should never be called"; 385 exit(1); 386 }); 387 }; 388 389 EXPECT_DEATH(FailToHandle(), "Program aborted due to an unhandled Error:") 390 << "Unhandled Error in handleAllErrors call did not cause an " 391 "abort()"; 392 } 393 #endif 394 395 // Test that handleAllUnhandledErrors crashes if an error is returned from a 396 // handler. 397 // Test runs in debug mode only. 398 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 399 TEST(Error, FailureFromHandler) { 400 auto ReturnErrorFromHandler = []() { 401 handleAllErrors(make_error<CustomError>(7), 402 [&](std::unique_ptr<CustomSubError> SE) { 403 return Error(std::move(SE)); 404 }); 405 }; 406 407 EXPECT_DEATH(ReturnErrorFromHandler(), 408 "Program aborted due to an unhandled Error:") 409 << " Error returned from handler in handleAllErrors call did not " 410 "cause abort()"; 411 } 412 #endif 413 414 // Test that we can return values from handleErrors. 415 TEST(Error, CatchErrorFromHandler) { 416 int ErrorInfo = 0; 417 418 Error E = handleErrors( 419 make_error<CustomError>(7), 420 [&](std::unique_ptr<CustomError> CE) { return Error(std::move(CE)); }); 421 422 handleAllErrors(std::move(E), 423 [&](const CustomError &CE) { ErrorInfo = CE.getInfo(); }); 424 425 EXPECT_EQ(ErrorInfo, 7) 426 << "Failed to handle Error returned from handleErrors."; 427 } 428 429 TEST(Error, StringError) { 430 std::string Msg; 431 raw_string_ostream S(Msg); 432 logAllUnhandledErrors(make_error<StringError>("foo" + Twine(42), 433 inconvertibleErrorCode()), 434 S, ""); 435 EXPECT_EQ(S.str(), "foo42\n") << "Unexpected StringError log result"; 436 437 auto EC = 438 errorToErrorCode(make_error<StringError>("", errc::invalid_argument)); 439 EXPECT_EQ(EC, errc::invalid_argument) 440 << "Failed to convert StringError to error_code."; 441 } 442 443 // Test that the ExitOnError utility works as expected. 444 TEST(Error, ExitOnError) { 445 ExitOnError ExitOnErr; 446 ExitOnErr.setBanner("Error in tool:"); 447 ExitOnErr.setExitCodeMapper([](const Error &E) { 448 if (E.isA<CustomSubError>()) 449 return 2; 450 return 1; 451 }); 452 453 // Make sure we don't bail on success. 454 ExitOnErr(Error::success()); 455 EXPECT_EQ(ExitOnErr(Expected<int>(7)), 7) 456 << "exitOnError returned an invalid value for Expected"; 457 458 int A = 7; 459 int &B = ExitOnErr(Expected<int&>(A)); 460 EXPECT_EQ(&A, &B) << "ExitOnError failed to propagate reference"; 461 462 // Exit tests. 463 EXPECT_EXIT(ExitOnErr(make_error<CustomError>(7)), 464 ::testing::ExitedWithCode(1), "Error in tool:") 465 << "exitOnError returned an unexpected error result"; 466 467 EXPECT_EXIT(ExitOnErr(Expected<int>(make_error<CustomSubError>(0, 0))), 468 ::testing::ExitedWithCode(2), "Error in tool:") 469 << "exitOnError returned an unexpected error result"; 470 } 471 472 // Test that the ExitOnError utility works as expected. 473 TEST(Error, CantFailSuccess) { 474 cantFail(Error::success()); 475 476 int X = cantFail(Expected<int>(42)); 477 EXPECT_EQ(X, 42) << "Expected value modified by cantFail"; 478 479 int Dummy = 42; 480 int &Y = cantFail(Expected<int&>(Dummy)); 481 EXPECT_EQ(&Dummy, &Y) << "Reference mangled by cantFail"; 482 } 483 484 // Test that cantFail results in a crash if you pass it a failure value. 485 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 486 TEST(Error, CantFailDeath) { 487 EXPECT_DEATH( 488 cantFail(make_error<StringError>("foo", inconvertibleErrorCode())), 489 "Failure value returned from cantFail wrapped call") 490 << "cantFail(Error) did not cause an abort for failure value"; 491 492 EXPECT_DEATH( 493 { 494 auto IEC = inconvertibleErrorCode(); 495 int X = cantFail(Expected<int>(make_error<StringError>("foo", IEC))); 496 (void)X; 497 }, 498 "Failure value returned from cantFail wrapped call") 499 << "cantFail(Expected<int>) did not cause an abort for failure value"; 500 } 501 #endif 502 503 504 // Test Checked Expected<T> in success mode. 505 TEST(Error, CheckedExpectedInSuccessMode) { 506 Expected<int> A = 7; 507 EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'"; 508 // Access is safe in second test, since we checked the error in the first. 509 EXPECT_EQ(*A, 7) << "Incorrect Expected non-error value"; 510 } 511 512 // Test Expected with reference type. 513 TEST(Error, ExpectedWithReferenceType) { 514 int A = 7; 515 Expected<int&> B = A; 516 // 'Check' B. 517 (void)!!B; 518 int &C = *B; 519 EXPECT_EQ(&A, &C) << "Expected failed to propagate reference"; 520 } 521 522 // Test Unchecked Expected<T> in success mode. 523 // We expect this to blow up the same way Error would. 524 // Test runs in debug mode only. 525 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 526 TEST(Error, UncheckedExpectedInSuccessModeDestruction) { 527 EXPECT_DEATH({ Expected<int> A = 7; }, 528 "Expected<T> must be checked before access or destruction.") 529 << "Unchecekd Expected<T> success value did not cause an abort()."; 530 } 531 #endif 532 533 // Test Unchecked Expected<T> in success mode. 534 // We expect this to blow up the same way Error would. 535 // Test runs in debug mode only. 536 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 537 TEST(Error, UncheckedExpectedInSuccessModeAccess) { 538 EXPECT_DEATH({ Expected<int> A = 7; *A; }, 539 "Expected<T> must be checked before access or destruction.") 540 << "Unchecekd Expected<T> success value did not cause an abort()."; 541 } 542 #endif 543 544 // Test Unchecked Expected<T> in success mode. 545 // We expect this to blow up the same way Error would. 546 // Test runs in debug mode only. 547 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 548 TEST(Error, UncheckedExpectedInSuccessModeAssignment) { 549 EXPECT_DEATH({ Expected<int> A = 7; A = 7; }, 550 "Expected<T> must be checked before access or destruction.") 551 << "Unchecekd Expected<T> success value did not cause an abort()."; 552 } 553 #endif 554 555 // Test Expected<T> in failure mode. 556 TEST(Error, ExpectedInFailureMode) { 557 Expected<int> A = make_error<CustomError>(42); 558 EXPECT_FALSE(!!A) << "Expected with error value doesn't convert to 'false'"; 559 Error E = A.takeError(); 560 EXPECT_TRUE(E.isA<CustomError>()) << "Incorrect Expected error value"; 561 consumeError(std::move(E)); 562 } 563 564 // Check that an Expected instance with an error value doesn't allow access to 565 // operator*. 566 // Test runs in debug mode only. 567 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 568 TEST(Error, AccessExpectedInFailureMode) { 569 Expected<int> A = make_error<CustomError>(42); 570 EXPECT_DEATH(*A, "Expected<T> must be checked before access or destruction.") 571 << "Incorrect Expected error value"; 572 consumeError(A.takeError()); 573 } 574 #endif 575 576 // Check that an Expected instance with an error triggers an abort if 577 // unhandled. 578 // Test runs in debug mode only. 579 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 580 TEST(Error, UnhandledExpectedInFailureMode) { 581 EXPECT_DEATH({ Expected<int> A = make_error<CustomError>(42); }, 582 "Expected<T> must be checked before access or destruction.") 583 << "Unchecked Expected<T> failure value did not cause an abort()"; 584 } 585 #endif 586 587 // Test covariance of Expected. 588 TEST(Error, ExpectedCovariance) { 589 class B {}; 590 class D : public B {}; 591 592 Expected<B *> A1(Expected<D *>(nullptr)); 593 // Check A1 by converting to bool before assigning to it. 594 (void)!!A1; 595 A1 = Expected<D *>(nullptr); 596 // Check A1 again before destruction. 597 (void)!!A1; 598 599 Expected<std::unique_ptr<B>> A2(Expected<std::unique_ptr<D>>(nullptr)); 600 // Check A2 by converting to bool before assigning to it. 601 (void)!!A2; 602 A2 = Expected<std::unique_ptr<D>>(nullptr); 603 // Check A2 again before destruction. 604 (void)!!A2; 605 } 606 607 TEST(Error, ErrorCodeConversions) { 608 // Round-trip a success value to check that it converts correctly. 609 EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())), 610 std::error_code()) 611 << "std::error_code() should round-trip via Error conversions"; 612 613 // Round-trip an error value to check that it converts correctly. 614 EXPECT_EQ(errorToErrorCode(errorCodeToError(errc::invalid_argument)), 615 errc::invalid_argument) 616 << "std::error_code error value should round-trip via Error " 617 "conversions"; 618 619 // Round-trip a success value through ErrorOr/Expected to check that it 620 // converts correctly. 621 { 622 auto Orig = ErrorOr<int>(42); 623 auto RoundTripped = 624 expectedToErrorOr(errorOrToExpected(ErrorOr<int>(42))); 625 EXPECT_EQ(*Orig, *RoundTripped) 626 << "ErrorOr<T> success value should round-trip via Expected<T> " 627 "conversions."; 628 } 629 630 // Round-trip a failure value through ErrorOr/Expected to check that it 631 // converts correctly. 632 { 633 auto Orig = ErrorOr<int>(errc::invalid_argument); 634 auto RoundTripped = 635 expectedToErrorOr( 636 errorOrToExpected(ErrorOr<int>(errc::invalid_argument))); 637 EXPECT_EQ(Orig.getError(), RoundTripped.getError()) 638 << "ErrorOr<T> failure value should round-trip via Expected<T> " 639 "conversions."; 640 } 641 } 642 643 // Test that error messages work. 644 TEST(Error, ErrorMessage) { 645 EXPECT_EQ(toString(Error::success()).compare(""), 0); 646 647 Error E1 = make_error<CustomError>(0); 648 EXPECT_EQ(toString(std::move(E1)).compare("CustomError { 0}"), 0); 649 650 Error E2 = make_error<CustomError>(0); 651 handleAllErrors(std::move(E2), [](const CustomError &CE) { 652 EXPECT_EQ(CE.message().compare("CustomError { 0}"), 0); 653 }); 654 655 Error E3 = joinErrors(make_error<CustomError>(0), make_error<CustomError>(1)); 656 EXPECT_EQ(toString(std::move(E3)) 657 .compare("CustomError { 0}\n" 658 "CustomError { 1}"), 659 0); 660 } 661 662 } // end anon namespace 663