Expected copycon didn't compile, added unit tests for copycon.

This commit is contained in:
Eelke Klein 2017-09-10 10:09:28 +02:00
parent b9bc00a389
commit fd41613b5c
2 changed files with 29 additions and 6 deletions

View file

@ -27,7 +27,7 @@ public:
Expected(const Expected& rhs) Expected(const Expected& rhs)
: m_valid(rhs.valid) : m_valid(rhs.m_valid)
{ {
if (m_valid) { if (m_valid) {
new (&m_value) T(rhs.m_value); new (&m_value) T(rhs.m_value);

View file

@ -18,6 +18,14 @@ TEST(expected, get_when_valid_returns_value)
ASSERT_EQ(v.get(), 42); ASSERT_EQ(v.get(), 42);
} }
TEST(expected, get_when_valid_returns_value_copycon)
{
Expected<int> t = getAnswerToEverything();
Expected<int> v(t);
ASSERT_TRUE(v.valid());
ASSERT_EQ(v.get(), 42);
}
TEST(expected, hasException_when_valid_returns_false) TEST(expected, hasException_when_valid_returns_false)
{ {
Expected<int> v = getAnswerToEverything(); Expected<int> v = getAnswerToEverything();
@ -36,6 +44,13 @@ TEST(expected, T_fromException_get_thows)
ASSERT_THROW (e.get(), std::runtime_error); ASSERT_THROW (e.get(), std::runtime_error);
} }
TEST(expected, T_fromException_get_thows_copycon)
{
auto f = Expected<int>::fromException(std::runtime_error("hello"));
auto e(f);
ASSERT_THROW (e.get(), std::runtime_error);
}
TEST(expected, T_fromException_has_exception_true) TEST(expected, T_fromException_has_exception_true)
{ {
auto e = Expected<int>::fromException(std::runtime_error("hello")); auto e = Expected<int>::fromException(std::runtime_error("hello"));
@ -97,6 +112,9 @@ TEST(expected, T_fromCode_E_has_derived_exception)
ASSERT_TRUE(e.hasException<std::exception>()); ASSERT_TRUE(e.hasException<std::exception>());
} }
//Expected<int> getIntWithStdRuntimeError() { return Expected<void>(); } //Expected<int> getIntWithStdRuntimeError() { return Expected<void>(); }
Expected<void> getNothing() { return Expected<void>(); } Expected<void> getNothing() { return Expected<void>(); }
@ -114,17 +132,20 @@ TEST(expected_void, get_when_valid_returns_value)
ASSERT_NO_THROW(v.get()); ASSERT_NO_THROW(v.get());
} }
TEST(expected_void, get_when_valid_returns_value_copycon)
{
Expected<void> t = getNothing();
auto v(t);
ASSERT_TRUE(v.valid());
ASSERT_NO_THROW(v.get());
}
TEST(expected_void, hasException_when_valid_returns_false) TEST(expected_void, hasException_when_valid_returns_false)
{ {
Expected<void> v = getNothing(); Expected<void> v = getNothing();
ASSERT_FALSE(v.hasException<std::exception>()); ASSERT_FALSE(v.hasException<std::exception>());
} }
TEST(expected_void, void_fromException_is_not_valid) TEST(expected_void, void_fromException_is_not_valid)
{ {
auto e = Expected<void>::fromException(std::runtime_error("hello")); auto e = Expected<void>::fromException(std::runtime_error("hello"));
@ -201,3 +222,5 @@ TEST(expected_void, void_fromCode_E_has_derived_exception)
auto e = Expected<void>::fromCode(expected_void_throws_func); auto e = Expected<void>::fromCode(expected_void_throws_func);
ASSERT_THAT(e.hasException<std::exception>(), Eq(true)); ASSERT_THAT(e.hasException<std::exception>(), Eq(true));
} }