set test_from_toml

This commit is contained in:
ToruNiina 2018-12-13 02:24:03 +09:00
parent 77b237c53a
commit 514f3c773f
3 changed files with 47 additions and 175 deletions

View File

@ -22,7 +22,7 @@ set(TEST_NAMES
test_get
test_get_related_func
test_to_toml
# test_from_toml
test_from_toml
# test_parse_file
# test_parse_unicode
)

View File

@ -12,180 +12,52 @@
#include <deque>
#include <array>
BOOST_AUTO_TEST_CASE(test_from_toml_exact)
BOOST_AUTO_TEST_CASE(test_from_toml)
{
toml::Boolean b(true);
toml::Integer i(42);
toml::Float f(3.14);
toml::String s("hoge");
toml::Datetime d(std::chrono::system_clock::now());
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
toml::boolean b = false;
toml::integer i = 0;
toml::floating f = 0.;
toml::string s;
toml::local_date dt;
toml::array a;
toml::table t;
{
toml::value v(true);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK_EQUAL(b, true);
}
{
toml::value v(42);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK_EQUAL(i, 42);
}
{
toml::value v(3.14);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK_EQUAL(f, 3.14);
}
{
toml::value v("foo");
toml::value v1(b);
toml::value v2(i);
toml::value v3(f);
toml::value v4(s);
toml::value v5(d);
toml::value v6(a);
toml::value v7(t);
toml::Boolean u1;
toml::Integer u2;
toml::Float u3;
toml::String u4;
toml::Datetime u5;
toml::Array u6;
toml::Table u7;
toml::from_toml(u1, v1);
toml::from_toml(u2, v2);
toml::from_toml(u3, v3);
toml::from_toml(u4, v4);
toml::from_toml(u5, v5);
toml::from_toml(u6, v6);
toml::from_toml(u7, v7);
BOOST_CHECK_EQUAL(u1, b);
BOOST_CHECK_EQUAL(u2, i);
BOOST_CHECK_EQUAL(u3, f);
BOOST_CHECK_EQUAL(u4, s);
BOOST_CHECK_EQUAL(u6.at(0).cast<toml::value_t::Integer>(), a.at(0).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(1).cast<toml::value_t::Integer>(), a.at(1).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(2).cast<toml::value_t::Integer>(), a.at(2).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(3).cast<toml::value_t::Integer>(), a.at(3).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(4).cast<toml::value_t::Integer>(), a.at(4).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u7.at("val1").cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(u7.at("val2").cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(u7.at("val3").cast<toml::value_t::Float>(),3.14, 1e-3);
BOOST_CHECK_EQUAL(u7.at("val4").cast<toml::value_t::String>(), "piyo");
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK_EQUAL(s, "foo");
}
{
toml::value v(toml::local_date(2018, toml::month_t::Apr, 22));
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK(dt == toml::local_date(2018, toml::month_t::Apr, 22));
}
{
toml::array ref{toml::value(42), toml::value(54)};
toml::value v(ref);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK(ref == a);
}
{
toml::table ref{{"key1", 42}, {"key2", 3.14}};
toml::value v(ref);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK(ref == t);
}
}
BOOST_AUTO_TEST_CASE(test_from_toml_cast)
{
toml::Integer i(42);
toml::Float f(3.14);
toml::Array a{2, 7, 1, 8, 2};
toml::Table t{{"val1", true}, {"val2", 42}, {"val3", 3.14}, {"val4", "piyo"}};
toml::value vi(i);
toml::value vf(f);
toml::value va(a);
toml::value vt(t);
int u1;
std::size_t u2;
float u3;
std::list<int> u4;
std::deque<std::size_t> u5;
std::array<long, 5> u6;
std::map<std::string, toml::value> u7;
std::list<int> expect_list{2,7,1,8,2};
std::deque<std::size_t> expect_deque{2,7,1,8,2};
std::array<long, 5> expect_array{{2,7,1,8,2}};
toml::from_toml(u1, vi);
toml::from_toml(u2, vi);
toml::from_toml(u3, vf);
toml::from_toml(u4, va);
toml::from_toml(u5, va);
toml::from_toml(u6, va);
toml::from_toml(u7, vt);
BOOST_CHECK_EQUAL(u1, 42);
BOOST_CHECK_EQUAL(u2, 42ul);
BOOST_CHECK_CLOSE_FRACTION(u3, 3.14, 1e-3);
const bool same_list = (u4 == expect_list);
const bool same_deque = (u5 == expect_deque);
const bool same_array = (u6 == expect_array);
BOOST_CHECK(same_list);
BOOST_CHECK(same_deque);
BOOST_CHECK(same_array);
BOOST_CHECK_EQUAL(u7["val1"].cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(u7["val2"].cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(u7["val3"].cast<toml::value_t::Float>(), 3.14, 1e-3);
BOOST_CHECK_EQUAL(u7["val4"].cast<toml::value_t::String >(), "piyo");
}
BOOST_AUTO_TEST_CASE(test_from_toml_tie)
{
toml::Boolean b(42);
toml::Integer i(42);
toml::Float f(3.14);
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
toml::value vb(b);
toml::value vi(i);
toml::value vf(f);
toml::value va(a);
toml::value vt(t);
bool ub;
int ui;
float uf;
std::deque<int> ua;
std::map<std::string, toml::value> ut;
toml::from_toml(std::tie(ub, ui, uf, ua, ut), vb);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), vi);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), vf);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), va);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), va);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), vt);
BOOST_CHECK_EQUAL(ub, true);
BOOST_CHECK_EQUAL(ui, 42);
BOOST_CHECK_CLOSE_FRACTION(uf, 3.14, 1e-3);
BOOST_CHECK_EQUAL(ua.at(0), 2);
BOOST_CHECK_EQUAL(ua.at(1), 7);
BOOST_CHECK_EQUAL(ua.at(2), 1);
BOOST_CHECK_EQUAL(ua.at(3), 8);
BOOST_CHECK_EQUAL(ua.at(4), 2);
BOOST_CHECK_EQUAL(ut["val1"].cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(ut["val2"].cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(ut["val3"].cast<toml::value_t::Float>(), 3.14, 1e-3);
BOOST_CHECK_EQUAL(ut["val4"].cast<toml::value_t::String >(), "piyo");
}
BOOST_AUTO_TEST_CASE(test_from_toml_tuple)
{
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::value v(a);
std::tuple<int, int, int, int, int> t;
toml::from_toml(t, v);
BOOST_CHECK_EQUAL(std::get<0>(t), 2);
BOOST_CHECK_EQUAL(std::get<1>(t), 7);
BOOST_CHECK_EQUAL(std::get<2>(t), 1);
BOOST_CHECK_EQUAL(std::get<3>(t), 8);
BOOST_CHECK_EQUAL(std::get<4>(t), 2);
}

View File

@ -8,7 +8,7 @@ namespace toml
template<typename T>
void from_toml(T& x, const toml::value& v)
{
x = toml::get<T>(v);
x = toml::get<typename std::remove_reference<T>::type>(v);
return;
}