mirror of
https://github.com/jarro2783/cxxopts.git
synced 2026-01-18 01:11:24 +01:00
fix the issue with default empty vectors behaviour. update unit test (#460)
This commit is contained in:
@@ -1100,22 +1100,19 @@ void parse_value(const std::string& text, char& c)
|
||||
c = text[0];
|
||||
}
|
||||
|
||||
template<typename T> void add_value(const std::string& text, std::vector<T>& value);
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
parse_value(const std::string& text, std::vector<T>& value)
|
||||
{
|
||||
if (text.empty()) {
|
||||
T v;
|
||||
parse_value(text, v);
|
||||
value.emplace_back(std::move(v));
|
||||
return;
|
||||
}
|
||||
std::stringstream in(text);
|
||||
std::string token;
|
||||
while(!in.eof() && std::getline(in, token, CXXOPTS_VECTOR_DELIMITER)) {
|
||||
T v;
|
||||
parse_value(token, v);
|
||||
value.emplace_back(std::move(v));
|
||||
add_value(token, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -709,6 +709,40 @@ TEST_CASE("std::vector", "[vector]") {
|
||||
CHECK(vector[3] == 4.5);
|
||||
}
|
||||
|
||||
TEST_CASE("empty std::vector", "[vector]") {
|
||||
std::vector<double> double_vector;
|
||||
std::vector<std::string> string_vector;
|
||||
|
||||
cxxopts::Options options("empty vector", " - tests behaviour of empty vector options");
|
||||
|
||||
SECTION("string vector") {
|
||||
options.add_options()
|
||||
("string_vector", "vector of strings", cxxopts::value(string_vector)->default_value(""));
|
||||
|
||||
Argv av({"empty vector"});
|
||||
auto** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse(argc, argv);
|
||||
|
||||
CHECK(string_vector.empty());
|
||||
}
|
||||
|
||||
SECTION("double vector") {
|
||||
options.add_options()
|
||||
("double_vector", "vector of doubles", cxxopts::value(double_vector)->default_value(""));
|
||||
|
||||
Argv av({"empty vector"});
|
||||
auto** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse(argc, argv); // throws
|
||||
|
||||
CHECK(double_vector.empty());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#ifdef CXXOPTS_HAS_OPTIONAL
|
||||
TEST_CASE("std::optional", "[optional]") {
|
||||
std::optional<std::string> optional;
|
||||
|
||||
Reference in New Issue
Block a user