|
| 1 | +#include <memory> |
| 2 | +#include <system_error> |
| 3 | +#include <type_traits> |
| 4 | + |
| 5 | +#include <ystdlib/error_handling/Result.hpp> |
| 6 | + |
| 7 | +#include <catch2/catch_test_macros.hpp> |
| 8 | + |
| 9 | +#include "types.hpp" |
| 10 | + |
| 11 | +using ystdlib::error_handling::Result; |
| 12 | +using ystdlib::error_handling::success; |
| 13 | +using ystdlib::error_handling::test::AlwaysSuccessErrorCode; |
| 14 | +using ystdlib::error_handling::test::AlwaysSuccessErrorCodeEnum; |
| 15 | +using ystdlib::error_handling::test::BinaryErrorCode; |
| 16 | +using ystdlib::error_handling::test::BinaryErrorCodeEnum; |
| 17 | + |
| 18 | +namespace { |
| 19 | +constexpr int cTestInt{123}; |
| 20 | +constexpr auto cVoidFunc = [](bool is_error) -> Result<void> { |
| 21 | + if (is_error) { |
| 22 | + return BinaryErrorCode{BinaryErrorCodeEnum::Failure}; |
| 23 | + } |
| 24 | + return success(); |
| 25 | +}; |
| 26 | +constexpr auto cIntFunc = [](bool is_error) -> Result<int> { |
| 27 | + if (is_error) { |
| 28 | + return std::errc::bad_message; |
| 29 | + } |
| 30 | + return cTestInt; |
| 31 | +}; |
| 32 | +constexpr auto cUniquePtrFunc = [](bool is_error) -> Result<std::unique_ptr<int>> { |
| 33 | + if (is_error) { |
| 34 | + return AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success}; |
| 35 | + } |
| 36 | + return std::make_unique<int>(cTestInt); |
| 37 | +}; |
| 38 | +} // namespace |
| 39 | + |
| 40 | +namespace ystdlib::error_handling::test { |
| 41 | +TEST_CASE("test_result_void", "[error_handling][Result]") { |
| 42 | + auto const result_no_error{cVoidFunc(false)}; |
| 43 | + REQUIRE_FALSE(result_no_error.has_error()); |
| 44 | + REQUIRE(std::is_void_v<decltype(result_no_error.value())>); |
| 45 | + |
| 46 | + auto const result_has_error{cVoidFunc(true)}; |
| 47 | + REQUIRE(result_has_error.has_error()); |
| 48 | + REQUIRE(BinaryErrorCode{BinaryErrorCodeEnum::Failure} == result_has_error.error()); |
| 49 | +} |
| 50 | + |
| 51 | +TEST_CASE("test_result_void_in_main", "[error_handling][Result]") { |
| 52 | + auto main_func = [&](bool is_error) -> Result<void> { |
| 53 | + YSTDLIB_ERROR_HANDLING_TRYV(cVoidFunc(is_error)); |
| 54 | + return success(); |
| 55 | + }; |
| 56 | + auto const main_no_error{main_func(false)}; |
| 57 | + REQUIRE_FALSE(main_no_error.has_error()); |
| 58 | + REQUIRE(std::is_void_v<decltype(main_no_error.value())>); |
| 59 | + |
| 60 | + auto const main_has_error{main_func(true)}; |
| 61 | + REQUIRE(main_has_error.has_error()); |
| 62 | + REQUIRE(BinaryErrorCode{BinaryErrorCodeEnum::Failure} == main_has_error.error()); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_CASE("test_result_int", "[error_handling][Result]") { |
| 66 | + auto const result_no_error{cIntFunc(false)}; |
| 67 | + REQUIRE_FALSE(result_no_error.has_error()); |
| 68 | + REQUIRE(cTestInt == result_no_error.value()); |
| 69 | + |
| 70 | + auto const result_has_error{cIntFunc(true)}; |
| 71 | + REQUIRE(result_has_error.has_error()); |
| 72 | + REQUIRE(std::errc::bad_message == result_has_error.error()); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_CASE("test_result_int_in_main", "[error_handling][Result]") { |
| 76 | + auto main_func = [&](bool is_error) -> Result<void> { |
| 77 | + YSTDLIB_ERROR_HANDLING_TRYV(cIntFunc(is_error)); |
| 78 | + return success(); |
| 79 | + }; |
| 80 | + auto const main_no_error{main_func(false)}; |
| 81 | + REQUIRE_FALSE(main_no_error.has_error()); |
| 82 | + REQUIRE(std::is_void_v<decltype(main_no_error.value())>); |
| 83 | + |
| 84 | + auto const main_has_error{main_func(true)}; |
| 85 | + REQUIRE(main_has_error.has_error()); |
| 86 | + REQUIRE(std::errc::bad_message == main_has_error.error()); |
| 87 | +} |
| 88 | + |
| 89 | +TEST_CASE("test_result_int_propagate", "[error_handling][Result]") { |
| 90 | + auto main_func = [&](bool is_error) -> Result<int> { |
| 91 | + return YSTDLIB_ERROR_HANDLING_TRYX(cIntFunc(is_error)); |
| 92 | + }; |
| 93 | + auto const main_no_error{main_func(false)}; |
| 94 | + REQUIRE_FALSE(main_no_error.has_error()); |
| 95 | + REQUIRE(cTestInt == main_no_error.value()); |
| 96 | + |
| 97 | + auto const main_has_error{main_func(true)}; |
| 98 | + REQUIRE(main_has_error.has_error()); |
| 99 | + REQUIRE(std::errc::bad_message == main_has_error.error()); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_CASE("test_result_unique_ptr", "[error_handling][Result]") { |
| 103 | + auto const result_no_error{cUniquePtrFunc(false)}; |
| 104 | + REQUIRE_FALSE(result_no_error.has_error()); |
| 105 | + REQUIRE(cTestInt == *(result_no_error.value())); |
| 106 | + |
| 107 | + auto const result_has_error{cUniquePtrFunc(true)}; |
| 108 | + REQUIRE(result_has_error.has_error()); |
| 109 | + REQUIRE(AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success} == result_has_error.error() |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +TEST_CASE("test_result_unique_ptr_in_main", "[error_handling][Result]") { |
| 114 | + auto main_func = [&](bool is_error) -> Result<void> { |
| 115 | + YSTDLIB_ERROR_HANDLING_TRYV(cUniquePtrFunc(is_error)); |
| 116 | + return success(); |
| 117 | + }; |
| 118 | + auto const main_no_error{main_func(false)}; |
| 119 | + REQUIRE_FALSE(main_no_error.has_error()); |
| 120 | + REQUIRE(std::is_void_v<decltype(main_no_error.value())>); |
| 121 | + |
| 122 | + auto const main_has_error{main_func(true)}; |
| 123 | + REQUIRE(main_has_error.has_error()); |
| 124 | + REQUIRE(AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success} == main_has_error.error()); |
| 125 | +} |
| 126 | + |
| 127 | +TEST_CASE("test_result_unique_ptr_propagate", "[error_handling][Result]") { |
| 128 | + auto main_func = [&](bool is_error) -> Result<std::unique_ptr<int>> { |
| 129 | + return YSTDLIB_ERROR_HANDLING_TRYX(cUniquePtrFunc(is_error)); |
| 130 | + }; |
| 131 | + auto const main_no_error{main_func(false)}; |
| 132 | + REQUIRE_FALSE(main_no_error.has_error()); |
| 133 | + REQUIRE(cTestInt == *(main_no_error.value())); |
| 134 | + |
| 135 | + auto const main_has_error{main_func(true)}; |
| 136 | + REQUIRE(main_has_error.has_error()); |
| 137 | + REQUIRE(AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success} == main_has_error.error()); |
| 138 | +} |
| 139 | +} // namespace ystdlib::error_handling::test |
0 commit comments