2 * Copyright 2015-present 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/futures/Future.h>
22 using folly::makeFuture;
24 #if 0 // compilation cursor
26 TEST(Values, canonicalForm) {
27 // The canonical way to make a Future from an immediate value is with the
28 // Future constructor.
29 Future<int> answer(__);
30 EXPECT_EQ(42, answer.get());
33 TEST(Values, typeDeduction) {
34 // If you use makeFuture, the compiler will deduce the type.
35 auto answer = makeFuture(__);
36 EXPECT_EQ(42, answer.get());
39 TEST(Values, exceptionNeedsType) {
40 // To create a Future holding an exception, you must
41 // use makeFuture with the type
42 std::runtime_error err("Don't Panic");
43 auto question = __(err);
45 //auto question = makeFuture(err);
46 EXPECT_THROW(question.get(), std::runtime_error);
49 TEST(Values, typeConversion) {
50 // Sometimes it's cleaner to give the type and let the compiler do implicit
54 //auto answer = makeFuture((double)42);
55 EXPECT_EQ(__, answer.get());
60 TEST(Values, tryInside) {
61 // Futures hold either a Value or Exception. This is accomplished under the
63 Try<int> t = makeFuture(42).__();
64 EXPECT_TRUE(t.hasValue());
65 EXPECT_EQ(42, t.value());
67 t = Future<int>(std::runtime_error("Don't Panic")).__();
68 EXPECT_TRUE(t.hasException());
69 EXPECT_THROW(t.value(), std::runtime_error);