[llvm] Unittest for Error forwarding, follow-up to PR #92208 (PR #92314)

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Wed May 15 13:43:52 PDT 2024


https://github.com/mtrofin created https://github.com/llvm/llvm-project/pull/92314

Added a unittest demonstrating Error -> Expected<T> forwarding.

>From f8de7d27ee42811253c14c72071987098b65069d Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Wed, 15 May 2024 13:41:52 -0700
Subject: [PATCH] Unittest for Error forwarding, follow-up to PR #92208

Added a unittest demonstrating Error -> Expected<T>
forwarding.
---
 llvm/unittests/Support/ErrorTest.cpp | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/llvm/unittests/Support/ErrorTest.cpp b/llvm/unittests/Support/ErrorTest.cpp
index 1229282cf0def..5c0179f256d6e 100644
--- a/llvm/unittests/Support/ErrorTest.cpp
+++ b/llvm/unittests/Support/ErrorTest.cpp
@@ -1158,4 +1158,20 @@ TEST(Error, BadAllocFatalErrorHandlersInteraction) {
   remove_bad_alloc_error_handler();
 }
 
+TEST(Error, ForwardToExpected) {
+  auto ErrorReturningFct = [](bool Fail) {
+    return Fail ? make_error<StringError>(llvm::errc::invalid_argument,
+                                          "Some Error")
+                : Error::success();
+  };
+  auto ExpectedReturningFct = [&](bool Fail) -> Expected<int> {
+    auto Err = ErrorReturningFct(Fail);
+    if (Err)
+      return Err;
+    return 42;
+  };
+  std::optional<int> MaybeV;
+  EXPECT_THAT_ERROR(ExpectedReturningFct(true).moveInto(MaybeV), Failed());
+  EXPECT_EQ(*ExpectedReturningFct(false), 42);
+}
 } // namespace



More information about the llvm-commits mailing list