|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include <folly/result/try.h> |
| 20 | + |
| 21 | +#if FOLLY_HAS_RESULT |
| 22 | + |
| 23 | +namespace folly { |
| 24 | + |
| 25 | +struct ThrowingMove { |
| 26 | + ThrowingMove(const ThrowingMove&) = default; |
| 27 | + ThrowingMove& operator=(const ThrowingMove&) = default; |
| 28 | + ThrowingMove(ThrowingMove&&) {} |
| 29 | + ThrowingMove& operator=(ThrowingMove&&) { return *this; } |
| 30 | +}; |
| 31 | +struct NothrowMove {}; |
| 32 | + |
| 33 | +static_assert(noexcept(result_to_try(FOLLY_DECLVAL(result<int>)))); |
| 34 | +static_assert(noexcept(result_to_try(FOLLY_DECLVAL(result<NothrowMove>)))); |
| 35 | +static_assert(!noexcept(result_to_try(FOLLY_DECLVAL(result<ThrowingMove>)))); |
| 36 | + |
| 37 | +static_assert(noexcept(try_to_result(FOLLY_DECLVAL(Try<int>)))); |
| 38 | +static_assert(noexcept(try_to_result(FOLLY_DECLVAL(Try<NothrowMove>)))); |
| 39 | +static_assert(!noexcept(try_to_result(FOLLY_DECLVAL(Try<ThrowingMove>)))); |
| 40 | + |
| 41 | +TEST(ResultTry, result_to_try) { |
| 42 | + auto tInt = result_to_try(result<int>{5}); |
| 43 | + static_assert(std::is_same_v<Try<int>, decltype(tInt)>); |
| 44 | + EXPECT_EQ(5, *tInt); |
| 45 | + |
| 46 | + auto tErr = result_to_try( |
| 47 | + result<int>{make_exception_wrapper<std::runtime_error>("foo")}); |
| 48 | + static_assert(std::is_same_v<Try<int>, decltype(tErr)>); |
| 49 | + EXPECT_STREQ("foo", tErr.tryGetExceptionObject<std::runtime_error>()->what()); |
| 50 | + |
| 51 | + // `void` -> `void` is a special case |
| 52 | + auto tVoid = result_to_try(result<void>{}); |
| 53 | + static_assert(std::is_same_v<Try<void>, decltype(tVoid)>); |
| 54 | + EXPECT_TRUE(tVoid.hasValue()); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(ResultTry, nonempty_try_to_result) { |
| 58 | + auto rInt = try_to_result(Try<int>{5}); |
| 59 | + static_assert(std::is_same_v<result<int>, decltype(rInt)>); |
| 60 | + EXPECT_EQ(5, rInt.value_or_throw()); |
| 61 | + |
| 62 | + auto rErr = try_to_result( |
| 63 | + Try<int>{make_exception_wrapper<std::runtime_error>("foo")}); |
| 64 | + static_assert(std::is_same_v<result<int>, decltype(rErr)>); |
| 65 | + EXPECT_STREQ("foo", get_exception<std::runtime_error>(rErr)->what()); |
| 66 | + |
| 67 | + // `void` -> `void` is a special case |
| 68 | + auto rVoid = try_to_result(Try<void>{}); |
| 69 | + static_assert(std::is_same_v<result<void>, decltype(rVoid)>); |
| 70 | + EXPECT_TRUE(rVoid.has_value()); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(ResultTry, empty_try_to_result_error) { |
| 74 | + auto rErr = try_to_result(Try<int>{}); |
| 75 | + static_assert(std::is_same_v<result<int>, decltype(rErr)>); |
| 76 | + EXPECT_NE(nullptr, get_exception<UsingUninitializedTry>(rErr)); |
| 77 | +} |
| 78 | + |
| 79 | +TEST(ResultTry, empty_try_to_result_default) { |
| 80 | + auto rInt = try_to_result(Try<int>{}, empty_try_with{[]() { return 1337; }}); |
| 81 | + static_assert(std::is_same_v<result<int>, decltype(rInt)>); |
| 82 | + EXPECT_EQ(1337, rInt.value_or_throw()); |
| 83 | + |
| 84 | + auto rErr = try_to_result( |
| 85 | + Try<int>{}, empty_try_with{[]() { |
| 86 | + return make_exception_wrapper<std::runtime_error>("baz"); |
| 87 | + }}); |
| 88 | + static_assert(std::is_same_v<result<int>, decltype(rErr)>); |
| 89 | + EXPECT_STREQ("baz", get_exception<std::runtime_error>(rErr)->what()); |
| 90 | +} |
| 91 | + |
| 92 | +} // namespace folly |
| 93 | + |
| 94 | +#endif // FOLLY_HAS_RESULT |
0 commit comments