blob: c2a1673f42dd392e5d107347367958a61aa18860 [file] [log] [blame]
Lang Hamesf7f6d3e2016-03-16 01:02:46 +00001//===----- 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#include "llvm/Support/Errc.h"
Lang Hamese7aad352016-03-23 23:57:28 +000012#include "llvm/Support/ErrorHandling.h"
Lang Hamesf7f6d3e2016-03-16 01:02:46 +000013#include "gtest/gtest.h"
14#include <memory>
15
16using namespace llvm;
17
18namespace {
19
Lang Hamesf7f6d3e2016-03-16 01:02:46 +000020// Custom error class with a default base class and some random 'info' attached.
21class CustomError : public ErrorInfo<CustomError> {
22public:
23 // Create an error with some info attached.
24 CustomError(int Info) : Info(Info) {}
25
26 // Get the info attached to this error.
27 int getInfo() const { return Info; }
28
29 // Log this error to a stream.
30 void log(raw_ostream &OS) const override {
31 OS << "CustomError { " << getInfo() << "}";
32 }
33
Lang Hamese7aad352016-03-23 23:57:28 +000034 std::error_code convertToErrorCode() const override {
35 llvm_unreachable("CustomError doesn't support ECError conversion");
36 }
37
Reid Klecknera15b76b2016-03-24 23:49:34 +000038 // Used by ErrorInfo::classID.
39 static char ID;
40
Lang Hamesf7f6d3e2016-03-16 01:02:46 +000041protected:
42 // This error is subclassed below, but we can't use inheriting constructors
43 // yet, so we can't propagate the constructors through ErrorInfo. Instead
44 // we have to have a default constructor and have the subclass initialize all
45 // fields.
46 CustomError() : Info(0) {}
47
48 int Info;
49};
50
Reid Klecknera15b76b2016-03-24 23:49:34 +000051char CustomError::ID = 0;
52
Lang Hamesf7f6d3e2016-03-16 01:02:46 +000053// Custom error class with a custom base class and some additional random
54// 'info'.
55class CustomSubError : public ErrorInfo<CustomSubError, CustomError> {
56public:
57 // Create a sub-error with some info attached.
58 CustomSubError(int Info, int ExtraInfo) : ExtraInfo(ExtraInfo) {
59 this->Info = Info;
60 }
61
62 // Get the extra info attached to this error.
63 int getExtraInfo() const { return ExtraInfo; }
64
65 // Log this error to a stream.
66 void log(raw_ostream &OS) const override {
67 OS << "CustomSubError { " << getInfo() << ", " << getExtraInfo() << "}";
68 }
69
Lang Hamese7aad352016-03-23 23:57:28 +000070 std::error_code convertToErrorCode() const override {
71 llvm_unreachable("CustomSubError doesn't support ECError conversion");
72 }
73
Reid Klecknera15b76b2016-03-24 23:49:34 +000074 // Used by ErrorInfo::classID.
75 static char ID;
76
Lang Hamesf7f6d3e2016-03-16 01:02:46 +000077protected:
78 int ExtraInfo;
79};
80
Reid Klecknera15b76b2016-03-24 23:49:34 +000081char CustomSubError::ID = 0;
82
Lang Hamesf7f6d3e2016-03-16 01:02:46 +000083static Error handleCustomError(const CustomError &CE) { return Error(); }
84
85static void handleCustomErrorVoid(const CustomError &CE) {}
86
87static Error handleCustomErrorUP(std::unique_ptr<CustomError> CE) {
88 return Error();
89}
90
91static void handleCustomErrorUPVoid(std::unique_ptr<CustomError> CE) {}
92
Lang Hames01864a22016-03-18 00:12:37 +000093// Test that success values implicitly convert to false, and don't cause crashes
94// once they've been implicitly converted.
95TEST(Error, CheckedSuccess) {
96 Error E;
97 EXPECT_FALSE(E) << "Unexpected error while testing Error 'Success'";
98}
99
100// Test that unchecked succes values cause an abort.
101#ifndef NDEBUG
102TEST(Error, UncheckedSuccess) {
103 EXPECT_DEATH({ Error E; }, "Program aborted due to an unhandled Error:")
104 << "Unchecked Error Succes value did not cause abort()";
105}
106#endif
107
Lang Hamesd1af8fc2016-03-25 23:54:32 +0000108// ErrorAsOutParameter tester.
109void errAsOutParamHelper(Error &Err) {
110 ErrorAsOutParameter ErrAsOutParam(Err);
111 // Verify that checked flag is raised - assignment should not crash.
112 Err = Error::success();
113 // Raise the checked bit manually - caller should still have to test the
114 // error.
115 (void)!!Err;
Lang Hamesd0ac31a2016-03-25 21:56:35 +0000116}
117
Lang Hamesd1af8fc2016-03-25 23:54:32 +0000118// Test that ErrorAsOutParameter sets the checked flag on construction.
119TEST(Error, ErrorAsOutParameterChecked) {
120 Error E;
121 errAsOutParamHelper(E);
122 (void)!!E;
123}
124
125// Test that ErrorAsOutParameter clears the checked flag on destruction.
126#ifndef NDEBUG
127TEST(Error, ErrorAsOutParameterUnchecked) {
128 EXPECT_DEATH({ Error E; errAsOutParamHelper(E); },
129 "Program aborted due to an unhandled Error:")
130 << "ErrorAsOutParameter did not clear the checked flag on destruction.";
131}
132#endif
133
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000134// Check that we abort on unhandled failure cases. (Force conversion to bool
135// to make sure that we don't accidentally treat checked errors as handled).
136// Test runs in debug mode only.
137#ifndef NDEBUG
Lang Hames01864a22016-03-18 00:12:37 +0000138TEST(Error, UncheckedError) {
139 auto DropUnhandledError = []() {
140 Error E = make_error<CustomError>(42);
141 (void)!E;
142 };
143 EXPECT_DEATH(DropUnhandledError(),
144 "Program aborted due to an unhandled Error:")
145 << "Unhandled Error failure value did not cause abort()";
146}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000147#endif
148
Lang Hames01864a22016-03-18 00:12:37 +0000149// Check 'Error::isA<T>' method handling.
150TEST(Error, IsAHandling) {
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000151 // Check 'isA' handling.
Lang Hames01864a22016-03-18 00:12:37 +0000152 Error E = make_error<CustomError>(1);
153 Error F = make_error<CustomSubError>(1, 2);
154 Error G = Error::success();
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000155
Lang Hames01864a22016-03-18 00:12:37 +0000156 EXPECT_TRUE(E.isA<CustomError>());
157 EXPECT_FALSE(E.isA<CustomSubError>());
158 EXPECT_TRUE(F.isA<CustomError>());
159 EXPECT_TRUE(F.isA<CustomSubError>());
160 EXPECT_FALSE(G.isA<CustomError>());
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000161
Lang Hames01864a22016-03-18 00:12:37 +0000162 consumeError(std::move(E));
163 consumeError(std::move(F));
164 consumeError(std::move(G));
165}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000166
Lang Hames01864a22016-03-18 00:12:37 +0000167// Check that we can handle a custom error.
168TEST(Error, HandleCustomError) {
169 int CaughtErrorInfo = 0;
170 handleAllErrors(make_error<CustomError>(42), [&](const CustomError &CE) {
171 CaughtErrorInfo = CE.getInfo();
172 });
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000173
Lang Hames01864a22016-03-18 00:12:37 +0000174 EXPECT_TRUE(CaughtErrorInfo == 42) << "Wrong result from CustomError handler";
175}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000176
Lang Hames01864a22016-03-18 00:12:37 +0000177// Check that handler type deduction also works for handlers
178// of the following types:
179// void (const Err&)
180// Error (const Err&) mutable
181// void (const Err&) mutable
182// Error (Err&)
183// void (Err&)
184// Error (Err&) mutable
185// void (Err&) mutable
186// Error (unique_ptr<Err>)
187// void (unique_ptr<Err>)
188// Error (unique_ptr<Err>) mutable
189// void (unique_ptr<Err>) mutable
190TEST(Error, HandlerTypeDeduction) {
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000191
192 handleAllErrors(make_error<CustomError>(42), [](const CustomError &CE) {});
193
194 handleAllErrors(
195 make_error<CustomError>(42),
196 [](const CustomError &CE) mutable { return Error::success(); });
197
198 handleAllErrors(make_error<CustomError>(42),
199 [](const CustomError &CE) mutable {});
200
201 handleAllErrors(make_error<CustomError>(42),
202 [](CustomError &CE) { return Error::success(); });
203
204 handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) {});
205
206 handleAllErrors(make_error<CustomError>(42),
207 [](CustomError &CE) mutable { return Error::success(); });
208
209 handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) mutable {});
210
211 handleAllErrors(
212 make_error<CustomError>(42),
213 [](std::unique_ptr<CustomError> CE) { return Error::success(); });
214
215 handleAllErrors(make_error<CustomError>(42),
216 [](std::unique_ptr<CustomError> CE) {});
217
218 handleAllErrors(
219 make_error<CustomError>(42),
220 [](std::unique_ptr<CustomError> CE) mutable { return Error::success(); });
221
222 handleAllErrors(make_error<CustomError>(42),
223 [](std::unique_ptr<CustomError> CE) mutable {});
224
225 // Check that named handlers of type 'Error (const Err&)' work.
226 handleAllErrors(make_error<CustomError>(42), handleCustomError);
227
228 // Check that named handlers of type 'void (const Err&)' work.
229 handleAllErrors(make_error<CustomError>(42), handleCustomErrorVoid);
230
231 // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work.
232 handleAllErrors(make_error<CustomError>(42), handleCustomErrorUP);
233
234 // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work.
235 handleAllErrors(make_error<CustomError>(42), handleCustomErrorUPVoid);
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000236}
237
Lang Hames01864a22016-03-18 00:12:37 +0000238// Test that we can handle errors with custom base classes.
239TEST(Error, HandleCustomErrorWithCustomBaseClass) {
240 int CaughtErrorInfo = 0;
241 int CaughtErrorExtraInfo = 0;
242 handleAllErrors(make_error<CustomSubError>(42, 7),
243 [&](const CustomSubError &SE) {
244 CaughtErrorInfo = SE.getInfo();
245 CaughtErrorExtraInfo = SE.getExtraInfo();
246 });
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000247
Lang Hames01864a22016-03-18 00:12:37 +0000248 EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7)
249 << "Wrong result from CustomSubError handler";
250}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000251
Lang Hames01864a22016-03-18 00:12:37 +0000252// Check that we trigger only the first handler that applies.
253TEST(Error, FirstHandlerOnly) {
254 int DummyInfo = 0;
255 int CaughtErrorInfo = 0;
256 int CaughtErrorExtraInfo = 0;
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000257
Lang Hames01864a22016-03-18 00:12:37 +0000258 handleAllErrors(make_error<CustomSubError>(42, 7),
259 [&](const CustomSubError &SE) {
260 CaughtErrorInfo = SE.getInfo();
261 CaughtErrorExtraInfo = SE.getExtraInfo();
262 },
263 [&](const CustomError &CE) { DummyInfo = CE.getInfo(); });
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000264
Lang Hames01864a22016-03-18 00:12:37 +0000265 EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7 &&
266 DummyInfo == 0)
267 << "Activated the wrong Error handler(s)";
268}
269
270// Check that general handlers shadow specific ones.
271TEST(Error, HandlerShadowing) {
272 int CaughtErrorInfo = 0;
273 int DummyInfo = 0;
274 int DummyExtraInfo = 0;
275
276 handleAllErrors(
277 make_error<CustomSubError>(42, 7),
278 [&](const CustomError &CE) { CaughtErrorInfo = CE.getInfo(); },
279 [&](const CustomSubError &SE) {
280 DummyInfo = SE.getInfo();
281 DummyExtraInfo = SE.getExtraInfo();
282 });
283
NAKAMURA Takumib2cef642016-03-24 15:19:22 +0000284 EXPECT_TRUE(CaughtErrorInfo == 42 && DummyInfo == 0 && DummyExtraInfo == 0)
Lang Hames01864a22016-03-18 00:12:37 +0000285 << "General Error handler did not shadow specific handler";
286}
287
288// Test joinErrors.
289TEST(Error, CheckJoinErrors) {
290 int CustomErrorInfo1 = 0;
291 int CustomErrorInfo2 = 0;
292 int CustomErrorExtraInfo = 0;
293 Error E =
294 joinErrors(make_error<CustomError>(7), make_error<CustomSubError>(42, 7));
295
296 handleAllErrors(std::move(E),
297 [&](const CustomSubError &SE) {
298 CustomErrorInfo2 = SE.getInfo();
299 CustomErrorExtraInfo = SE.getExtraInfo();
300 },
301 [&](const CustomError &CE) {
302 // Assert that the CustomError instance above is handled
303 // before the
304 // CustomSubError - joinErrors should preserve error
305 // ordering.
306 EXPECT_EQ(CustomErrorInfo2, 0)
307 << "CustomErrorInfo2 should be 0 here. "
308 "joinErrors failed to preserve ordering.\n";
309 CustomErrorInfo1 = CE.getInfo();
310 });
311
312 EXPECT_TRUE(CustomErrorInfo1 == 7 && CustomErrorInfo2 == 42 &&
313 CustomErrorExtraInfo == 7)
314 << "Failed handling compound Error.";
315}
316
317// Test that we can consume success values.
318TEST(Error, ConsumeSuccess) {
319 Error E;
320 consumeError(std::move(E));
321}
322
323TEST(Error, ConsumeError) {
324 Error E = make_error<CustomError>(7);
325 consumeError(std::move(E));
326}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000327
328// Test that handleAllUnhandledErrors crashes if an error is not caught.
329// Test runs in debug mode only.
330#ifndef NDEBUG
Lang Hames01864a22016-03-18 00:12:37 +0000331TEST(Error, FailureToHandle) {
332 auto FailToHandle = []() {
333 handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) {
334 errs() << "This should never be called";
335 exit(1);
336 });
337 };
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000338
Lang Hames01864a22016-03-18 00:12:37 +0000339 EXPECT_DEATH(FailToHandle(), "Program aborted due to an unhandled Error:")
340 << "Unhandled Error in handleAllErrors call did not cause an "
341 "abort()";
342}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000343#endif
344
345// Test that handleAllUnhandledErrors crashes if an error is returned from a
346// handler.
347// Test runs in debug mode only.
348#ifndef NDEBUG
Lang Hames01864a22016-03-18 00:12:37 +0000349TEST(Error, FailureFromHandler) {
350 auto ReturnErrorFromHandler = []() {
351 handleAllErrors(make_error<CustomError>(7),
352 [&](std::unique_ptr<CustomSubError> SE) {
353 return Error(std::move(SE));
354 });
355 };
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000356
Lang Hames01864a22016-03-18 00:12:37 +0000357 EXPECT_DEATH(ReturnErrorFromHandler(),
358 "Program aborted due to an unhandled Error:")
359 << " Error returned from handler in handleAllErrors call did not "
360 "cause abort()";
361}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000362#endif
363
Lang Hames01864a22016-03-18 00:12:37 +0000364// Test that we can return values from handleErrors.
365TEST(Error, CatchErrorFromHandler) {
366 int ErrorInfo = 0;
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000367
Lang Hames01864a22016-03-18 00:12:37 +0000368 Error E = handleErrors(
369 make_error<CustomError>(7),
370 [&](std::unique_ptr<CustomError> CE) { return Error(std::move(CE)); });
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000371
Lang Hames01864a22016-03-18 00:12:37 +0000372 handleAllErrors(std::move(E),
373 [&](const CustomError &CE) { ErrorInfo = CE.getInfo(); });
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000374
Lang Hames01864a22016-03-18 00:12:37 +0000375 EXPECT_EQ(ErrorInfo, 7)
376 << "Failed to handle Error returned from handleErrors.";
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000377}
378
Lang Hames01864a22016-03-18 00:12:37 +0000379// Test that the ExitOnError utility works as expected.
380TEST(Error, ExitOnError) {
381 ExitOnError ExitOnErr;
382 ExitOnErr.setBanner("Error in tool:");
383 ExitOnErr.setExitCodeMapper([](const Error &E) {
384 if (E.isA<CustomSubError>())
385 return 2;
386 return 1;
387 });
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000388
Lang Hames01864a22016-03-18 00:12:37 +0000389 // Make sure we don't bail on success.
390 ExitOnErr(Error::success());
391 EXPECT_EQ(ExitOnErr(Expected<int>(7)), 7)
392 << "exitOnError returned an invalid value for Expected";
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000393
Lang Hames285639f2016-04-25 19:21:57 +0000394 int A = 7;
395 int &B = ExitOnErr(Expected<int&>(A));
396 EXPECT_EQ(&A, &B) << "ExitOnError failed to propagate reference";
397
Lang Hames01864a22016-03-18 00:12:37 +0000398 // Exit tests.
399 EXPECT_EXIT(ExitOnErr(make_error<CustomError>(7)),
400 ::testing::ExitedWithCode(1), "Error in tool:")
401 << "exitOnError returned an unexpected error result";
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000402
Lang Hames01864a22016-03-18 00:12:37 +0000403 EXPECT_EXIT(ExitOnErr(Expected<int>(make_error<CustomSubError>(0, 0))),
404 ::testing::ExitedWithCode(2), "Error in tool:")
405 << "exitOnError returned an unexpected error result";
406}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000407
Lang Hames580ca232016-04-05 19:57:03 +0000408// Test Checked Expected<T> in success mode.
409TEST(Error, CheckedExpectedInSuccessMode) {
Lang Hames01864a22016-03-18 00:12:37 +0000410 Expected<int> A = 7;
411 EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'";
Lang Hames580ca232016-04-05 19:57:03 +0000412 // Access is safe in second test, since we checked the error in the first.
Lang Hames01864a22016-03-18 00:12:37 +0000413 EXPECT_EQ(*A, 7) << "Incorrect Expected non-error value";
414}
415
Lang Hames285639f2016-04-25 19:21:57 +0000416// Test Expected with reference type.
417TEST(Error, ExpectedWithReferenceType) {
418 int A = 7;
419 Expected<int&> B = A;
420 // 'Check' B.
421 (void)!!B;
422 int &C = *B;
423 EXPECT_EQ(&A, &C) << "Expected failed to propagate reference";
424}
425
Lang Hames580ca232016-04-05 19:57:03 +0000426// Test Unchecked Expected<T> in success mode.
427// We expect this to blow up the same way Error would.
428// Test runs in debug mode only.
429#ifndef NDEBUG
430TEST(Error, UncheckedExpectedInSuccessModeDestruction) {
431 EXPECT_DEATH({ Expected<int> A = 7; },
432 "Expected<T> must be checked before access or destruction.")
433 << "Unchecekd Expected<T> success value did not cause an abort().";
434}
435#endif
436
437// Test Unchecked Expected<T> in success mode.
438// We expect this to blow up the same way Error would.
439// Test runs in debug mode only.
440#ifndef NDEBUG
441TEST(Error, UncheckedExpectedInSuccessModeAccess) {
442 EXPECT_DEATH({ Expected<int> A = 7; *A; },
443 "Expected<T> must be checked before access or destruction.")
444 << "Unchecekd Expected<T> success value did not cause an abort().";
445}
446#endif
447
448// Test Unchecked Expected<T> in success mode.
449// We expect this to blow up the same way Error would.
450// Test runs in debug mode only.
451#ifndef NDEBUG
452TEST(Error, UncheckedExpectedInSuccessModeAssignment) {
453 EXPECT_DEATH({ Expected<int> A = 7; A = 7; },
454 "Expected<T> must be checked before access or destruction.")
455 << "Unchecekd Expected<T> success value did not cause an abort().";
456}
457#endif
458
Lang Hames01864a22016-03-18 00:12:37 +0000459// Test Expected<T> in failure mode.
460TEST(Error, ExpectedInFailureMode) {
461 Expected<int> A = make_error<CustomError>(42);
462 EXPECT_FALSE(!!A) << "Expected with error value doesn't convert to 'false'";
463 Error E = A.takeError();
464 EXPECT_TRUE(E.isA<CustomError>()) << "Incorrect Expected error value";
465 consumeError(std::move(E));
466}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000467
468// Check that an Expected instance with an error value doesn't allow access to
469// operator*.
470// Test runs in debug mode only.
471#ifndef NDEBUG
Lang Hames01864a22016-03-18 00:12:37 +0000472TEST(Error, AccessExpectedInFailureMode) {
473 Expected<int> A = make_error<CustomError>(42);
Lang Hames580ca232016-04-05 19:57:03 +0000474 EXPECT_DEATH(*A, "Expected<T> must be checked before access or destruction.")
Lang Hames01864a22016-03-18 00:12:37 +0000475 << "Incorrect Expected error value";
476 consumeError(A.takeError());
477}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000478#endif
479
480// Check that an Expected instance with an error triggers an abort if
481// unhandled.
482// Test runs in debug mode only.
483#ifndef NDEBUG
Lang Hames01864a22016-03-18 00:12:37 +0000484TEST(Error, UnhandledExpectedInFailureMode) {
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000485 EXPECT_DEATH({ Expected<int> A = make_error<CustomError>(42); },
Lang Hames580ca232016-04-05 19:57:03 +0000486 "Expected<T> must be checked before access or destruction.")
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000487 << "Unchecked Expected<T> failure value did not cause an abort()";
Lang Hames01864a22016-03-18 00:12:37 +0000488}
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000489#endif
490
Lang Hames01864a22016-03-18 00:12:37 +0000491// Test covariance of Expected.
492TEST(Error, ExpectedCovariance) {
493 class B {};
494 class D : public B {};
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000495
Lang Hames01864a22016-03-18 00:12:37 +0000496 Expected<B *> A1(Expected<D *>(nullptr));
Lang Hames580ca232016-04-05 19:57:03 +0000497 // Check A1 by converting to bool before assigning to it.
498 (void)!!A1;
Lang Hames01864a22016-03-18 00:12:37 +0000499 A1 = Expected<D *>(nullptr);
Lang Hames580ca232016-04-05 19:57:03 +0000500 // Check A1 again before destruction.
501 (void)!!A1;
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000502
Lang Hames01864a22016-03-18 00:12:37 +0000503 Expected<std::unique_ptr<B>> A2(Expected<std::unique_ptr<D>>(nullptr));
Lang Hames580ca232016-04-05 19:57:03 +0000504 // Check A2 by converting to bool before assigning to it.
505 (void)!!A2;
Lang Hames01864a22016-03-18 00:12:37 +0000506 A2 = Expected<std::unique_ptr<D>>(nullptr);
Lang Hames580ca232016-04-05 19:57:03 +0000507 // Check A2 again before destruction.
508 (void)!!A2;
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000509}
510
Lang Hamesd21a5352016-03-24 02:00:10 +0000511TEST(Error, ErrorCodeConversions) {
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000512 // Round-trip a success value to check that it converts correctly.
513 EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())),
514 std::error_code())
515 << "std::error_code() should round-trip via Error conversions";
516
517 // Round-trip an error value to check that it converts correctly.
518 EXPECT_EQ(errorToErrorCode(errorCodeToError(errc::invalid_argument)),
519 errc::invalid_argument)
520 << "std::error_code error value should round-trip via Error "
521 "conversions";
Lang Hamesd21a5352016-03-24 02:00:10 +0000522
523 // Round-trip a success value through ErrorOr/Expected to check that it
524 // converts correctly.
525 {
526 auto Orig = ErrorOr<int>(42);
527 auto RoundTripped =
528 expectedToErrorOr(errorOrToExpected(ErrorOr<int>(42)));
529 EXPECT_EQ(*Orig, *RoundTripped)
530 << "ErrorOr<T> success value should round-trip via Expected<T> "
531 "conversions.";
532 }
533
534 // Round-trip a failure value through ErrorOr/Expected to check that it
535 // converts correctly.
536 {
537 auto Orig = ErrorOr<int>(errc::invalid_argument);
538 auto RoundTripped =
539 expectedToErrorOr(
540 errorOrToExpected(ErrorOr<int>(errc::invalid_argument)));
541 EXPECT_EQ(Orig.getError(), RoundTripped.getError())
542 << "ErrorOr<T> failure value should round-trip via Expected<T> "
543 "conversions.";
544 }
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000545}
546
Vedant Kumar27370a02016-05-03 23:32:31 +0000547// Test that error messages work.
548TEST(Error, ErrorMessage) {
549 EXPECT_EQ(toString(Error::success()).compare(""), 0);
550
551 Error E1 = make_error<CustomError>(0);
552 EXPECT_EQ(toString(std::move(E1)).compare("CustomError { 0}"), 0);
553
554 Error E2 = make_error<CustomError>(0);
555 handleAllErrors(std::move(E2), [](const CustomError &CE) {
556 EXPECT_EQ(CE.message().compare("CustomError { 0}"), 0);
557 });
558
559 Error E3 = joinErrors(make_error<CustomError>(0), make_error<CustomError>(1));
560 EXPECT_EQ(toString(std::move(E3))
561 .compare("CustomError { 0}\n"
562 "CustomError { 1}"),
563 0);
564}
565
Lang Hamesf7f6d3e2016-03-16 01:02:46 +0000566} // end anon namespace