2 * Copyright 2016 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 <gtest/gtest.h>
19 #include <folly/futures/Future.h>
20 #include <folly/futures/Unit.h>
22 using namespace folly;
24 std::runtime_error eggs("eggs");
26 TEST(Unit, futureDefaultCtor) {
30 TEST(Unit, operatorEq) {
31 EXPECT_TRUE(Unit{} == Unit{});
34 TEST(Unit, operatorNe) {
35 EXPECT_FALSE(Unit{} != Unit{});
38 TEST(Unit, promiseSetValue) {
44 using lifted = Unit::Lift<int>;
45 using actual = std::is_same<int, lifted::type>;
46 EXPECT_TRUE(actual::value);
49 TEST(Unit, liftUnit) {
50 using lifted = Unit::Lift<Unit>;
51 using actual = std::is_same<Unit, lifted::type>;
52 EXPECT_TRUE(actual::value);
55 TEST(Unit, liftVoid) {
56 using lifted = Unit::Lift<void>;
57 using actual = std::is_same<Unit, lifted::type>;
58 EXPECT_TRUE(actual::value);
62 using dropped = Unit::Drop<int>;
63 using actual = std::is_same<int, dropped::type>;
64 EXPECT_TRUE(actual::value);
67 TEST(Unit, dropUnit) {
68 using dropped = Unit::Drop<Unit>;
69 using actual = std::is_same<void, dropped::type>;
70 EXPECT_TRUE(actual::value);
73 TEST(Unit, dropVoid) {
74 using dropped = Unit::Drop<void>;
75 using actual = std::is_same<void, dropped::type>;
76 EXPECT_TRUE(actual::value);
79 TEST(Unit, futureToUnit) {
80 Future<Unit> fu = makeFuture(42).unit();
82 EXPECT_TRUE(makeFuture<int>(eggs).unit().hasException());
85 TEST(Unit, voidFutureToUnit) {
86 Future<Unit> fu = makeFuture().unit();
88 EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
91 TEST(Unit, unitFutureToUnitIdentity) {
92 Future<Unit> fu = makeFuture(Unit{}).unit();
94 EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
97 TEST(Unit, toUnitWhileInProgress) {
99 Future<Unit> fu = p.getFuture().unit();
100 EXPECT_FALSE(fu.isReady());
102 EXPECT_TRUE(fu.isReady());
105 TEST(Unit, makeFutureWith) {
107 Future<Unit> fu = makeFutureWith([&]{ count++; });