Skip to content

Commit

Permalink
add tests for ANYWHO_LEGACY
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Graeter committed Apr 28, 2024
1 parent f5640e6 commit 9623031
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions test/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace direct_return_expected {

std::expected<int, std::string> myfuncValid() { return 3; }

std::expected<int, std::string> myfunc0()
std::expected<int, std::string> myfuncUnexpectedRaised()
{
const int val = ANYWHO(myfuncUnexpected());
return 3 * val;
}

std::expected<int, std::string> myfunc1()
std::expected<int, std::string> myfuncValidRaised()
{
const int val = ANYWHO(myfuncValid());
return 3 * val;
Expand All @@ -39,33 +39,37 @@ namespace direct_return_optional {

std::expected<int, DummyError> myfuncValid() { return 3; }

// std::optional<DummyError> myfuncOptError(int &output)
// {
// output = 3;
// return std::make_optional(DummyError{});
// }

// std::optional<DummyError> myfuncOptValid(int &output)
// {
// output = 3;
// return std::nullopt;
// }

std::optional<DummyError> myfunc0(int &val)
std::optional<DummyError> myfuncUnexpectedRaised(int &val)
{
int val_cur = ANYWHO_OPT(myfuncUnexpected());
val = 3 * val_cur;

return std::nullopt;
}

std::optional<DummyError> myfunc1(int &val)
std::optional<DummyError> myfuncValidRaised(int &val)
{
int val_cur = ANYWHO_OPT(myfuncValid());
val = 3 * val_cur;

return std::nullopt;
}

std::optional<DummyError> myfuncUnexpectedRaised2(int &val)
{
ANYWHO_LEGACY(myfuncUnexpectedRaised(val));
val = 3 * val;

return std::nullopt;
}

std::optional<DummyError> myfuncValidRaised2(int &val)
{
ANYWHO_LEGACY(myfuncValidRaised(val));
val = 3 * val;

return std::nullopt;
}
}// namespace direct_return_optional
std::expected<int, DummyError> testError() { return std::unexpected(DummyError{}); }

Expand Down Expand Up @@ -115,31 +119,45 @@ std::optional<DummyError> positiveOnlySquareWithOptional(int num, int &output)

TEST_CASE("error get returned with anywho", "[direct_return]")
{
auto result = direct_return_expected::myfunc0();
auto result = direct_return_expected::myfuncUnexpectedRaised();
REQUIRE(!result.has_value());
REQUIRE(result.error() == "my_msg");
}
TEST_CASE("value get returned with anywho", "[direct_return]")
{
auto result = direct_return_expected::myfunc1();
auto result = direct_return_expected::myfuncValidRaised();
REQUIRE(result.has_value());
REQUIRE(result.value() == 9);
}

TEST_CASE("error get returned with anywho_opt", "[direct_return]")
{
int val = 0;
std::optional<DummyError> result = direct_return_optional::myfunc0(val);
std::optional<DummyError> result = direct_return_optional::myfuncUnexpectedRaised(val);
REQUIRE(anywho::has_error(result));
}
TEST_CASE("value get returned with anywho_opt", "[direct_return]")
{
int val = 0;
std::optional<DummyError> result = direct_return_optional::myfunc1(val);
std::optional<DummyError> result = direct_return_optional::myfuncValidRaised(val);
REQUIRE(!anywho::has_error(result));
REQUIRE(val == 9);
}

TEST_CASE("error get returned with anywho_legacy", "[direct_return]")
{
int val = 0;
std::optional<DummyError> result = direct_return_optional::myfuncUnexpectedRaised2(val);
REQUIRE(anywho::has_error(result));
}
TEST_CASE("value get returned with anywho_legacy", "[direct_return]")
{
int val = 0;
std::optional<DummyError> result = direct_return_optional::myfuncValidRaised2(val);
REQUIRE(!anywho::has_error(result));
REQUIRE(val == 27);
}

TEST_CASE("fixed string", "[FixedString]")
{
static constexpr int StringSize = 128;
Expand Down

0 comments on commit 9623031

Please sign in to comment.