1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Broken tests only because adding new desired behavior

This commit is contained in:
Henry Fredrick Schreiner 2017-03-01 16:01:29 -05:00
parent 78dc2a5b92
commit 7856de43ca
4 changed files with 86 additions and 3 deletions

View File

@ -830,7 +830,7 @@ protected:
[](std::pair<detail::Classifer, std::string>& val){return val.first != detail::Classifer::POSITIONAL_MARK;}); [](std::pair<detail::Classifer, std::string>& val){return val.first != detail::Classifer::POSITIONAL_MARK;});
if(num_left_over>0 && !allow_extras_) if(num_left_over>0 && !allow_extras_)
throw ExtrasError("[" + detail::join(args, " ") + "]"); throw ExtrasError("[" + detail::rjoin(args, " ") + "]");
pre_callback(); pre_callback();
run_callback(); run_callback();
@ -916,7 +916,7 @@ protected:
auto op_ptr = std::find_if(std::begin(options_), std::end(options_), [name](const Option_p &v){return v->check_lname(name);}); auto op_ptr = std::find_if(std::begin(options_), std::end(options_), [name](const Option_p &v){return v->check_lname(name);});
if(op_ptr == std::end(options_)) { if(op_ptr == std::end(options_)) {
missing_.emplace_back(detail::Classifer::LONG, "--" + name); missing_.emplace_back(detail::Classifer::LONG, current);
return; return;
} }

View File

@ -27,6 +27,17 @@ std::string join(const T& v, std::string delim = ",") {
return s.str(); return s.str();
} }
template<typename T>
std::string rjoin(const T& v, std::string delim = ",") {
std::ostringstream s;
for(size_t start=0; start<v.size(); start++) {
if(start > 0)
s << delim;
s << v[v.size() - start - 1];
}
return s.str();
}
// Based on http://stackoverflow.com/questions/25829143/c-trim-whitespace-from-a-string // Based on http://stackoverflow.com/questions/25829143/c-trim-whitespace-from-a-string
/// Trim whitespace from left of string /// Trim whitespace from left of string

View File

@ -188,3 +188,16 @@ TEST(String, ToLower) {
EXPECT_EQ("one and two", CLI::detail::to_lower("one And TWO")); EXPECT_EQ("one and two", CLI::detail::to_lower("one And TWO"));
} }
TEST(Join, Forward) {
std::vector<std::string> val {{"one", "two", "three"}};
EXPECT_EQ("one,two,three", CLI::detail::join(val));
EXPECT_EQ("one;two;three", CLI::detail::join(val, ";"));
}
TEST(Join, Backward) {
std::vector<std::string> val {{"three", "two", "one"}};
EXPECT_EQ("one,two,three", CLI::detail::join(val));
EXPECT_EQ("one;two;three", CLI::detail::join(val, ";"));
}

View File

@ -36,7 +36,6 @@ TEST_F(TApp, Callbacks) {
val = true; val = true;
}); });
app.reset();
args = {"sub2"}; args = {"sub2"};
EXPECT_FALSE(val); EXPECT_FALSE(val);
EXPECT_NO_THROW(run()); EXPECT_NO_THROW(run());
@ -44,6 +43,66 @@ TEST_F(TApp, Callbacks) {
} }
TEST_F(TApp, FallThroughRegular) {
int val = 1;
app.add_option("--val", val);
auto sub = app.add_subcommand("sub");
args = {"sub", "--val", "2"};
// Should not throw
run();
}
TEST_F(TApp, FallThroughEquals) {
int val = 1;
app.add_option("--val", val);
auto sub = app.add_subcommand("sub");
args = {"sub", "--val=2"};
// Should not throw
run();
}
TEST_F(TApp, EvilParseFallthrough) {
int val1 = 0, val2 = 0;
app.add_option("--val1", val1);
auto sub = app.add_subcommand("sub");
sub->add_option("val2", val2);
args = {"sub", "--val1", "1", "2"};
// Should not throw
run();
EXPECT_EQ(1, val1);
EXPECT_EQ(2, val2);
}
TEST_F(TApp, CallbackOrdering) {
int val = 1, sub_val = 0;
app.add_option("--val", val);
auto sub = app.add_subcommand("sub");
sub->set_callback([&val, &sub_val](){
sub_val = val;
});
args = {"sub", "--val=2"};
run();
EXPECT_EQ(2, val);
EXPECT_EQ(2, sub_val);
app.reset();
args = {"--val=2", "sub"};
run();
EXPECT_EQ(2, val);
EXPECT_EQ(2, sub_val);
}
TEST_F(TApp, RequiredSubCom) { TEST_F(TApp, RequiredSubCom) {
app.add_subcommand("sub1"); app.add_subcommand("sub1");
app.add_subcommand("sub2"); app.add_subcommand("sub2");