2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Try.h>
19 #include <glog/logging.h>
21 #include <folly/Memory.h>
22 #include <folly/portability/GTest.h>
24 using namespace folly;
39 Try<A> t_a(std::move(a));
43 EXPECT_EQ(5, t_a.value().x());
46 // Make sure we can copy Trys for copyable types
52 // But don't choke on move-only types
54 Try<std::unique_ptr<int>> t;
55 std::vector<Try<std::unique_ptr<int>>> v;
59 TEST(Try, makeTryWith) {
61 return std::make_unique<int>(1);
64 auto result = makeTryWith(func);
65 EXPECT_TRUE(result.hasValue());
66 EXPECT_EQ(*result.value(), 1);
69 TEST(Try, makeTryWithThrow) {
70 auto func = []() -> std::unique_ptr<int> {
71 throw std::runtime_error("Runtime");
74 auto result = makeTryWith(func);
75 EXPECT_TRUE(result.hasException<std::runtime_error>());
78 TEST(Try, makeTryWithVoid) {
83 auto result = makeTryWith(func);
84 EXPECT_TRUE(result.hasValue());
87 TEST(Try, makeTryWithVoidThrow) {
89 throw std::runtime_error("Runtime");
92 auto result = makeTryWith(func);
93 EXPECT_TRUE(result.hasException<std::runtime_error>());
97 static E* get_exception(std::exception_ptr eptr) {
99 std::rethrow_exception(eptr);
107 TEST(Try, tryGetExceptionObject) {
108 auto epexn = std::make_exception_ptr(std::range_error("oops"));
109 auto epnum = std::make_exception_ptr(17);
111 auto exn = CHECK_NOTNULL(get_exception<std::range_error>(epexn));
112 auto num = CHECK_NOTNULL(get_exception<int>(epnum));
115 auto t = Try<bool>(true);
116 EXPECT_EQ(nullptr, t.tryGetExceptionObject());
117 EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
118 EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
122 auto t = Try<bool>(exception_wrapper(epexn, *exn));
123 EXPECT_EQ(exn, t.tryGetExceptionObject());
124 EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
125 EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
129 auto t = Try<bool>(exception_wrapper(epnum, *num));
130 EXPECT_EQ(nullptr, t.tryGetExceptionObject());
131 EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
132 EXPECT_EQ(num, t.tryGetExceptionObject<int>());
136 auto t = Try<void>();
137 EXPECT_EQ(nullptr, t.tryGetExceptionObject());
138 EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
139 EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
143 auto t = Try<void>(exception_wrapper(epexn, *exn));
144 EXPECT_EQ(exn, t.tryGetExceptionObject());
145 EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
146 EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
150 auto t = Try<void>(exception_wrapper(epnum, *num));
151 EXPECT_EQ(nullptr, t.tryGetExceptionObject());
152 EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
153 EXPECT_EQ(num, t.tryGetExceptionObject<int>());
157 auto const t = Try<bool>(true);
158 EXPECT_EQ(nullptr, t.tryGetExceptionObject());
159 EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
160 EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
164 auto const t = Try<bool>(exception_wrapper(epexn, *exn));
165 EXPECT_EQ(exn, t.tryGetExceptionObject());
166 EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
167 EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
171 auto const t = Try<bool>(exception_wrapper(epnum, *num));
172 EXPECT_EQ(nullptr, t.tryGetExceptionObject());
173 EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
174 EXPECT_EQ(num, t.tryGetExceptionObject<int>());
178 auto const t = Try<void>();
179 EXPECT_EQ(nullptr, t.tryGetExceptionObject());
180 EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
181 EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
185 auto const t = Try<void>(exception_wrapper(epexn, *exn));
186 EXPECT_EQ(exn, t.tryGetExceptionObject());
187 EXPECT_EQ(exn, t.tryGetExceptionObject<std::runtime_error>());
188 EXPECT_EQ(nullptr, t.tryGetExceptionObject<int>());
192 auto const t = Try<void>(exception_wrapper(epnum, *num));
193 EXPECT_EQ(nullptr, t.tryGetExceptionObject());
194 EXPECT_EQ(nullptr, t.tryGetExceptionObject<std::runtime_error>());
195 EXPECT_EQ(num, t.tryGetExceptionObject<int>());
199 TEST(Try, withException) {
200 auto ew = make_exception_wrapper<std::range_error>("oops");
203 auto t = Try<bool>(true);
204 EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
205 EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
206 EXPECT_FALSE(t.withException([](std::runtime_error&) {}));
207 EXPECT_FALSE(t.withException([](std::logic_error&) {}));
211 auto t = Try<bool>(ew);
212 EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
213 EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
214 EXPECT_TRUE(t.withException([](std::runtime_error&) {}));
215 EXPECT_FALSE(t.withException([](std::logic_error&) {}));
219 auto t = Try<void>();
220 EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
221 EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
222 EXPECT_FALSE(t.withException([](std::runtime_error&) {}));
223 EXPECT_FALSE(t.withException([](std::logic_error&) {}));
227 auto t = Try<void>(ew);
228 EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
229 EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
230 EXPECT_TRUE(t.withException([](std::runtime_error&) {}));
231 EXPECT_FALSE(t.withException([](std::logic_error&) {}));
235 auto const t = Try<bool>(true);
236 EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
237 EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
238 EXPECT_FALSE(t.withException([](std::runtime_error const&) {}));
239 EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
243 auto const t = Try<bool>(ew);
244 EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
245 EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
246 EXPECT_TRUE(t.withException([](std::runtime_error const&) {}));
247 EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
251 auto const t = Try<void>();
252 EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
253 EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
254 EXPECT_FALSE(t.withException([](std::runtime_error const&) {}));
255 EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
259 auto const t = Try<void>(ew);
260 EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
261 EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
262 EXPECT_TRUE(t.withException([](std::runtime_error const&) {}));
263 EXPECT_FALSE(t.withException([](std::logic_error const&) {}));