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 "common/libs/utils/result.h"
18
19 #include <string>
20 #include <type_traits>
21
22 #include <android-base/expected.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25
26 #include "common/libs/utils/result_matchers.h"
27
28 namespace cuttlefish {
29 namespace {
30
31 using ::testing::HasSubstr;
32 using ::testing::StrEq;
33
34 } // namespace
35
TEST(ResultTest,ExpectBoolGoodNoMessage)36 TEST(ResultTest, ExpectBoolGoodNoMessage) {
37 const auto result = []() -> Result<std::string> {
38 CF_EXPECT(true);
39 return "okay";
40 }();
41 EXPECT_THAT(result, IsOkAndValue(StrEq("okay")));
42 }
43
TEST(ResultTest,ExpectBoolGoodWithMessage)44 TEST(ResultTest, ExpectBoolGoodWithMessage) {
45 const auto result = []() -> Result<std::string> {
46 CF_EXPECT(true, "Failed");
47 return "okay";
48 }();
49 EXPECT_THAT(result, IsOkAndValue(StrEq("okay")));
50 }
51
TEST(ResultTest,ExpectBoolBadNoMessage)52 TEST(ResultTest, ExpectBoolBadNoMessage) {
53 const auto result = []() -> Result<std::string> {
54 CF_EXPECT(false);
55 return "okay";
56 }();
57 EXPECT_THAT(result, IsError());
58 }
59
TEST(ResultTest,ExpectBoolBadWithMessage)60 TEST(ResultTest, ExpectBoolBadWithMessage) {
61 const auto result = []() -> Result<std::string> {
62 CF_EXPECT(false, "ExpectBoolBadWithMessage message");
63 return "okay";
64 }();
65 EXPECT_THAT(result,
66 IsErrorAndMessage(HasSubstr("ExpectBoolBadWithMessage message")));
67 }
68
TEST(ResultTest,ExpectWithResultGoodNoMessage)69 TEST(ResultTest, ExpectWithResultGoodNoMessage) {
70 const auto result = []() -> Result<std::string> {
71 const auto inner_result = []() -> Result<std::string> {
72 CF_EXPECT(true);
73 return "inner okay";
74 };
75 CF_EXPECT(inner_result());
76 return "outer okay";
77 }();
78 EXPECT_THAT(result, IsOkAndValue(StrEq("outer okay")));
79 }
80
TEST(ResultTest,ExpectWithResultGoodWithMessage)81 TEST(ResultTest, ExpectWithResultGoodWithMessage) {
82 const auto result = []() -> Result<std::string> {
83 const auto inner_result = []() -> Result<std::string> {
84 CF_EXPECT(true);
85 return "inner okay";
86 };
87 CF_EXPECT(inner_result(), "Failed inner result.");
88 return "outer okay";
89 }();
90 EXPECT_THAT(result, IsOkAndValue(StrEq("outer okay")));
91 }
92
TEST(ResultTest,ExpectWithResultBadNoMessage)93 TEST(ResultTest, ExpectWithResultBadNoMessage) {
94 const auto result = []() -> Result<std::string> {
95 const auto inner_result = []() -> Result<std::string> {
96 CF_EXPECT(false, "inner bad");
97 return "inner okay";
98 };
99 CF_EXPECT(inner_result());
100 return "okay";
101 }();
102 EXPECT_THAT(result, IsError());
103 }
104
TEST(ResultTest,ExpectWithResultBadWithMessage)105 TEST(ResultTest, ExpectWithResultBadWithMessage) {
106 const auto result = []() -> Result<std::string> {
107 const auto inner_result = []() -> Result<std::string> {
108 CF_EXPECT(false, "inner bad");
109 return "inner okay";
110 };
111 CF_EXPECT(inner_result(), "ExpectWithResultBadWithMessage message");
112 return "okay";
113 }();
114 EXPECT_THAT(result, IsErrorAndMessage(
115 HasSubstr("ExpectWithResultBadWithMessage message")));
116 }
117
TEST(ResultTest,ExpectEqGoodNoMessage)118 TEST(ResultTest, ExpectEqGoodNoMessage) {
119 const auto result = []() -> Result<std::string> {
120 CF_EXPECT_EQ(1, 1);
121 return "okay";
122 }();
123 EXPECT_THAT(result, IsOkAndValue(StrEq("okay")));
124 }
125
TEST(ResultTest,ExpectEqGoodWithMessage)126 TEST(ResultTest, ExpectEqGoodWithMessage) {
127 const auto result = []() -> Result<std::string> {
128 CF_EXPECT_EQ(1, 1, "Failed comparison");
129 return "okay";
130 }();
131 EXPECT_THAT(result, IsOkAndValue(StrEq("okay")));
132 }
133
TEST(ResultTest,ExpectEqBadNoMessage)134 TEST(ResultTest, ExpectEqBadNoMessage) {
135 const auto result = []() -> Result<std::string> {
136 CF_EXPECT_EQ(1, 2);
137 return "okay";
138 }();
139 EXPECT_THAT(result, IsError());
140 }
141
TEST(ResultTest,ExpectEqBadWithMessage)142 TEST(ResultTest, ExpectEqBadWithMessage) {
143 const auto result = []() -> Result<std::string> {
144 CF_EXPECT_EQ(1, 2, "ExpectEqBadWithMessage message");
145 return "okay";
146 }();
147 EXPECT_THAT(result,
148 IsErrorAndMessage(HasSubstr("ExpectEqBadWithMessage message")));
149 }
150
151 } // namespace cuttlefish
152