• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
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 <error/expected_utils.h>
18 #include <gtest/gtest.h>
19 
20 #define LOG_TAG "Result-test"
21 
22 namespace android {
23 namespace foo {
24 
25 class Value {
26   public:
Value(int i)27     explicit Value(int i) : mInt(i) {}
28     Value(const Value&) = delete;
29     Value(Value&&) = default;
30 
operator int() const31     operator int() const { return mInt; }
32 
33   private:
34     const int mInt;
35 };
36 
37 class Status {
38   public:
Status(int i)39     explicit Status(int i) : mInt(i) {}
40     Status(const Status&) = delete;
41     Status(Status&&) = default;
42 
operator int() const43     operator int() const { return mInt; }
44 
45   private:
46     const int mInt;
47 };
48 
errorIsOk(const Status & e)49 bool errorIsOk(const Status& e) {
50     return e == 0;
51 }
52 
errorToString(const Status & e)53 std::string errorToString(const Status& e) {
54     std::ostringstream str;
55     str << e;
56     return str.str();
57 }
58 
59 using Result = base::expected<Value, Status>;
60 
61 }  // namespace foo
62 
63 namespace {
64 
65 using foo::Result;
66 using foo::Status;
67 using foo::Value;
68 
TEST(Result,ValueOrReturnSuccess)69 TEST(Result, ValueOrReturnSuccess) {
70     Result result = []() -> Result {
71         Value intermediate = VALUE_OR_RETURN(Result(Value(3)));
72         return Value(intermediate + 1);
73     }();
74     ASSERT_TRUE(result.ok());
75     EXPECT_EQ(4, result.value());
76 }
77 
TEST(Result,ValueOrReturnFailure)78 TEST(Result, ValueOrReturnFailure) {
79     Result result = []() -> Result {
80         Value intermediate = VALUE_OR_RETURN(Result(base::unexpected(Status(2))));
81         return Value(intermediate + 1);
82     }();
83     ASSERT_FALSE(result.ok());
84     EXPECT_EQ(2, result.error());
85 }
86 
TEST(Result,ValueOrReturnStatusSuccess)87 TEST(Result, ValueOrReturnStatusSuccess) {
88     Status status = []() -> Status {
89         Value intermediate = VALUE_OR_RETURN_STATUS(Result(Value(3)));
90         (void) intermediate;
91         return Status(0);
92     }();
93     EXPECT_EQ(0, status);
94 }
95 
TEST(Result,ValueOrReturnStatusFailure)96 TEST(Result, ValueOrReturnStatusFailure) {
97     Status status = []() -> Status {
98         Value intermediate = VALUE_OR_RETURN_STATUS(Result(base::unexpected(Status(1))));
99         (void) intermediate;
100         return Status(0);
101     }();
102     EXPECT_EQ(1, status);
103 }
104 
TEST(Result,ReturnIfErrorSuccess)105 TEST(Result, ReturnIfErrorSuccess) {
106     Result result = []() -> Result {
107         RETURN_IF_ERROR(Status(0));
108         return Value(5);
109     }();
110     ASSERT_TRUE(result.ok());
111     EXPECT_EQ(5, result.value());
112 }
113 
TEST(Result,ReturnIfErrorFailure)114 TEST(Result, ReturnIfErrorFailure) {
115     Result result = []() -> Result {
116         RETURN_IF_ERROR(Status(4));
117         return Value(5);
118     }();
119     ASSERT_FALSE(result.ok());
120     EXPECT_EQ(4, result.error());
121 }
122 
TEST(Result,ReturnStatusIfErrorSuccess)123 TEST(Result, ReturnStatusIfErrorSuccess) {
124     Status status = []() -> Status {
125         RETURN_STATUS_IF_ERROR(Status(0));
126         return Status(7);
127     }();
128     EXPECT_EQ(7, status);
129 }
130 
TEST(Result,ReturnStatusIfErrorFailure)131 TEST(Result, ReturnStatusIfErrorFailure) {
132     Status status = []() -> Status {
133         RETURN_STATUS_IF_ERROR(Status(3));
134         return Status(0);
135     }();
136     EXPECT_EQ(3, status);
137 }
138 
TEST(Result,ValueOrFatalSuccess)139 TEST(Result, ValueOrFatalSuccess) {
140     Value value = VALUE_OR_FATAL(Result(Value(7)));
141     EXPECT_EQ(7, value);
142 }
143 
TEST(Result,ValueOrFatalFailure)144 TEST(Result, ValueOrFatalFailure) {
145     EXPECT_DEATH(VALUE_OR_FATAL(Result(base::unexpected(Status(3)))), "");
146 }
147 
TEST(Result,FatalIfErrorSuccess)148 TEST(Result, FatalIfErrorSuccess) {
149     FATAL_IF_ERROR(Status(0));
150 }
151 
TEST(Result,FatalIfErrorFailure)152 TEST(Result, FatalIfErrorFailure) {
153     EXPECT_DEATH(FATAL_IF_ERROR(Status(3)), "");
154 }
155 
156 }  // namespace
157 }  // namespace android
158