Skip to content

Commit

Permalink
#733-Fix-etl--expected-assignment-from-etl--unexpected
Browse files Browse the repository at this point in the history
Added unit tests for changes
  • Loading branch information
jwellbelove committed Jul 18, 2023
1 parent 9cd491e commit 4a1dc78
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
10 changes: 6 additions & 4 deletions include/etl/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ namespace etl
{
ETL_STATIC_ASSERT(etl::is_copy_constructible<TValue>::value, "Value not copy assignable");

storage.template emplace<Value_Type>(value);
storage = value;
return *this;
}

Expand All @@ -444,7 +444,7 @@ namespace etl
{
ETL_STATIC_ASSERT(etl::is_move_constructible<TValue>::value, "Value not move assignable");

storage.template emplace<Value_Type>(etl::move(value));
storage = etl::move(value);
return *this;
}
#endif
Expand All @@ -456,7 +456,8 @@ namespace etl
{
ETL_STATIC_ASSERT(etl::is_copy_constructible<TError>::value, "Error not copy assignable");

storage.template emplace<Error_Type>(ue.error());
storage = ue.error();

return *this;
}

Expand All @@ -468,7 +469,8 @@ namespace etl
{
ETL_STATIC_ASSERT(etl::is_move_constructible<TError>::value, "Error not move assignable");

storage.template emplace<Error_Type>(etl::move(ue.error()));
storage = etl::move(ue.error());

return *this;
}
#endif
Expand Down
39 changes: 39 additions & 0 deletions test/test_expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ namespace
{
}

operator std::string() const
{
return e;
}

ErrorM(ErrorM&&) = default;
ErrorM& operator =(ErrorM&&) = default;

Expand Down Expand Up @@ -322,6 +327,23 @@ namespace
//CHECK(output2.v == input2.v);
}

//*************************************************************************
TEST(test_copy_assign_from_error)
{
Value input = { "value 1" };
Expected expected(input);

Error error = { "error 1" };
Unexpected unexpected(error);

expected = unexpected;

Error output = expected.error();

CHECK_FALSE(expected.has_value());
CHECK_EQUAL(std::string(error), std::string(expected.error()));
}

//*************************************************************************
TEST(test_move_construct)
{
Expand Down Expand Up @@ -360,6 +382,23 @@ namespace
CHECK_EQUAL("value 1", output2.v);
}

//*************************************************************************
TEST(test_move_assign_from_error)
{
ValueM input = { "value 1" };
ExpectedM expected(etl::move(input));

ErrorM error = { "error 1" };
UnexpectedM unexpected(etl::move(error));

expected = etl::move(unexpected);

ErrorM output = etl::move(expected.error());

CHECK_FALSE(expected.has_value());
CHECK_EQUAL(std::string(error), std::string(expected.error()));
}

//*************************************************************************
TEST(test_copy_construct_void_value)
{
Expand Down

0 comments on commit 4a1dc78

Please sign in to comment.