Skip to content

Commit

Permalink
Code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gknowles committed Apr 22, 2024
1 parent fab486e commit d837cfc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
16 changes: 6 additions & 10 deletions libs/dimcli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,13 +737,14 @@ void Cli::OptIndex::index(OptBase & opt) {
close = 0;

for (;; ch = *cur++) {
if (isspace(ch)) {
if (cur - nameptr == 2)
if (isalnum(ch))
goto IN_UNQUOTED_NAME;
if (cur == last || isspace(ch)) {
auto len = cur - nameptr - (cur != last); // len w/o space
if (len == 1)
goto ADD_SHORT_NAME;
break;
}
if (isalnum(ch))
goto IN_UNQUOTED_NAME;
switch (ch) {
case '?':
flags |= fNameOptional;
Expand Down Expand Up @@ -771,11 +772,6 @@ void Cli::OptIndex::index(OptBase & opt) {
flags |= fNameError;
break;
}
if (cur == last) {
if (cur - nameptr == 1)
goto ADD_SHORT_NAME;
break;
}
}
assert(!"Prefix modifiers without option name.");
goto IN_GAP;
Expand Down Expand Up @@ -819,7 +815,7 @@ void Cli::OptIndex::index(OptBase & opt) {
assert(!"Bad option name, contains '='.");
flags |= fNameError;
} else if (isspace(ch)) {
assert(!"Bad option name, contains white space");
assert(!"Bad option name, contains white space.");
flags |= fNameError;
}
}
Expand Down
55 changes: 53 additions & 2 deletions tests/cli/clitest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ void assertTests() {
cli.opt<bool>("a=b");
EXPECT_ASSERT(1 + R"(
!"Bad option name, contains '='."
)");
cli.opt<bool>("(a=b)"); // in quotes
EXPECT_ASSERT(1 + R"(
!"Bad option name, contains '='."
)");
cli.opt<bool>("(a b)");
EXPECT_ASSERT(1 + R"(
!"Bad option name, contains white space."
)");
cli.opt<bool>("[a b");
EXPECT_ASSERT(1 + R"(
!"Bad name, unmatched '(', '[', or '<'."
)");
cli.opt<int>("[B] [C]");
EXPECT_ASSERT(1 + R"(
Expand All @@ -267,10 +279,34 @@ void assertTests() {
cli.opt<int>("-d");
EXPECT_ASSERT(1 + R"(
!"Unknown prefix modifier for name."
)");
cli.opt<int>("d-");
EXPECT_ASSERT(1 + R"(
!"Unknown suffix modifier for name."
)");
cli.opt<bool>("?e");
EXPECT_ASSERT(1 + R"(
!"Bad prefix modifier '?' for bool option."
)");
cli.opt<bool>("*e");
EXPECT_ASSERT(1 + R"(
!"Bad prefix modifier '*' for bool option."
)");
cli.opt<bool>("!!");
EXPECT_ASSERT(1 + R"(
!"Prefix modifiers without option name."
)");
cli.opt<bool>("*[a]");
EXPECT_ASSERT(1 + R"(
!"Bad prefix modifier '*' for operand name."
)");
cli.opt<bool>("[a].");
EXPECT_ASSERT(1 + R"(
!"Bad suffix modifier '.' for operand name."
)");
cli.opt<bool>("-");
EXPECT_ASSERT(1 + R"(
!"Bad option short name, '-' or '='."
)");
cli.opt<bool>("f.");
EXPECT_ASSERT(1 + R"(
Expand All @@ -281,9 +317,18 @@ Usage: test [--help] [B]
)");
EXPECT_ASSERT(1 + R"(
!"Bad option name, contains '='."
!"Bad option name, contains '='."
!"Bad option name, contains white space."
!"Bad name, unmatched '(', '[', or '<'."
!"Opt with multiple operand names."
!"Unknown prefix modifier for name."
!"Unknown suffix modifier for name."
!"Bad prefix modifier '?' for bool option."
!"Bad prefix modifier '*' for bool option."
!"Prefix modifiers without option name."
!"Bad prefix modifier '*' for operand name."
!"Bad suffix modifier '.' for operand name."
!"Bad option short name, '-' or '='."
!"Bad suffix modifier '.' for short name."
)");

Expand Down Expand Up @@ -561,6 +606,11 @@ void parseTests() {
EXPECT_PARSE(cli, "-m 1 2 3", false);
EXPECT_ERR(cli, "Error: Unexpected argument: 3\n");

cli = {};
cli.opt<int>("long");
EXPECT_PARSE(cli, "--long", false);
EXPECT_ERR(cli, "Error: No value given for --long\n");

cli = {};
cli.opt("<n>", 1);
EXPECT_PARSE(cli, "", false);
Expand Down Expand Up @@ -1265,7 +1315,8 @@ Usage: test unknown [ARGS...]
});
EXPECT_PARSE(cli, "unknown a b c");
EXPECT(cli.commandMatched() == "unknown");
EXPECT(cli.unknownArgs() == vector<string>{"a", "b", "c"});
auto args = CliTest(cli).unknownArgs();
EXPECT(args == vector<string>{"a", "b", "c"});
EXPECT_PARSE(cli, "");
EXPECT_HELP(cli, "", 1 + R"(
Usage: test [OPTIONS] [COMMAND] [ARGS...]
Expand All @@ -1281,7 +1332,7 @@ Usage: test unknown [ARGS...]
// unknownArgs
{
cli = {};
cli.unknownArgs(true);
CliTest(cli).unknownArgs(true);
EXPECT_PARSE(cli, "a b c");
EXPECT(cli.unknownArgs() == vector<string>{"a", "b", "c"});
cli = {};
Expand Down

0 comments on commit d837cfc

Please sign in to comment.