add map_err_or_else to result

This commit is contained in:
ToruNiina
2018-12-08 22:44:15 +09:00
parent 2b2a05148e
commit 8388664fc6
2 changed files with 68 additions and 0 deletions

View File

@@ -259,6 +259,49 @@ BOOST_AUTO_TEST_CASE(test_map_or_else)
}
}
BOOST_AUTO_TEST_CASE(test_map_err_or_else)
{
{
const toml::result<int, std::string> result(toml::ok(42));
const auto mapped = result.map_err_or_else(
[](const std::string i) -> std::string {
return i + i;
}, "foobar");
BOOST_CHECK_EQUAL(mapped, "foobar");
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::ok(std::unique_ptr<int>(new int(42))));
const auto mapped = std::move(result).map_err_or_else(
[](const std::string i) -> std::string {
return i + i;
}, "foobar");
BOOST_CHECK_EQUAL(mapped, "foobar");
}
{
const toml::result<int, std::string> result(toml::err<std::string>("hoge"));
const auto mapped = result.map_err_or_else(
[](const std::string i) -> std::string {
return i + i;
}, "foobar");
BOOST_CHECK_EQUAL(mapped, "hogehoge");
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::err<std::string>("hoge"));
const auto mapped = result.map_err_or_else(
[](const std::string i) -> std::string {
return i + i;
}, "foobar");
BOOST_CHECK_EQUAL(mapped, "hogehoge");
}
}
BOOST_AUTO_TEST_CASE(test_and_then)
{
{