mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-07-31 06:13:29 +02:00
Add some tests for '--' stripping.
2004-12-20 Matthias Clasen <mclasen@redhat.com> * tests/option-test.c: Add some tests for '--' stripping. * glib/goption.c (g_option_context_parse): Don't strip '--' if it would be needed by a second option parser. (#161701)
This commit is contained in:
committed by
Matthias Clasen
parent
ad90d2de03
commit
9d8c5bc757
@@ -581,6 +581,75 @@ rest_test2 (void)
|
||||
retval = g_option_context_parse (context, &argc, &argv, &error);
|
||||
g_assert (retval);
|
||||
|
||||
/* Check array */
|
||||
g_assert (ignore_test1_boolean);
|
||||
g_assert (strcmp (argv[0], "program") == 0);
|
||||
g_assert (strcmp (argv[1], "foo") == 0);
|
||||
g_assert (strcmp (argv[2], "--") == 0);
|
||||
g_assert (strcmp (argv[3], "-bar") == 0);
|
||||
g_assert (argv[4] == NULL);
|
||||
|
||||
g_strfreev (argv);
|
||||
g_option_context_free (context);
|
||||
}
|
||||
|
||||
/* check that -- stripping works */
|
||||
void
|
||||
rest_test2a (void)
|
||||
{
|
||||
GOptionContext *context;
|
||||
gboolean retval;
|
||||
GError *error = NULL;
|
||||
gchar **argv;
|
||||
int argc;
|
||||
GOptionEntry entries [] = {
|
||||
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
context = g_option_context_new (NULL);
|
||||
g_option_context_add_main_entries (context, entries, NULL);
|
||||
|
||||
/* Now try parsing */
|
||||
argv = split_string ("program foo --test -- bar", &argc);
|
||||
|
||||
retval = g_option_context_parse (context, &argc, &argv, &error);
|
||||
g_assert (retval);
|
||||
|
||||
/* Check array */
|
||||
g_assert (ignore_test1_boolean);
|
||||
g_assert (strcmp (argv[0], "program") == 0);
|
||||
g_assert (strcmp (argv[1], "foo") == 0);
|
||||
g_assert (strcmp (argv[2], "bar") == 0);
|
||||
g_assert (argv[3] == NULL);
|
||||
|
||||
g_strfreev (argv);
|
||||
g_option_context_free (context);
|
||||
}
|
||||
|
||||
void
|
||||
rest_test2b (void)
|
||||
{
|
||||
GOptionContext *context;
|
||||
gboolean retval;
|
||||
GError *error = NULL;
|
||||
gchar **argv;
|
||||
int argc;
|
||||
GOptionEntry entries [] = {
|
||||
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
context = g_option_context_new (NULL);
|
||||
g_option_context_set_ignore_unknown_options (context, TRUE);
|
||||
g_option_context_add_main_entries (context, entries, NULL);
|
||||
|
||||
/* Now try parsing */
|
||||
argv = split_string ("program foo --test -bar --", &argc);
|
||||
|
||||
retval = g_option_context_parse (context, &argc, &argv, &error);
|
||||
g_assert (retval);
|
||||
|
||||
/* Check array */
|
||||
g_assert (ignore_test1_boolean);
|
||||
g_assert (strcmp (argv[0], "program") == 0);
|
||||
@@ -592,6 +661,73 @@ rest_test2 (void)
|
||||
g_option_context_free (context);
|
||||
}
|
||||
|
||||
void
|
||||
rest_test2c (void)
|
||||
{
|
||||
GOptionContext *context;
|
||||
gboolean retval;
|
||||
GError *error = NULL;
|
||||
gchar **argv;
|
||||
int argc;
|
||||
GOptionEntry entries [] = {
|
||||
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
context = g_option_context_new (NULL);
|
||||
g_option_context_add_main_entries (context, entries, NULL);
|
||||
|
||||
/* Now try parsing */
|
||||
argv = split_string ("program --test foo -- bar", &argc);
|
||||
|
||||
retval = g_option_context_parse (context, &argc, &argv, &error);
|
||||
g_assert (retval);
|
||||
|
||||
/* Check array */
|
||||
g_assert (ignore_test1_boolean);
|
||||
g_assert (strcmp (argv[0], "program") == 0);
|
||||
g_assert (strcmp (argv[1], "foo") == 0);
|
||||
g_assert (strcmp (argv[2], "bar") == 0);
|
||||
g_assert (argv[3] == NULL);
|
||||
|
||||
g_strfreev (argv);
|
||||
g_option_context_free (context);
|
||||
}
|
||||
|
||||
void
|
||||
rest_test2d (void)
|
||||
{
|
||||
GOptionContext *context;
|
||||
gboolean retval;
|
||||
GError *error = NULL;
|
||||
gchar **argv;
|
||||
int argc;
|
||||
GOptionEntry entries [] = {
|
||||
{ "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
context = g_option_context_new (NULL);
|
||||
g_option_context_add_main_entries (context, entries, NULL);
|
||||
|
||||
/* Now try parsing */
|
||||
argv = split_string ("program --test -- -bar", &argc);
|
||||
|
||||
retval = g_option_context_parse (context, &argc, &argv, &error);
|
||||
g_assert (retval);
|
||||
|
||||
/* Check array */
|
||||
g_assert (ignore_test1_boolean);
|
||||
g_assert (strcmp (argv[0], "program") == 0);
|
||||
g_assert (strcmp (argv[1], "--") == 0);
|
||||
g_assert (strcmp (argv[2], "-bar") == 0);
|
||||
g_assert (argv[3] == NULL);
|
||||
|
||||
g_strfreev (argv);
|
||||
g_option_context_free (context);
|
||||
}
|
||||
|
||||
|
||||
/* check that G_OPTION_REMAINING collects non-option arguments */
|
||||
void
|
||||
rest_test3 (void)
|
||||
@@ -736,6 +872,10 @@ main (int argc, char **argv)
|
||||
/* Test handling of rest args */
|
||||
rest_test1 ();
|
||||
rest_test2 ();
|
||||
rest_test2a ();
|
||||
rest_test2b ();
|
||||
rest_test2c ();
|
||||
rest_test2d ();
|
||||
rest_test3 ();
|
||||
rest_test4 ();
|
||||
rest_test5 ();
|
||||
|
Reference in New Issue
Block a user