ErrorTest.cpp 20 KB

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