From dd4bbd88479d7d9806d0c4d03d7534d97c3bcc35 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Sat, 7 Oct 2023 08:20:29 -0700 Subject: [PATCH] Coverage to 100% (#929) remove old code since all arguments are quoted now and the code was not being used, add coverage exclusion on some code that should never be executed and add an additional test --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/CLI/StringTools.hpp | 3 --- include/CLI/impl/StringTools_inl.hpp | 11 --------- include/CLI/impl/Validators_inl.hpp | 2 +- tests/AppTest.cpp | 36 ++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/include/CLI/StringTools.hpp b/include/CLI/StringTools.hpp index 4a035b32..87af9d93 100644 --- a/include/CLI/StringTools.hpp +++ b/include/CLI/StringTools.hpp @@ -220,9 +220,6 @@ CLI11_INLINE std::vector split_up(std::string str, char delimiter = /// the return value is the offset+1 which is required by the find_and_modify function. CLI11_INLINE std::size_t escape_detect(std::string &str, std::size_t offset); -/// Add quotes if the string contains spaces -CLI11_INLINE std::string &add_quotes_if_needed(std::string &str); - /// get the value of an environmental variable or empty string if empty CLI11_INLINE std::string get_environment_value(const std::string &env_name); } // namespace detail diff --git a/include/CLI/impl/StringTools_inl.hpp b/include/CLI/impl/StringTools_inl.hpp index fd3dcc2f..e5761c23 100644 --- a/include/CLI/impl/StringTools_inl.hpp +++ b/include/CLI/impl/StringTools_inl.hpp @@ -244,17 +244,6 @@ CLI11_INLINE std::size_t escape_detect(std::string &str, std::size_t offset) { return offset + 1; } -CLI11_INLINE std::string &add_quotes_if_needed(std::string &str) { - if((str.front() != '"' && str.front() != '\'') || str.front() != str.back()) { - char quote = str.find('"') < str.find('\'') ? '\'' : '"'; - if(str.find(' ') != std::string::npos) { - str.insert(0, 1, quote); - str.append(1, quote); - } - } - return str; -} - std::string get_environment_value(const std::string &env_name) { char *buffer = nullptr; std::string ename_string; diff --git a/include/CLI/impl/Validators_inl.hpp b/include/CLI/impl/Validators_inl.hpp index a2295ecd..1332e55e 100644 --- a/include/CLI/impl/Validators_inl.hpp +++ b/include/CLI/impl/Validators_inl.hpp @@ -135,7 +135,7 @@ CLI11_INLINE path_type check_path(const char *file) noexcept { switch(stat.type()) { case std::filesystem::file_type::none: // LCOV_EXCL_LINE case std::filesystem::file_type::not_found: - return path_type::nonexistent; + return path_type::nonexistent; // LCOV_EXCL_LINE case std::filesystem::file_type::directory: return path_type::directory; case std::filesystem::file_type::symlink: diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 386418d4..8e5d57f2 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -1101,6 +1101,42 @@ TEST_CASE_METHOD(TApp, "emptyVectorReturn", "[app]") { CHECK_FALSE(strs3.empty()); } +TEST_CASE_METHOD(TApp, "emptyVectorReturnReduce", "[app]") { + + std::vector strs; + std::vector strs2; + std::vector strs3; + auto *opt1 = app.add_option("--str", strs)->required()->expected(0, 2); + app.add_option("--str3", strs3)->expected(1, 3); + app.add_option("--str2", strs2)->expected(1, 1)->take_first(); + args = {"--str"}; + + CHECK_NOTHROW(run()); + CHECK(std::vector({""}) == strs); + args = {"--str", "one", "two"}; + + run(); + + CHECK(std::vector({"one", "two"}) == strs); + + args = {"--str", "{}", "--str2", "{}", "test"}; + + run(); + + CHECK(strs.empty()); + CHECK(std::vector{"{}"} == strs2); + opt1->default_str("{}"); + args = {"--str"}; + + CHECK_NOTHROW(run()); + CHECK(strs.empty()); + opt1->required(false); + args = {"--str3", "{}"}; + + CHECK_NOTHROW(run()); + CHECK_FALSE(strs3.empty()); +} + TEST_CASE_METHOD(TApp, "RequiredOptsDoubleShort", "[app]") { std::vector strs;