X-Git-Url: http://git.efficios.com/?p=argpar.git;a=blobdiff_plain;f=tests%2Ftest_argpar.c;h=2a24567f30f1f7c89132de2dd9f958ce6a6a044c;hp=fa915d87a81a8082e670179fcc11f54c51db8c7a;hb=fc07e5264804963a3de6d1b929a9eb4fd37e49d4;hpb=903a5b8ab5ab38d3b200b1d692ba0d29d080c92c diff --git a/tests/test_argpar.c b/tests/test_argpar.c index fa915d8..2a24567 100644 --- a/tests/test_argpar.c +++ b/tests/test_argpar.c @@ -1,5 +1,6 @@ /* - * Copyright (c) 2019 Philippe Proulx + * Copyright (c) 2019-2021 Philippe Proulx + * Copyright (c) 2020-2021 Simon Marchi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,49 +16,88 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include #include #include #include #include "tap/tap.h" -#include "common/assert.h" - #include "argpar/argpar.h" /* - * Tests that the command line `cmdline`, with non-quoted - * space-delimited arguments, once parsed given the option descriptors - * `descrs` and the option `fail_on_unknown_opt`, succeeds and gives the - * expected command line `expected_cmd_line` and number of ingested - * original arguments `expected_ingested_orig_args`. - * - * The resulting command-line is built from the resulting arguments, - * space-delimiting each argument, preferring the `--long-opt=arg` style - * over the `-s arg` style, and using the `arg` form for non-option - * arguments where `A` is the original argument index and `B` is the - * non-option argument index. + * Append `item` to `res_str` to incrementally build an expected command line string. */ static -void test_succeed(const char *cmdline, +void append_to_res_str(GString *res_str, const struct argpar_item *item) +{ + if (res_str->len > 0) { + g_string_append_c(res_str, ' '); + } + + switch (item->type) { + case ARGPAR_ITEM_TYPE_OPT: + { + const struct argpar_item_opt *item_opt = + (const void *) item; + + if (item_opt->descr->long_name) { + g_string_append_printf(res_str, "--%s", + item_opt->descr->long_name); + + if (item_opt->arg) { + g_string_append_printf(res_str, "=%s", + item_opt->arg); + } + } else if (item_opt->descr->short_name) { + g_string_append_printf(res_str, "-%c", + item_opt->descr->short_name); + + if (item_opt->arg) { + g_string_append_printf(res_str, " %s", + item_opt->arg); + } + } + + break; + } + case ARGPAR_ITEM_TYPE_NON_OPT: + { + const struct argpar_item_non_opt *item_non_opt = + (const void *) item; + + g_string_append_printf(res_str, "%s<%u,%u>", + item_non_opt->arg, item_non_opt->orig_index, + item_non_opt->non_opt_index); + break; + } + default: + abort(); + } +} + +/* Test using the all-at-once argpar_parse function. */ + +static +void test_succeed_argpar_parse(const char *cmdline, const char *expected_cmd_line, - const struct bt_argpar_opt_descr *descrs, + const struct argpar_opt_descr *descrs, unsigned int expected_ingested_orig_args) { - struct bt_argpar_parse_ret parse_ret; + struct argpar_parse_ret parse_ret; GString *res_str = g_string_new(NULL); gchar **argv = g_strsplit(cmdline, " ", 0); unsigned int i; - BT_ASSERT(argv); - BT_ASSERT(res_str); - parse_ret = bt_argpar_parse(g_strv_length(argv), + assert(argv); + assert(res_str); + parse_ret = argpar_parse(g_strv_length(argv), (const char * const *) argv, descrs, false); ok(parse_ret.items, - "bt_argpar_parse() succeeds for command line `%s`", cmdline); + "argpar_parse() succeeds for command line `%s`", cmdline); ok(!parse_ret.error, - "bt_argpar_parse() does not write an error for command line `%s`", cmdline); + "argpar_parse() does not write an error for command line `%s`", cmdline); ok(parse_ret.ingested_orig_args == expected_ingested_orig_args, - "bt_argpar_parse() returns the correct number of ingested " + "argpar_parse() returns the correct number of ingested " "original arguments for command line `%s`", cmdline); if (parse_ret.ingested_orig_args != expected_ingested_orig_args) { diag("Expected: %u Got: %u", expected_ingested_orig_args, @@ -65,75 +105,127 @@ void test_succeed(const char *cmdline, } if (!parse_ret.items) { - fail("bt_argpar_parse() returns the expected parsed arguments " + fail("argpar_parse() returns the expected parsed arguments " "for command line `%s`", cmdline); goto end; } - for (i = 0; i < parse_ret.items->len; i++) { - const struct bt_argpar_item *arg = parse_ret.items->pdata[i]; + for (i = 0; i < parse_ret.items->n_items; i++) { + const struct argpar_item *item = parse_ret.items->items[i]; + + append_to_res_str(res_str, item); + } + + ok(strcmp(expected_cmd_line, res_str->str) == 0, + "argpar_parse() returns the expected parsed arguments " + "for command line `%s`", cmdline); + if (strcmp(expected_cmd_line, res_str->str) != 0) { + diag("Expected: `%s`", expected_cmd_line); + diag("Got: `%s`", res_str->str); + } + +end: + argpar_parse_ret_fini(&parse_ret); + g_string_free(res_str, TRUE); + g_strfreev(argv); +} + +/* Test using the iterator API. */ - switch (arg->type) { - case BT_ARGPAR_ITEM_TYPE_OPT: - { - const struct bt_argpar_item_opt *arg_opt = - (const void *) arg; +static +void test_succeed_argpar_iter(const char *cmdline, + const char *expected_cmd_line, + const struct argpar_opt_descr *descrs, + unsigned int expected_ingested_orig_args) +{ + struct argpar_iter *iter = NULL; + const struct argpar_item *item = NULL; + char *error = NULL; + GString *res_str = g_string_new(NULL); + gchar **argv = g_strsplit(cmdline, " ", 0); + unsigned int i, actual_ingested_orig_args; - if (arg_opt->descr->long_name) { - g_string_append_printf(res_str, "--%s", - arg_opt->descr->long_name); + assert(argv); + assert(res_str); - if (arg_opt->arg) { - g_string_append_printf(res_str, "=%s", - arg_opt->arg); - } + iter = argpar_iter_create(g_strv_length(argv), + (const char * const *) argv, descrs); + assert(iter); - g_string_append_c(res_str, ' '); - } else if (arg_opt->descr->short_name) { - g_string_append_printf(res_str, "-%c", - arg_opt->descr->short_name); + for (i = 0; ; i++) { + enum argpar_iter_parse_next_status status; - if (arg_opt->arg) { - g_string_append_printf(res_str, " %s", - arg_opt->arg); - } + ARGPAR_ITEM_DESTROY_AND_RESET(item); + status = argpar_iter_parse_next(iter, &item, &error); - g_string_append_c(res_str, ' '); - } + ok(status == ARGPAR_ITER_PARSE_NEXT_STATUS_OK || + status == ARGPAR_ITER_PARSE_NEXT_STATUS_END || + status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT, + "argpar_iter_parse_next() for command line `%s` call #%u: status", cmdline, i); - break; + if (status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) { + ok(error, "argpar_iter_parse_next() for command line `%s` call #%u: error returned", cmdline, i); + } else { + ok(!error, "argpar_iter_parse_next() for command line `%s` call #%u: no error returned", cmdline, i); } - case BT_ARGPAR_ITEM_TYPE_NON_OPT: - { - const struct bt_argpar_item_non_opt *arg_non_opt = - (const void *) arg; - - g_string_append_printf(res_str, "%s<%u,%u> ", - arg_non_opt->arg, arg_non_opt->orig_index, - arg_non_opt->non_opt_index); + + if (status == ARGPAR_ITER_PARSE_NEXT_STATUS_END || + status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) { + ok(!item, "argpar_iter_parse_next() for command line `%s` call #%u: no item returned", cmdline, i); break; } - default: - abort(); - } + + append_to_res_str(res_str, item); } - if (res_str->len > 0) { - g_string_truncate(res_str, res_str->len - 1); + actual_ingested_orig_args = argpar_iter_get_ingested_orig_args(iter); + ok(actual_ingested_orig_args == expected_ingested_orig_args, + "argpar_iter_get_ingested_orig_args() returns the correct number of ingested " + "original arguments for command line `%s`", cmdline); + if (actual_ingested_orig_args != expected_ingested_orig_args) { + diag("Expected: %u Got: %u", expected_ingested_orig_args, + actual_ingested_orig_args); } ok(strcmp(expected_cmd_line, res_str->str) == 0, - "bt_argpar_parse() returns the expected parsed arguments " + "argpar_iter_parse_next() returns the expected parsed arguments " "for command line `%s`", cmdline); if (strcmp(expected_cmd_line, res_str->str) != 0) { diag("Expected: `%s`", expected_cmd_line); diag("Got: `%s`", res_str->str); } -end: - bt_argpar_parse_ret_fini(&parse_ret); + argpar_item_destroy(item); + argpar_iter_destroy(iter); g_string_free(res_str, TRUE); g_strfreev(argv); + free(error); +} + +/* + * Tests that the command line `cmdline`, with non-quoted + * space-delimited arguments, once parsed given the option descriptors + * `descrs`, succeeds and gives the expected command line `expected_cmd_line` + * and number of ingested original arguments `expected_ingested_orig_args`. + * + * The resulting command-line is built from the resulting arguments, + * space-delimiting each argument, preferring the `--long-opt=arg` style + * over the `-s arg` style, and using the `arg` form for non-option + * arguments where `A` is the original argument index and `B` is the + * non-option argument index. + * + * Test with both the parse-all-at-once and iterator-style APIs. + */ +static +void test_succeed(const char *cmdline, + const char *expected_cmd_line, + const struct argpar_opt_descr *descrs, + unsigned int expected_ingested_orig_args) +{ + test_succeed_argpar_parse(cmdline, expected_cmd_line, descrs, + expected_ingested_orig_args); + test_succeed_argpar_iter(cmdline, expected_cmd_line, descrs, + expected_ingested_orig_args); } static @@ -141,8 +233,8 @@ void succeed_tests(void) { /* No arguments */ { - const struct bt_argpar_opt_descr descrs[] = { - BT_ARGPAR_OPT_DESCR_SENTINEL + const struct argpar_opt_descr descrs[] = { + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -153,9 +245,9 @@ void succeed_tests(void) /* Single long option */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "salut", false }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -166,9 +258,9 @@ void succeed_tests(void) /* Single short option */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'f', NULL, false }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -179,9 +271,9 @@ void succeed_tests(void) /* Short and long option (aliases) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'f', "flaw", false }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -192,9 +284,9 @@ void succeed_tests(void) /* Long option with argument (space form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "tooth", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -205,9 +297,9 @@ void succeed_tests(void) /* Long option with argument (equal form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "polish", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -218,9 +310,9 @@ void succeed_tests(void) /* Short option with argument (space form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -231,9 +323,9 @@ void succeed_tests(void) /* Short option with argument (glued form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -244,9 +336,9 @@ void succeed_tests(void) /* Short and long option (aliases) with argument (all forms) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'd', "dry", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -257,11 +349,11 @@ void succeed_tests(void) /* Many short options, last one with argument (glued form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'd', NULL, false }, { 0, 'e', NULL, false }, { 0, 'f', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -272,11 +364,11 @@ void succeed_tests(void) /* Many options */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'd', NULL, false }, { 0, 'e', "east", true }, { 0, '\0', "mind", false }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -287,8 +379,8 @@ void succeed_tests(void) /* Single non-option argument */ { - const struct bt_argpar_opt_descr descrs[] = { - BT_ARGPAR_OPT_DESCR_SENTINEL + const struct argpar_opt_descr descrs[] = { + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -299,8 +391,8 @@ void succeed_tests(void) /* Two non-option arguments */ { - const struct bt_argpar_opt_descr descrs[] = { - BT_ARGPAR_OPT_DESCR_SENTINEL + const struct argpar_opt_descr descrs[] = { + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -311,10 +403,10 @@ void succeed_tests(void) /* Single non-option argument mixed with options */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'd', NULL, false }, { 0, '\0', "squeeze", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -325,9 +417,9 @@ void succeed_tests(void) /* Unknown short option (space form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'd', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -338,9 +430,9 @@ void succeed_tests(void) /* Unknown short option (glued form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'd', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -351,9 +443,9 @@ void succeed_tests(void) /* Unknown long option (space form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "sink", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -364,9 +456,9 @@ void succeed_tests(void) /* Unknown long option (equal form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "sink", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -377,9 +469,9 @@ void succeed_tests(void) /* Unknown option before non-option argument */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -390,9 +482,9 @@ void succeed_tests(void) /* Unknown option after non-option argument */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -403,9 +495,9 @@ void succeed_tests(void) /* Valid `---opt` */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "-fuel", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -416,9 +508,9 @@ void succeed_tests(void) /* Long option containing `=` in argument (equal form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "zebra", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -429,9 +521,9 @@ void succeed_tests(void) /* Short option's argument starting with `-` (glued form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'z', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -442,9 +534,9 @@ void succeed_tests(void) /* Short option's argument starting with `-` (space form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'z', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -455,9 +547,9 @@ void succeed_tests(void) /* Long option's argument starting with `-` (space form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "janine", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -468,9 +560,9 @@ void succeed_tests(void) /* Long option's argument starting with `-` (equal form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "janine", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -481,10 +573,10 @@ void succeed_tests(void) /* Long option's empty argument (equal form) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'f', NULL, false }, { 0, '\0', "yeah", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_succeed( @@ -494,51 +586,115 @@ void succeed_tests(void) } } -/* - * Tests that the command line `cmdline`, with non-quoted - * space-delimited arguments, once parsed given the option descriptors - * `descrs`, fails and gives the expected error `expected_error`. - */ +/* Test using the all-at-once argpar_parse function. */ + static -void test_fail(const char *cmdline, const char *expected_error, - const struct bt_argpar_opt_descr *descrs) +void test_fail_argpar_parse(const char *cmdline, const char *expected_error, + const struct argpar_opt_descr *descrs) { - struct bt_argpar_parse_ret parse_ret; + struct argpar_parse_ret parse_ret; gchar **argv = g_strsplit(cmdline, " ", 0); - parse_ret = bt_argpar_parse(g_strv_length(argv), + parse_ret = argpar_parse(g_strv_length(argv), (const char * const *) argv, descrs, true); ok(!parse_ret.items, - "bt_argpar_parse() fails for command line `%s`", cmdline); + "argpar_parse() fails for command line `%s`", cmdline); ok(parse_ret.error, - "bt_argpar_parse() writes an error string for command line `%s`", + "argpar_parse() writes an error string for command line `%s`", cmdline); if (parse_ret.items) { - fail("bt_argpar_parse() writes the expected error string"); + fail("argpar_parse() writes the expected error string"); goto end; } - ok(strcmp(expected_error, parse_ret.error->str) == 0, - "bt_argpar_parse() writes the expected error string " + ok(strcmp(expected_error, parse_ret.error) == 0, + "argpar_parse() writes the expected error string " "for command line `%s`", cmdline); - if (strcmp(expected_error, parse_ret.error->str) != 0) { + if (strcmp(expected_error, parse_ret.error) != 0) { diag("Expected: `%s`", expected_error); - diag("Got: `%s`", parse_ret.error->str); + diag("Got: `%s`", parse_ret.error); } end: - bt_argpar_parse_ret_fini(&parse_ret); + argpar_parse_ret_fini(&parse_ret); g_strfreev(argv); } +/* Test using the iterator API. */ + +static +void test_fail_argpar_iter(const char *cmdline, const char *expected_error, + const struct argpar_opt_descr *descrs) +{ + struct argpar_iter *iter = NULL; + const struct argpar_item *item = NULL; + gchar **argv = g_strsplit(cmdline, " ", 0); + unsigned int i; + char *error = NULL; + + iter = argpar_iter_create(g_strv_length(argv), + (const char * const *) argv, descrs); + assert(iter); + + for (i = 0; ; i++) { + enum argpar_iter_parse_next_status status; + + ARGPAR_ITEM_DESTROY_AND_RESET(item); + status = argpar_iter_parse_next(iter, &item, &error); + + ok(status == ARGPAR_ITER_PARSE_NEXT_STATUS_OK || + status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR || + status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT, + "argpar_iter_parse_next() for command line `%s` call #%u: status", cmdline, i); + + if (status != ARGPAR_ITER_PARSE_NEXT_STATUS_OK) { + ok(!item, "argpar_iter_parse_next() for command line `%s` call #%u: no item returned", cmdline, i); + ok(error, "argpar_iter_parse_next() for command line `%s` call #%u: error returned", cmdline, i); + break; + } + + ok(item, "argpar_iter_parse_next() for command line `%s` call #%u: item returned", cmdline, i); + ok(!error, "argpar_iter_parse_next() for command line `%s` call #%u: no error returned", cmdline, i); + } + + ok(strcmp(expected_error, error) == 0, + "argpar_iter_parse_next() writes the expected error string " + "for command line `%s`", cmdline); + if (strcmp(expected_error, error) != 0) { + diag("Expected: `%s`", expected_error); + diag("Got: `%s`", error); + } + + argpar_item_destroy(item); + argpar_iter_destroy(iter); + free(error); + g_strfreev(argv); +} + +/* + * Tests that the command line `cmdline`, with non-quoted + * space-delimited arguments, once parsed given the option descriptors + * `descrs`, fails and gives the expected error `expected_error`. + * + * Test with both the parse-all-at-once and iterator-style APIs. + */ + +static +void test_fail(const char *cmdline, const char *expected_error, + const struct argpar_opt_descr *descrs) +{ + test_fail_argpar_parse(cmdline, expected_error, descrs); + test_fail_argpar_iter(cmdline, expected_error, descrs); +} + static void fail_tests(void) { /* Unknown long option */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_fail( @@ -549,9 +705,9 @@ void fail_tests(void) /* Unknown short option */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_fail( @@ -562,9 +718,9 @@ void fail_tests(void) /* Missing long option argument */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, '\0', "thumb", true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_fail( @@ -575,9 +731,9 @@ void fail_tests(void) /* Missing short option argument */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'k', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_fail( @@ -588,11 +744,11 @@ void fail_tests(void) /* Missing short option argument (multiple glued) */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'a', NULL, false }, { 0, 'b', NULL, false }, { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_fail( @@ -603,11 +759,11 @@ void fail_tests(void) /* Invalid `-` */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'a', NULL, false }, { 0, 'b', NULL, false }, { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_fail( @@ -618,11 +774,11 @@ void fail_tests(void) /* Invalid `--` */ { - const struct bt_argpar_opt_descr descrs[] = { + const struct argpar_opt_descr descrs[] = { { 0, 'a', NULL, false }, { 0, 'b', NULL, false }, { 0, 'c', NULL, true }, - BT_ARGPAR_OPT_DESCR_SENTINEL + ARGPAR_OPT_DESCR_SENTINEL }; test_fail( @@ -630,11 +786,23 @@ void fail_tests(void) "While parsing argument #2 (`--`): Invalid argument", descrs); } + + { + const struct argpar_opt_descr descrs[] = { + { 0, 'c', "chevre", false }, + ARGPAR_OPT_DESCR_SENTINEL + }; + + test_fail( + "--chevre=fromage", + "While parsing argument #1 (`--chevre=fromage`): Unexpected argument for option `--chevre`", + descrs); + } } int main(void) { - plan_tests(129); + plan_tests(419); succeed_tests(); fail_tests(); return exit_status();