Skip to content

Commit

Permalink
test: Add some Expected::andThen() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
taminob committed Feb 19, 2025
1 parent 3af5a12 commit 703f04b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/expected_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,45 @@ TEST(Expected, comparisonEqual)
},
e1, e2);
}

TEST(Expected, andThenReturnRawValue)
{
const Expected<const char*, int> v { "test" };

auto result = v.andThen([]() { return 2.0; });

EXPECT_TRUE(result.hasValue());
EXPECT_EQ(result.value().value_or(0.0), 2.0);
}

TEST(Expected, andThenReturnValueInExpected)
{
const Expected<const char*, int> v { "test" };

auto result = v.andThen([]() -> Expected<double, int> { return 3.0; });

EXPECT_TRUE(result.hasValue());
EXPECT_EQ(result.value().value_or(0.0), 3.0);
}

TEST(Expected, andThenReturnError)
{
const Expected<const char*, int> v { "test" };

auto result = v.andThen([]() -> decltype(v) { return 1; });

// static_assert(std::is_same_v<const decltype(result), decltype(v)>);

EXPECT_FALSE(result.hasValue());
EXPECT_EQ(result.error().value_or(-1), 1);
}

TEST(Expected, andThenError)
{
const Expected<const char*, int> e { 5 };

auto result = e.andThen([]() { return "abc"; });

EXPECT_FALSE(result.hasValue());
EXPECT_EQ(result.error().value_or(-1), 5);
}

0 comments on commit 703f04b

Please sign in to comment.