Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Test an option handling corner-case
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Clasen committed Aug 5, 2010
1 parent e2657d8 commit 008615f
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion glib/tests/option-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ missing_arg_test (void)
g_strfreev (argv);

/* Try parsing again */
argv = split_string ("program --t", &argc);
argv = split_string ("program -t", &argc);

retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval == FALSE);
Expand All @@ -1717,6 +1717,62 @@ missing_arg_test (void)
g_option_context_free (context);
}

static gchar *test_arg;

static gboolean cb (const gchar *option_name,
const gchar *value,
gpointer data,
GError **error)
{
test_arg = g_strdup (value);
return TRUE;
}

void
dash_arg_test (void)
{
GOptionContext *context;
gboolean retval;
GError *error = NULL;
gchar **argv;
int argc;
gboolean argb = FALSE;
GOptionEntry entries [] =
{ { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, cb, NULL, NULL },
{ "three", '3', 0, G_OPTION_ARG_NONE, &argb, NULL, NULL },
{ NULL } };

g_test_bug ("577638");

context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, entries, NULL);

/* Now try parsing */
argv = split_string ("program --test=-3", &argc);

test_arg = NULL;
error = NULL;
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert (retval);
g_assert_no_error (error);
g_assert_cmpstr (test_arg, ==, "-3");

g_strfreev (argv);
g_free (test_arg);
test_arg = NULL;

/* Try parsing again */
argv = split_string ("program --test -3", &argc);

error = NULL;
retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert_no_error (error);
g_assert (retval);
g_assert_cmpstr (test_arg, ==, NULL);

g_option_context_free (context);
}

static void
test_basic (void)
{
Expand Down Expand Up @@ -1886,6 +1942,7 @@ main (int argc,
g_test_add_func ("/option/bug/unknown-short", unknown_short_test);
g_test_add_func ("/option/bug/lonely-dash", lonely_dash_test);
g_test_add_func ("/option/bug/missing-arg", missing_arg_test);
g_test_add_func ("/option/bug/dash-arg", dash_arg_test);

return g_test_run();
}

0 comments on commit 008615f

Please sign in to comment.