added result class

This commit is contained in:
2024-04-10 07:58:41 +02:00
parent 1fe5c9bac4
commit b7c12d8217
5 changed files with 136 additions and 2 deletions
+2 -1
View File
@@ -41,7 +41,8 @@ add_executable(tests
models/book.hpp
FieldTest.cpp
models/recipe.hpp
ValueTest.cpp)
ValueTest.cpp
ResultTest.cpp)
target_link_libraries(tests PRIVATE
Catch2::Catch2WithMain
+47
View File
@@ -0,0 +1,47 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/utils/result.hpp"
namespace matador::test {
enum class math_error : int32_t {
DIVISION_BY_ZERO = 1
};
utils::result<float, math_error>divide(int x, int y) {
if (y == 0) {
return utils::error(math_error::DIVISION_BY_ZERO);
}
return utils::ok(float(x) / y);
}
utils::result<float, math_error>multiply(int x, int y) {
return utils::ok(float(x) * y);
}
}
using namespace matador;
TEST_CASE("Result tests", "[result]") {
auto res = test::divide(4, 2);
REQUIRE(res);
REQUIRE(res.is_ok());
REQUIRE(!res.is_error());
REQUIRE((res.value() == 2.0));
REQUIRE_THROWS(res.err());
res = test::divide(4, 0);
REQUIRE(!res);
REQUIRE(!res.is_ok());
REQUIRE(res.is_error());
REQUIRE((res.err() == test::math_error::DIVISION_BY_ZERO));
// res = test::divide(4, 2)
// .and_then<>()
}