toml11/tests/test_get_related_func.cpp

77 lines
2.1 KiB
C++
Raw Normal View History

2017-12-11 05:30:25 +00:00
#define BOOST_TEST_MODULE "test_get_or"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
2017-12-11 03:04:57 +00:00
#include <boost/test/included/unit_test.hpp>
2017-12-11 05:30:25 +00:00
#endif
2017-12-11 03:04:57 +00:00
#include <toml.hpp>
#include <map>
#include <unordered_map>
#include <list>
#include <deque>
#include <array>
2018-12-12 16:30:06 +00:00
BOOST_AUTO_TEST_CASE(test_find_and_get)
2017-12-11 03:04:57 +00:00
{
2018-12-12 16:30:06 +00:00
{
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);
}
2017-12-11 03:04:57 +00:00
2018-12-12 16:30:06 +00:00
{
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"));
}
2017-12-11 03:04:57 +00:00
2018-12-12 16:30:06 +00:00
{
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"));
}
2017-12-11 03:04:57 +00:00
}
2018-12-12 16:30:06 +00:00
BOOST_AUTO_TEST_CASE(test_get_or)
2017-12-11 03:04:57 +00:00
{
2018-12-12 16:30:06 +00:00
{
toml::table v{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::get_or<int>(v, "num", 0));
BOOST_CHECK_EQUAL(0, toml::get_or<int>(v, "foo", 0));
}
{
toml::value v = toml::table{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::get_or<int>(v, "num", 0));
BOOST_CHECK_EQUAL(0, toml::get_or<int>(v, "foo", 0));
}
{
toml::value v1(42);
toml::value v2(3.14);
BOOST_CHECK_EQUAL(42, toml::get_or<int>(v1, 0));
BOOST_CHECK_EQUAL(0, toml::get_or<int>(v2, 0));
}
}
2017-12-11 03:04:57 +00:00
2018-12-12 16:30:06 +00:00
BOOST_AUTO_TEST_CASE(test_expect)
{
{
toml::value v1(42);
toml::value v2(3.14);
BOOST_CHECK_EQUAL(42, toml::expect<int>(v1).unwrap_or(0));
BOOST_CHECK_EQUAL( 0, toml::expect<int>(v2).unwrap_or(0));
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")));
}
2017-12-11 03:04:57 +00:00
}