X-Git-Url: http://git.efficios.com/?p=argpar.git;a=blobdiff_plain;f=tests%2Ftest_argpar.c;h=a111d423275a87cfb47b1053f2ddcf2fdc17d21b;hp=5c4700aa27e8a8ac7afcb5087cb6c24d30951214;hb=4d6198b505eaeb2e60a14331ccb52f90ea397bb4;hpb=d4d05805c9634c2ca9ae92950d01196917b6b260 diff --git a/tests/test_argpar.c b/tests/test_argpar.c index 5c4700a..a111d42 100644 --- a/tests/test_argpar.c +++ b/tests/test_argpar.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -87,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`. @@ -99,73 +100,7 @@ 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) @@ -184,39 +119,39 @@ void test_succeed_argpar_iter(const char * const cmdline, assert(iter); for (i = 0; ; i++) { - enum argpar_iter_parse_next_status status; + enum argpar_iter_next_status status; ARGPAR_ITEM_DESTROY_AND_RESET(item); - status = argpar_iter_parse_next(iter, &item, &error); + status = argpar_iter_next(iter, &item, &error); - 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() returns the expected status " + ok(status == ARGPAR_ITER_NEXT_STATUS_OK || + status == ARGPAR_ITER_NEXT_STATUS_END || + status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, + "argpar_iter_next() returns the expected status " "(%d) for command line `%s` (call %u)", status, cmdline, i + 1); - if (status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) { + if (status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) { ok(error, - "argpar_iter_parse_next() sets an error for " - "status `ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT` " + "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_parse_next() doesn't set an error " + "argpar_iter_next() doesn't set an error " "for other status than " - "`ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT` " + "`ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT` " "and command line `%s` (call %u)", cmdline, i + 1); } - if (status == ARGPAR_ITER_PARSE_NEXT_STATUS_END || - status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) { + if (status == ARGPAR_ITER_NEXT_STATUS_END || + status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) { ok(!item, - "argpar_iter_parse_next() doesn't set an item " - "for status `ARGPAR_ITER_PARSE_NEXT_STATUS_END` " - "or `ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT` " + "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; @@ -225,9 +160,9 @@ void test_succeed_argpar_iter(const char * const cmdline, append_to_res_str(res_str, item); } - actual_ingested_orig_args = argpar_iter_get_ingested_orig_args(iter); + actual_ingested_orig_args = argpar_iter_ingested_orig_args(iter); ok(actual_ingested_orig_args == expected_ingested_orig_args, - "argpar_iter_get_ingested_orig_args() returns the expected " + "argpar_iter_ingested_orig_args() returns the expected " "number of ingested original arguments for command line `%s`", cmdline); @@ -237,7 +172,7 @@ void test_succeed_argpar_iter(const char * const cmdline, } ok(strcmp(expected_cmd_line, res_str->str) == 0, - "argpar_iter_parse_next() returns the expected parsing items " + "argpar_iter_next() returns the expected parsing items " "for command line `%s`", cmdline); if (strcmp(expected_cmd_line, res_str->str) != 0) { @@ -252,22 +187,6 @@ void test_succeed_argpar_iter(const char * const cmdline, 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 void succeed_tests(void) { @@ -624,54 +543,65 @@ void succeed_tests(void) "-f --yeah= -f", descrs, 3); } -} - -/* - * 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); + /* `-` non-option argument */ + { + const struct argpar_opt_descr descrs[] = { + { 0, 'f', NULL, false }, + ARGPAR_OPT_DESCR_SENTINEL + }; - if (parse_ret.items) { - fail("argpar_parse() sets the expected error string"); - goto end; + test_succeed( + "-f - -f", + "-f -<1,0> -f", + descrs, 3); } - ok(strcmp(expected_error, parse_ret.error) == 0, - "argpar_parse() sets the expected error string " - "for command line `%s`", cmdline); + /* `--` non-option argument */ + { + const struct argpar_opt_descr descrs[] = { + { 0, 'f', NULL, false }, + ARGPAR_OPT_DESCR_SENTINEL + }; - if (strcmp(expected_error, parse_ret.error) != 0) { - diag("Expected: `%s`", expected_error); - diag("Got: `%s`", parse_ret.error); + test_succeed( + "-f -- -f", + "-f --<1,0> -f", + descrs, 3); } -end: - argpar_parse_ret_fini(&parse_ret); - g_strfreev(argv); + /* Very long name of long option */ + { + const char opt_name[] = + "kale-chips-waistcoat-yr-bicycle-rights-gochujang-" + "woke-tumeric-flexitarian-biodiesel-chillwave-cliche-" + "ethical-cardigan-listicle-pok-pok-sustainable-live-" + "edge-jianbing-gochujang-butcher-disrupt-tattooed-" + "tumeric-prism-photo-booth-vape-kogi-jean-shorts-" + "blog-williamsburg-fingerstache-palo-santo-artisan-" + "affogato-occupy-skateboard-adaptogen-neutra-celiac-" + "put-a-bird-on-it-kombucha-everyday-carry-hot-chicken-" + "craft-beer-subway-tile-tote-bag-disrupt-selvage-" + "raclette-art-party-readymade-paleo-heirloom-trust-" + "fund-small-batch-kinfolk-woke-cardigan-prism-" + "chambray-la-croix-hashtag-unicorn-edison-bulb-tbh-" + "cornhole-cliche-tattooed-green-juice-adaptogen-" + "kitsch-lo-fi-vexillologist-migas-gentrify-" + "viral-raw-denim"; + const struct argpar_opt_descr descrs[] = { + { 0, '\0', opt_name, true }, + ARGPAR_OPT_DESCR_SENTINEL + }; + char cmdline[1024]; + + sprintf(cmdline, "--%s=23", opt_name); + test_succeed(cmdline, cmdline, descrs, 1); + } } /* - * Parses `cmdline` with the iterator API using the option descriptors - * `descrs`, and ensures that argpar_iter_parse_next() fails with status + * 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`. * @@ -679,9 +609,8 @@ end: * argument array. */ static -void test_fail_argpar_iter(const char * const cmdline, - const char * const expected_error, - const enum argpar_iter_parse_next_status expected_status, +void test_fail(const char * const cmdline, const char * const expected_error, + const enum argpar_iter_next_status expected_status, const struct argpar_opt_descr * const descrs) { struct argpar_iter *iter = NULL; @@ -695,46 +624,46 @@ void test_fail_argpar_iter(const char * const cmdline, assert(iter); for (i = 0; ; i++) { - enum argpar_iter_parse_next_status status; + enum argpar_iter_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_next(iter, &item, &error); + ok(status == ARGPAR_ITER_NEXT_STATUS_OK || status == expected_status, - "argpar_iter_parse_next() returns the expected status " + "argpar_iter_next() returns the expected status " "(%d) for command line `%s` (call %u)", status, cmdline, i + 1); - if (status != ARGPAR_ITER_PARSE_NEXT_STATUS_OK) { + if (status != ARGPAR_ITER_NEXT_STATUS_OK) { ok(!item, - "argpar_iter_parse_next() doesn't set an item " + "argpar_iter_next() doesn't set an item " "for other status than " - "`ARGPAR_ITER_PARSE_NEXT_STATUS_OK` " + "`ARGPAR_ITER_NEXT_STATUS_OK` " "and command line `%s` (call %u)", cmdline, i + 1); ok(error, - "argpar_iter_parse_next() sets an error for " + "argpar_iter_next() sets an error for " "other status than " - " `ARGPAR_ITER_PARSE_NEXT_STATUS_OK` " + " `ARGPAR_ITER_NEXT_STATUS_OK` " "and command line `%s` (call %u)", cmdline, i + 1); break; } ok(item, - "argpar_iter_parse_next() sets an item for status " - "`ARGPAR_ITER_PARSE_NEXT_STATUS_OK` " + "argpar_iter_next() sets an item for status " + "`ARGPAR_ITER_NEXT_STATUS_OK` " "and command line `%s` (call %u)", cmdline, i + 1); ok(!error, - "argpar_iter_parse_next() doesn't set an error for status " - "`ARGPAR_ITER_PARSE_NEXT_STATUS_OK` " + "argpar_iter_next() doesn't set an error for status " + "`ARGPAR_ITER_NEXT_STATUS_OK` " "and command line `%s` (call %u)", cmdline, i + 1); } ok(strcmp(expected_error, error) == 0, - "argpar_iter_parse_next() sets the expected error string " + "argpar_iter_next() sets the expected error string " "for command line `%s`", cmdline); if (strcmp(expected_error, error) != 0) { @@ -748,20 +677,6 @@ void test_fail_argpar_iter(const char * const cmdline, 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_parse_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) { @@ -775,7 +690,7 @@ void fail_tests(void) test_fail( "--thumb=party --meow", "While parsing argument #2 (`--meow`): Unknown option `--meow`", - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT, + ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, descrs); } @@ -789,7 +704,7 @@ void fail_tests(void) test_fail( "--thumb=party -x", "While parsing argument #2 (`-x`): Unknown option `-x`", - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT, + ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT, descrs); } @@ -803,7 +718,7 @@ void fail_tests(void) test_fail( "--thumb", "While parsing argument #1 (`--thumb`): Missing required argument for option `--thumb`", - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_MISSING_OPT_ARG, + ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, descrs); } @@ -817,7 +732,7 @@ void fail_tests(void) test_fail( "-k", "While parsing argument #1 (`-k`): Missing required argument for option `-k`", - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_MISSING_OPT_ARG, + ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, descrs); } @@ -833,42 +748,11 @@ void fail_tests(void) test_fail( "-abc", "While parsing argument #1 (`-abc`): Missing required argument for option `-c`", - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_MISSING_OPT_ARG, - descrs); - } - - /* Invalid `-` */ - { - const struct argpar_opt_descr descrs[] = { - { 0, 'a', NULL, false }, - { 0, 'b', NULL, false }, - { 0, 'c', NULL, true }, - ARGPAR_OPT_DESCR_SENTINEL - }; - - test_fail( - "-ab - -c", - "While parsing argument #2 (`-`): Invalid argument", - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_INVALID_ARG, - descrs); - } - - /* Invalid `--` */ - { - const struct argpar_opt_descr descrs[] = { - { 0, 'a', NULL, false }, - { 0, 'b', NULL, false }, - { 0, 'c', NULL, true }, - ARGPAR_OPT_DESCR_SENTINEL - }; - - test_fail( - "-ab -- -c", - "While parsing argument #2 (`--`): Invalid argument", - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_INVALID_ARG, + ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG, descrs); } + /* Unexpected long option argument */ { const struct argpar_opt_descr descrs[] = { { 0, 'c', "chevre", false }, @@ -878,14 +762,14 @@ void fail_tests(void) test_fail( "--chevre=fromage", "While parsing argument #1 (`--chevre=fromage`): Unexpected argument for option `--chevre`", - ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG, + ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG, descrs); } } int main(void) { - plan_tests(419); + plan_tests(296); succeed_tests(); fail_tests(); return exit_status();