#define BOOST_TEST_MODULE "test_get_or" #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else #define BOOST_TEST_NO_LIB #include #endif #include #include #include #include #include #include BOOST_AUTO_TEST_CASE(test_find_and_get) { { toml::value v(true); bool thrown = false; try { toml::get(v, "key"); } catch(toml::type_error const& te) { thrown = true; } BOOST_CHECK(thrown); } { toml::table v{{"num", 42}}; BOOST_CHECK_EQUAL(42, toml::get(v, "num")); toml::get(v, "num") = 54; BOOST_CHECK_EQUAL(54, toml::get(v, "num")); } { toml::value v = toml::table{{"num", 42}}; BOOST_CHECK_EQUAL(42, toml::get(v, "num")); toml::get(v, "num") = 54; BOOST_CHECK_EQUAL(54, toml::get(v, "num")); } } BOOST_AUTO_TEST_CASE(test_get_or) { { toml::table v{{"num", 42}}; BOOST_CHECK_EQUAL(42, toml::get_or(v, "num", 0)); BOOST_CHECK_EQUAL(0, toml::get_or(v, "foo", 0)); } { toml::value v = toml::table{{"num", 42}}; BOOST_CHECK_EQUAL(42, toml::get_or(v, "num", 0)); BOOST_CHECK_EQUAL(0, toml::get_or(v, "foo", 0)); } { toml::value v1(42); toml::value v2(3.14); BOOST_CHECK_EQUAL(42, toml::get_or(v1, 0)); BOOST_CHECK_EQUAL(0, toml::get_or(v2, 0)); } } BOOST_AUTO_TEST_CASE(test_expect) { { toml::value v1(42); toml::value v2(3.14); BOOST_CHECK_EQUAL(42, toml::expect(v1).unwrap_or(0)); BOOST_CHECK_EQUAL( 0, toml::expect(v2).unwrap_or(0)); BOOST_CHECK_EQUAL("42", toml::expect(v1).map([](int i){return std::to_string(i);}).unwrap_or(std::string("none"))); BOOST_CHECK_EQUAL("none", toml::expect(v2).map([](int i){return std::to_string(i);}).unwrap_or(std::string("none"))); } }