#define BOOST_TEST_MODULE "test_result" #include #include #include BOOST_AUTO_TEST_CASE(test_construct) { { auto s = toml::ok(42); toml::result result(s); BOOST_TEST(!!result); BOOST_TEST(result.is_ok()); BOOST_TEST(!result.is_err()); BOOST_TEST(result.unwrap() == 42); } { const auto s = toml::ok(42); toml::result result(s); BOOST_TEST(!!result); BOOST_TEST(result.is_ok()); BOOST_TEST(!result.is_err()); BOOST_TEST(result.unwrap() == 42); } { toml::result result(toml::ok(42)); BOOST_TEST(!!result); BOOST_TEST(result.is_ok()); BOOST_TEST(!result.is_err()); BOOST_TEST(result.unwrap() == 42); } { auto f = toml::err("foobar"); toml::result result(f); BOOST_TEST(!result); BOOST_TEST(!result.is_ok()); BOOST_TEST(result.is_err()); BOOST_TEST(result.unwrap_err() == "foobar"); } { const auto f = toml::err("foobar"); toml::result result(f); BOOST_TEST(!result); BOOST_TEST(!result.is_ok()); BOOST_TEST(result.is_err()); BOOST_TEST(result.unwrap_err() == "foobar"); } { toml::result result(toml::err("foobar")); BOOST_TEST(!result); BOOST_TEST(!result.is_ok()); BOOST_TEST(result.is_err()); BOOST_TEST(result.unwrap_err() == "foobar"); } } BOOST_AUTO_TEST_CASE(test_assignment) { { toml::result result(toml::err("foobar")); result = toml::ok(42); BOOST_TEST(!!result); BOOST_TEST(result.is_ok()); BOOST_TEST(!result.is_err()); BOOST_TEST(result.unwrap() == 42); } { toml::result result(toml::err("foobar")); auto s = toml::ok(42); result = s; BOOST_TEST(!!result); BOOST_TEST(result.is_ok()); BOOST_TEST(!result.is_err()); BOOST_TEST(result.unwrap() == 42); } { toml::result result(toml::err("foobar")); const auto s = toml::ok(42); result = s; BOOST_TEST(!!result); BOOST_TEST(result.is_ok()); BOOST_TEST(!result.is_err()); BOOST_TEST(result.unwrap() == 42); } { toml::result result(toml::err("foobar")); result = toml::err("hoge"); BOOST_TEST(!result); BOOST_TEST(!result.is_ok()); BOOST_TEST(result.is_err()); BOOST_TEST(result.unwrap_err() == "hoge"); } { toml::result result(toml::err("foobar")); auto f = toml::err("hoge"); result = f; BOOST_TEST(!result); BOOST_TEST(!result.is_ok()); BOOST_TEST(result.is_err()); BOOST_TEST(result.unwrap_err() == "hoge"); } { toml::result result(toml::err("foobar")); const auto f = toml::err("hoge"); result = f; BOOST_TEST(!result); BOOST_TEST(!result.is_ok()); BOOST_TEST(result.is_err()); BOOST_TEST(result.unwrap_err() == "hoge"); } } BOOST_AUTO_TEST_CASE(test_map) { { const toml::result result(toml::ok(42)); const auto mapped = result.map( [](const int i) -> int { return i * 2; }); BOOST_TEST(!!mapped); BOOST_TEST(mapped.is_ok()); BOOST_TEST(!mapped.is_err()); BOOST_TEST(mapped.unwrap() == 42 * 2); } { toml::result, std::string> result(toml::ok(std::unique_ptr(new int(42)))); const auto mapped = std::move(result).map( [](std::unique_ptr i) -> int { return *i; }); BOOST_TEST(!!mapped); BOOST_TEST(mapped.is_ok()); BOOST_TEST(!mapped.is_err()); BOOST_TEST(mapped.unwrap() == 42); } { const toml::result result(toml::err("hoge")); const auto mapped = result.map( [](const int i) -> int { return i * 2; }); BOOST_TEST(!mapped); BOOST_TEST(!mapped.is_ok()); BOOST_TEST(mapped.is_err()); BOOST_TEST(mapped.unwrap_err() == "hoge"); } { toml::result, std::string> result(toml::err("hoge")); const auto mapped = std::move(result).map( [](std::unique_ptr i) -> int { return *i; }); BOOST_TEST(!mapped); BOOST_TEST(!mapped.is_ok()); BOOST_TEST(mapped.is_err()); BOOST_TEST(mapped.unwrap_err() == "hoge"); } } BOOST_AUTO_TEST_CASE(test_map_err) { { const toml::result result(toml::ok(42)); const auto mapped = result.map_err( [](const std::string s) -> std::string { return s + s; }); BOOST_TEST(!!mapped); BOOST_TEST(mapped.is_ok()); BOOST_TEST(!mapped.is_err()); BOOST_TEST(mapped.unwrap() == 42); } { toml::result, std::string> result(toml::ok(std::unique_ptr(new int(42)))); const auto mapped = std::move(result).map_err( [](const std::string s) -> std::string { return s + s; }); BOOST_TEST(!!mapped); BOOST_TEST(mapped.is_ok()); BOOST_TEST(!mapped.is_err()); BOOST_TEST(*(mapped.unwrap()) == 42); } { const toml::result result(toml::err("hoge")); const auto mapped = result.map_err( [](const std::string s) -> std::string { return s + s; }); BOOST_TEST(!mapped); BOOST_TEST(!mapped.is_ok()); BOOST_TEST(mapped.is_err()); BOOST_TEST(mapped.unwrap_err() == "hogehoge"); } { toml::result> result(toml::err(std::unique_ptr(new std::string("hoge")))); const auto mapped = std::move(result).map_err( [](std::unique_ptr p) -> std::string { return *p; }); BOOST_TEST(!mapped); BOOST_TEST(!mapped.is_ok()); BOOST_TEST(mapped.is_err()); BOOST_TEST(mapped.unwrap_err() == "hoge"); } } BOOST_AUTO_TEST_CASE(test_map_or_else) { { const toml::result result(toml::ok(42)); const auto mapped = result.map_or_else( [](const int i) -> int { return i * 2; }, 54); BOOST_TEST(mapped == 42 * 2); } { toml::result, std::string> result(toml::ok(std::unique_ptr(new int(42)))); const auto mapped = std::move(result).map_or_else( [](std::unique_ptr i) -> int { return *i; }, 54); BOOST_TEST(mapped == 42); } { const toml::result result(toml::err("hoge")); const auto mapped = result.map_or_else( [](const int i) -> int { return i * 2; }, 54); BOOST_TEST(mapped == 54); } { toml::result, std::string> result(toml::err("hoge")); const auto mapped = std::move(result).map_or_else( [](std::unique_ptr i) -> int { return *i; }, 54); BOOST_TEST(mapped == 54); } } BOOST_AUTO_TEST_CASE(test_map_err_or_else) { { const toml::result result(toml::ok(42)); const auto mapped = result.map_err_or_else( [](const std::string i) -> std::string { return i + i; }, "foobar"); BOOST_TEST(mapped == "foobar"); } { toml::result, std::string> result(toml::ok(std::unique_ptr(new int(42)))); const auto mapped = std::move(result).map_err_or_else( [](const std::string i) -> std::string { return i + i; }, "foobar"); BOOST_TEST(mapped == "foobar"); } { const toml::result result(toml::err("hoge")); const auto mapped = result.map_err_or_else( [](const std::string i) -> std::string { return i + i; }, "foobar"); BOOST_TEST(mapped == "hogehoge"); } { toml::result, std::string> result(toml::err("hoge")); const auto mapped = result.map_err_or_else( [](const std::string i) -> std::string { return i + i; }, "foobar"); BOOST_TEST(mapped == "hogehoge"); } } BOOST_AUTO_TEST_CASE(test_and_then) { { const toml::result result(toml::ok(42)); const auto mapped = result.and_then( [](const int i) -> toml::result { return toml::ok(i * 2); }); BOOST_TEST(!!mapped); BOOST_TEST(mapped.is_ok()); BOOST_TEST(!mapped.is_err()); BOOST_TEST(mapped.unwrap() == 42 * 2); } { toml::result, std::string> result(toml::ok(std::unique_ptr(new int(42)))); const auto mapped = std::move(result).and_then( [](std::unique_ptr i) -> toml::result { return toml::ok(*i); }); BOOST_TEST(!!mapped); BOOST_TEST(mapped.is_ok()); BOOST_TEST(!mapped.is_err()); BOOST_TEST(mapped.unwrap() == 42); } { const toml::result result(toml::err("hoge")); const auto mapped = result.and_then( [](const int i) -> toml::result { return toml::ok(i * 2); }); BOOST_TEST(!mapped); BOOST_TEST(!mapped.is_ok()); BOOST_TEST(mapped.is_err()); BOOST_TEST(mapped.unwrap_err() == "hoge"); } { toml::result, std::string> result(toml::err("hoge")); const auto mapped = std::move(result).and_then( [](std::unique_ptr i) -> toml::result { return toml::ok(*i); }); BOOST_TEST(!mapped); BOOST_TEST(!mapped.is_ok()); BOOST_TEST(mapped.is_err()); BOOST_TEST(mapped.unwrap_err() == "hoge"); } } BOOST_AUTO_TEST_CASE(test_or_else) { { const toml::result result(toml::ok(42)); const auto mapped = result.or_else( [](const std::string& s) -> toml::result { return toml::err(s + s); }); BOOST_TEST(!!mapped); BOOST_TEST(mapped.is_ok()); BOOST_TEST(!mapped.is_err()); BOOST_TEST(mapped.unwrap() == 42); } { toml::result, std::string> result(toml::ok(std::unique_ptr(new int(42)))); const auto mapped = std::move(result).or_else( [](const std::string& s) -> toml::result, std::string> { return toml::err(s + s); }); BOOST_TEST(!!mapped); BOOST_TEST(mapped.is_ok()); BOOST_TEST(!mapped.is_err()); BOOST_TEST(*mapped.unwrap() == 42); } { const toml::result result(toml::err("hoge")); const auto mapped = result.or_else( [](const std::string& s) -> toml::result { return toml::err(s + s); }); BOOST_TEST(!mapped); BOOST_TEST(!mapped.is_ok()); BOOST_TEST(mapped.is_err()); BOOST_TEST(mapped.unwrap_err() == "hogehoge"); } { toml::result, std::string> result(toml::err("hoge")); const auto mapped = std::move(result).or_else( [](const std::string& s) -> toml::result, std::string> { return toml::err(s + s); }); BOOST_TEST(!mapped); BOOST_TEST(!mapped.is_ok()); BOOST_TEST(mapped.is_err()); BOOST_TEST(mapped.unwrap_err() == "hogehoge"); } } BOOST_AUTO_TEST_CASE(test_and_or_other) { { const toml::result r1(toml::ok(42)); const toml::result r2(toml::err("foo")); BOOST_TEST(r1 == r1.or_other(r2)); BOOST_TEST(r2 == r1.and_other(r2)); BOOST_TEST(42 == r1.or_other(r2).unwrap()); BOOST_TEST("foo" == r1.and_other(r2).unwrap_err()); } { auto r1_gen = []() -> toml::result { return toml::ok(42); }; auto r2_gen = []() -> toml::result { return toml::err("foo"); }; const auto r3 = r1_gen(); const auto r4 = r2_gen(); BOOST_TEST(r3 == r1_gen().or_other (r2_gen())); BOOST_TEST(r4 == r1_gen().and_other(r2_gen())); BOOST_TEST(42 == r1_gen().or_other (r2_gen()).unwrap()); BOOST_TEST("foo" == r1_gen().and_other(r2_gen()).unwrap_err()); } }