-
Notifications
You must be signed in to change notification settings - Fork 385
fix universal: make utils::expected description consistent #1204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
|
|
@@ -42,46 +43,80 @@ TEST(Expected, ErrorCtor) { | |
| EXPECT_FALSE(ei); | ||
| EXPECT_FALSE(ev.has_value()); | ||
| EXPECT_FALSE(ev); | ||
| EXPECT_EQ(const_cast<const ExpectedInt&>(ei).error(), "string error"); | ||
| EXPECT_EQ(const_cast<const ExpectedVoid&>(ev).error(), "string error"); | ||
| ASSERT_TRUE(ei.has_error()); | ||
| ASSERT_TRUE(ev.has_error()); | ||
| ASSERT_NO_THROW(ei.error()); | ||
| ASSERT_NO_THROW(ev.error()); | ||
| EXPECT_EQ(std::as_const(ei).error(), "string error"); | ||
| EXPECT_EQ(std::as_const(ev).error(), "string error"); | ||
|
|
||
| ei.error() = "another error"; | ||
| ev.error() = "one more error"; | ||
|
|
||
| EXPECT_EQ(ei.error(), "another error"); | ||
| EXPECT_EQ(ev.error(), "one more error"); | ||
|
|
||
| ei = ExpectedInt{utils::unexpected<const char*>("converted error")}; | ||
| ev = ExpectedVoid{utils::unexpected<const char*>("converted error")}; | ||
| auto error2 = utils::unexpected<const char*>("converted error"); | ||
|
|
||
| ei = ExpectedInt{error2}; | ||
| ev = ExpectedVoid{std::move(error2)}; | ||
|
|
||
| EXPECT_FALSE(ei.has_value()); | ||
| EXPECT_FALSE(ei); | ||
| EXPECT_FALSE(ev.has_value()); | ||
| EXPECT_FALSE(ev); | ||
| ASSERT_TRUE(ei.has_error()); | ||
| ASSERT_TRUE(ev.has_error()); | ||
| ASSERT_NO_THROW(ei.error()); | ||
| ASSERT_NO_THROW(ev.error()); | ||
| EXPECT_EQ(ei.error(), "converted error"); | ||
| EXPECT_EQ(ev.error(), "converted error"); | ||
| } | ||
|
|
||
| TEST(Expected, ValueThrowsIfExpectedContainsError) { | ||
| TEST(Expected, ValueThrowsIfExpectedContainsNoValue) { | ||
| auto error = utils::unexpected{std::string("string error")}; | ||
|
|
||
| ExpectedInt ei{error}; | ||
| ExpectedVoid ev{std::move(error)}; | ||
|
|
||
| EXPECT_THROW(const_cast<const ExpectedInt&>(ei).value(), utils::bad_expected_access); | ||
| ASSERT_FALSE(ei.has_value()); | ||
| ASSERT_FALSE(ev.has_value()); | ||
| EXPECT_THROW(std::as_const(ei).value(), utils::bad_expected_access); | ||
| EXPECT_THROW(ei.value(), utils::bad_expected_access); | ||
| EXPECT_THROW(std::move(ei).value(), utils::bad_expected_access); | ||
| EXPECT_THROW(ev.value(), utils::bad_expected_access); | ||
| } | ||
|
|
||
| TEST(Expected, ErrorThrowsIfExpectedContainsValue) { | ||
| TEST(Expected, ErrorThrowsIfExpectedContainsNoError) { | ||
| ExpectedInt ei{10}; | ||
| ExpectedVoid ev; | ||
|
|
||
| EXPECT_THROW(const_cast<const ExpectedInt&>(ei).error(), utils::bad_expected_access); | ||
| ASSERT_FALSE(ei.has_error()); | ||
| ASSERT_FALSE(ev.has_error()); | ||
| EXPECT_THROW(std::as_const(ei).error(), utils::bad_expected_access); | ||
| EXPECT_THROW(ei.error(), utils::bad_expected_access); | ||
| EXPECT_THROW(const_cast<const ExpectedVoid&>(ev).error(), utils::bad_expected_access); | ||
| EXPECT_THROW(std::as_const(ev).error(), utils::bad_expected_access); | ||
| EXPECT_THROW(ev.error(), utils::bad_expected_access); | ||
| } | ||
|
|
||
| TEST(Expected, ValuelessByException) { | ||
| struct Throw { | ||
| Throw() = default; | ||
| Throw(const Throw&) { throw 0; } | ||
| Throw& operator=(const Throw&) { throw 0; } | ||
| }; | ||
| using Expected = utils::expected<Throw, int>; | ||
|
|
||
| Expected e(utils::unexpected(0)); | ||
| try { | ||
| e = Expected{}; | ||
| FAIL(); | ||
| } catch (...) {} | ||
|
|
||
| EXPECT_FALSE(e.has_value()); | ||
| EXPECT_THROW(e.value(), utils::bad_expected_access); | ||
| EXPECT_FALSE(e.has_error()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should behave differently http://eel.is/c++draft/expected.object.assign
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you suggesting adding an implementation of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep... We are planning to remove utils::expected after a few years and replace it with std::expected The closer the behavior of those two types - the better. |
||
| EXPECT_THROW(e.error(), utils::bad_expected_access); | ||
| } | ||
|
|
||
| USERVER_NAMESPACE_END | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's easy to accidentally confuse constants 1 and 2. Make those a named constants kSuccessIndex and kErrorIndex and use the readable names everywhere in this file