mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-12-16 03:08:52 +08:00
test: update test codes to v4
This commit is contained in:
@@ -1,440 +1,148 @@
|
||||
#include <toml/result.hpp>
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
|
||||
#include "unit_test.hpp"
|
||||
#include <toml11/result.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_construct)
|
||||
TEST_CASE("testing constructor")
|
||||
{
|
||||
{
|
||||
auto s = toml::ok(42);
|
||||
toml::result<int, std::string> result(s);
|
||||
BOOST_TEST(!!result);
|
||||
BOOST_TEST(result.is_ok());
|
||||
BOOST_TEST(!result.is_err());
|
||||
BOOST_TEST(result.unwrap() == 42);
|
||||
CHECK(!!result);
|
||||
CHECK(result.is_ok());
|
||||
CHECK(!result.is_err());
|
||||
CHECK(result.unwrap() == 42);
|
||||
}
|
||||
{
|
||||
const auto s = toml::ok(42);
|
||||
toml::result<int, std::string> result(s);
|
||||
BOOST_TEST(!!result);
|
||||
BOOST_TEST(result.is_ok());
|
||||
BOOST_TEST(!result.is_err());
|
||||
BOOST_TEST(result.unwrap() == 42);
|
||||
CHECK(!!result);
|
||||
CHECK(result.is_ok());
|
||||
CHECK(!result.is_err());
|
||||
CHECK(result.unwrap() == 42);
|
||||
}
|
||||
{
|
||||
toml::result<int, std::string> result(toml::ok(42));
|
||||
BOOST_TEST(!!result);
|
||||
BOOST_TEST(result.is_ok());
|
||||
BOOST_TEST(!result.is_err());
|
||||
BOOST_TEST(result.unwrap() == 42);
|
||||
CHECK(!!result);
|
||||
CHECK(result.is_ok());
|
||||
CHECK(!result.is_err());
|
||||
CHECK(result.unwrap() == 42);
|
||||
}
|
||||
|
||||
{
|
||||
auto f = toml::err<std::string>("foobar");
|
||||
auto f = toml::err("foobar");
|
||||
toml::result<int, std::string> result(f);
|
||||
BOOST_TEST(!result);
|
||||
BOOST_TEST(!result.is_ok());
|
||||
BOOST_TEST(result.is_err());
|
||||
BOOST_TEST(result.unwrap_err() == "foobar");
|
||||
CHECK(!result);
|
||||
CHECK(!result.is_ok());
|
||||
CHECK(result.is_err());
|
||||
CHECK(result.unwrap_err() == "foobar");
|
||||
}
|
||||
{
|
||||
const auto f = toml::err<std::string>("foobar");
|
||||
const auto f = toml::err("foobar");
|
||||
toml::result<int, std::string> result(f);
|
||||
BOOST_TEST(!result);
|
||||
BOOST_TEST(!result.is_ok());
|
||||
BOOST_TEST(result.is_err());
|
||||
BOOST_TEST(result.unwrap_err() == "foobar");
|
||||
CHECK(!result);
|
||||
CHECK(!result.is_ok());
|
||||
CHECK(result.is_err());
|
||||
CHECK(result.unwrap_err() == "foobar");
|
||||
}
|
||||
{
|
||||
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
|
||||
BOOST_TEST(!result);
|
||||
BOOST_TEST(!result.is_ok());
|
||||
BOOST_TEST(result.is_err());
|
||||
BOOST_TEST(result.unwrap_err() == "foobar");
|
||||
toml::result<int, std::string> result(toml::err("foobar"));
|
||||
CHECK(!result);
|
||||
CHECK(!result.is_ok());
|
||||
CHECK(result.is_err());
|
||||
CHECK(result.unwrap_err() == "foobar");
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_assignment)
|
||||
TEST_CASE("testing assignment op")
|
||||
{
|
||||
{
|
||||
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
|
||||
toml::result<int, std::string> 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);
|
||||
CHECK(!!result);
|
||||
CHECK(result.is_ok());
|
||||
CHECK(!result.is_err());
|
||||
CHECK(result.unwrap() == 42);
|
||||
}
|
||||
{
|
||||
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
|
||||
toml::result<int, std::string> 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);
|
||||
CHECK(!!result);
|
||||
CHECK(result.is_ok());
|
||||
CHECK(!result.is_err());
|
||||
CHECK(result.unwrap() == 42);
|
||||
}
|
||||
{
|
||||
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
|
||||
toml::result<int, std::string> 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);
|
||||
CHECK(!!result);
|
||||
CHECK(result.is_ok());
|
||||
CHECK(!result.is_err());
|
||||
CHECK(result.unwrap() == 42);
|
||||
}
|
||||
{
|
||||
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
|
||||
result = toml::err<std::string>("hoge");
|
||||
BOOST_TEST(!result);
|
||||
BOOST_TEST(!result.is_ok());
|
||||
BOOST_TEST(result.is_err());
|
||||
BOOST_TEST(result.unwrap_err() == "hoge");
|
||||
toml::result<int, std::string> result(toml::err("foobar"));
|
||||
result = toml::err("hoge");
|
||||
CHECK(!result);
|
||||
CHECK(!result.is_ok());
|
||||
CHECK(result.is_err());
|
||||
CHECK(result.unwrap_err() == "hoge");
|
||||
}
|
||||
{
|
||||
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
|
||||
auto f = toml::err<std::string>("hoge");
|
||||
toml::result<int, std::string> 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");
|
||||
CHECK(!result);
|
||||
CHECK(!result.is_ok());
|
||||
CHECK(result.is_err());
|
||||
CHECK(result.unwrap_err() == "hoge");
|
||||
}
|
||||
{
|
||||
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
|
||||
const auto f = toml::err<std::string>("hoge");
|
||||
toml::result<int, std::string> 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");
|
||||
CHECK(!result);
|
||||
CHECK(!result.is_ok());
|
||||
CHECK(result.is_err());
|
||||
CHECK(result.unwrap_err() == "hoge");
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_map)
|
||||
TEST_CASE("testing result<reference_wrapper>")
|
||||
{
|
||||
{
|
||||
const toml::result<int, std::string> result(toml::ok(42));
|
||||
const auto mapped = result.map(
|
||||
[](const int i) -> int {
|
||||
return i * 2;
|
||||
});
|
||||
int a = 42;
|
||||
|
||||
BOOST_TEST(!!mapped);
|
||||
BOOST_TEST(mapped.is_ok());
|
||||
BOOST_TEST(!mapped.is_err());
|
||||
BOOST_TEST(mapped.unwrap() == 42 * 2);
|
||||
toml::result<std::reference_wrapper<int>, std::string> result(toml::ok(std::ref(a)));
|
||||
CHECK_UNARY(result);
|
||||
CHECK_UNARY(result.is_ok());
|
||||
CHECK_UNARY_FALSE(result.is_err());
|
||||
|
||||
CHECK_EQ(result.unwrap(), 42);
|
||||
CHECK_EQ(a, 42);
|
||||
|
||||
result.unwrap() = 6 * 9;
|
||||
|
||||
CHECK_EQ(result.unwrap(), 6*9);
|
||||
CHECK_EQ(a, 6*9);
|
||||
}
|
||||
{
|
||||
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(
|
||||
[](std::unique_ptr<int> 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<int, std::string> result(toml::err<std::string>("hoge"));
|
||||
const auto mapped = result.map(
|
||||
[](const int i) -> int {
|
||||
return i * 2;
|
||||
});
|
||||
std::string b = "foo";
|
||||
|
||||
BOOST_TEST(!mapped);
|
||||
BOOST_TEST(!mapped.is_ok());
|
||||
BOOST_TEST(mapped.is_err());
|
||||
BOOST_TEST(mapped.unwrap_err() == "hoge");
|
||||
}
|
||||
{
|
||||
toml::result<std::unique_ptr<int>, std::string>
|
||||
result(toml::err<std::string>("hoge"));
|
||||
const auto mapped = std::move(result).map(
|
||||
[](std::unique_ptr<int> i) -> int {
|
||||
return *i;
|
||||
});
|
||||
toml::result<int, std::reference_wrapper<std::string>> result(toml::err(std::ref(b)));
|
||||
CHECK_UNARY_FALSE(result);
|
||||
CHECK_UNARY_FALSE(result.is_ok());
|
||||
CHECK_UNARY(result.is_err());
|
||||
|
||||
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<int, std::string> 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::unique_ptr<int>, std::string>
|
||||
result(toml::ok(std::unique_ptr<int>(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<int, std::string> result(toml::err<std::string>("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<int, std::unique_ptr<std::string>>
|
||||
result(toml::err(std::unique_ptr<std::string>(new std::string("hoge"))));
|
||||
const auto mapped = std::move(result).map_err(
|
||||
[](std::unique_ptr<std::string> 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<int, std::string> 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::unique_ptr<int>, std::string>
|
||||
result(toml::ok(std::unique_ptr<int>(new int(42))));
|
||||
const auto mapped = std::move(result).map_or_else(
|
||||
[](std::unique_ptr<int> i) -> int {
|
||||
return *i;
|
||||
}, 54);
|
||||
|
||||
BOOST_TEST(mapped == 42);
|
||||
}
|
||||
{
|
||||
const toml::result<int, std::string> result(toml::err<std::string>("hoge"));
|
||||
const auto mapped = result.map_or_else(
|
||||
[](const int i) -> int {
|
||||
return i * 2;
|
||||
}, 54);
|
||||
|
||||
BOOST_TEST(mapped == 54);
|
||||
}
|
||||
{
|
||||
toml::result<std::unique_ptr<int>, std::string>
|
||||
result(toml::err<std::string>("hoge"));
|
||||
const auto mapped = std::move(result).map_or_else(
|
||||
[](std::unique_ptr<int> i) -> int {
|
||||
return *i;
|
||||
}, 54);
|
||||
|
||||
BOOST_TEST(mapped == 54);
|
||||
}
|
||||
}
|
||||
|
||||
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_TEST(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_TEST(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_TEST(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_TEST(mapped == "hogehoge");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_and_then)
|
||||
{
|
||||
{
|
||||
const toml::result<int, std::string> result(toml::ok(42));
|
||||
const auto mapped = result.and_then(
|
||||
[](const int i) -> toml::result<int, std::string> {
|
||||
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::unique_ptr<int>, std::string>
|
||||
result(toml::ok(std::unique_ptr<int>(new int(42))));
|
||||
const auto mapped = std::move(result).and_then(
|
||||
[](std::unique_ptr<int> i) -> toml::result<int, std::string> {
|
||||
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<int, std::string> result(toml::err<std::string>("hoge"));
|
||||
const auto mapped = result.and_then(
|
||||
[](const int i) -> toml::result<int, std::string> {
|
||||
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::unique_ptr<int>, std::string>
|
||||
result(toml::err<std::string>("hoge"));
|
||||
const auto mapped = std::move(result).and_then(
|
||||
[](std::unique_ptr<int> i) -> toml::result<int, std::string> {
|
||||
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<int, std::string> result(toml::ok(42));
|
||||
const auto mapped = result.or_else(
|
||||
[](const std::string& s) -> toml::result<int, 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);
|
||||
}
|
||||
{
|
||||
toml::result<std::unique_ptr<int>, std::string>
|
||||
result(toml::ok(std::unique_ptr<int>(new int(42))));
|
||||
const auto mapped = std::move(result).or_else(
|
||||
[](const std::string& s) -> toml::result<std::unique_ptr<int>, 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<int, std::string> result(toml::err<std::string>("hoge"));
|
||||
const auto mapped = result.or_else(
|
||||
[](const std::string& s) -> toml::result<int, 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");
|
||||
}
|
||||
{
|
||||
toml::result<std::unique_ptr<int>, std::string>
|
||||
result(toml::err<std::string>("hoge"));
|
||||
const auto mapped = std::move(result).or_else(
|
||||
[](const std::string& s) -> toml::result<std::unique_ptr<int>, 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<int, std::string> r1(toml::ok(42));
|
||||
const toml::result<int, std::string> r2(toml::err<std::string>("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<int, std::string> {
|
||||
return toml::ok(42);
|
||||
};
|
||||
auto r2_gen = []() -> toml::result<int, std::string> {
|
||||
return toml::err<std::string>("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());
|
||||
CHECK_EQ(result.unwrap_err(), "foo");
|
||||
CHECK_EQ(b, "foo");
|
||||
|
||||
result.unwrap_err() = "foobar";
|
||||
|
||||
CHECK_EQ(result.unwrap_err(), "foobar");
|
||||
CHECK_EQ(b, "foobar");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user