glib r7685 - in branches/glib-2-18: . glib glib/tests



Author: matthiasc
Date: Fri Nov 28 05:19:15 2008
New Revision: 7685
URL: http://svn.gnome.org/viewvc/glib?rev=7685&view=rev

Log:
2008-11-28  Matthias Clasen  <mclasen redhat com>

        Bug 562378 â callback return value not respected for callback option
        with no arg

        * glib/goption.c (parse_long_option): Return the parse_arg return
        value even for no-arg callbacks. Patch by Christian Persch

        * glib/tests/option-context.c: Add a test for a callback which
        returns FALSE.




Modified:
   branches/glib-2-18/ChangeLog
   branches/glib-2-18/glib/goption.c
   branches/glib-2-18/glib/tests/option-context.c

Modified: branches/glib-2-18/glib/goption.c
==============================================================================
--- branches/glib-2-18/glib/goption.c	(original)
+++ branches/glib-2-18/glib/goption.c	Fri Nov 28 05:19:15 2008
@@ -1303,14 +1303,17 @@
 	  strcmp (arg, group->entries[j].long_name) == 0)
 	{
 	  gchar *option_name;
+	  gboolean retval;
 
 	  option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
-	  parse_arg (context, group, &group->entries[j],
-		     NULL, option_name, error);
+	  retval = parse_arg (context, group, &group->entries[j],
+			      NULL, option_name, error);
 	  g_free(option_name);
 	  
 	  add_pending_null (context, &((*argv)[*index]), NULL);
 	  *parsed = TRUE;
+
+	  return retval;
 	}
       else
 	{

Modified: branches/glib-2-18/glib/tests/option-context.c
==============================================================================
--- branches/glib-2-18/glib/tests/option-context.c	(original)
+++ branches/glib-2-18/glib/tests/option-context.c	Fri Nov 28 05:19:15 2008
@@ -1025,6 +1025,82 @@
   g_option_context_free (context);
 }
 
+static gboolean
+callback_error (const gchar *option_name, const gchar *value,
+                gpointer data, GError **error)
+{
+  g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "42");
+  return FALSE;
+}
+
+static void
+callback_returns_false (void)
+{
+  GOptionContext *context;
+  gboolean retval;
+  GError *error = NULL;
+  gchar **argv;
+  int argc;
+  GOptionEntry entries [] =
+    { { "error", 0, 0, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
+      { "error-no-arg", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
+      { "error-optional-arg", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, callback_error, 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 --error value", &argc);
+  
+  retval = g_option_context_parse (context, &argc, &argv, &error);
+  g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
+  g_assert (retval == FALSE);
+
+  g_option_context_free (context);
+  g_clear_error (&error);
+
+  /* And again, this time with a no-arg variant */
+  context = g_option_context_new (NULL);
+  g_option_context_add_main_entries (context, entries, NULL);
+
+  argv = split_string ("program --error-no-arg", &argc);
+  
+  retval = g_option_context_parse (context, &argc, &argv, &error);
+  g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
+  g_assert (retval == FALSE);
+
+  g_option_context_free (context);
+  g_clear_error (&error);
+
+  /* And again, this time with a optional arg variant, with argument */
+  context = g_option_context_new (NULL);
+  g_option_context_add_main_entries (context, entries, NULL);
+
+  argv = split_string ("program --error-optional-arg value", &argc);
+  
+  retval = g_option_context_parse (context, &argc, &argv, &error);
+  g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
+  g_assert (retval == FALSE);
+
+  g_option_context_free (context);
+  g_clear_error (&error);
+
+  /* And again, this time with a optional arg variant, without argument */
+  context = g_option_context_new (NULL);
+  g_option_context_add_main_entries (context, entries, NULL);
+
+  argv = split_string ("program --error-optional-arg", &argc);
+  
+  retval = g_option_context_parse (context, &argc, &argv, &error);
+  g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
+  g_assert (retval == FALSE);
+
+  g_option_context_free (context);
+  g_clear_error (&error);
+}
+
+
 void
 ignore_test1 (void)
 {
@@ -1683,6 +1759,9 @@
   /* Test callback with G_OPTION_REMAINING */
   g_test_add_func ("/arg/remaining/callback", callback_remaining_test1);
   
+  /* Test callbacks which return FALSE */
+  g_test_add_func ("/arg/remaining/callback-false", callback_returns_false);
+  
   /* Test ignoring options */
   g_test_add_func ("/arg/ignore/long", ignore_test1);
   g_test_add_func ("/arg/ignore/short", ignore_test2);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]