Files
toml11/tests/test_result.cpp

149 lines
3.9 KiB
C++
Raw Normal View History

2024-06-15 19:14:06 +09:00
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
2024-06-15 19:14:06 +09:00
#include <toml11/result.hpp>
2024-06-15 19:14:06 +09:00
TEST_CASE("testing constructor")
{
{
auto s = toml::ok(42);
toml::result<int, std::string> result(s);
2024-06-15 19:14:06 +09:00
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);
2024-06-15 19:14:06 +09:00
CHECK(!!result);
CHECK(result.is_ok());
CHECK(!result.is_err());
CHECK(result.unwrap() == 42);
}
{
toml::result<int, std::string> result(toml::ok(42));
2024-06-15 19:14:06 +09:00
CHECK(!!result);
CHECK(result.is_ok());
CHECK(!result.is_err());
CHECK(result.unwrap() == 42);
}
{
2024-06-15 19:14:06 +09:00
auto f = toml::err("foobar");
toml::result<int, std::string> result(f);
2024-06-15 19:14:06 +09:00
CHECK(!result);
CHECK(!result.is_ok());
CHECK(result.is_err());
CHECK(result.unwrap_err() == "foobar");
}
{
2024-06-15 19:14:06 +09:00
const auto f = toml::err("foobar");
toml::result<int, std::string> result(f);
2024-06-15 19:14:06 +09:00
CHECK(!result);
CHECK(!result.is_ok());
CHECK(result.is_err());
CHECK(result.unwrap_err() == "foobar");
}
{
2024-06-15 19:14:06 +09:00
toml::result<int, std::string> result(toml::err("foobar"));
CHECK(!result);
CHECK(!result.is_ok());
CHECK(result.is_err());
CHECK(result.unwrap_err() == "foobar");
}
}
2024-06-15 19:14:06 +09:00
TEST_CASE("testing assignment op")
{
{
2024-06-15 19:14:06 +09:00
toml::result<int, std::string> result(toml::err("foobar"));
result = toml::ok(42);
2024-06-15 19:14:06 +09:00
CHECK(!!result);
CHECK(result.is_ok());
CHECK(!result.is_err());
CHECK(result.unwrap() == 42);
}
{
2024-06-15 19:14:06 +09:00
toml::result<int, std::string> result(toml::err("foobar"));
auto s = toml::ok(42);
result = s;
2024-06-15 19:14:06 +09:00
CHECK(!!result);
CHECK(result.is_ok());
CHECK(!result.is_err());
CHECK(result.unwrap() == 42);
}
{
2024-06-15 19:14:06 +09:00
toml::result<int, std::string> result(toml::err("foobar"));
const auto s = toml::ok(42);
result = s;
2024-06-15 19:14:06 +09:00
CHECK(!!result);
CHECK(result.is_ok());
CHECK(!result.is_err());
CHECK(result.unwrap() == 42);
}
{
2024-06-15 19:14:06 +09:00
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");
}
{
2024-06-15 19:14:06 +09:00
toml::result<int, std::string> result(toml::err("foobar"));
auto f = toml::err("hoge");
result = f;
2024-06-15 19:14:06 +09:00
CHECK(!result);
CHECK(!result.is_ok());
CHECK(result.is_err());
CHECK(result.unwrap_err() == "hoge");
}
{
2024-06-15 19:14:06 +09:00
toml::result<int, std::string> result(toml::err("foobar"));
const auto f = toml::err("hoge");
result = f;
2024-06-15 19:14:06 +09:00
CHECK(!result);
CHECK(!result.is_ok());
CHECK(result.is_err());
CHECK(result.unwrap_err() == "hoge");
2018-12-06 12:47:14 +09:00
}
}
2024-06-15 19:14:06 +09:00
TEST_CASE("testing result<reference_wrapper>")
2018-12-08 22:44:15 +09:00
{
{
2024-06-15 19:14:06 +09:00
int a = 42;
2018-12-06 12:47:14 +09:00
2024-06-15 19:14:06 +09:00
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());
2018-12-06 12:47:14 +09:00
2024-06-15 19:14:06 +09:00
CHECK_EQ(result.unwrap(), 42);
CHECK_EQ(a, 42);
2018-12-06 12:47:14 +09:00
2024-06-15 19:14:06 +09:00
result.unwrap() = 6 * 9;
2018-12-06 12:47:14 +09:00
2024-06-15 19:14:06 +09:00
CHECK_EQ(result.unwrap(), 6*9);
CHECK_EQ(a, 6*9);
2018-12-06 12:47:14 +09:00
}
{
2024-06-15 19:14:06 +09:00
std::string b = "foo";
2018-12-06 12:47:14 +09:00
2024-06-15 19:14:06 +09:00
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());
2024-06-15 19:14:06 +09:00
CHECK_EQ(result.unwrap_err(), "foo");
CHECK_EQ(b, "foo");
2024-06-15 19:14:06 +09:00
result.unwrap_err() = "foobar";
2024-06-15 19:14:06 +09:00
CHECK_EQ(result.unwrap_err(), "foobar");
CHECK_EQ(b, "foobar");
}
}