move test for find to get_related

This commit is contained in:
ToruNiina
2018-12-13 01:30:06 +09:00
parent affa159c82
commit be1a310ae5
2 changed files with 55 additions and 98 deletions

View File

@@ -363,34 +363,3 @@ BOOST_AUTO_TEST_CASE(test_get_toml_offset_datetime)
} }
} }
BOOST_AUTO_TEST_CASE(test_find_and_get)
{
{
toml::value v(true);
bool thrown = false;
try
{
toml::get<toml::boolean>(v, "key");
}
catch(toml::type_error const& te)
{
thrown = true;
}
BOOST_CHECK(thrown);
}
{
toml::table v{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::get<int>(v, "num"));
toml::get<toml::integer>(v, "num") = 54;
BOOST_CHECK_EQUAL(54, toml::get<int>(v, "num"));
}
{
toml::value v = toml::table{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::get<int>(v, "num"));
toml::get<toml::integer>(v, "num") = 54;
BOOST_CHECK_EQUAL(54, toml::get<int>(v, "num"));
}
}

View File

@@ -12,77 +12,65 @@
#include <deque> #include <deque>
#include <array> #include <array>
BOOST_AUTO_TEST_CASE(test_find_and_get)
BOOST_AUTO_TEST_CASE(test_get_or_exist)
{ {
toml::Boolean raw_v1(true); {
toml::Integer raw_v2(42); toml::value v(true);
toml::Float raw_v3(3.14); bool thrown = false;
toml::String raw_v4("hoge"); try
toml::Array raw_v5{2,7,1,8,2}; {
toml::Table raw_v6{{"key", 42}}; toml::get<toml::boolean>(v, "key");
}
catch(toml::type_error const& te)
{
thrown = true;
}
BOOST_CHECK(thrown);
}
toml::value v1(raw_v1); {
toml::value v2(raw_v2); toml::table v{{"num", 42}};
toml::value v3(raw_v3); BOOST_CHECK_EQUAL(42, toml::get<int>(v, "num"));
toml::value v4(raw_v4); toml::get<toml::integer>(v, "num") = 54;
toml::value v5(raw_v5); BOOST_CHECK_EQUAL(54, toml::get<int>(v, "num"));
toml::value v6(raw_v6); }
toml::Table table{ {
{"value1", v1}, toml::value v = toml::table{{"num", 42}};
{"value2", v2}, BOOST_CHECK_EQUAL(42, toml::get<int>(v, "num"));
{"value3", v3}, toml::get<toml::integer>(v, "num") = 54;
{"value4", v4}, BOOST_CHECK_EQUAL(54, toml::get<int>(v, "num"));
{"value5", v5}, }
{"value6", v6}
};
toml::Boolean u1 = toml::get_or(table, "value1", raw_v1);
toml::Integer u2 = toml::get_or(table, "value2", raw_v2);
toml::Float u3 = toml::get_or(table, "value3", raw_v3);
toml::String u4 = toml::get_or(table, "value4", raw_v4);
toml::Array u5 = toml::get_or(table, "value5", raw_v5);
toml::Table u6 = toml::get_or(table, "value6", raw_v6);
BOOST_CHECK_EQUAL(u1, raw_v1);
BOOST_CHECK_EQUAL(u2, raw_v2);
BOOST_CHECK_EQUAL(u3, raw_v3);
BOOST_CHECK_EQUAL(u4, raw_v4);
BOOST_CHECK_EQUAL(u5.at(0).cast<toml::value_t::Integer>(), raw_v5.at(0).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(1).cast<toml::value_t::Integer>(), raw_v5.at(1).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(2).cast<toml::value_t::Integer>(), raw_v5.at(2).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(3).cast<toml::value_t::Integer>(), raw_v5.at(3).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(4).cast<toml::value_t::Integer>(), raw_v5.at(4).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at("key").cast<toml::value_t::Integer>(), 42);
} }
BOOST_AUTO_TEST_CASE(test_get_or_empty) BOOST_AUTO_TEST_CASE(test_get_or)
{ {
toml::Boolean raw_v1(true); {
toml::Integer raw_v2(42); toml::table v{{"num", 42}};
toml::Float raw_v3(3.14); BOOST_CHECK_EQUAL(42, toml::get_or<int>(v, "num", 0));
toml::String raw_v4("hoge"); BOOST_CHECK_EQUAL(0, toml::get_or<int>(v, "foo", 0));
toml::Array raw_v5{2,7,1,8,2}; }
toml::Table raw_v6{{"key", 42}}; {
toml::value v = toml::table{{"num", 42}};
toml::Table table; // empty! BOOST_CHECK_EQUAL(42, toml::get_or<int>(v, "num", 0));
BOOST_CHECK_EQUAL(0, toml::get_or<int>(v, "foo", 0));
toml::Boolean u1 = toml::get_or(table, std::string("value1"), raw_v1); }
toml::Integer u2 = toml::get_or(table, std::string("value2"), raw_v2); {
toml::Float u3 = toml::get_or(table, std::string("value3"), raw_v3); toml::value v1(42);
toml::String u4 = toml::get_or(table, std::string("value4"), raw_v4); toml::value v2(3.14);
toml::Array u5 = toml::get_or(table, std::string("value5"), raw_v5); BOOST_CHECK_EQUAL(42, toml::get_or<int>(v1, 0));
toml::Table u6 = toml::get_or(table, std::string("value6"), raw_v6); BOOST_CHECK_EQUAL(0, toml::get_or<int>(v2, 0));
}
BOOST_CHECK_EQUAL(u1, raw_v1); }
BOOST_CHECK_EQUAL(u2, raw_v2);
BOOST_CHECK_EQUAL(u3, raw_v3); BOOST_AUTO_TEST_CASE(test_expect)
BOOST_CHECK_EQUAL(u4, raw_v4); {
BOOST_CHECK_EQUAL(u5.at(0).cast<toml::value_t::Integer>(), raw_v5.at(0).cast<toml::value_t::Integer>()); {
BOOST_CHECK_EQUAL(u5.at(1).cast<toml::value_t::Integer>(), raw_v5.at(1).cast<toml::value_t::Integer>()); toml::value v1(42);
BOOST_CHECK_EQUAL(u5.at(2).cast<toml::value_t::Integer>(), raw_v5.at(2).cast<toml::value_t::Integer>()); toml::value v2(3.14);
BOOST_CHECK_EQUAL(u5.at(3).cast<toml::value_t::Integer>(), raw_v5.at(3).cast<toml::value_t::Integer>()); BOOST_CHECK_EQUAL(42, toml::expect<int>(v1).unwrap_or(0));
BOOST_CHECK_EQUAL(u5.at(4).cast<toml::value_t::Integer>(), raw_v5.at(4).cast<toml::value_t::Integer>()); BOOST_CHECK_EQUAL( 0, toml::expect<int>(v2).unwrap_or(0));
BOOST_CHECK_EQUAL(u6.at("key").cast<toml::value_t::Integer>(), 42); BOOST_CHECK_EQUAL("42", toml::expect<int>(v1).map([](int i){return std::to_string(i);}).unwrap_or(std::string("none")));
BOOST_CHECK_EQUAL("none", toml::expect<int>(v2).map([](int i){return std::to_string(i);}).unwrap_or(std::string("none")));
}
} }