* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "tap/tap.h"
-#include "common/assert.h"
-
#include "argpar/argpar.h"
/*
static
void test_succeed(const char *cmdline,
const char *expected_cmd_line,
- const struct bt_argpar_opt_descr *descrs,
+ const struct argpar_opt_descr *descrs,
unsigned int expected_ingested_orig_args)
{
- struct bt_argpar_parse_ret parse_ret;
+ struct argpar_parse_ret parse_ret;
GString *res_str = g_string_new(NULL);
gchar **argv = g_strsplit(cmdline, " ", 0);
unsigned int i;
- BT_ASSERT(argv);
- BT_ASSERT(res_str);
- parse_ret = bt_argpar_parse(g_strv_length(argv),
+ assert(argv);
+ assert(res_str);
+ parse_ret = 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);
+ "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);
+ "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 "
+ "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,
}
if (!parse_ret.items) {
- fail("bt_argpar_parse() returns the expected parsed arguments "
+ fail("argpar_parse() returns the expected parsed arguments "
"for command line `%s`", cmdline);
goto end;
}
- for (i = 0; i < parse_ret.items->len; i++) {
- const struct bt_argpar_item *arg = parse_ret.items->pdata[i];
+ for (i = 0; i < parse_ret.items->n_items; i++) {
+ const struct argpar_item *arg = parse_ret.items->items[i];
switch (arg->type) {
- case BT_ARGPAR_ITEM_TYPE_OPT:
+ case ARGPAR_ITEM_TYPE_OPT:
{
- const struct bt_argpar_item_opt *arg_opt =
+ const struct argpar_item_opt *arg_opt =
(const void *) arg;
if (arg_opt->descr->long_name) {
break;
}
- case BT_ARGPAR_ITEM_TYPE_NON_OPT:
+ case ARGPAR_ITEM_TYPE_NON_OPT:
{
- const struct bt_argpar_item_non_opt *arg_non_opt =
+ const struct argpar_item_non_opt *arg_non_opt =
(const void *) arg;
g_string_append_printf(res_str, "%s<%u,%u> ",
}
ok(strcmp(expected_cmd_line, res_str->str) == 0,
- "bt_argpar_parse() returns the expected parsed arguments "
+ "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);
}
end:
- bt_argpar_parse_ret_fini(&parse_ret);
+ argpar_parse_ret_fini(&parse_ret);
g_string_free(res_str, TRUE);
g_strfreev(argv);
}
{
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
*/
static
void test_fail(const char *cmdline, const char *expected_error,
- const struct bt_argpar_opt_descr *descrs)
+ const struct argpar_opt_descr *descrs)
{
- struct bt_argpar_parse_ret parse_ret;
+ struct argpar_parse_ret parse_ret;
gchar **argv = g_strsplit(cmdline, " ", 0);
- parse_ret = bt_argpar_parse(g_strv_length(argv),
+ parse_ret = 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);
+ "argpar_parse() fails for command line `%s`", cmdline);
ok(parse_ret.error,
- "bt_argpar_parse() writes an error string for command line `%s`",
+ "argpar_parse() writes an error string for command line `%s`",
cmdline);
if (parse_ret.items) {
- fail("bt_argpar_parse() writes the expected error string");
+ fail("argpar_parse() writes the expected error string");
goto end;
}
- ok(strcmp(expected_error, parse_ret.error->str) == 0,
- "bt_argpar_parse() writes the expected error string "
+ ok(strcmp(expected_error, parse_ret.error) == 0,
+ "argpar_parse() writes the expected error string "
"for command line `%s`", cmdline);
- if (strcmp(expected_error, parse_ret.error->str) != 0) {
+ if (strcmp(expected_error, parse_ret.error) != 0) {
diag("Expected: `%s`", expected_error);
- diag("Got: `%s`", parse_ret.error->str);
+ diag("Got: `%s`", parse_ret.error);
}
end:
- bt_argpar_parse_ret_fini(&parse_ret);
+ argpar_parse_ret_fini(&parse_ret);
g_strfreev(argv);
}
{
/* 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(
/* 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(
/* 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(
/* 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(
/* 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(
/* Invalid `-` */
{
- 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(
/* Invalid `--` */
{
- 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(
"While parsing argument #2 (`--`): Invalid argument",
descrs);
}
+
+ {
+ const struct argpar_opt_descr descrs[] = {
+ { 0, 'c', "chevre", false },
+ ARGPAR_OPT_DESCR_SENTINEL
+ };
+
+ test_fail(
+ "--chevre=fromage",
+ "While parsing argument #1 (`--chevre=fromage`): Unexpected argument for option `--chevre`",
+ descrs);
+ }
}
int main(void)
{
- plan_tests(129);
+ plan_tests(132);
succeed_tests();
fail_tests();
return exit_status();