+/* 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);
+}
+