Parse `-` and `--` as non-option arguments
[argpar.git] / tests / test_argpar.c
index b04b412db3f12258370c6e4fe64414ddd4d67e50..c78b6e8a6babb71c7b362ba62eb45932e84644fb 100644 (file)
@@ -44,27 +44,26 @@ void append_to_res_str(GString * const res_str,
                g_string_append_c(res_str, ' ');
        }
 
-       switch (item->type) {
+       switch (argpar_item_type(item)) {
        case ARGPAR_ITEM_TYPE_OPT:
        {
-               const struct argpar_item_opt *const item_opt =
-                       (const void *) item;
+               const struct argpar_opt_descr * const descr =
+                       argpar_item_opt_descr(item);
+               const char * const arg = argpar_item_opt_arg(item);
 
-               if (item_opt->descr->long_name) {
+               if (descr->long_name) {
                        g_string_append_printf(res_str, "--%s",
-                               item_opt->descr->long_name);
+                               descr->long_name);
 
-                       if (item_opt->arg) {
-                               g_string_append_printf(res_str, "=%s",
-                                       item_opt->arg);
+                       if (arg) {
+                               g_string_append_printf(res_str, "=%s", arg);
                        }
-               } else if (item_opt->descr->short_name) {
+               } else if (descr->short_name) {
                        g_string_append_printf(res_str, "-%c",
-                               item_opt->descr->short_name);
+                               descr->short_name);
 
-                       if (item_opt->arg) {
-                               g_string_append_printf(res_str, " %s",
-                                       item_opt->arg);
+                       if (arg) {
+                               g_string_append_printf(res_str, " %s", arg);
                        }
                }
 
@@ -72,12 +71,14 @@ void append_to_res_str(GString * const res_str,
        }
        case ARGPAR_ITEM_TYPE_NON_OPT:
        {
-               const struct argpar_item_non_opt * const item_non_opt =
-                       (const void *) item;
+               const char * const arg = argpar_item_non_opt_arg(item);
+               const unsigned int orig_index =
+                       argpar_item_non_opt_orig_index(item);
+               const unsigned int non_opt_index =
+                       argpar_item_non_opt_non_opt_index(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);
+               g_string_append_printf(res_str, "%s<%u,%u>", arg, orig_index,
+                       non_opt_index);
                break;
        }
        default:
@@ -183,39 +184,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;
@@ -224,9 +225,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);
 
@@ -236,7 +237,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) {
@@ -623,6 +624,32 @@ void succeed_tests(void)
                        "-f --yeah= -f",
                        descrs, 3);
        }
+
+       /* `-` non-option argument */
+       {
+               const struct argpar_opt_descr descrs[] = {
+                       { 0, 'f', NULL, false },
+                       ARGPAR_OPT_DESCR_SENTINEL
+               };
+
+               test_succeed(
+                       "-f - -f",
+                       "-f -<1,0> -f",
+                       descrs, 3);
+       }
+
+       /* `--` non-option argument */
+       {
+               const struct argpar_opt_descr descrs[] = {
+                       { 0, 'f', NULL, false },
+                       ARGPAR_OPT_DESCR_SENTINEL
+               };
+
+               test_succeed(
+                       "-f -- -f",
+                       "-f --<1,0> -f",
+                       descrs, 3);
+       }
 }
 
 /*
@@ -670,8 +697,9 @@ end:
 
 /*
  * Parses `cmdline` with the iterator API using the option descriptors
- * `descrs`, and ensures that argpar_iter_parse_next() fails and that it
- * sets an error which is equal to `expected_error`.
+ * `descrs`, and ensures that argpar_iter_next() fails with status
+ * `expected_status` and that it sets an error which is equal to
+ * `expected_error`.
  *
  * This function splits `cmdline` on spaces to create an original
  * argument array.
@@ -679,6 +707,7 @@ end:
 static
 void test_fail_argpar_iter(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;
@@ -692,48 +721,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_PARSE_NEXT_STATUS_ERROR ||
-                       status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT,
-                       "argpar_iter_parse_next() returns the expected status "
+               status = argpar_iter_next(iter, &item, &error);
+               ok(status == ARGPAR_ITER_NEXT_STATUS_OK ||
+                       status == 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) {
@@ -753,10 +780,12 @@ void test_fail_argpar_iter(const char * const cmdline,
  */
 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, descrs);
+       test_fail_argpar_iter(cmdline, expected_error,
+               expected_iter_next_status, descrs);
 }
 
 static
@@ -772,6 +801,7 @@ void fail_tests(void)
                test_fail(
                        "--thumb=party --meow",
                        "While parsing argument #2 (`--meow`): Unknown option `--meow`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT,
                        descrs);
        }
 
@@ -785,6 +815,7 @@ void fail_tests(void)
                test_fail(
                        "--thumb=party -x",
                        "While parsing argument #2 (`-x`): Unknown option `-x`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT,
                        descrs);
        }
 
@@ -798,6 +829,7 @@ void fail_tests(void)
                test_fail(
                        "--thumb",
                        "While parsing argument #1 (`--thumb`): Missing required argument for option `--thumb`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG,
                        descrs);
        }
 
@@ -811,6 +843,7 @@ void fail_tests(void)
                test_fail(
                        "-k",
                        "While parsing argument #1 (`-k`): Missing required argument for option `-k`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG,
                        descrs);
        }
 
@@ -826,39 +859,11 @@ 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,
                        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",
-                       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",
-                       descrs);
-       }
-
+       /* Unexpected long option argument */
        {
                const struct argpar_opt_descr descrs[] = {
                        { 0, 'c', "chevre", false },
@@ -868,13 +873,14 @@ void fail_tests(void)
                test_fail(
                        "--chevre=fromage",
                        "While parsing argument #1 (`--chevre=fromage`): Unexpected argument for option `--chevre`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_UNEXPECTED_OPT_ARG,
                        descrs);
        }
 }
 
 int main(void)
 {
-       plan_tests(419);
+       plan_tests(423);
        succeed_tests();
        fail_tests();
        return exit_status();
This page took 0.027129 seconds and 4 git commands to generate.