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"
12e7aad357SLang Hames #include "llvm/Support/ErrorHandling.h"
13f7f6d3e9SLang Hames #include "gtest/gtest.h"
14f7f6d3e9SLang Hames #include <memory>
15f7f6d3e9SLang Hames 
16f7f6d3e9SLang Hames using namespace llvm;
17f7f6d3e9SLang Hames 
18f7f6d3e9SLang Hames namespace {
19f7f6d3e9SLang Hames 
20f7f6d3e9SLang Hames // Custom error class with a default base class and some random 'info' attached.
21f7f6d3e9SLang Hames class CustomError : public ErrorInfo<CustomError> {
22f7f6d3e9SLang Hames public:
23f7f6d3e9SLang Hames   // Create an error with some info attached.
24f7f6d3e9SLang Hames   CustomError(int Info) : Info(Info) {}
25f7f6d3e9SLang Hames 
26f7f6d3e9SLang Hames   // Get the info attached to this error.
27f7f6d3e9SLang Hames   int getInfo() const { return Info; }
28f7f6d3e9SLang Hames 
29f7f6d3e9SLang Hames   // Log this error to a stream.
30f7f6d3e9SLang Hames   void log(raw_ostream &OS) const override {
31f7f6d3e9SLang Hames     OS << "CustomError { " << getInfo() << "}";
32f7f6d3e9SLang Hames   }
33f7f6d3e9SLang Hames 
34e7aad357SLang Hames   std::error_code convertToErrorCode() const override {
35e7aad357SLang Hames     llvm_unreachable("CustomError doesn't support ECError conversion");
36e7aad357SLang Hames   }
37e7aad357SLang Hames 
38*a15b76b3SReid Kleckner   // Used by ErrorInfo::classID.
39*a15b76b3SReid Kleckner   static char ID;
40*a15b76b3SReid Kleckner 
41f7f6d3e9SLang Hames protected:
42f7f6d3e9SLang Hames   // This error is subclassed below, but we can't use inheriting constructors
43f7f6d3e9SLang Hames   // yet, so we can't propagate the constructors through ErrorInfo. Instead
44f7f6d3e9SLang Hames   // we have to have a default constructor and have the subclass initialize all
45f7f6d3e9SLang Hames   // fields.
46f7f6d3e9SLang Hames   CustomError() : Info(0) {}
47f7f6d3e9SLang Hames 
48f7f6d3e9SLang Hames   int Info;
49f7f6d3e9SLang Hames };
50f7f6d3e9SLang Hames 
51*a15b76b3SReid Kleckner char CustomError::ID = 0;
52*a15b76b3SReid Kleckner 
53f7f6d3e9SLang Hames // Custom error class with a custom base class and some additional random
54f7f6d3e9SLang Hames // 'info'.
55f7f6d3e9SLang Hames class CustomSubError : public ErrorInfo<CustomSubError, CustomError> {
56f7f6d3e9SLang Hames public:
57f7f6d3e9SLang Hames   // Create a sub-error with some info attached.
58f7f6d3e9SLang Hames   CustomSubError(int Info, int ExtraInfo) : ExtraInfo(ExtraInfo) {
59f7f6d3e9SLang Hames     this->Info = Info;
60f7f6d3e9SLang Hames   }
61f7f6d3e9SLang Hames 
62f7f6d3e9SLang Hames   // Get the extra info attached to this error.
63f7f6d3e9SLang Hames   int getExtraInfo() const { return ExtraInfo; }
64f7f6d3e9SLang Hames 
65f7f6d3e9SLang Hames   // Log this error to a stream.
66f7f6d3e9SLang Hames   void log(raw_ostream &OS) const override {
67f7f6d3e9SLang Hames     OS << "CustomSubError { " << getInfo() << ", " << getExtraInfo() << "}";
68f7f6d3e9SLang Hames   }
69f7f6d3e9SLang Hames 
70e7aad357SLang Hames   std::error_code convertToErrorCode() const override {
71e7aad357SLang Hames     llvm_unreachable("CustomSubError doesn't support ECError conversion");
72e7aad357SLang Hames   }
73e7aad357SLang Hames 
74*a15b76b3SReid Kleckner   // Used by ErrorInfo::classID.
75*a15b76b3SReid Kleckner   static char ID;
76*a15b76b3SReid Kleckner 
77f7f6d3e9SLang Hames protected:
78f7f6d3e9SLang Hames   int ExtraInfo;
79f7f6d3e9SLang Hames };
80f7f6d3e9SLang Hames 
81*a15b76b3SReid Kleckner char CustomSubError::ID = 0;
82*a15b76b3SReid Kleckner 
83f7f6d3e9SLang Hames static Error handleCustomError(const CustomError &CE) { return Error(); }
84f7f6d3e9SLang Hames 
85f7f6d3e9SLang Hames static void handleCustomErrorVoid(const CustomError &CE) {}
86f7f6d3e9SLang Hames 
87f7f6d3e9SLang Hames static Error handleCustomErrorUP(std::unique_ptr<CustomError> CE) {
88f7f6d3e9SLang Hames   return Error();
89f7f6d3e9SLang Hames }
90f7f6d3e9SLang Hames 
91f7f6d3e9SLang Hames static void handleCustomErrorUPVoid(std::unique_ptr<CustomError> CE) {}
92f7f6d3e9SLang Hames 
9301864a2dSLang Hames // Test that success values implicitly convert to false, and don't cause crashes
9401864a2dSLang Hames // once they've been implicitly converted.
9501864a2dSLang Hames TEST(Error, CheckedSuccess) {
9601864a2dSLang Hames   Error E;
9701864a2dSLang Hames   EXPECT_FALSE(E) << "Unexpected error while testing Error 'Success'";
9801864a2dSLang Hames }
9901864a2dSLang Hames 
10001864a2dSLang Hames // Test that unchecked succes values cause an abort.
10101864a2dSLang Hames #ifndef NDEBUG
10201864a2dSLang Hames TEST(Error, UncheckedSuccess) {
10301864a2dSLang Hames   EXPECT_DEATH({ Error E; }, "Program aborted due to an unhandled Error:")
10401864a2dSLang Hames       << "Unchecked Error Succes value did not cause abort()";
10501864a2dSLang Hames }
10601864a2dSLang Hames #endif
10701864a2dSLang Hames 
108f7f6d3e9SLang Hames // Check that we abort on unhandled failure cases. (Force conversion to bool
109f7f6d3e9SLang Hames // to make sure that we don't accidentally treat checked errors as handled).
110f7f6d3e9SLang Hames // Test runs in debug mode only.
111f7f6d3e9SLang Hames #ifndef NDEBUG
11201864a2dSLang Hames TEST(Error, UncheckedError) {
113f7f6d3e9SLang Hames   auto DropUnhandledError = []() {
114f7f6d3e9SLang Hames     Error E = make_error<CustomError>(42);
115f7f6d3e9SLang Hames     (void)!E;
116f7f6d3e9SLang Hames   };
117f7f6d3e9SLang Hames   EXPECT_DEATH(DropUnhandledError(),
118f7f6d3e9SLang Hames                "Program aborted due to an unhandled Error:")
119f7f6d3e9SLang Hames       << "Unhandled Error failure value did not cause abort()";
120f7f6d3e9SLang Hames }
121f7f6d3e9SLang Hames #endif
122f7f6d3e9SLang Hames 
12301864a2dSLang Hames // Check 'Error::isA<T>' method handling.
12401864a2dSLang Hames TEST(Error, IsAHandling) {
125f7f6d3e9SLang Hames   // Check 'isA' handling.
126f7f6d3e9SLang Hames   Error E = make_error<CustomError>(1);
127f7f6d3e9SLang Hames   Error F = make_error<CustomSubError>(1, 2);
12801a3cf4dSLang Hames   Error G = Error::success();
129f7f6d3e9SLang Hames 
130f7f6d3e9SLang Hames   EXPECT_TRUE(E.isA<CustomError>());
131f7f6d3e9SLang Hames   EXPECT_FALSE(E.isA<CustomSubError>());
132f7f6d3e9SLang Hames   EXPECT_TRUE(F.isA<CustomError>());
133f7f6d3e9SLang Hames   EXPECT_TRUE(F.isA<CustomSubError>());
13401a3cf4dSLang Hames   EXPECT_FALSE(G.isA<CustomError>());
135f7f6d3e9SLang Hames 
136f7f6d3e9SLang Hames   consumeError(std::move(E));
137f7f6d3e9SLang Hames   consumeError(std::move(F));
13801a3cf4dSLang Hames   consumeError(std::move(G));
139f7f6d3e9SLang Hames }
140f7f6d3e9SLang Hames 
141f7f6d3e9SLang Hames // Check that we can handle a custom error.
14201864a2dSLang Hames TEST(Error, HandleCustomError) {
143f7f6d3e9SLang Hames   int CaughtErrorInfo = 0;
144f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42), [&](const CustomError &CE) {
145f7f6d3e9SLang Hames     CaughtErrorInfo = CE.getInfo();
146f7f6d3e9SLang Hames   });
147f7f6d3e9SLang Hames 
14801864a2dSLang Hames   EXPECT_TRUE(CaughtErrorInfo == 42) << "Wrong result from CustomError handler";
149f7f6d3e9SLang Hames }
150f7f6d3e9SLang Hames 
151f7f6d3e9SLang Hames // Check that handler type deduction also works for handlers
152f7f6d3e9SLang Hames // of the following types:
153f7f6d3e9SLang Hames // void (const Err&)
154f7f6d3e9SLang Hames // Error (const Err&) mutable
155f7f6d3e9SLang Hames // void (const Err&) mutable
156f7f6d3e9SLang Hames // Error (Err&)
157f7f6d3e9SLang Hames // void (Err&)
158f7f6d3e9SLang Hames // Error (Err&) mutable
159f7f6d3e9SLang Hames // void (Err&) mutable
160f7f6d3e9SLang Hames // Error (unique_ptr<Err>)
161f7f6d3e9SLang Hames // void (unique_ptr<Err>)
162f7f6d3e9SLang Hames // Error (unique_ptr<Err>) mutable
163f7f6d3e9SLang Hames // void (unique_ptr<Err>) mutable
16401864a2dSLang Hames TEST(Error, HandlerTypeDeduction) {
165f7f6d3e9SLang Hames 
166f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42), [](const CustomError &CE) {});
167f7f6d3e9SLang Hames 
168f7f6d3e9SLang Hames   handleAllErrors(
169f7f6d3e9SLang Hames       make_error<CustomError>(42),
170f7f6d3e9SLang Hames       [](const CustomError &CE) mutable { return Error::success(); });
171f7f6d3e9SLang Hames 
172f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42),
173f7f6d3e9SLang Hames                   [](const CustomError &CE) mutable {});
174f7f6d3e9SLang Hames 
175f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42),
176f7f6d3e9SLang Hames                   [](CustomError &CE) { return Error::success(); });
177f7f6d3e9SLang Hames 
178f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) {});
179f7f6d3e9SLang Hames 
180f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42),
181f7f6d3e9SLang Hames                   [](CustomError &CE) mutable { return Error::success(); });
182f7f6d3e9SLang Hames 
183f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42), [](CustomError &CE) mutable {});
184f7f6d3e9SLang Hames 
185f7f6d3e9SLang Hames   handleAllErrors(
186f7f6d3e9SLang Hames       make_error<CustomError>(42),
187f7f6d3e9SLang Hames       [](std::unique_ptr<CustomError> CE) { return Error::success(); });
188f7f6d3e9SLang Hames 
189f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42),
190f7f6d3e9SLang Hames                   [](std::unique_ptr<CustomError> CE) {});
191f7f6d3e9SLang Hames 
192f7f6d3e9SLang Hames   handleAllErrors(
193f7f6d3e9SLang Hames       make_error<CustomError>(42),
194f7f6d3e9SLang Hames       [](std::unique_ptr<CustomError> CE) mutable { return Error::success(); });
195f7f6d3e9SLang Hames 
196f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42),
197f7f6d3e9SLang Hames                   [](std::unique_ptr<CustomError> CE) mutable {});
198f7f6d3e9SLang Hames 
199f7f6d3e9SLang Hames   // Check that named handlers of type 'Error (const Err&)' work.
200f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42), handleCustomError);
201f7f6d3e9SLang Hames 
202f7f6d3e9SLang Hames   // Check that named handlers of type 'void (const Err&)' work.
203f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42), handleCustomErrorVoid);
204f7f6d3e9SLang Hames 
205f7f6d3e9SLang Hames   // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work.
206f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42), handleCustomErrorUP);
207f7f6d3e9SLang Hames 
208f7f6d3e9SLang Hames   // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work.
209f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomError>(42), handleCustomErrorUPVoid);
21001864a2dSLang Hames }
211f7f6d3e9SLang Hames 
21201864a2dSLang Hames // Test that we can handle errors with custom base classes.
21301864a2dSLang Hames TEST(Error, HandleCustomErrorWithCustomBaseClass) {
214f7f6d3e9SLang Hames   int CaughtErrorInfo = 0;
215f7f6d3e9SLang Hames   int CaughtErrorExtraInfo = 0;
216f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomSubError>(42, 7),
217f7f6d3e9SLang Hames                   [&](const CustomSubError &SE) {
218f7f6d3e9SLang Hames                     CaughtErrorInfo = SE.getInfo();
219f7f6d3e9SLang Hames                     CaughtErrorExtraInfo = SE.getExtraInfo();
220f7f6d3e9SLang Hames                   });
221f7f6d3e9SLang Hames 
222f7f6d3e9SLang Hames   EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7)
223f7f6d3e9SLang Hames       << "Wrong result from CustomSubError handler";
224f7f6d3e9SLang Hames }
225f7f6d3e9SLang Hames 
226f7f6d3e9SLang Hames // Check that we trigger only the first handler that applies.
22701864a2dSLang Hames TEST(Error, FirstHandlerOnly) {
228f7f6d3e9SLang Hames   int DummyInfo = 0;
229f7f6d3e9SLang Hames   int CaughtErrorInfo = 0;
230f7f6d3e9SLang Hames   int CaughtErrorExtraInfo = 0;
231f7f6d3e9SLang Hames 
232f7f6d3e9SLang Hames   handleAllErrors(make_error<CustomSubError>(42, 7),
233f7f6d3e9SLang Hames                   [&](const CustomSubError &SE) {
234f7f6d3e9SLang Hames                     CaughtErrorInfo = SE.getInfo();
235f7f6d3e9SLang Hames                     CaughtErrorExtraInfo = SE.getExtraInfo();
236f7f6d3e9SLang Hames                   },
237f7f6d3e9SLang Hames                   [&](const CustomError &CE) { DummyInfo = CE.getInfo(); });
238f7f6d3e9SLang Hames 
239f7f6d3e9SLang Hames   EXPECT_TRUE(CaughtErrorInfo == 42 && CaughtErrorExtraInfo == 7 &&
240f7f6d3e9SLang Hames               DummyInfo == 0)
241f7f6d3e9SLang Hames       << "Activated the wrong Error handler(s)";
242f7f6d3e9SLang Hames }
243f7f6d3e9SLang Hames 
244f7f6d3e9SLang Hames // Check that general handlers shadow specific ones.
24501864a2dSLang Hames TEST(Error, HandlerShadowing) {
246f7f6d3e9SLang Hames   int CaughtErrorInfo = 0;
247f7f6d3e9SLang Hames   int DummyInfo = 0;
248f7f6d3e9SLang Hames   int DummyExtraInfo = 0;
249f7f6d3e9SLang Hames 
250f7f6d3e9SLang Hames   handleAllErrors(
251f7f6d3e9SLang Hames       make_error<CustomSubError>(42, 7),
252f7f6d3e9SLang Hames       [&](const CustomError &CE) { CaughtErrorInfo = CE.getInfo(); },
253f7f6d3e9SLang Hames       [&](const CustomSubError &SE) {
254f7f6d3e9SLang Hames         DummyInfo = SE.getInfo();
255f7f6d3e9SLang Hames         DummyExtraInfo = SE.getExtraInfo();
256f7f6d3e9SLang Hames       });
257f7f6d3e9SLang Hames 
258b2cef64bSNAKAMURA Takumi   EXPECT_TRUE(CaughtErrorInfo == 42 && DummyInfo == 0 && DummyExtraInfo == 0)
259f7f6d3e9SLang Hames       << "General Error handler did not shadow specific handler";
260f7f6d3e9SLang Hames }
261f7f6d3e9SLang Hames 
26201864a2dSLang Hames // Test joinErrors.
26301864a2dSLang Hames TEST(Error, CheckJoinErrors) {
264f7f6d3e9SLang Hames   int CustomErrorInfo1 = 0;
265f7f6d3e9SLang Hames   int CustomErrorInfo2 = 0;
266f7f6d3e9SLang Hames   int CustomErrorExtraInfo = 0;
26701864a2dSLang Hames   Error E =
26801864a2dSLang Hames       joinErrors(make_error<CustomError>(7), make_error<CustomSubError>(42, 7));
269f7f6d3e9SLang Hames 
270f7f6d3e9SLang Hames   handleAllErrors(std::move(E),
271f7f6d3e9SLang Hames                   [&](const CustomSubError &SE) {
272f7f6d3e9SLang Hames                     CustomErrorInfo2 = SE.getInfo();
273f7f6d3e9SLang Hames                     CustomErrorExtraInfo = SE.getExtraInfo();
274f7f6d3e9SLang Hames                   },
275f7f6d3e9SLang Hames                   [&](const CustomError &CE) {
276f7f6d3e9SLang Hames                     // Assert that the CustomError instance above is handled
277f7f6d3e9SLang Hames                     // before the
278f7f6d3e9SLang Hames                     // CustomSubError - joinErrors should preserve error
279f7f6d3e9SLang Hames                     // ordering.
280f7f6d3e9SLang Hames                     EXPECT_EQ(CustomErrorInfo2, 0)
281f7f6d3e9SLang Hames                         << "CustomErrorInfo2 should be 0 here. "
282f7f6d3e9SLang Hames                            "joinErrors failed to preserve ordering.\n";
283f7f6d3e9SLang Hames                     CustomErrorInfo1 = CE.getInfo();
284f7f6d3e9SLang Hames                   });
285f7f6d3e9SLang Hames 
286f7f6d3e9SLang Hames   EXPECT_TRUE(CustomErrorInfo1 == 7 && CustomErrorInfo2 == 42 &&
287f7f6d3e9SLang Hames               CustomErrorExtraInfo == 7)
288f7f6d3e9SLang Hames       << "Failed handling compound Error.";
289f7f6d3e9SLang Hames }
290f7f6d3e9SLang Hames 
29101864a2dSLang Hames // Test that we can consume success values.
29201864a2dSLang Hames TEST(Error, ConsumeSuccess) {
293f7f6d3e9SLang Hames   Error E;
294f7f6d3e9SLang Hames   consumeError(std::move(E));
295f7f6d3e9SLang Hames }
29601864a2dSLang Hames 
29701864a2dSLang Hames TEST(Error, ConsumeError) {
298f7f6d3e9SLang Hames   Error E = make_error<CustomError>(7);
299f7f6d3e9SLang Hames   consumeError(std::move(E));
300f7f6d3e9SLang Hames }
301f7f6d3e9SLang Hames 
302f7f6d3e9SLang Hames // Test that handleAllUnhandledErrors crashes if an error is not caught.
303f7f6d3e9SLang Hames // Test runs in debug mode only.
304f7f6d3e9SLang Hames #ifndef NDEBUG
30501864a2dSLang Hames TEST(Error, FailureToHandle) {
306f7f6d3e9SLang Hames   auto FailToHandle = []() {
30701864a2dSLang Hames     handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) {
308f7f6d3e9SLang Hames       errs() << "This should never be called";
309f7f6d3e9SLang Hames       exit(1);
310f7f6d3e9SLang Hames     });
311f7f6d3e9SLang Hames   };
312f7f6d3e9SLang Hames 
313f7f6d3e9SLang Hames   EXPECT_DEATH(FailToHandle(), "Program aborted due to an unhandled Error:")
314f7f6d3e9SLang Hames       << "Unhandled Error in handleAllErrors call did not cause an "
315f7f6d3e9SLang Hames          "abort()";
316f7f6d3e9SLang Hames }
317f7f6d3e9SLang Hames #endif
318f7f6d3e9SLang Hames 
319f7f6d3e9SLang Hames // Test that handleAllUnhandledErrors crashes if an error is returned from a
320f7f6d3e9SLang Hames // handler.
321f7f6d3e9SLang Hames // Test runs in debug mode only.
322f7f6d3e9SLang Hames #ifndef NDEBUG
32301864a2dSLang Hames TEST(Error, FailureFromHandler) {
324f7f6d3e9SLang Hames   auto ReturnErrorFromHandler = []() {
325f7f6d3e9SLang Hames     handleAllErrors(make_error<CustomError>(7),
326f7f6d3e9SLang Hames                     [&](std::unique_ptr<CustomSubError> SE) {
327f7f6d3e9SLang Hames                       return Error(std::move(SE));
328f7f6d3e9SLang Hames                     });
329f7f6d3e9SLang Hames   };
330f7f6d3e9SLang Hames 
331f7f6d3e9SLang Hames   EXPECT_DEATH(ReturnErrorFromHandler(),
332f7f6d3e9SLang Hames                "Program aborted due to an unhandled Error:")
333f7f6d3e9SLang Hames       << " Error returned from handler in handleAllErrors call did not "
334f7f6d3e9SLang Hames          "cause abort()";
335f7f6d3e9SLang Hames }
336f7f6d3e9SLang Hames #endif
337f7f6d3e9SLang Hames 
338f7f6d3e9SLang Hames // Test that we can return values from handleErrors.
33901864a2dSLang Hames TEST(Error, CatchErrorFromHandler) {
340f7f6d3e9SLang Hames   int ErrorInfo = 0;
341f7f6d3e9SLang Hames 
342f7f6d3e9SLang Hames   Error E = handleErrors(
343f7f6d3e9SLang Hames       make_error<CustomError>(7),
344f7f6d3e9SLang Hames       [&](std::unique_ptr<CustomError> CE) { return Error(std::move(CE)); });
345f7f6d3e9SLang Hames 
346f7f6d3e9SLang Hames   handleAllErrors(std::move(E),
347f7f6d3e9SLang Hames                   [&](const CustomError &CE) { ErrorInfo = CE.getInfo(); });
348f7f6d3e9SLang Hames 
349f7f6d3e9SLang Hames   EXPECT_EQ(ErrorInfo, 7)
350f7f6d3e9SLang Hames       << "Failed to handle Error returned from handleErrors.";
351f7f6d3e9SLang Hames }
3526935c2d3SLang Hames 
35301864a2dSLang Hames // Test that the ExitOnError utility works as expected.
35401864a2dSLang Hames TEST(Error, ExitOnError) {
3556935c2d3SLang Hames   ExitOnError ExitOnErr;
3566935c2d3SLang Hames   ExitOnErr.setBanner("Error in tool:");
35701864a2dSLang Hames   ExitOnErr.setExitCodeMapper([](const Error &E) {
3586935c2d3SLang Hames     if (E.isA<CustomSubError>())
3596935c2d3SLang Hames       return 2;
3606935c2d3SLang Hames     return 1;
3616935c2d3SLang Hames   });
3626935c2d3SLang Hames 
3636935c2d3SLang Hames   // Make sure we don't bail on success.
3646935c2d3SLang Hames   ExitOnErr(Error::success());
3656935c2d3SLang Hames   EXPECT_EQ(ExitOnErr(Expected<int>(7)), 7)
3666935c2d3SLang Hames       << "exitOnError returned an invalid value for Expected";
3676935c2d3SLang Hames 
3686935c2d3SLang Hames   // Exit tests.
3696935c2d3SLang Hames   EXPECT_EXIT(ExitOnErr(make_error<CustomError>(7)),
3706935c2d3SLang Hames               ::testing::ExitedWithCode(1), "Error in tool:")
3716935c2d3SLang Hames       << "exitOnError returned an unexpected error result";
3726935c2d3SLang Hames 
3736935c2d3SLang Hames   EXPECT_EXIT(ExitOnErr(Expected<int>(make_error<CustomSubError>(0, 0))),
3746935c2d3SLang Hames               ::testing::ExitedWithCode(2), "Error in tool:")
3756935c2d3SLang Hames       << "exitOnError returned an unexpected error result";
3766935c2d3SLang Hames }
377f7f6d3e9SLang Hames 
37801864a2dSLang Hames // Test Expected<T> in success mode.
37901864a2dSLang Hames TEST(Error, ExpectedInSuccessMode) {
380f7f6d3e9SLang Hames   Expected<int> A = 7;
38101864a2dSLang Hames   EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'";
382f7f6d3e9SLang Hames   EXPECT_EQ(*A, 7) << "Incorrect Expected non-error value";
383f7f6d3e9SLang Hames }
384f7f6d3e9SLang Hames 
38501864a2dSLang Hames // Test Expected<T> in failure mode.
38601864a2dSLang Hames TEST(Error, ExpectedInFailureMode) {
387f7f6d3e9SLang Hames   Expected<int> A = make_error<CustomError>(42);
388f7f6d3e9SLang Hames   EXPECT_FALSE(!!A) << "Expected with error value doesn't convert to 'false'";
389f7f6d3e9SLang Hames   Error E = A.takeError();
390f7f6d3e9SLang Hames   EXPECT_TRUE(E.isA<CustomError>()) << "Incorrect Expected error value";
391f7f6d3e9SLang Hames   consumeError(std::move(E));
392f7f6d3e9SLang Hames }
393f7f6d3e9SLang Hames 
394f7f6d3e9SLang Hames // Check that an Expected instance with an error value doesn't allow access to
395f7f6d3e9SLang Hames // operator*.
396f7f6d3e9SLang Hames // Test runs in debug mode only.
397f7f6d3e9SLang Hames #ifndef NDEBUG
39801864a2dSLang Hames TEST(Error, AccessExpectedInFailureMode) {
399f7f6d3e9SLang Hames   Expected<int> A = make_error<CustomError>(42);
4009fb8e1b2SLang Hames   EXPECT_DEATH(*A, "!HasError && \"Cannot get value when an error exists!\"")
401f7f6d3e9SLang Hames       << "Incorrect Expected error value";
402f7f6d3e9SLang Hames   consumeError(A.takeError());
403f7f6d3e9SLang Hames }
404f7f6d3e9SLang Hames #endif
405f7f6d3e9SLang Hames 
406f7f6d3e9SLang Hames // Check that an Expected instance with an error triggers an abort if
407f7f6d3e9SLang Hames // unhandled.
408f7f6d3e9SLang Hames // Test runs in debug mode only.
409f7f6d3e9SLang Hames #ifndef NDEBUG
41001864a2dSLang Hames TEST(Error, UnhandledExpectedInFailureMode) {
411f7f6d3e9SLang Hames   EXPECT_DEATH({ Expected<int> A = make_error<CustomError>(42); },
412f7f6d3e9SLang Hames                "Program aborted due to an unhandled Error:")
413f7f6d3e9SLang Hames       << "Unchecked Expected<T> failure value did not cause an abort()";
41401864a2dSLang Hames }
415f7f6d3e9SLang Hames #endif
416f7f6d3e9SLang Hames 
417f7f6d3e9SLang Hames // Test covariance of Expected.
41801864a2dSLang Hames TEST(Error, ExpectedCovariance) {
419f7f6d3e9SLang Hames   class B {};
420f7f6d3e9SLang Hames   class D : public B {};
421f7f6d3e9SLang Hames 
422f7f6d3e9SLang Hames   Expected<B *> A1(Expected<D *>(nullptr));
423f7f6d3e9SLang Hames   A1 = Expected<D *>(nullptr);
424f7f6d3e9SLang Hames 
425f7f6d3e9SLang Hames   Expected<std::unique_ptr<B>> A2(Expected<std::unique_ptr<D>>(nullptr));
426f7f6d3e9SLang Hames   A2 = Expected<std::unique_ptr<D>>(nullptr);
427f7f6d3e9SLang Hames }
428f7f6d3e9SLang Hames 
429d21a535bSLang Hames TEST(Error, ErrorCodeConversions) {
430f7f6d3e9SLang Hames   // Round-trip a success value to check that it converts correctly.
431f7f6d3e9SLang Hames   EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())),
432f7f6d3e9SLang Hames             std::error_code())
433f7f6d3e9SLang Hames       << "std::error_code() should round-trip via Error conversions";
434f7f6d3e9SLang Hames 
435f7f6d3e9SLang Hames   // Round-trip an error value to check that it converts correctly.
436f7f6d3e9SLang Hames   EXPECT_EQ(errorToErrorCode(errorCodeToError(errc::invalid_argument)),
437f7f6d3e9SLang Hames             errc::invalid_argument)
438f7f6d3e9SLang Hames       << "std::error_code error value should round-trip via Error "
439f7f6d3e9SLang Hames          "conversions";
440d21a535bSLang Hames 
441d21a535bSLang Hames   // Round-trip a success value through ErrorOr/Expected to check that it
442d21a535bSLang Hames   // converts correctly.
443d21a535bSLang Hames   {
444d21a535bSLang Hames     auto Orig = ErrorOr<int>(42);
445d21a535bSLang Hames     auto RoundTripped =
446d21a535bSLang Hames       expectedToErrorOr(errorOrToExpected(ErrorOr<int>(42)));
447d21a535bSLang Hames     EXPECT_EQ(*Orig, *RoundTripped)
448d21a535bSLang Hames       << "ErrorOr<T> success value should round-trip via Expected<T> "
449d21a535bSLang Hames          "conversions.";
450d21a535bSLang Hames   }
451d21a535bSLang Hames 
452d21a535bSLang Hames   // Round-trip a failure value through ErrorOr/Expected to check that it
453d21a535bSLang Hames   // converts correctly.
454d21a535bSLang Hames   {
455d21a535bSLang Hames     auto Orig = ErrorOr<int>(errc::invalid_argument);
456d21a535bSLang Hames     auto RoundTripped =
457d21a535bSLang Hames       expectedToErrorOr(
458d21a535bSLang Hames           errorOrToExpected(ErrorOr<int>(errc::invalid_argument)));
459d21a535bSLang Hames     EXPECT_EQ(Orig.getError(), RoundTripped.getError())
460d21a535bSLang Hames       << "ErrorOr<T> failure value should round-trip via Expected<T> "
461d21a535bSLang Hames          "conversions.";
462d21a535bSLang Hames   }
463f7f6d3e9SLang Hames }
464f7f6d3e9SLang Hames 
465f7f6d3e9SLang Hames } // end anon namespace
466