Remove the argpar_parse() API
[argpar.git] / tests / test_argpar.c
index 4674d8984fc706cecab75cc5b3d032e25f4531ed..a111d423275a87cfb47b1053f2ddcf2fdc17d21b 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
+ * Copyright (c) 2019-2021 Philippe Proulx <pproulx@efficios.com>
+ * Copyright (c) 2020-2021 Simon Marchi <simon.marchi@efficios.com>
  *
  * 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
 
 #include <assert.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
+#include <stdbool.h>
 #include <glib.h>
 
 #include "tap/tap.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`.
+ * Formats `item` and appends the resulting string to `res_str` to
+ * incrementally build an expected command line string.
  *
- * 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<A,B>` form for non-option
- * arguments where `A` is the original argument index and `B` is the
- * non-option argument index.
+ * This function:
+ *
+ * * Prefers the `--long-opt=arg` style over the `-s arg` style.
+ *
+ * * Uses the `arg<A,B>` form for non-option arguments, where `A` is the
+ *   original argument index and `B` is the non-option argument index.
  */
 static
-void test_succeed(const char *cmdline,
-               const char *expected_cmd_line,
-               const struct bt_argpar_opt_descr *descrs,
-               unsigned int expected_ingested_orig_args)
+void append_to_res_str(GString * const res_str,
+               const struct argpar_item * const item)
 {
-       struct bt_argpar_parse_ret parse_ret;
-       GString *res_str = g_string_new(NULL);
-       gchar **argv = g_strsplit(cmdline, " ", 0);
-       unsigned int i;
-
-       assert(argv);
-       assert(res_str);
-       parse_ret = bt_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);
-       ok(!parse_ret.error,
-               "bt_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 "
-               "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("bt_argpar_parse() returns the expected parsed arguments "
-                       "for command line `%s`", cmdline);
-               goto end;
+       if (res_str->len > 0) {
+               g_string_append_c(res_str, ' ');
        }
 
-       for (i = 0; i < parse_ret.items->n_items; i++) {
-               const struct bt_argpar_item *arg = parse_ret.items->items[i];
+       switch (argpar_item_type(item)) {
+       case ARGPAR_ITEM_TYPE_OPT:
+       {
+               const struct argpar_opt_descr * const descr =
+                       argpar_item_opt_descr(item);
+               const char * const arg = argpar_item_opt_arg(item);
 
-               switch (arg->type) {
-               case BT_ARGPAR_ITEM_TYPE_OPT:
-               {
-                       const struct bt_argpar_item_opt *arg_opt =
-                               (const void *) arg;
+               if (descr->long_name) {
+                       g_string_append_printf(res_str, "--%s",
+                               descr->long_name);
 
-                       if (arg_opt->descr->long_name) {
-                               g_string_append_printf(res_str, "--%s",
-                                       arg_opt->descr->long_name);
+                       if (arg) {
+                               g_string_append_printf(res_str, "=%s", arg);
+                       }
+               } else if (descr->short_name) {
+                       g_string_append_printf(res_str, "-%c",
+                               descr->short_name);
 
-                               if (arg_opt->arg) {
-                                       g_string_append_printf(res_str, "=%s",
-                                               arg_opt->arg);
-                               }
+                       if (arg) {
+                               g_string_append_printf(res_str, " %s", arg);
+                       }
+               }
 
-                               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);
+               break;
+       }
+       case ARGPAR_ITEM_TYPE_NON_OPT:
+       {
+               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);
 
-                               if (arg_opt->arg) {
-                                       g_string_append_printf(res_str, " %s",
-                                               arg_opt->arg);
-                               }
+               g_string_append_printf(res_str, "%s<%u,%u>", arg, orig_index,
+                       non_opt_index);
+               break;
+       }
+       default:
+               abort();
+       }
+}
 
-                               g_string_append_c(res_str, ' ');
-                       }
+/*
+ * 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`.
+ *
+ * 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(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;
+       GString * const res_str = g_string_new(NULL);
+       gchar ** const argv = g_strsplit(cmdline, " ", 0);
+       unsigned int i, actual_ingested_orig_args;
 
-                       break;
+       assert(argv);
+       assert(res_str);
+       iter = argpar_iter_create(g_strv_length(argv),
+               (const char * const *) argv, descrs);
+       assert(iter);
+
+       for (i = 0; ; i++) {
+               enum argpar_iter_next_status status;
+
+               ARGPAR_ITEM_DESTROY_AND_RESET(item);
+               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,
+                       "argpar_iter_next() returns the expected status "
+                       "(%d) for command line `%s` (call %u)",
+                       status, 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);
                }
-               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_NEXT_STATUS_END ||
+                               status == ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT) {
+                       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;
                }
-               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_ingested_orig_args(iter);
+       ok(actual_ingested_orig_args == expected_ingested_orig_args,
+               "argpar_iter_ingested_orig_args() returns the expected "
+               "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_next() returns the expected parsing items "
                "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);
 }
 
 static
