From d53026837abeca22a57127ab700f6a2e807f6bf6 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 1 Sep 2019 14:46:46 +0900 Subject: [PATCH] test: add test for find and check throw --- tests/test_find_fuzzy.cpp | 170 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/tests/test_find_fuzzy.cpp b/tests/test_find_fuzzy.cpp index ae9ab97..6bf9845 100644 --- a/tests/test_find_fuzzy.cpp +++ b/tests/test_find_fuzzy.cpp @@ -103,6 +103,102 @@ BOOST_AUTO_TEST_CASE(test_find_fuzzy) static_assert(std::is_same< toml::value&&, decltype(toml::find_fuzzy(std::move(v), "key"))>::value, ""); } + + // find with conversion + + { + toml::value v{ + {"keu", 42} // typo! key -> keu + }; + + BOOST_TEST(toml::find_fuzzy(v, "key") == 42); + BOOST_CHECK_THROW(toml::find_fuzzy(v, "kiwi"), std::out_of_range); + + static_assert(std::is_same(v, "key"))>::value, ""); + } + { + const toml::value v{ + {"keu", 42} // typo! key -> keu + }; + + BOOST_TEST(toml::find_fuzzy(v, "key") == 42); + BOOST_CHECK_THROW(toml::find_fuzzy(v, "kiwi"), std::out_of_range); + + static_assert(std::is_same(v, "key"))>::value, ""); + } + { + toml::value v{ + {"keu", 42} // typo! key -> keu + }; + + BOOST_TEST(toml::find_fuzzy(std::move(v), "key") == 42); + + static_assert(std::is_same(std::move(v), "key"))>::value, ""); + } + { + toml::value v{ + {"keu", 42} // typo! key -> keu + }; + + BOOST_CHECK_THROW(toml::find_fuzzy(std::move(v), "kiwi"), std::out_of_range); + } +} + +BOOST_AUTO_TEST_CASE(test_find_fuzzy_throw) +{ + { + toml::value v{ + {"keu", "value"}, // typo! key -> keu + {"ky", "value"} // typo! key -> ky + }; + + BOOST_CHECK_THROW(toml::find_fuzzy(v, "key"), std::out_of_range); + } + { + const toml::value v{ + {"keu", "value"}, // typo! key -> keu + {"ky", "value"} // typo! key -> ky + }; + + BOOST_CHECK_THROW(toml::find_fuzzy(v, "key"), std::out_of_range); + } + { + toml::value v{ + {"keu", "value"}, // typo! key -> keu + {"ky", "value"} // typo! key -> ky + }; + + BOOST_CHECK_THROW(toml::find_fuzzy(std::move(v), "key"), std::out_of_range); + } + + { + toml::value v{ + {"keu", 42}, // typo! key -> keu + {"ky", 42} // typo! key -> ky + }; + + BOOST_CHECK_THROW(toml::find_fuzzy(v, "key"), std::out_of_range); + } + { + const toml::value v{ + {"keu", 42}, // typo! key -> keu + {"ky", 42} // typo! key -> ky + }; + + BOOST_CHECK_THROW(toml::find_fuzzy(v, "key"), std::out_of_range); + } + { + toml::value v{ + {"keu", 42}, // typo! key -> keu + {"ky", 42} // typo! key -> ky + }; + + BOOST_CHECK_THROW(toml::find_fuzzy(std::move(v), "key"), std::out_of_range); + } + } BOOST_AUTO_TEST_CASE(test_find_throw_typo_aware_exception) @@ -178,3 +274,77 @@ BOOST_AUTO_TEST_CASE(test_find_throw_typo_aware_exception) toml::value&, decltype(toml::find(v, "key"))>::value, ""); } } + +BOOST_AUTO_TEST_CASE(test_find_throw_conversion_typo_aware_exception) +{ + using namespace toml::literals::toml_literals; + const toml::levenstein_matcher lev(1); + { + toml::value v = u8R"( + keu = 42 + )"_toml; + + BOOST_CHECK_THROW(toml::find(v, "key", lev), std::out_of_range); + try + { + const auto& ret = toml::find(v, "key", lev); + (void)ret; // suppress unused variable + } + catch(const std::out_of_range& oor) + { + // exception.what() should include the typo-ed key name + const std::string what(oor.what()); + BOOST_TEST(what.find("keu") != std::string::npos); + +// std::cout << what << std::endl; + } + static_assert(std::is_same(v, "key"))>::value, ""); + } + { + const toml::value v = u8R"( + keu = 42 + )"_toml; + + BOOST_CHECK_THROW(toml::find(v, "key", lev), std::out_of_range); + try + { + const auto& ret = toml::find(v, "key", lev); + (void)ret; + } + catch(const std::out_of_range& oor) + { + // exception.what() should include the typo-ed key name + const std::string what(oor.what()); + BOOST_TEST(what.find("keu") != std::string::npos); + +// std::cout << what << std::endl; + } + static_assert(std::is_same(v, "key"))>::value, ""); + } + { + toml::value v = u8R"( + keu = 42 + )"_toml; + + bool thrown = false; // since it moves, we need to check both once + try + { + const auto& ret = toml::find(std::move(v), "key", lev); + (void)ret; + } + catch(const std::out_of_range& oor) + { + // exception.what() should include the typo-ed key name + const std::string what(oor.what()); + BOOST_TEST(what.find("keu") != std::string::npos); + thrown = true; + +// std::cout << what << std::endl; + } + BOOST_TEST(thrown); + static_assert(std::is_same(v, "key"))>::value, ""); + } +}