+/*
+ * 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`.
+ *
+ * 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,
+ 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;
+
+ 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() returns the expected status "
+ "(%d) for command line `%s` (call %u)",
+ status, cmdline, i + 1);
+
+ if (status != ARGPAR_ITER_PARSE_NEXT_STATUS_OK) {
+ ok(!item,
+ "argpar_iter_parse_next() doesn't set an item "
+ "for other status than "
+ "`ARGPAR_ITER_PARSE_NEXT_STATUS_OK` "
+ "and command line `%s` (call %u)",
+ cmdline, i + 1);
+ ok(error,
+ "argpar_iter_parse_next() sets an error for "
+ "other status than "
+ " `ARGPAR_ITER_PARSE_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` "
+ "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` "
+ "and command line `%s` (call %u)",
+ cmdline, i + 1);
+ }
+
+ ok(strcmp(expected_error, error) == 0,
+ "argpar_iter_parse_next() sets 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);
+}
+
+/*
+ * 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 struct argpar_opt_descr * const descrs)
+{
+ test_fail_argpar_parse(cmdline, expected_error, descrs);
+ test_fail_argpar_iter(cmdline, expected_error, descrs);
+}
+