proxygen
01-Values.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-present Facebook, Inc.
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 <folly/futures/Future.h>
18 
20 
21 using folly::Future;
22 using folly::makeFuture;
23 
24 #if 0 // compilation cursor
25 
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());
31 }
32 
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());
37 }
38 
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);
44  // not
45  //auto question = makeFuture(err);
46  EXPECT_THROW(question.get(), std::runtime_error);
47 }
48 
49 TEST(Values, typeConversion) {
50  // Sometimes it's cleaner to give the type and let the compiler do implicit
51  // type conversion
52  __ answer(42);
53  // not
54  //auto answer = makeFuture((double)42);
55  EXPECT_EQ(__, answer.get());
56 }
57 
58 using folly::Try;
59 
60 TEST(Values, tryInside) {
61  // Futures hold either a Value or Exception. This is accomplished under the
62  // covers with Try
63  Try<int> t = makeFuture(42).__();
64  EXPECT_TRUE(t.hasValue());
65  EXPECT_EQ(42, t.value());
66 
67  t = Future<int>(std::runtime_error("Don't Panic")).__();
68  EXPECT_TRUE(t.hasException());
69  EXPECT_THROW(t.value(), std::runtime_error);
70 }
71 
72 #endif
#define TEST(test_case_name, test_name)
Definition: gtest.h:2187
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Definition: Try.h:51
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310