From 102e201dc7edbc8bdf018d03f3ae92037ce49793 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 27 Jul 2020 19:10:18 -0700 Subject: [PATCH] fix: a failing test case for toml string_vector processing (#491) --- include/CLI/StringTools.hpp | 7 ++++++- tests/ConfigFileTest.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/CLI/StringTools.hpp b/include/CLI/StringTools.hpp index 70f202aa..1500b821 100644 --- a/include/CLI/StringTools.hpp +++ b/include/CLI/StringTools.hpp @@ -310,7 +310,12 @@ inline std::vector split_up(std::string str, char delimiter = '\0') } if(end != std::string::npos) { output.push_back(str.substr(1, end - 1)); - str = str.substr(end + 1); + if(end + 2 < str.size()) { + str = str.substr(end + 2); + } else { + str.clear(); + } + } else { output.push_back(str.substr(1)); str = ""; diff --git a/tests/ConfigFileTest.cpp b/tests/ConfigFileTest.cpp index 370ad976..dc8ee05e 100644 --- a/tests/ConfigFileTest.cpp +++ b/tests/ConfigFileTest.cpp @@ -770,6 +770,30 @@ TEST_F(TApp, TOMLVectordirect) { EXPECT_EQ(std::vector({1, 2, 3}), three); } +TEST_F(TApp, TOMLStringVector) { + + TempFile tmptoml{"TestTomlTmp.toml"}; + + app.set_config("--config", tmptoml); + + { + std::ofstream out{tmptoml}; + out << "#this is a comment line\n"; + out << "[default]\n"; + out << "two=[\"2\",\"3\"]\n"; + out << "three=[\"1\",\"2\",\"3\"]\n"; + } + + std::vector two, three; + app.add_option("--two", two)->required(); + app.add_option("--three", three)->required(); + + run(); + + EXPECT_EQ(std::vector({"2", "3"}), two); + EXPECT_EQ(std::vector({"1", "2", "3"}), three); +} + TEST_F(TApp, IniVectorCsep) { TempFile tmpini{"TestIniTmp.ini"};