Commit 102e201dc7edbc8bdf018d03f3ae92037ce49793

Authored by Philip Top
Committed by GitHub
1 parent 34bd8097

fix: a failing test case for toml string_vector processing (#491)

include/CLI/StringTools.hpp
@@ -310,7 +310,12 @@ inline std::vector<std::string> split_up(std::string str, char delimiter = '\0') @@ -310,7 +310,12 @@ inline std::vector<std::string> split_up(std::string str, char delimiter = '\0')
310 } 310 }
311 if(end != std::string::npos) { 311 if(end != std::string::npos) {
312 output.push_back(str.substr(1, end - 1)); 312 output.push_back(str.substr(1, end - 1));
313 - str = str.substr(end + 1); 313 + if(end + 2 < str.size()) {
  314 + str = str.substr(end + 2);
  315 + } else {
  316 + str.clear();
  317 + }
  318 +
314 } else { 319 } else {
315 output.push_back(str.substr(1)); 320 output.push_back(str.substr(1));
316 str = ""; 321 str = "";
tests/ConfigFileTest.cpp
@@ -770,6 +770,30 @@ TEST_F(TApp, TOMLVectordirect) { @@ -770,6 +770,30 @@ TEST_F(TApp, TOMLVectordirect) {
770 EXPECT_EQ(std::vector<int>({1, 2, 3}), three); 770 EXPECT_EQ(std::vector<int>({1, 2, 3}), three);
771 } 771 }
772 772
  773 +TEST_F(TApp, TOMLStringVector) {
  774 +
  775 + TempFile tmptoml{"TestTomlTmp.toml"};
  776 +
  777 + app.set_config("--config", tmptoml);
  778 +
  779 + {
  780 + std::ofstream out{tmptoml};
  781 + out << "#this is a comment line\n";
  782 + out << "[default]\n";
  783 + out << "two=[\"2\",\"3\"]\n";
  784 + out << "three=[\"1\",\"2\",\"3\"]\n";
  785 + }
  786 +
  787 + std::vector<std::string> two, three;
  788 + app.add_option("--two", two)->required();
  789 + app.add_option("--three", three)->required();
  790 +
  791 + run();
  792 +
  793 + EXPECT_EQ(std::vector<std::string>({"2", "3"}), two);
  794 + EXPECT_EQ(std::vector<std::string>({"1", "2", "3"}), three);
  795 +}
  796 +
773 TEST_F(TApp, IniVectorCsep) { 797 TEST_F(TApp, IniVectorCsep) {
774 798
775 TempFile tmpini{"TestIniTmp.ini"}; 799 TempFile tmpini{"TestIniTmp.ini"};