X-Git-Url: http://git.efficios.com/?p=argpar.git;a=blobdiff_plain;f=tests%2Ftest_argpar.c;h=bd94f3ff034db28c8e41aa9b117584cd8db395eb;hp=fee82a9ed06031c32ae5dc70ff159735c3f6da75;hb=8b95d883334b188d05cb1967980eae628e295d10;hpb=d1f7bbdbd28b96b360e815f28697a2f81d699cb4 diff --git a/tests/test_argpar.c b/tests/test_argpar.c index fee82a9..bd94f3f 100644 --- a/tests/test_argpar.c +++ b/tests/test_argpar.c @@ -88,7 +88,7 @@ void append_to_res_str(GString * const res_str, } /* - * Parses `cmdline` with argpar_parse() using the option descriptors + * Parses `cmdline` with the argpar API using the option descriptors * `descrs`, and ensures that the resulting effective command line is * `expected_cmd_line` and that the number of ingested original * arguments is `expected_ingested_orig_args`. @@ -100,80 +100,14 @@ void append_to_res_str(GString * const res_str, * by space-separating each formatted item (see append_to_res_str()). */ static -void test_succeed_argpar_parse(const char * const cmdline, - const char * const expected_cmd_line, - const struct argpar_opt_descr * const descrs, - const unsigned int expected_ingested_orig_args) -{ - struct argpar_parse_ret parse_ret; - GString * const res_str = g_string_new(NULL); - gchar ** const argv = g_strsplit(cmdline, " ", 0); - unsigned int i; - - assert(argv); - assert(res_str); - parse_ret = argpar_parse(g_strv_length(argv), - (const char * const *) argv, descrs, false); - ok(parse_ret.items, - "argpar_parse() succeeds for command line `%s`", cmdline); - ok(!parse_ret.error, - "argpar_parse() doesn't set an error for command line `%s`", - cmdline); - ok(parse_ret.ingested_orig_args == expected_ingested_orig_args, - "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, - parse_ret.ingested_orig_args); - } - - if (!parse_ret.items) { - fail("argpar_parse() returns the expected parsing items " - "for command line `%s`", cmdline); - goto end; - } - - for (i = 0; i < parse_ret.items->n_items; i++) { - append_to_res_str(res_str, parse_ret.items->items[i]); - } - - 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); -} - -/* - * Parses `cmdline` with the iterator API using the option descriptors - * `descrs`, and ensures that the resulting effective command line is - * `expected_cmd_line` and that the number of ingested original - * arguments is `expected_ingested_orig_args`. - * - * This function splits `cmdline` on spaces to create an original - * argument array. - * - * This function builds the resulting command line from parsing items - * by space-separating each formatted item (see append_to_res_str()). - */ -static -void test_succeed_argpar_iter(const char * const cmdline, +void test_succeed(const char * const cmdline, const char * const expected_cmd_line, const struct argpar_opt_descr * const descrs, const unsigned int expected_ingested_orig_args) { struct argpar_iter *iter = NULL; const struct argpar_item *item = NULL; - char *error = NULL; + const struct argpar_error *error = NULL; GString * const res_str = g_string_new(NULL); gchar ** const argv = g_strsplit(cmdline, " ", 0); unsigned int i, actual_ingested_orig_args; @@ -191,33 +125,19 @@ void test_succeed_argpar_iter(const char * const cmdline, status = argpar_iter_next(iter, &item, &error); ok(status == ARGPAR_ITER_NEXT_STATUS_OK || - status == ARGPAR_ITER_NEXT_STATUS_END || - status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, + status == ARGPAR_ITER_NEXT_STATUS_END, "argpar_iter_next() returns the expected status " "(%d) for command line `%s` (call %u)", status, cmdline, i + 1); + ok(!error, + "argpar_iter_next() doesn't set an error for " + "command line `%s` (call %u)", + cmdline, i + 1); - if (status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) { - ok(error, - "argpar_iter_next() sets an error for " - "status `ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " - "and command line `%s` (call %u)", - cmdline, i + 1); - } else { - ok(!error, - "argpar_iter_next() doesn't set an error " - "for other status than " - "`ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " - "and command line `%s` (call %u)", - cmdline, i + 1); - } - - if (status == ARGPAR_ITER_NEXT_STATUS_END || - status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) { + if (status == ARGPAR_ITER_NEXT_STATUS_END) { ok(!item, "argpar_iter_next() doesn't set an item " "for status `ARGPAR_ITER_NEXT_STATUS_END` " - "or `ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " "and command line `%s` (call %u)", cmdline, i + 1); break; @@ -248,25 +168,9 @@ void test_succeed_argpar_iter(const char * const cmdline, argpar_item_destroy(item); argpar_iter_destroy(iter); + assert(!error); g_string_free(res_str, TRUE); g_strfreev(argv); - free(error); -} - -/* - * Calls test_succeed_argpar_parse() and test_succeed_argpar_iter() - * with the provided parameters. - */ -static -void test_succeed(const char * const cmdline, - const char * const expected_cmd_line, - const struct argpar_opt_descr * const descrs, - const 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 @@ -456,84 +360,6 @@ void succeed_tests(void) descrs, 7); } - /* Unknown short option (space form) */ - { - const struct argpar_opt_descr descrs[] = { - { 0, 'd', NULL, true }, - ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-d salut -e -d meow", - "-d salut", - descrs, 2); - } - - /* Unknown short option (glued form) */ - { - const struct argpar_opt_descr descrs[] = { - { 0, 'd', NULL, true }, - ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "-dsalut -e -d meow", - "-d salut", - descrs, 1); - } - - /* Unknown long option (space form) */ - { - const struct argpar_opt_descr descrs[] = { - { 0, '\0', "sink", true }, - ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--sink party --food --sink impulse", - "--sink=party", - descrs, 2); - } - - /* Unknown long option (equal form) */ - { - const struct argpar_opt_descr descrs[] = { - { 0, '\0', "sink", true }, - ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--sink=party --food --sink=impulse", - "--sink=party", - descrs, 1); - } - - /* Unknown option before non-option argument */ - { - const struct argpar_opt_descr descrs[] = { - { 0, '\0', "thumb", true }, - ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--thumb=party --food bateau --thumb waves", - "--thumb=party", - descrs, 1); - } - - /* Unknown option after non-option argument */ - { - const struct argpar_opt_descr descrs[] = { - { 0, '\0', "thumb", true }, - ARGPAR_OPT_DESCR_SENTINEL - }; - - test_succeed( - "--thumb=party wound --food --thumb waves", - "--thumb=party wound<1,0>", - descrs, 2); - } - /* Valid `---opt` */ { const struct argpar_opt_descr descrs[] = { @@ -682,68 +508,38 @@ void succeed_tests(void) } /* - * Parses `cmdline` with argpar_parse() using the option descriptors - * `descrs`, and ensures that the function fails and that it sets an - * error which is equal to `expected_error`. - * - * This function splits `cmdline` on spaces to create an original - * argument array. - */ -static -void test_fail_argpar_parse(const char * const cmdline, - const char * const expected_error, - const struct argpar_opt_descr * const descrs) -{ - struct argpar_parse_ret parse_ret; - gchar ** const argv = g_strsplit(cmdline, " ", 0); - - parse_ret = argpar_parse(g_strv_length(argv), - (const char * const *) argv, descrs, true); - ok(!parse_ret.items, - "argpar_parse() fails for command line `%s`", cmdline); - ok(parse_ret.error, - "argpar_parse() sets an error string for command line `%s`", - cmdline); - - if (parse_ret.items) { - fail("argpar_parse() sets the expected error string"); - goto end; - } - - ok(strcmp(expected_error, parse_ret.error) == 0, - "argpar_parse() sets the expected error string " - "for command line `%s`", cmdline); - - if (strcmp(expected_error, parse_ret.error) != 0) { - diag("Expected: `%s`", expected_error); - diag("Got: `%s`", parse_ret.error); - } - -end: - argpar_parse_ret_fini(&parse_ret); - g_strfreev(argv); -} - -/* - * Parses `cmdline` with the iterator API using the option descriptors + * Parses `cmdline` with the argpar API using the option descriptors * `descrs`, and ensures that argpar_iter_next() fails with status - * `expected_status` and that it sets an error which is equal to - * `expected_error`. + * `expected_status` and that it sets an error having: + * + * * The original argument index `expected_orig_index`. + * + * * If applicable: + * + * * The unknown option name `expected_unknown_opt_name`. + * + * * The option descriptor at index `expected_opt_descr_index` of + * `descrs`. + * + * * The option type `expected_is_short`. * * This function splits `cmdline` on spaces to create an original * argument array. */ static -void test_fail_argpar_iter(const char * const cmdline, - const char * const expected_error, +void test_fail(const char * const cmdline, const enum argpar_iter_next_status expected_status, + const unsigned int expected_orig_index, + const char * const expected_unknown_opt_name, + const unsigned int expected_opt_descr_index, + const bool expected_is_short, const struct argpar_opt_descr * const descrs) { struct argpar_iter *iter = NULL; const struct argpar_item *item = NULL; gchar ** const argv = g_strsplit(cmdline, " ", 0); unsigned int i; - char *error = NULL; + const struct argpar_error *error = NULL; iter = argpar_iter_create(g_strv_length(argv), (const char * const *) argv, descrs); @@ -773,6 +569,35 @@ void test_fail_argpar_iter(const char * const cmdline, " `ARGPAR_ITER_NEXT_STATUS_OK` " "and command line `%s` (call %u)", cmdline, i + 1); + ok(argpar_error_orig_index(error) == + expected_orig_index, + "argpar_iter_next() sets an error with " + "the expected original argument index " + "for command line `%s` (call %u)", + cmdline, i + 1); + + if (status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) { + ok(strcmp(argpar_error_unknown_opt_name(error), + expected_unknown_opt_name) == 0, + "argpar_iter_next() sets an error with " + "the expected unknown option name " + "for command line `%s` (call %u)", + cmdline, i + 1); + } else { + bool is_short; + + ok(argpar_error_opt_descr(error, &is_short) == + &descrs[expected_opt_descr_index], + "argpar_iter_next() sets an error with " + "the expected option descriptor " + "for command line `%s` (call %u)", + cmdline, i + 1); + ok(is_short == expected_is_short, + "argpar_iter_next() sets an error with " + "the expected option type " + "for command line `%s` (call %u)", + cmdline, i + 1); + } break; } @@ -788,6 +613,7 @@ void test_fail_argpar_iter(const char * const cmdline, cmdline, i + 1); } + /* ok(strcmp(expected_error, error) == 0, "argpar_iter_next() sets the expected error string " "for command line `%s`", cmdline); @@ -796,31 +622,75 @@ void test_fail_argpar_iter(const char * const cmdline, diag("Expected: `%s`", expected_error); diag("Got: `%s`", error); } + */ argpar_item_destroy(item); argpar_iter_destroy(iter); - free(error); + argpar_error_destroy(error); g_strfreev(argv); } -/* - * Calls test_fail_argpar_parse() and test_fail_argpar_iter() with the - * provided parameters. - */ -static -void test_fail(const char * const cmdline, const char * const expected_error, - const enum argpar_iter_next_status expected_iter_next_status, - const struct argpar_opt_descr * const descrs) -{ - test_fail_argpar_parse(cmdline, expected_error, descrs); - test_fail_argpar_iter(cmdline, expected_error, - expected_iter_next_status, descrs); -} - static void fail_tests(void) { - /* Unknown long option */ + + /* Unknown short option (space form) */ + { + const struct argpar_opt_descr descrs[] = { + { 0, 'd', NULL, true }, + ARGPAR_OPT_DESCR_SENTINEL + }; + + test_fail( + "-d salut -e -d meow", + ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, + 2, "-e", 0, false, + descrs); + } + + /* Unknown short option (glued form) */ + { + const struct argpar_opt_descr descrs[] = { + { 0, 'd', 0, true }, + ARGPAR_OPT_DESCR_SENTINEL + }; + + test_fail( + "-dsalut -e -d meow", + ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, + 1, "-e", 0, false, + descrs); + } + + /* Unknown long option (space form) */ + { + const struct argpar_opt_descr descrs[] = { + { 0, '\0', "sink", true }, + ARGPAR_OPT_DESCR_SENTINEL + }; + + test_fail( + "--sink party --food --sink impulse", + ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, + 2, "--food", 0, false, + descrs); + } + + /* Unknown long option (equal form) */ + { + const struct argpar_opt_descr descrs[] = { + { 0, '\0', "sink", true }, + ARGPAR_OPT_DESCR_SENTINEL + }; + + test_fail( + "--sink=party --food --sink=impulse", + ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, + 1, "--food", 0, false, + descrs); + } + + /* Unknown option before non-option argument */ { const struct argpar_opt_descr descrs[] = { { 0, '\0', "thumb", true }, @@ -828,13 +698,13 @@ void fail_tests(void) }; test_fail( - "--thumb=party --meow", - "While parsing argument #2 (`--meow`): Unknown option `--meow`", + "--thumb=party --food=18 bateau --thumb waves", ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, + 1, "--food", 0, false, descrs); } - /* Unknown short option */ + /* Unknown option after non-option argument */ { const struct argpar_opt_descr descrs[] = { { 0, '\0', "thumb", true }, @@ -842,9 +712,9 @@ void fail_tests(void) }; test_fail( - "--thumb=party -x", - "While parsing argument #2 (`-x`): Unknown option `-x`", + "--thumb=party wound --food --thumb waves", ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, + 2, "--food", 0, false, descrs); } @@ -856,9 +726,9 @@ void fail_tests(void) }; test_fail( - "--thumb", - "While parsing argument #1 (`--thumb`): Missing required argument for option `--thumb`", + "allo --thumb", ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, + 1, NULL, 0, false, descrs); } @@ -870,9 +740,9 @@ void fail_tests(void) }; test_fail( - "-k", - "While parsing argument #1 (`-k`): Missing required argument for option `-k`", + "zoom heille -k", ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, + 2, NULL, 0, true, descrs); } @@ -887,8 +757,8 @@ void fail_tests(void) test_fail( "-abc", - "While parsing argument #1 (`-abc`): Missing required argument for option `-c`", ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, + 0, NULL, 2, true, descrs); } @@ -900,16 +770,16 @@ void fail_tests(void) }; test_fail( - "--chevre=fromage", - "While parsing argument #1 (`--chevre=fromage`): Unexpected argument for option `--chevre`", + "ambulance --chevre=fromage tar -cjv", ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG, + 1, NULL, 0, false, descrs); } } int main(void) { - plan_tests(434); + plan_tests(309); succeed_tests(); fail_tests(); return exit_status();