X-Git-Url: https://git.openafs.org/?p=openafs.git;a=blobdiff_plain;f=tests%2Fcmd%2Fcommand-t.c;h=b4718f37e168501b2af03b43ea2fc80903820583;hp=c69d38eef023675b951e888d171546ae4e080b00;hb=c8e52c1f457a237ec83be0fbe535077d5bccdd51;hpb=898d84cefe4b0bf54cc77ccca998e596710a1ee6 diff --git a/tests/cmd/command-t.c b/tests/cmd/command-t.c index c69d38e..b4718f3 100644 --- a/tests/cmd/command-t.c +++ b/tests/cmd/command-t.c @@ -33,43 +33,90 @@ #include -#include +#include + +enum cmdOptions { + copt_flag = 0, + copt_first, + copt_second, + copt_sugar, + copt_fourth, + copt_fifth, + copt_perhaps, + copt_sanity +}; static int testproc(struct cmd_syndesc *as, void *rock) { - is_string("foo", as->parms[0].items->data, + is_string("foo", as->parms[copt_first].items->data, "first option matches"); - is_string("bar", as->parms[1].items->data, + is_string("bar", as->parms[copt_second].items->data, "second option matches"); - ok(as->parms[2].items != NULL, + ok(as->parms[copt_flag].items != NULL, "flag is set"); return 0; } +static void +checkList(struct cmd_item *list, ...) +{ + va_list ap; + char *el; + + va_start(ap, list); + el = va_arg(ap, char *); + while (el != NULL && list != NULL) { + is_string(el, list->data, "list element matches"); + list = list->next; + el = va_arg(ap, char *); + } + + if (el == NULL && list == NULL) { + ok(1, "List has correct number of elements"); + } else if (el == NULL) { + ok(0, "List has too many elements"); + } else if (list == NULL) { + ok(0, "List has too few elements"); + } +} + int main(int argc, char **argv) { char *tv[100]; struct cmd_syndesc *opts; + struct cmd_syndesc *retopts; + struct cmd_item *list; int code; int tc; + int retval; + char *path; + char *retstring = NULL; - plan(29); + plan(109); initialize_CMD_error_table(); opts = cmd_CreateSyntax(NULL, testproc, NULL, NULL); + cmd_AddParm(opts, "-flag", CMD_FLAG, CMD_OPTIONAL, "a flag"); cmd_AddParm(opts, "-first", CMD_SINGLE, CMD_REQUIRED, "first option"); cmd_AddParm(opts, "-second", CMD_LIST, CMD_OPTIONAL, "second option"); - cmd_AddParm(opts, "-flag", CMD_FLAG, CMD_OPTIONAL, "a flag"); /* A simple command line */ code = cmd_ParseLine("-first foo -second bar -flag", tv, &tc, 100); is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); is_int(0, code, "dispatching simple comamnd line succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "parsing simple command line succeeds"); + is_string("foo", retopts->parms[copt_first].items->data, + " ... 1st option matches"); + is_string("bar", retopts->parms[copt_second].items->data, + " ... 2nd option matches"); + ok(retopts->parms[copt_flag].items != NULL, " ... 3rd option matches"); + cmd_FreeOptions(&retopts); cmd_FreeArgv(tv); /* unknown switch */ @@ -77,6 +124,8 @@ main(int argc, char **argv) is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); is_int(CMD_UNKNOWNSWITCH, code, "invalid options fail as expected"); + code = cmd_Parse(tc, tv, &retopts); + is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse"); cmd_FreeArgv(tv); /* missing parameter */ @@ -84,6 +133,8 @@ main(int argc, char **argv) is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); is_int(CMD_TOOFEW, code, "missing parameters fail as expected"); + code = cmd_Parse(tc, tv, &retopts); + is_int(CMD_TOOFEW, code, "and still fail with cmd_Parse"); cmd_FreeArgv(tv); /* missing option */ @@ -91,12 +142,16 @@ main(int argc, char **argv) is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); is_int(CMD_UNKNOWNSWITCH, code, "missing options fail as expected"); + code = cmd_Parse(tc, tv, &retopts); + is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse"); cmd_FreeArgv(tv); code = cmd_ParseLine("-first foo baz -second bar -third -flag", tv, &tc, 100); is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); is_int(CMD_NOTLIST, code, "too many parameters fails as expected"); + code = cmd_Parse(tc, tv, &retopts); + is_int(CMD_NOTLIST, code, "and still fail with cmd_Parse"); cmd_FreeArgv(tv); /* Positional parameters */ @@ -104,6 +159,9 @@ main(int argc, char **argv) is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); is_int(0, code, "dispatching positional parameters succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "and works with cmd_Parse"); + cmd_FreeOptions(&retopts); cmd_FreeArgv(tv); /* Abbreviations */ @@ -111,6 +169,10 @@ main(int argc, char **argv) is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); is_int(0, code, "dispatching abbreviations succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "and works with cmd_Parse"); + cmd_FreeOptions(&retopts); + cmd_FreeArgv(tv); /* Ambiguous */ @@ -118,6 +180,21 @@ main(int argc, char **argv) is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); is_int(CMD_UNKNOWNSWITCH, code, "ambiguous abbreviations correctly fail"); + code = cmd_Parse(tc, tv, &retopts); + is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too"); + cmd_FreeArgv(tv); + + /* Check that paramaters with abbreviation disabled don't make things + * ambiguous */ + cmd_AddParmAtOffset(opts, copt_sugar, "-sugar", CMD_SINGLE, + CMD_OPTIONAL | CMD_NOABBRV, "sugar with that"); + code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(0, code, "disabling specific abbreviations succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "and works with cmd_Parse into the bargain"); + cmd_FreeOptions(&retopts); cmd_FreeArgv(tv); /* Disable positional commands */ @@ -125,7 +202,9 @@ main(int argc, char **argv) code = cmd_ParseLine("foo bar -flag", tv, &tc, 100); is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); - is_int(3359746, code, "positional parameters can be disabled"); + is_int(CMD_NOTLIST, code, "positional parameters can be disabled"); + code = cmd_Parse(tc, tv, &retopts); + is_int(CMD_NOTLIST, code, "and fail with cmd_Parse too"); cmd_FreeArgv(tv); /* Disable abbreviations */ @@ -134,8 +213,179 @@ main(int argc, char **argv) is_int(0, code, "cmd_ParseLine succeeds"); code = cmd_Dispatch(tc, tv); is_int(CMD_UNKNOWNSWITCH, code, "dispatching abbreviations succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too"); + + cmd_FreeArgv(tv); + + /* Try the new cmd_Parse function with something different*/ + code = cmd_ParseLine("-first one -second two -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "Parsing with cmd_Parse works"); + is_string("one", retopts->parms[copt_first].items->data, + " ... 1st option matches"); + is_string("two", retopts->parms[copt_second].items->data, + " ... 2nd option matches"); + ok(retopts->parms[copt_flag].items != NULL, + " ... 3rd option matches"); + + cmd_FreeOptions(&retopts); + cmd_FreeArgv(tv); + /* Try adding a couple of parameters at specific positions */ + cmd_AddParmAtOffset(opts, copt_fifth, "-fifth", CMD_SINGLE, CMD_OPTIONAL, + "fifth option"); + cmd_AddParmAtOffset(opts, copt_fourth, "-fourth", CMD_SINGLE, CMD_OPTIONAL, + "fourth option" ); + code = cmd_ParseLine("-first a -fourth b -fifth c", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "parsing our new options succeeds"); + is_string("b", retopts->parms[copt_fourth].items->data, + " Fourth option in right place"); + is_string("c", retopts->parms[copt_fifth].items->data, + " Fifth option in right place"); + cmd_FreeOptions(&retopts); + cmd_FreeArgv(tv); + + /* Check Accessors */ + code = cmd_ParseLine("-first 1 -second second -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + + code = cmd_OptionAsInt(retopts, copt_first, &retval); + is_int(0, code, "cmd_OptionsAsInt succeeds"); + is_int(1, retval, " ... and returns correct value"); + ok(cmd_OptionPresent(retopts, copt_first), + " ... and is marked as present"); + + code = cmd_OptionAsString(retopts, copt_second, &retstring); + is_int(0, code, "cmd_OptionsAsString succeeds"); + is_string("second", retstring, " ... and returns correct value"); + free(retstring); + retstring = NULL; + ok(cmd_OptionPresent(retopts, copt_second), + " ... and is marked as present"); + + code = cmd_OptionAsFlag(retopts, copt_flag, &retval); + is_int(0, code, "cmd_OptionsAsFlag succeeds"); + ok(retval, " ... and flag is correct"); + ok(cmd_OptionPresent(retopts, copt_flag), + " ... and is marked as present"); + + ok(!cmd_OptionPresent(retopts, copt_sugar), + "Missing option is marked as such"); + + cmd_FreeOptions(&retopts); cmd_FreeArgv(tv); + /* Add an alias */ + code = cmd_AddParmAlias(opts, copt_second, "-twa"); + is_int(0, code, "cmd_AddParmAlias succeeds"); + + code = cmd_ParseLine("-first 1 -twa tup", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "cmd_Parse succeeds for alias"); + cmd_OptionAsString(retopts, copt_second, &retstring); + is_string("tup", retstring, " ... and we have the correct value"); + free(retstring); + retstring = NULL; + + cmd_FreeOptions(&retopts); + cmd_FreeArgv(tv); + + /* Add something that can be a flag or a value, and put something after + * it so we can check for parse problems*/ + cmd_AddParm(opts, "-perhaps", CMD_SINGLE_OR_FLAG, CMD_OPTIONAL, + "what am I"); + cmd_AddParm(opts, "-sanity", CMD_SINGLE, CMD_OPTIONAL, "sanity check"); + + /* Try using as an option */ + + code = cmd_ParseLine("-first 1 -perhaps 2 -sanity 3", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "cmd_Parse succeeds for option-as-flag as opt"); + code = cmd_OptionAsInt(retopts, copt_perhaps, &retval); + is_int(0, code, "cmd_OptionAsInt succeeds"); + is_int(2, retval, " ... and we have the correct value"); + cmd_FreeOptions(&retopts); + cmd_FreeArgv(tv); + + /* And now, as a flag */ + + code = cmd_ParseLine("-first 1 -perhaps -sanity 3", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "cmd_Parse succeeds for option-as-flag as flag"); + code = cmd_OptionAsInt(retopts, copt_perhaps, &retval); + is_int(CMD_MISSING, code, " ... pulling out a value fails as expected"); + cmd_OptionAsFlag(retopts, copt_perhaps, &retval); + ok(retval, " ... but parsing as a flag works"); + cmd_FreeOptions(&retopts); + cmd_FreeArgv(tv); + + /* Check that we can produce help output */ + code = cmd_ParseLine("-help", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(CMD_USAGE, code, "cmd_Parse returns usage error with help output"); + ok(retopts == NULL, " ... and options is empty"); + + /* Check splitting with '=' */ + + code = cmd_ParseLine("-first 1 -perhaps=6 -sanity=3", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "cmd_Parse succeeds for items split with '='"); + code = cmd_OptionAsInt(retopts, copt_perhaps, &retval); + is_int(0, code, "cmd_OptionAsInt succeeds"); + is_int(6, retval, " ... and we have the correct value once"); + code = cmd_OptionAsInt(retopts, copt_sanity, &retval); + is_int(0, code, "cmd_OptionAsInt succeeds"); + is_int(3, retval, " ... and we have the correct value twice"); + cmd_FreeOptions(&retopts); + cmd_FreeArgv(tv); + + /* Check list behaviour */ + code = cmd_ParseLine("-first 1 -second one two three", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "cmd_Parse succeeds for a list"); + code = cmd_OptionAsList(retopts, copt_second, &list); + is_int(0, code, "cmd_OptionAsList succeeds"); + checkList(list, "one", "two", "three", NULL); + cmd_FreeOptions(&retopts); + cmd_FreeArgv(tv); + + /* Now, try adding a configuration file into the mix */ + if (getenv("SOURCE") == NULL) + path = strdup("test1.conf"); + else + asprintf(&path, "%s/cmd/test1.conf", getenv("SOURCE")); + + cmd_SetCommandName("test"); + code = cmd_OpenConfigFile(path); + is_int(0, code, "cmd_OpenConfigFile succeeds"); + + code = cmd_ParseLine("-first 1", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "cmd_Parse succeeds when we have a config file"); + code = cmd_OptionAsInt(retopts, copt_perhaps, &retval); + is_int(0, code, "cmd_OptionsAsInt succeeds"); + is_int(10, retval, " ... and we have the correct value for perhaps"); + code = cmd_OptionAsString(retopts, copt_sanity, &retstring); + is_int(0, code, "cmd_OptionAsString succeeds"); + is_string("testing", retstring, + " ... and we have the correct value for sanity"); + + /* Check breaking up a list of options */ + code = cmd_OptionAsList(retopts, copt_second, &list); + is_int(0, code, "cmd_OptionAsList succeeds"); + checkList(list, "one", "two", "three", "four", NULL); + return 0; }