Copyright 2014->2015
[folly.git] / folly / futures / test / Try.cpp
1 /*
2  * Copyright 2015 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 <gtest/gtest.h>
18
19 #include <folly/Memory.h>
20 #include <folly/futures/Try.h>
21
22 using namespace folly;
23
24 TEST(Try, makeTryFunction) {
25   auto func = []() {
26     return folly::make_unique<int>(1);
27   };
28
29   auto result = makeTryFunction(func);
30   EXPECT_TRUE(result.hasValue());
31   EXPECT_EQ(*result.value(), 1);
32 }
33
34 TEST(Try, makeTryFunctionThrow) {
35   auto func = []() {
36     throw std::runtime_error("Runtime");
37     return folly::make_unique<int>(1);
38   };
39
40   auto result = makeTryFunction(func);
41   EXPECT_TRUE(result.hasException<std::runtime_error>());
42 }
43
44 TEST(Try, makeTryFunctionVoid) {
45   auto func = []() {
46     return;
47   };
48
49   auto result = makeTryFunction(func);
50   EXPECT_TRUE(result.hasValue());
51 }
52
53 TEST(Try, makeTryFunctionVoidThrow) {
54   auto func = []() {
55     throw std::runtime_error("Runtime");
56     return;
57   };
58
59   auto result = makeTryFunction(func);
60   EXPECT_TRUE(result.hasException<std::runtime_error>());
61 }