Debian Codegraph @

test/core/promise/try_seq_test.cc

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/core/lib/promise/try_seq.h"
16
17 #include <stdlib.h>
18
19 #include <algorithm>
20 #include <string>
21 #include <vector>
22
23 #include "gtest/gtest.h"
24
25 namespace grpc_core {
26
27 TEST(TrySeqTest, SucceedAndThen) {
28 EXPECT_EQ(TrySeq([] { return absl::StatusOr<int>(1); },
29 [](int i) {
30 return [i]() { return absl::StatusOr<int>(i + 1); };
31 })(),
32 Poll<absl::StatusOr<int>>(absl::StatusOr<int>(2)));
33 }
34
35 TEST(TrySeqTest, SucceedDirectlyAndThenDirectly) {
36 EXPECT_EQ(
37 TrySeq([] { return 1; }, [](int i) { return [i]() { return i + 1; }; })(),
38 Poll<absl::StatusOr<int>>(absl::StatusOr<int>(2)));
39 }
40
41 TEST(TrySeqTest, SucceedAndThenChangeType) {
42 EXPECT_EQ(
43 TrySeq([] { return absl::StatusOr<int>(42); },
44 [](int i) {
45 return [i]() {
46 return absl::StatusOr<std::string>(std::to_string(i));
47 };
48 })(),
49 Poll<absl::StatusOr<std::string>>(absl::StatusOr<std::string>("42")));
50 }
51
52 TEST(TrySeqTest, FailAndThen) {
53 EXPECT_EQ(TrySeq([]() { return absl::StatusOr<int>(absl::CancelledError()); },
54 [](int) {
55 return []() -> Poll<absl::StatusOr<double>> { abort(); };
56 })(),
57 Poll<absl::StatusOr<double>>(
58 absl::StatusOr<double>(absl::CancelledError())));
59 }
60
61 TEST(TrySeqTest, RawSucceedAndThen) {
62 EXPECT_EQ(TrySeq([] { return absl::OkStatus(); },
63 [] { return []() { return absl::OkStatus(); }; })(),
64 Poll<absl::Status>(absl::OkStatus()));
65 }
66
67 TEST(TrySeqTest, RawFailAndThen) {
68 EXPECT_EQ(TrySeq([] { return absl::CancelledError(); },
69 []() { return []() -> Poll<absl::Status> { abort(); }; })(),
70 Poll<absl::Status>(absl::CancelledError()));
71 }
72
73 TEST(TrySeqTest, RawSucceedAndThenValue) {
74 EXPECT_EQ(TrySeq([] { return absl::OkStatus(); },
75 [] { return []() { return absl::StatusOr<int>(42); }; })(),
76 Poll<absl::StatusOr<int>>(absl::StatusOr<int>(42)));
77 }
78
79 TEST(TrySeqIterTest, Ok) {
80 std::vector<int> v{1, 2, 3, 4, 5};
81 EXPECT_EQ(TrySeqIter(v.begin(), v.end(), 0,
82 [](int elem, int accum) {
83 return [elem, accum]() -> absl::StatusOr<int> {
84 return elem + accum;
85 };
86 })(),
87 Poll<absl::StatusOr<int>>(15));
88 }
89
90 TEST(TrySeqIterTest, ErrorAt3) {
91 std::vector<int> v{1, 2, 3, 4, 5};
92 EXPECT_EQ(TrySeqIter(v.begin(), v.end(), 0,
93 [](int elem, int accum) {
94 return [elem, accum]() -> absl::StatusOr<int> {
95 if (elem < 3) {
96 return elem + accum;
97 }
98 if (elem == 3) {
99 return absl::CancelledError();
100 }
101 abort(); // unreachable
102 };
103 })(),
104 Poll<absl::StatusOr<int>>(absl::CancelledError()));
105 }
106
107 } // namespace grpc_core
108
109 int main(int argc, char** argv) {
110 ::testing::InitGoogleTest(&argc, argv);
111 return RUN_ALL_TESTS();
112 }