test: expand argument parsing tests

Test and use the new .present field, only output the parameters
given. Test space between parameter name and value.
This commit is contained in:
Jani Nikula 2017-10-01 23:53:15 +03:00 committed by David Bremner
parent bc99087ff9
commit 9165df2135
2 changed files with 40 additions and 15 deletions

View file

@ -15,4 +15,26 @@ positional arg 2 pos2
EOF EOF
test_expect_equal_file EXPECTED OUTPUT test_expect_equal_file EXPECTED OUTPUT
test_begin_subtest "sanity check zero values"
$TEST_DIRECTORY/arg-test --keyword=zero --boolean=false --int=0 > OUTPUT
cat <<EOF > EXPECTED
boolean 0
keyword 0
int 0
EOF
test_expect_equal_file EXPECTED OUTPUT
test_begin_subtest "space instead of = between parameter name and value"
# Note: spaces aren't allowed for booleans. false turns into a positional arg!
$TEST_DIRECTORY/arg-test --keyword one --boolean false --string foo --int 7 --flag one --flag three > OUTPUT
cat <<EOF > EXPECTED
boolean 1
keyword 1
flags 5
int 7
string foo
positional arg 1 false
EOF
test_expect_equal_file EXPECTED OUTPUT
test_done test_done

View file

@ -13,27 +13,30 @@ int main(int argc, char **argv){
const char *pos_arg2=NULL; const char *pos_arg2=NULL;
const char *string_val=NULL; const char *string_val=NULL;
notmuch_bool_t bool_val = FALSE; notmuch_bool_t bool_val = FALSE;
notmuch_bool_t fl_set = FALSE, int_set = FALSE, bool_set = FALSE,
kw_set = FALSE, string_set = FALSE, pos1_set = FALSE, pos2_set = FALSE;
notmuch_opt_desc_t parent_options[] = { notmuch_opt_desc_t parent_options[] = {
{ .opt_flags = &fl_val, .name = "flag", .keywords = { .opt_flags = &fl_val, .name = "flag", .present = &fl_set, .keywords =
(notmuch_keyword_t []){ { "one", 1 << 0}, (notmuch_keyword_t []){ { "one", 1 << 0},
{ "two", 1 << 1 }, { "two", 1 << 1 },
{ "three", 1 << 2 }, { "three", 1 << 2 },
{ 0, 0 } } }, { 0, 0 } } },
{ .opt_int = &int_val, .name = "int" }, { .opt_int = &int_val, .name = "int", .present = &int_set },
{ } { }
}; };
notmuch_opt_desc_t options[] = { notmuch_opt_desc_t options[] = {
{ .opt_bool = &bool_val, .name = "boolean" }, { .opt_bool = &bool_val, .name = "boolean", .present = &bool_set },
{ .opt_keyword = &kw_val, .name = "keyword", .keywords = { .opt_keyword = &kw_val, .name = "keyword", .present = &kw_set, .keywords =
(notmuch_keyword_t []){ { "one", 1 }, (notmuch_keyword_t []){ { "zero", 0 },
{ "one", 1 },
{ "two", 2 }, { "two", 2 },
{ 0, 0 } } }, { 0, 0 } } },
{ .opt_inherit = parent_options }, { .opt_inherit = parent_options },
{ .opt_string = &string_val, .name = "string" }, { .opt_string = &string_val, .name = "string", .present = &string_set },
{ .opt_position = &pos_arg1 }, { .opt_position = &pos_arg1, .present = &pos1_set },
{ .opt_position = &pos_arg2 }, { .opt_position = &pos_arg2, .present = &pos2_set },
{ } { }
}; };
@ -42,25 +45,25 @@ int main(int argc, char **argv){
if (opt_index < 0) if (opt_index < 0)
return 1; return 1;
if (bool_val) if (bool_set)
printf("boolean %d\n", bool_val); printf("boolean %d\n", bool_val);
if (kw_val) if (kw_set)
printf("keyword %d\n", kw_val); printf("keyword %d\n", kw_val);
if (fl_val) if (fl_set)
printf("flags %d\n", fl_val); printf("flags %d\n", fl_val);
if (int_val) if (int_set)
printf("int %d\n", int_val); printf("int %d\n", int_val);
if (string_val) if (string_set)
printf("string %s\n", string_val); printf("string %s\n", string_val);
if (pos_arg1) if (pos1_set)
printf("positional arg 1 %s\n", pos_arg1); printf("positional arg 1 %s\n", pos_arg1);
if (pos_arg2) if (pos2_set)
printf("positional arg 2 %s\n", pos_arg2); printf("positional arg 2 %s\n", pos_arg2);