1 //===----- unittests/ErrorTest.cpp - Error.h tests ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Error.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/Errc.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Testing/Support/Error.h"
16 #include "gtest/gtest-spi.h"
17 #include "gtest/gtest.h"
24 // Custom error class with a default base class and some random 'info' attached.
25 class CustomError
: public ErrorInfo
<CustomError
> {
27 // Create an error with some info attached.
28 CustomError(int Info
) : Info(Info
) {}
30 // Get the info attached to this error.
31 int getInfo() const { return Info
; }
33 // Log this error to a stream.
34 void log(raw_ostream
&OS
) const override
{
35 OS
<< "CustomError { " << getInfo() << "}";
38 std::error_code
convertToErrorCode() const override
{
39 llvm_unreachable("CustomError doesn't support ECError conversion");
42 // Used by ErrorInfo::classID.
46 // This error is subclassed below, but we can't use inheriting constructors
47 // yet, so we can't propagate the constructors through ErrorInfo. Instead
48 // we have to have a default constructor and have the subclass initialize all
50 CustomError() : Info(0) {}
55 char CustomError::ID
= 0;
57 // Custom error class with a custom base class and some additional random
59 class CustomSubError
: public ErrorInfo
<CustomSubError
, CustomError
> {
61 // Create a sub-error with some info attached.
62 CustomSubError(int Info
, int ExtraInfo
) : ExtraInfo(ExtraInfo
) {
66 // Get the extra info attached to this error.
67 int getExtraInfo() const { return ExtraInfo
; }
69 // Log this error to a stream.
70 void log(raw_ostream
&OS
) const override
{
71 OS
<< "CustomSubError { " << getInfo() << ", " << getExtraInfo() << "}";
74 std::error_code
convertToErrorCode() const override
{
75 llvm_unreachable("CustomSubError doesn't support ECError conversion");
78 // Used by ErrorInfo::classID.
85 char CustomSubError::ID
= 0;
87 static Error
handleCustomError(const CustomError
&CE
) {
88 return Error::success();
91 static void handleCustomErrorVoid(const CustomError
&CE
) {}
93 static Error
handleCustomErrorUP(std::unique_ptr
<CustomError
> CE
) {
94 return Error::success();
97 static void handleCustomErrorUPVoid(std::unique_ptr
<CustomError
> CE
) {}
99 // Test that success values implicitly convert to false, and don't cause crashes
100 // once they've been implicitly converted.
101 TEST(Error
, CheckedSuccess
) {
102 Error E
= Error::success();
103 EXPECT_FALSE(E
) << "Unexpected error while testing Error 'Success'";
106 // Test that unchecked succes values cause an abort.
107 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
108 TEST(Error
, UncheckedSuccess
) {
109 EXPECT_DEATH({ Error E
= Error::success(); },
110 "Program aborted due to an unhandled Error:")
111 << "Unchecked Error Succes value did not cause abort()";
115 // ErrorAsOutParameter tester.
116 void errAsOutParamHelper(Error
&Err
) {
117 ErrorAsOutParameter
ErrAsOutParam(&Err
);
118 // Verify that checked flag is raised - assignment should not crash.
119 Err
= Error::success();
120 // Raise the checked bit manually - caller should still have to test the
125 // Test that ErrorAsOutParameter sets the checked flag on construction.
126 TEST(Error
, ErrorAsOutParameterChecked
) {
127 Error E
= Error::success();
128 errAsOutParamHelper(E
);
132 // Test that ErrorAsOutParameter clears the checked flag on destruction.
133 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
134 TEST(Error
, ErrorAsOutParameterUnchecked
) {
135 EXPECT_DEATH({ Error E
= Error::success(); errAsOutParamHelper(E
); },
136 "Program aborted due to an unhandled Error:")
137 << "ErrorAsOutParameter did not clear the checked flag on destruction.";
141 // Check that we abort on unhandled failure cases. (Force conversion to bool
142 // to make sure that we don't accidentally treat checked errors as handled).
143 // Test runs in debug mode only.
144 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
145 TEST(Error
, UncheckedError
) {
146 auto DropUnhandledError
= []() {
147 Error E
= make_error
<CustomError
>(42);
150 EXPECT_DEATH(DropUnhandledError(),
151 "Program aborted due to an unhandled Error:")
152 << "Unhandled Error failure value did not cause abort()";
156 // Check 'Error::isA<T>' method handling.
157 TEST(Error
, IsAHandling
) {
158 // Check 'isA' handling.
159 Error E
= make_error
<CustomError
>(1);
160 Error F
= make_error
<CustomSubError
>(1, 2);
161 Error G
= Error::success();
163 EXPECT_TRUE(E
.isA
<CustomError
>());
164 EXPECT_FALSE(E
.isA
<CustomSubError
>());
165 EXPECT_TRUE(F
.isA
<CustomError
>());
166 EXPECT_TRUE(F
.isA
<CustomSubError
>());
167 EXPECT_FALSE(G
.isA
<CustomError
>());
169 consumeError(std::move(E
));
170 consumeError(std::move(F
));
171 consumeError(std::move(G
));
174 // Check that we can handle a custom error.
175 TEST(Error
, HandleCustomError
) {
176 int CaughtErrorInfo
= 0;
177 handleAllErrors(make_error
<CustomError
>(42), [&](const CustomError
&CE
) {
178 CaughtErrorInfo
= CE
.getInfo();
181 EXPECT_TRUE(CaughtErrorInfo
== 42) << "Wrong result from CustomError handler";
184 // Check that handler type deduction also works for handlers
185 // of the following types:
187 // Error (const Err&) mutable
188 // void (const Err&) mutable
191 // Error (Err&) mutable
192 // void (Err&) mutable
193 // Error (unique_ptr<Err>)
194 // void (unique_ptr<Err>)
195 // Error (unique_ptr<Err>) mutable
196 // void (unique_ptr<Err>) mutable
197 TEST(Error
, HandlerTypeDeduction
) {
199 handleAllErrors(make_error
<CustomError
>(42), [](const CustomError
&CE
) {});
202 make_error
<CustomError
>(42),
203 [](const CustomError
&CE
) mutable -> Error
{ return Error::success(); });
205 handleAllErrors(make_error
<CustomError
>(42),
206 [](const CustomError
&CE
) mutable {});
208 handleAllErrors(make_error
<CustomError
>(42),
209 [](CustomError
&CE
) -> Error
{ return Error::success(); });
211 handleAllErrors(make_error
<CustomError
>(42), [](CustomError
&CE
) {});
213 handleAllErrors(make_error
<CustomError
>(42),
214 [](CustomError
&CE
) mutable -> Error
{ return Error::success(); });
216 handleAllErrors(make_error
<CustomError
>(42), [](CustomError
&CE
) mutable {});
219 make_error
<CustomError
>(42),
220 [](std::unique_ptr
<CustomError
> CE
) -> Error
{ return Error::success(); });
222 handleAllErrors(make_error
<CustomError
>(42),
223 [](std::unique_ptr
<CustomError
> CE
) {});
226 make_error
<CustomError
>(42),
227 [](std::unique_ptr
<CustomError
> CE
) mutable -> Error
{ return Error::success(); });
229 handleAllErrors(make_error
<CustomError
>(42),
230 [](std::unique_ptr
<CustomError
> CE
) mutable {});
232 // Check that named handlers of type 'Error (const Err&)' work.
233 handleAllErrors(make_error
<CustomError
>(42), handleCustomError
);
235 // Check that named handlers of type 'void (const Err&)' work.
236 handleAllErrors(make_error
<CustomError
>(42), handleCustomErrorVoid
);
238 // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work.
239 handleAllErrors(make_error
<CustomError
>(42), handleCustomErrorUP
);
241 // Check that named handlers of type 'Error (std::unique_ptr<Err>)' work.
242 handleAllErrors(make_error
<CustomError
>(42), handleCustomErrorUPVoid
);
245 // Test that we can handle errors with custom base classes.
246 TEST(Error
, HandleCustomErrorWithCustomBaseClass
) {
247 int CaughtErrorInfo
= 0;
248 int CaughtErrorExtraInfo
= 0;
249 handleAllErrors(make_error
<CustomSubError
>(42, 7),
250 [&](const CustomSubError
&SE
) {
251 CaughtErrorInfo
= SE
.getInfo();
252 CaughtErrorExtraInfo
= SE
.getExtraInfo();
255 EXPECT_TRUE(CaughtErrorInfo
== 42 && CaughtErrorExtraInfo
== 7)
256 << "Wrong result from CustomSubError handler";
259 // Check that we trigger only the first handler that applies.
260 TEST(Error
, FirstHandlerOnly
) {
262 int CaughtErrorInfo
= 0;
263 int CaughtErrorExtraInfo
= 0;
265 handleAllErrors(make_error
<CustomSubError
>(42, 7),
266 [&](const CustomSubError
&SE
) {
267 CaughtErrorInfo
= SE
.getInfo();
268 CaughtErrorExtraInfo
= SE
.getExtraInfo();
270 [&](const CustomError
&CE
) { DummyInfo
= CE
.getInfo(); });
272 EXPECT_TRUE(CaughtErrorInfo
== 42 && CaughtErrorExtraInfo
== 7 &&
274 << "Activated the wrong Error handler(s)";
277 // Check that general handlers shadow specific ones.
278 TEST(Error
, HandlerShadowing
) {
279 int CaughtErrorInfo
= 0;
281 int DummyExtraInfo
= 0;
284 make_error
<CustomSubError
>(42, 7),
285 [&](const CustomError
&CE
) { CaughtErrorInfo
= CE
.getInfo(); },
286 [&](const CustomSubError
&SE
) {
287 DummyInfo
= SE
.getInfo();
288 DummyExtraInfo
= SE
.getExtraInfo();
291 EXPECT_TRUE(CaughtErrorInfo
== 42 && DummyInfo
== 0 && DummyExtraInfo
== 0)
292 << "General Error handler did not shadow specific handler";
296 TEST(Error
, CheckJoinErrors
) {
297 int CustomErrorInfo1
= 0;
298 int CustomErrorInfo2
= 0;
299 int CustomErrorExtraInfo
= 0;
301 joinErrors(make_error
<CustomError
>(7), make_error
<CustomSubError
>(42, 7));
303 handleAllErrors(std::move(E
),
304 [&](const CustomSubError
&SE
) {
305 CustomErrorInfo2
= SE
.getInfo();
306 CustomErrorExtraInfo
= SE
.getExtraInfo();
308 [&](const CustomError
&CE
) {
309 // Assert that the CustomError instance above is handled
311 // CustomSubError - joinErrors should preserve error
313 EXPECT_EQ(CustomErrorInfo2
, 0)
314 << "CustomErrorInfo2 should be 0 here. "
315 "joinErrors failed to preserve ordering.\n";
316 CustomErrorInfo1
= CE
.getInfo();
319 EXPECT_TRUE(CustomErrorInfo1
== 7 && CustomErrorInfo2
== 42 &&
320 CustomErrorExtraInfo
== 7)
321 << "Failed handling compound Error.";
323 // Test appending a single item to a list.
328 joinErrors(make_error
<CustomError
>(7),
329 make_error
<CustomError
>(7)),
330 make_error
<CustomError
>(7)),
331 [&](const CustomError
&CE
) {
334 EXPECT_EQ(Sum
, 21) << "Failed to correctly append error to error list.";
337 // Test prepending a single item to a list.
342 make_error
<CustomError
>(7),
343 joinErrors(make_error
<CustomError
>(7),
344 make_error
<CustomError
>(7))),
345 [&](const CustomError
&CE
) {
348 EXPECT_EQ(Sum
, 21) << "Failed to correctly prepend error to error list.";
351 // Test concatenating two error lists.
357 make_error
<CustomError
>(7),
358 make_error
<CustomError
>(7)),
360 make_error
<CustomError
>(7),
361 make_error
<CustomError
>(7))),
362 [&](const CustomError
&CE
) {
365 EXPECT_EQ(Sum
, 28) << "Failed to correctly concatenate error lists.";
369 // Test that we can consume success values.
370 TEST(Error
, ConsumeSuccess
) {
371 Error E
= Error::success();
372 consumeError(std::move(E
));
375 TEST(Error
, ConsumeError
) {
376 Error E
= make_error
<CustomError
>(7);
377 consumeError(std::move(E
));
380 // Test that handleAllUnhandledErrors crashes if an error is not caught.
381 // Test runs in debug mode only.
382 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
383 TEST(Error
, FailureToHandle
) {
384 auto FailToHandle
= []() {
385 handleAllErrors(make_error
<CustomError
>(7), [&](const CustomSubError
&SE
) {
386 errs() << "This should never be called";
391 EXPECT_DEATH(FailToHandle(),
392 "Failure value returned from cantFail wrapped call")
393 << "Unhandled Error in handleAllErrors call did not cause an "
398 // Test that handleAllUnhandledErrors crashes if an error is returned from a
400 // Test runs in debug mode only.
401 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
402 TEST(Error
, FailureFromHandler
) {
403 auto ReturnErrorFromHandler
= []() {
404 handleAllErrors(make_error
<CustomError
>(7),
405 [&](std::unique_ptr
<CustomSubError
> SE
) {
406 return Error(std::move(SE
));
410 EXPECT_DEATH(ReturnErrorFromHandler(),
411 "Failure value returned from cantFail wrapped call")
412 << " Error returned from handler in handleAllErrors call did not "
417 // Test that we can return values from handleErrors.
418 TEST(Error
, CatchErrorFromHandler
) {
421 Error E
= handleErrors(
422 make_error
<CustomError
>(7),
423 [&](std::unique_ptr
<CustomError
> CE
) { return Error(std::move(CE
)); });
425 handleAllErrors(std::move(E
),
426 [&](const CustomError
&CE
) { ErrorInfo
= CE
.getInfo(); });
428 EXPECT_EQ(ErrorInfo
, 7)
429 << "Failed to handle Error returned from handleErrors.";
432 TEST(Error
, StringError
) {
434 raw_string_ostream
S(Msg
);
435 logAllUnhandledErrors(make_error
<StringError
>("foo" + Twine(42),
436 inconvertibleErrorCode()),
438 EXPECT_EQ(S
.str(), "foo42\n") << "Unexpected StringError log result";
441 errorToErrorCode(make_error
<StringError
>("", errc::invalid_argument
));
442 EXPECT_EQ(EC
, errc::invalid_argument
)
443 << "Failed to convert StringError to error_code.";
446 // Test that the ExitOnError utility works as expected.
447 TEST(Error
, ExitOnError
) {
448 ExitOnError ExitOnErr
;
449 ExitOnErr
.setBanner("Error in tool:");
450 ExitOnErr
.setExitCodeMapper([](const Error
&E
) {
451 if (E
.isA
<CustomSubError
>())
456 // Make sure we don't bail on success.
457 ExitOnErr(Error::success());
458 EXPECT_EQ(ExitOnErr(Expected
<int>(7)), 7)
459 << "exitOnError returned an invalid value for Expected";
462 int &B
= ExitOnErr(Expected
<int&>(A
));
463 EXPECT_EQ(&A
, &B
) << "ExitOnError failed to propagate reference";
466 EXPECT_EXIT(ExitOnErr(make_error
<CustomError
>(7)),
467 ::testing::ExitedWithCode(1), "Error in tool:")
468 << "exitOnError returned an unexpected error result";
470 EXPECT_EXIT(ExitOnErr(Expected
<int>(make_error
<CustomSubError
>(0, 0))),
471 ::testing::ExitedWithCode(2), "Error in tool:")
472 << "exitOnError returned an unexpected error result";
475 // Test that the ExitOnError utility works as expected.
476 TEST(Error
, CantFailSuccess
) {
477 cantFail(Error::success());
479 int X
= cantFail(Expected
<int>(42));
480 EXPECT_EQ(X
, 42) << "Expected value modified by cantFail";
483 int &Y
= cantFail(Expected
<int&>(Dummy
));
484 EXPECT_EQ(&Dummy
, &Y
) << "Reference mangled by cantFail";
487 // Test that cantFail results in a crash if you pass it a failure value.
488 #if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG)
489 TEST(Error
, CantFailDeath
) {
491 cantFail(make_error
<StringError
>("foo", inconvertibleErrorCode()),
492 "Cantfail call failed"),
493 "Cantfail call failed")
494 << "cantFail(Error) did not cause an abort for failure value";
498 auto IEC
= inconvertibleErrorCode();
499 int X
= cantFail(Expected
<int>(make_error
<StringError
>("foo", IEC
)));
502 "Failure value returned from cantFail wrapped call")
503 << "cantFail(Expected<int>) did not cause an abort for failure value";
508 // Test Checked Expected<T> in success mode.
509 TEST(Error
, CheckedExpectedInSuccessMode
) {
511 EXPECT_TRUE(!!A
) << "Expected with non-error value doesn't convert to 'true'";
512 // Access is safe in second test, since we checked the error in the first.
513 EXPECT_EQ(*A
, 7) << "Incorrect Expected non-error value";
516 // Test Expected with reference type.
517 TEST(Error
, ExpectedWithReferenceType
) {
519 Expected
<int&> B
= A
;
523 EXPECT_EQ(&A
, &C
) << "Expected failed to propagate reference";
526 // Test Unchecked Expected<T> in success mode.
527 // We expect this to blow up the same way Error would.
528 // Test runs in debug mode only.
529 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
530 TEST(Error
, UncheckedExpectedInSuccessModeDestruction
) {
531 EXPECT_DEATH({ Expected
<int> A
= 7; },
532 "Expected<T> must be checked before access or destruction.")
533 << "Unchecekd Expected<T> success value did not cause an abort().";
537 // Test Unchecked Expected<T> in success mode.
538 // We expect this to blow up the same way Error would.
539 // Test runs in debug mode only.
540 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
541 TEST(Error
, UncheckedExpectedInSuccessModeAccess
) {
542 EXPECT_DEATH({ Expected
<int> A
= 7; *A
; },
543 "Expected<T> must be checked before access or destruction.")
544 << "Unchecekd Expected<T> success value did not cause an abort().";
548 // Test Unchecked Expected<T> in success mode.
549 // We expect this to blow up the same way Error would.
550 // Test runs in debug mode only.
551 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
552 TEST(Error
, UncheckedExpectedInSuccessModeAssignment
) {
553 EXPECT_DEATH({ Expected
<int> A
= 7; A
= 7; },
554 "Expected<T> must be checked before access or destruction.")
555 << "Unchecekd Expected<T> success value did not cause an abort().";
559 // Test Expected<T> in failure mode.
560 TEST(Error
, ExpectedInFailureMode
) {
561 Expected
<int> A
= make_error
<CustomError
>(42);
562 EXPECT_FALSE(!!A
) << "Expected with error value doesn't convert to 'false'";
563 Error E
= A
.takeError();
564 EXPECT_TRUE(E
.isA
<CustomError
>()) << "Incorrect Expected error value";
565 consumeError(std::move(E
));
568 // Check that an Expected instance with an error value doesn't allow access to
570 // Test runs in debug mode only.
571 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
572 TEST(Error
, AccessExpectedInFailureMode
) {
573 Expected
<int> A
= make_error
<CustomError
>(42);
574 EXPECT_DEATH(*A
, "Expected<T> must be checked before access or destruction.")
575 << "Incorrect Expected error value";
576 consumeError(A
.takeError());
580 // Check that an Expected instance with an error triggers an abort if
582 // Test runs in debug mode only.
583 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
584 TEST(Error
, UnhandledExpectedInFailureMode
) {
585 EXPECT_DEATH({ Expected
<int> A
= make_error
<CustomError
>(42); },
586 "Expected<T> must be checked before access or destruction.")
587 << "Unchecked Expected<T> failure value did not cause an abort()";
591 // Test covariance of Expected.
592 TEST(Error
, ExpectedCovariance
) {
594 class D
: public B
{};
596 Expected
<B
*> A1(Expected
<D
*>(nullptr));
597 // Check A1 by converting to bool before assigning to it.
599 A1
= Expected
<D
*>(nullptr);
600 // Check A1 again before destruction.
603 Expected
<std::unique_ptr
<B
>> A2(Expected
<std::unique_ptr
<D
>>(nullptr));
604 // Check A2 by converting to bool before assigning to it.
606 A2
= Expected
<std::unique_ptr
<D
>>(nullptr);
607 // Check A2 again before destruction.
611 // Test that handleExpected just returns success values.
612 TEST(Error
, HandleExpectedSuccess
) {
614 handleExpected(Expected
<int>(42),
615 []() { return Expected
<int>(43); });
616 EXPECT_TRUE(!!ValOrErr
)
617 << "handleExpected should have returned a success value here";
618 EXPECT_EQ(*ValOrErr
, 42)
619 << "handleExpected should have returned the original success value here";
622 enum FooStrategy
{ Aggressive
, Conservative
};
624 static Expected
<int> foo(FooStrategy S
) {
626 return make_error
<CustomError
>(7);
630 // Test that handleExpected invokes the error path if errors are not handled.
631 TEST(Error
, HandleExpectedUnhandledError
) {
632 // foo(Aggressive) should return a CustomError which should pass through as
633 // there is no handler for CustomError.
637 []() { return foo(Conservative
); });
639 EXPECT_FALSE(!!ValOrErr
)
640 << "handleExpected should have returned an error here";
641 auto Err
= ValOrErr
.takeError();
642 EXPECT_TRUE(Err
.isA
<CustomError
>())
643 << "handleExpected should have returned the CustomError generated by "
644 "foo(Aggressive) here";
645 consumeError(std::move(Err
));
648 // Test that handleExpected invokes the fallback path if errors are handled.
649 TEST(Error
, HandleExpectedHandledError
) {
650 // foo(Aggressive) should return a CustomError which should handle triggering
651 // the fallback path.
655 []() { return foo(Conservative
); },
656 [](const CustomError
&) { /* do nothing */ });
658 EXPECT_TRUE(!!ValOrErr
)
659 << "handleExpected should have returned a success value here";
660 EXPECT_EQ(*ValOrErr
, 42)
661 << "handleExpected returned the wrong success value";
664 TEST(Error
, ErrorCodeConversions
) {
665 // Round-trip a success value to check that it converts correctly.
666 EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())),
668 << "std::error_code() should round-trip via Error conversions";
670 // Round-trip an error value to check that it converts correctly.
671 EXPECT_EQ(errorToErrorCode(errorCodeToError(errc::invalid_argument
)),
672 errc::invalid_argument
)
673 << "std::error_code error value should round-trip via Error "
676 // Round-trip a success value through ErrorOr/Expected to check that it
677 // converts correctly.
679 auto Orig
= ErrorOr
<int>(42);
681 expectedToErrorOr(errorOrToExpected(ErrorOr
<int>(42)));
682 EXPECT_EQ(*Orig
, *RoundTripped
)
683 << "ErrorOr<T> success value should round-trip via Expected<T> "
687 // Round-trip a failure value through ErrorOr/Expected to check that it
688 // converts correctly.
690 auto Orig
= ErrorOr
<int>(errc::invalid_argument
);
693 errorOrToExpected(ErrorOr
<int>(errc::invalid_argument
)));
694 EXPECT_EQ(Orig
.getError(), RoundTripped
.getError())
695 << "ErrorOr<T> failure value should round-trip via Expected<T> "
700 // Test that error messages work.
701 TEST(Error
, ErrorMessage
) {
702 EXPECT_EQ(toString(Error::success()).compare(""), 0);
704 Error E1
= make_error
<CustomError
>(0);
705 EXPECT_EQ(toString(std::move(E1
)).compare("CustomError { 0}"), 0);
707 Error E2
= make_error
<CustomError
>(0);
708 handleAllErrors(std::move(E2
), [](const CustomError
&CE
) {
709 EXPECT_EQ(CE
.message().compare("CustomError { 0}"), 0);
712 Error E3
= joinErrors(make_error
<CustomError
>(0), make_error
<CustomError
>(1));
713 EXPECT_EQ(toString(std::move(E3
))
714 .compare("CustomError { 0}\n"
719 TEST(Error
, ErrorMatchers
) {
720 EXPECT_THAT_ERROR(Error::success(), Succeeded());
721 EXPECT_NONFATAL_FAILURE(
722 EXPECT_THAT_ERROR(make_error
<CustomError
>(0), Succeeded()),
723 "Expected: succeeded\n Actual: failed (CustomError { 0})");
725 EXPECT_THAT_ERROR(make_error
<CustomError
>(0), Failed());
726 EXPECT_NONFATAL_FAILURE(EXPECT_THAT_ERROR(Error::success(), Failed()),
727 "Expected: failed\n Actual: succeeded");
729 EXPECT_THAT_ERROR(make_error
<CustomError
>(0), Failed
<CustomError
>());
730 EXPECT_NONFATAL_FAILURE(
731 EXPECT_THAT_ERROR(Error::success(), Failed
<CustomError
>()),
732 "Expected: failed with Error of given type\n Actual: succeeded");
733 EXPECT_NONFATAL_FAILURE(
734 EXPECT_THAT_ERROR(make_error
<CustomError
>(0), Failed
<CustomSubError
>()),
735 "Error was not of given type");
736 EXPECT_NONFATAL_FAILURE(
738 joinErrors(make_error
<CustomError
>(0), make_error
<CustomError
>(1)),
739 Failed
<CustomError
>()),
743 make_error
<CustomError
>(0),
744 Failed
<CustomError
>(testing::Property(&CustomError::getInfo
, 0)));
745 EXPECT_NONFATAL_FAILURE(
747 make_error
<CustomError
>(0),
748 Failed
<CustomError
>(testing::Property(&CustomError::getInfo
, 1))),
749 "Expected: failed with Error of given type and the error is an object "
750 "whose given property is equal to 1\n"
751 " Actual: failed (CustomError { 0})");
752 EXPECT_THAT_ERROR(make_error
<CustomError
>(0), Failed
<ErrorInfoBase
>());
754 EXPECT_THAT_EXPECTED(Expected
<int>(0), Succeeded());
755 EXPECT_NONFATAL_FAILURE(
756 EXPECT_THAT_EXPECTED(Expected
<int>(make_error
<CustomError
>(0)),
758 "Expected: succeeded\n Actual: failed (CustomError { 0})");
760 EXPECT_THAT_EXPECTED(Expected
<int>(make_error
<CustomError
>(0)), Failed());
761 EXPECT_NONFATAL_FAILURE(
762 EXPECT_THAT_EXPECTED(Expected
<int>(0), Failed()),
763 "Expected: failed\n Actual: succeeded with value 0");
765 EXPECT_THAT_EXPECTED(Expected
<int>(0), HasValue(0));
766 EXPECT_NONFATAL_FAILURE(
767 EXPECT_THAT_EXPECTED(Expected
<int>(make_error
<CustomError
>(0)),
769 "Expected: succeeded with value (is equal to 0)\n"
770 " Actual: failed (CustomError { 0})");
771 EXPECT_NONFATAL_FAILURE(
772 EXPECT_THAT_EXPECTED(Expected
<int>(1), HasValue(0)),
773 "Expected: succeeded with value (is equal to 0)\n"
774 " Actual: succeeded with value 1, (isn't equal to 0)");
776 EXPECT_THAT_EXPECTED(Expected
<int &>(make_error
<CustomError
>(0)), Failed());
778 EXPECT_THAT_EXPECTED(Expected
<int &>(a
), Succeeded());
779 EXPECT_THAT_EXPECTED(Expected
<int &>(a
), HasValue(testing::Eq(1)));
781 EXPECT_THAT_EXPECTED(Expected
<int>(1), HasValue(testing::Gt(0)));
782 EXPECT_NONFATAL_FAILURE(
783 EXPECT_THAT_EXPECTED(Expected
<int>(0), HasValue(testing::Gt(1))),
784 "Expected: succeeded with value (is > 1)\n"
785 " Actual: succeeded with value 0, (isn't > 1)");
786 EXPECT_NONFATAL_FAILURE(
787 EXPECT_THAT_EXPECTED(Expected
<int>(make_error
<CustomError
>(0)),
788 HasValue(testing::Gt(1))),
789 "Expected: succeeded with value (is > 1)\n"
790 " Actual: failed (CustomError { 0})");
793 } // end anon namespace