Home | History | Annotate | Download | only in init

Lines Matching refs:result

17 #include "result.h"
30 TEST(result, result_accessors) {
31 Result<std::string> result = "success";
32 ASSERT_TRUE(result);
33 ASSERT_TRUE(result.has_value());
35 EXPECT_EQ("success", *result);
36 EXPECT_EQ("success", result.value());
38 EXPECT_EQ('s', result->data()[0]);
41 TEST(result, result_accessors_rvalue) {
42 ASSERT_TRUE(Result<std::string>("success"));
43 ASSERT_TRUE(Result<std::string>("success").has_value());
45 EXPECT_EQ("success", *Result<std::string>("success"));
46 EXPECT_EQ("success", Result<std::string>("success").value());
48 EXPECT_EQ('s', Result<std::string>("success")->data()[0]);
51 TEST(result, result_success) {
52 Result<Success> result = Success();
53 ASSERT_TRUE(result);
54 ASSERT_TRUE(result.has_value());
56 EXPECT_EQ(Success(), *result);
57 EXPECT_EQ(Success(), result.value());
60 TEST(result, result_success_rvalue) {
61 // Success() doesn't actually create a Result<Success> object, but rather an object that can be
62 // implicitly constructed into a Result<Success> object.
64 auto MakeRvalueSuccessResult = []() -> Result<Success> { return Success(); };
72 TEST(result, result_error) {
73 Result<Success> result = Error() << "failure" << 1;
74 ASSERT_FALSE(result);
75 ASSERT_FALSE(result.has_value());
77 EXPECT_EQ(0, result.error_errno());
78 EXPECT_EQ("failure1", result.error_string());
81 TEST(result, result_error_empty) {
82 Result<Success> result = Error();
83 ASSERT_FALSE(result);
84 ASSERT_FALSE(result.has_value());
86 EXPECT_EQ(0, result.error_errno());
87 EXPECT_EQ("", result.error_string());
90 TEST(result, result_error_rvalue) {
91 // Error() and ErrnoError() aren't actually used to create a Result<T> object.
93 // Result<T>. This is needed both to create the ostream and because Error() itself, by
94 // definition will not know what the type, T, of the underlying Result<T> object that it would
97 auto MakeRvalueErrorResult = []() -> Result<Success> { return Error() << "failure" << 1; };
105 TEST(result, result_errno_error) {
108 Result<Success> result = ErrnoError() << "failure" << 1;
110 ASSERT_FALSE(result);
111 ASSERT_FALSE(result.has_value());
113 EXPECT_EQ(test_errno, result.error_errno());
114 EXPECT_EQ("failure1: "s + strerror(test_errno), result.error_string());
117 TEST(result, result_errno_error_no_text) {
120 Result<Success> result = ErrnoError();
122 ASSERT_FALSE(result);
123 ASSERT_FALSE(result.has_value());
125 EXPECT_EQ(test_errno, result.error_errno());
126 EXPECT_EQ(strerror(test_errno), result.error_string());
129 TEST(result, result_error_from_other_result) {
131 Result<Success> result = Error() << error_text;
133 ASSERT_FALSE(result);
134 ASSERT_FALSE(result.has_value());
136 Result<std::string> result2 = result.error();
141 EXPECT_EQ(0, result.error_errno());
142 EXPECT_EQ(error_text, result.error_string());
145 TEST(result, result_error_through_ostream) {
147 Result<Success> result = Error() << error_text;
149 ASSERT_FALSE(result);
150 ASSERT_FALSE(result.has_value());
152 Result<std::string> result2 = Error() << result.error();
157 EXPECT_EQ(0, result.error_errno());
158 EXPECT_EQ(error_text, result.error_string());
161 TEST(result, result_errno_error_through_ostream) {
165 Result<Success> result = ErrnoError() << error_text;
169 ASSERT_FALSE(result);
170 ASSERT_FALSE(result.has_value());
172 Result<std::string> result2 = Error() << result.error();
177 EXPECT_EQ(test_errno, result.error_errno());
178 EXPECT_EQ(error_text + ": " + strerror(test_errno), result.error_string());
181 TEST(result, constructor_forwarding) {
182 auto result = Result<std::string>(5, 'a');
184 ASSERT_TRUE(result);
185 ASSERT_TRUE(result.has_value());
187 EXPECT_EQ("aaaaa", *result);
230 Result<ConstructorTracker> ReturnConstructorTracker(const std::string& in) {
237 ConstructorTracker result(in + " " + in);
238 return result;
241 TEST(result, no_copy_on_return) {
242 // If returning parameters that may be used to implicitly construct the type T of Result<T>,
243 // then those parameters are forwarded to the construction of Result<T>.
246 // Result<T>.
281 // and only type is Result<T>.
282 TEST(result, result_result_with_success) {
283 auto return_result_result_with_success = []() -> Result<Result<Success>> {
284 return Result<Success>();
286 auto result = return_result_result_with_success();
287 ASSERT_TRUE(result);
288 ASSERT_TRUE(*result);
290 auto inner_result = result.value();
294 TEST(result, result_result_with_failure) {
295 auto return_result_result_with_error = []() -> Result<Result<Success>> {
296 return Result<Success>(ResultError("failure string", 6));
298 auto result = return_result_result_with_error();
299 ASSERT_TRUE(result);
300 ASSERT_FALSE(*result);
301 EXPECT_EQ("failure string", result->error_string());
302 EXPECT_EQ(6, result->error_errno());
305 // This test requires that we disable the forwarding reference constructor if Result<T> is the
306 // *only* type that we are forwarding. In otherwords, if we are forwarding Result<T>, int to
307 // construct a Result<T>, then we still need the constructor.
308 TEST(result, result_two_parameter_constructor_same_type) {
311 TestStruct(Result<TestStruct> result, int value) : value_(result->value_ * value) {}
315 auto return_test_struct = []() -> Result<TestStruct> { return {Result<TestStruct>(6), 6}; };
317 auto result = return_test_struct();
318 ASSERT_TRUE(result);
319 EXPECT_EQ(36, result->value_);
322 TEST(result, die_on_access_failed_result) {
323 Result<std::string> result = Error();
324 ASSERT_DEATH(*result, "");
327 TEST(result, die_on_get_error_succesful_result) {
328 Result<std::string> result = "success";
329 ASSERT_DEATH(result.error_string(), "");