| 1 |
#include <gtest/gtest.h> |
| 2 |
|
| 3 |
#include "utils/result.hpp" |
| 4 |
|
| 5 |
using cpptrace::detail::Result; |
| 6 |
|
| 7 |
namespace { |
| 8 |
|
| 9 |
// A simple custom error type that behaves like a standard exception. |
| 10 |
struct error { |
| 11 |
int x; |
| 12 |
const char* what() const { |
| 13 |
return "error..."; |
| 14 |
} |
| 15 |
}; |
| 16 |
|
| 17 |
class ResultFixture : public testing::Test { |
| 18 |
public: |
| 19 |
ResultFixture() { |
| 20 |
cpptrace::absorb_trace_exceptions(true); |
| 21 |
} |
| 22 |
|
| 23 |
~ResultFixture() override { |
| 24 |
cpptrace::absorb_trace_exceptions(false); |
| 25 |
} |
| 26 |
}; |
| 27 |
|
| 28 |
TEST_F(ResultFixture, ConstructWithValueRValue) { |
| 29 |
Result<std::string, error> result("test"); |
| 30 |
EXPECT_TRUE(result.has_value()); |
| 31 |
EXPECT_FALSE(result.is_error()); |
| 32 |
EXPECT_TRUE(static_cast<bool>(result)); |
| 33 |
|
| 34 |
EXPECT_EQ(result.unwrap_value(), "test"); |
| 35 |
EXPECT_FALSE(result.error().has_value()); |
| 36 |
} |
| 37 |
|
| 38 |
TEST_F(ResultFixture, ConstructWithValueLValue) { |
| 39 |
std::string s = "test"; |
| 40 |
Result<std::string, error> result(s); |
| 41 |
|
| 42 |
EXPECT_TRUE(result.has_value()); |
| 43 |
EXPECT_FALSE(result.is_error()); |
| 44 |
EXPECT_EQ(result.unwrap_value(), "test"); |
| 45 |
|
| 46 |
s = "x"; |
| 47 |
EXPECT_EQ(result.unwrap_value(), "test"); |
| 48 |
|
| 49 |
Result<std::string&, error> r2(s); |
| 50 |
EXPECT_EQ(r2.unwrap_value(), "x"); |
| 51 |
s = "y"; |
| 52 |
EXPECT_EQ(r2.unwrap_value(), "y"); |
| 53 |
} |
| 54 |
|
| 55 |
TEST_F(ResultFixture, ConstructWithErrorRValue) { |
| 56 |
Result<std::string, error> result(error{1}); |
| 57 |
EXPECT_FALSE(result.has_value()); |
| 58 |
EXPECT_TRUE(result.is_error()); |
| 59 |
EXPECT_FALSE(static_cast<bool>(result)); |
| 60 |
|
| 61 |
EXPECT_EQ(result.unwrap_error().x, 1); |
| 62 |
|
| 63 |
// Check that value() returns nullopt in this scenario |
| 64 |
EXPECT_FALSE(result.value().has_value()); |
| 65 |
} |
| 66 |
|
| 67 |
TEST_F(ResultFixture, ConstructWithErrorLValue) { |
| 68 |
error e{1}; |
| 69 |
Result<std::string, error> result(e); |
| 70 |
|
| 71 |
EXPECT_FALSE(result.has_value()); |
| 72 |
EXPECT_TRUE(result.is_error()); |
| 73 |
EXPECT_EQ(result.unwrap_error().x, 1); |
| 74 |
} |
| 75 |
|
| 76 |
TEST_F(ResultFixture, MoveConstructorValue) { |
| 77 |
Result<std::string, error> original(std::string("move")); |
| 78 |
Result<std::string, error> moved(std::move(original)); |
| 79 |
EXPECT_TRUE(moved.has_value()); |
| 80 |
EXPECT_EQ(moved.unwrap_value(), "move"); |
| 81 |
EXPECT_TRUE(original.has_value()); |
| 82 |
|
| 83 |
std::string s = "test"; |
| 84 |
Result<std::string&, error> r1(s); |
| 85 |
Result<std::string&, error> r2(std::move(r1)); |
| 86 |
EXPECT_TRUE(r2.has_value()); |
| 87 |
EXPECT_EQ(r2.unwrap_value(), "test"); |
| 88 |
s = "foo"; |
| 89 |
EXPECT_EQ(r2.unwrap_value(), "foo"); |
| 90 |
EXPECT_TRUE(r2.has_value()); |
| 91 |
} |
| 92 |
|
| 93 |
TEST_F(ResultFixture, MoveConstructorError) { |
| 94 |
Result<std::string, error> original(error{1}); |
| 95 |
Result<std::string, error> moved(std::move(original)); |
| 96 |
|
| 97 |
EXPECT_TRUE(moved.is_error()); |
| 98 |
EXPECT_EQ(moved.unwrap_error().x, 1); |
| 99 |
EXPECT_TRUE(original.is_error()); |
| 100 |
} |
| 101 |
|
| 102 |
TEST_F(ResultFixture, MoveAssignmentValue) { |
| 103 |
Result<std::string, error> original(std::string("move")); |
| 104 |
Result<std::string, error> target = std::string{}; |
| 105 |
target = std::move(original); |
| 106 |
EXPECT_TRUE(target.has_value()); |
| 107 |
EXPECT_EQ(target.unwrap_value(), "move"); |
| 108 |
EXPECT_TRUE(original.has_value()); |
| 109 |
|
| 110 |
std::string s = "test"; |
| 111 |
std::string n; |
| 112 |
Result<std::string&, error> r1(s); |
| 113 |
Result<std::string&, error> r2(n); |
| 114 |
r2 = std::move(r1); |
| 115 |
EXPECT_TRUE(r2.has_value()); |
| 116 |
EXPECT_EQ(r2.unwrap_value(), "test"); |
| 117 |
s = "foo"; |
| 118 |
EXPECT_EQ(r2.unwrap_value(), "foo"); |
| 119 |
EXPECT_TRUE(r2.has_value()); |
| 120 |
} |
| 121 |
|
| 122 |
TEST_F(ResultFixture, MoveAssignmentError) { |
| 123 |
Result<std::string, error> original(error{1}); |
| 124 |
Result<std::string, error> target = std::string{}; |
| 125 |
target = std::move(original); |
| 126 |
|
| 127 |
EXPECT_TRUE(target.is_error()); |
| 128 |
EXPECT_EQ(target.unwrap_error().x, 1); |
| 129 |
EXPECT_TRUE(original.is_error()); |
| 130 |
} |
| 131 |
|
| 132 |
TEST_F(ResultFixture, CopyAssignmentValue) { |
| 133 |
Result<std::string, error> original(std::string("move")); |
| 134 |
Result<std::string, error> target = std::string{}; |
| 135 |
target = original; |
| 136 |
EXPECT_TRUE(target.has_value()); |
| 137 |
EXPECT_EQ(target.unwrap_value(), "move"); |
| 138 |
EXPECT_TRUE(original.has_value()); |
| 139 |
|
| 140 |
std::string s = "test"; |
| 141 |
std::string n; |
| 142 |
Result<std::string&, error> r1(s); |
| 143 |
Result<std::string&, error> r2(n); |
| 144 |
r2 = r1; |
| 145 |
EXPECT_TRUE(r2.has_value()); |
| 146 |
EXPECT_EQ(r2.unwrap_value(), "test"); |
| 147 |
s = "foo"; |
| 148 |
EXPECT_EQ(r2.unwrap_value(), "foo"); |
| 149 |
EXPECT_TRUE(r2.has_value()); |
| 150 |
} |
| 151 |
|
| 152 |
TEST_F(ResultFixture, CopyAssignmentError) { |
| 153 |
Result<std::string, error> original(error{1}); |
| 154 |
Result<std::string, error> target = std::string{}; |
| 155 |
target = original; |
| 156 |
|
| 157 |
EXPECT_TRUE(target.is_error()); |
| 158 |
EXPECT_EQ(target.unwrap_error().x, 1); |
| 159 |
EXPECT_TRUE(original.is_error()); |
| 160 |
} |
| 161 |
|
| 162 |
TEST_F(ResultFixture, ValueOr) { |
| 163 |
{ |
| 164 |
Result<int, error> res_with_value(42); |
| 165 |
EXPECT_EQ(res_with_value.value_or(-1), 42); |
| 166 |
EXPECT_EQ(std::move(res_with_value).value_or(-1), 42); |
| 167 |
} |
| 168 |
{ |
| 169 |
Result<int, error> res_with_error(error{}); |
| 170 |
EXPECT_EQ(res_with_error.value_or(-1), -1); |
| 171 |
EXPECT_EQ(std::move(res_with_error).value_or(-1), -1); |
| 172 |
} |
| 173 |
{ |
| 174 |
int x = 2; |
| 175 |
int y = 3; |
| 176 |
Result<int&, error> res_with_value(x); |
| 177 |
EXPECT_EQ(res_with_value.value_or(y), 2); |
| 178 |
EXPECT_EQ(std::move(res_with_value).value_or(y), 2); |
| 179 |
} |
| 180 |
{ |
| 181 |
int x = 2; |
| 182 |
Result<int&, error> res_with_error(error{}); |
| 183 |
EXPECT_EQ(res_with_error.value_or(x), 2); |
| 184 |
EXPECT_EQ(&res_with_error.value_or(x), &x); |
| 185 |
EXPECT_EQ(std::move(res_with_error).value_or(x), 2); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
} |