added result class
This commit is contained in:
+2
-1
@@ -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
|
||||
|
||||
@@ -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<>()
|
||||
}
|
||||
Reference in New Issue
Block a user