@@ -140,8 +192,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(
@@ -152,9 +204,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(
@@ -165,9 +217,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(
@@ -178,9 +230,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(
@@ -191,9 +243,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(
@@ -204,9 +256,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(
@@ -217,9 +269,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(
@@ -230,9 +282,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(
@@ -243,9 +295,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(
@@ -256,11 +308,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(
@@ -271,11 +323,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(
@@ -286,8 +338,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(
@@ -298,8 +350,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(
@@ -310,10 +362,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(
@@ -324,9 +376,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(
@@ -337,9 +389,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(
@@ -350,9 +402,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(
@@ -363,9 +415,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(
@@ -376,9 +428,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(
@@ -389,9 +441,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(
@@ -402,9 +454,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(
@@ -415,9 +467,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(
@@ -428,9 +480,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(
@@ -441,9 +493,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(
@@ -454,9 +506,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(
@@ -467,9 +519,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(
@@ -480,10 +532,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(
@@ -491,42 +543,137 @@ 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);
+       }
+
+       /* 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);
+       }
 }
 
 /*
- * 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`.
+ * 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`.
+ *
+ * This function splits `cmdline` on spaces to create an original
+ * argument array.
  */
 static
-void test_fail(const char *cmdline, const char *expected_error,
-               const struct bt_argpar_opt_descr *descrs)
+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 bt_argpar_parse_ret parse_ret;
-       gchar **argv = g_strsplit(cmdline, " ", 0);
-
-       parse_ret = bt_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);
-       ok(parse_ret.error,
-               "bt_argpar_parse() writes an error string for command line `%s`",
-               cmdline);
-       if (parse_ret.items) {
-               fail("bt_argpar_parse() writes the expected error string");
-               goto end;
+       struct argpar_iter *iter = NULL;
+       const struct argpar_item *item = NULL;
+       gchar ** const 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_next_status status;
+
+               ARGPAR_ITEM_DESTROY_AND_RESET(item);
+               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_NEXT_STATUS_OK) {
+                       ok(!item,
+                               "argpar_iter_next() doesn't set an item "
+                               "for other status than "
+                               "`ARGPAR_ITER_NEXT_STATUS_OK` "
+                               "and command line `%s` (call %u)",
+                               cmdline, i + 1);
+                       ok(error,
+                               "argpar_iter_next() sets an error for "
+                               "other status than "
+                               " `ARGPAR_ITER_NEXT_STATUS_OK` "
+                               "and command line `%s` (call %u)",
+                               cmdline, i + 1);
+                       break;
+               }
+
+               ok(item,
+                       "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_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, parse_ret.error) == 0,
-               "bt_argpar_parse() writes the expected error string "
+       ok(strcmp(expected_error, error) == 0,
+               "argpar_iter_next() sets the expected error string "
                "for command line `%s`", cmdline);
-       if (strcmp(expected_error, parse_ret.error) != 0) {
+
+       if (strcmp(expected_error, error) != 0) {
                diag("Expected: `%s`", expected_error);
-               diag("Got:      `%s`", parse_ret.error);
+               diag("Got:      `%s`", error);
        }
 
-end:
-       bt_argpar_parse_ret_fini(&parse_ret);
+       argpar_item_destroy(item);
+       argpar_iter_destroy(iter);
+       free(error);
        g_strfreev(argv);
 }
 
@@ -535,105 +682,94 @@ 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(
                        "--thumb=party --meow",
                        "While parsing argument #2 (`--meow`): Unknown option `--meow`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT,
                        descrs);
        }
 
        /* 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(
                        "--thumb=party -x",
                        "While parsing argument #2 (`-x`): Unknown option `-x`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_UNKNOWN_OPT,
                        descrs);
        }
 
        /* 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(
                        "--thumb",
                        "While parsing argument #1 (`--thumb`): Missing required argument for option `--thumb`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG,
                        descrs);
        }
 
        /* 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(
                        "-k",
                        "While parsing argument #1 (`-k`): Missing required argument for option `-k`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG,
                        descrs);
        }
 
        /* 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(
                        "-abc",
                        "While parsing argument #1 (`-abc`): Missing required argument for option `-c`",
+                       ARGPAR_ITER_NEXT_STATUS_ERROR_MISSING_OPT_ARG,
                        descrs);
        }
 
-       /* Invalid `-` */
-       {
-               const struct bt_argpar_opt_descr descrs[] = {
-                       { 0, 'a', NULL, false },
-                       { 0, 'b', NULL, false },
-                       { 0, 'c', NULL, true },
-                       BT_ARGPAR_OPT_DESCR_SENTINEL
-               };
-
-               test_fail(
-                       "-ab - -c",
-                       "While parsing argument #2 (`-`): Invalid argument",
-                       descrs);
-       }
-
-       /* Invalid `--` */
+       /* Unexpected long option argument */
        {
-               const struct bt_argpar_opt_descr descrs[] = {
-                       { 0, 'a', NULL, false },
-                       { 0, 'b', NULL, false },
-                       { 0, 'c', NULL, true },
-                       BT_ARGPAR_OPT_DESCR_SENTINEL
+               const struct argpar_opt_descr descrs[] = {
+                       { 0, 'c', "chevre", false },
+                       ARGPAR_OPT_DESCR_SENTINEL
                };
 
                test_fail(
-                       "-ab -- -c",
-                       "While parsing argument #2 (`--`): Invalid argument",
+                       "--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(129);
+       plan_tests(296);
        succeed_tests();
        fail_tests();
        return exit_status();
This page took 0.034313 seconds and 4 git commands to generate.