glib/tsrc/glib_nonstif/src/toption.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #undef G_DISABLE_ASSERT
       
    21 #undef G_LOG_DOMAIN
       
    22 
       
    23 
       
    24 #include <stdio.h>
       
    25 #include <string.h>
       
    26 #include <glib.h>
       
    27 #include <fcntl.h>
       
    28 #include <goption.h>
       
    29 
       
    30 #ifdef __SYMBIAN32__
       
    31 #include "mrt2_glib2_test.h"
       
    32 #endif /*__SYMBIAN32__*/
       
    33 
       
    34 #define	C2P(c)		((gpointer) ((long) (c)))
       
    35 #define GINT_TO_POINTER(i)	((gpointer)  (i))
       
    36 #define GPOINTER_TO_INT(p)	((gint)   (p))
       
    37 #define TESTPASS	1
       
    38 #define TESTFAIL	0
       
    39 
       
    40 int arg_test1_int;
       
    41 int arg_test2_int;
       
    42 
       
    43 static gboolean error_test1_pre_parse (GOptionContext *context,
       
    44 				GOptionGroup   *group,
       
    45 		       gpointer	       data,
       
    46 		       GError        **error)
       
    47 {
       
    48   g_assert (arg_test1_int == 0x12345678);
       
    49 
       
    50   return TRUE;
       
    51 }
       
    52 
       
    53 static gboolean error_test1_post_parse (GOptionContext *context,
       
    54 			GOptionGroup   *group,
       
    55 			gpointer	  data,
       
    56 			GError        **error)
       
    57 {
       
    58   g_assert (arg_test1_int == 20);
       
    59 
       
    60   /* Set an error in the post hook */
       
    61   g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
       
    62 
       
    63   return FALSE;
       
    64 }
       
    65 
       
    66 gchar** split_string (const char *str, int *argc)
       
    67 {
       
    68   gchar **argv;
       
    69   int len;
       
    70   
       
    71   argv = g_strsplit (str, " ", 0);
       
    72 
       
    73   for (len = 0; argv[len] != NULL; len++);
       
    74 
       
    75   if (argc)
       
    76     *argc = len;
       
    77     
       
    78   return argv;
       
    79 }
       
    80 
       
    81 
       
    82 void error_func (GOptionContext *context,GOptionGroup *group, gpointer data,GError **error)
       
    83 {
       
    84 	
       
    85 }
       
    86 
       
    87 void tg_option_tests()
       
    88 {
       
    89 
       
    90 	GOptionContext *context;
       
    91 	GOptionContext *context1;//for testing set main group
       
    92 	GOptionContext *context2;//for testing add group
       
    93 	gboolean retval;
       
    94 	GError *error = NULL;
       
    95 	gchar **argv;
       
    96 	int argc;
       
    97 	gchar **argv1;
       
    98 	int argc1;
       
    99 	GOptionEntry entries [] =
       
   100 	    { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "test_description", "test_arg" },
       
   101 	      { NULL } };
       
   102 	GOptionEntry entries1 [] =
       
   103 	    { { "try", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "try_description", "try_arg" },
       
   104 	      { NULL } };
       
   105 	GOptionEntry entries2 [] =
       
   106 	    { { "again", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "again_description", "again_arg" },
       
   107 	      { NULL } };
       
   108 	
       
   109 	GOptionGroup *main_group;
       
   110 		
       
   111 	context = g_option_context_new (NULL);
       
   112 	context1 = g_option_context_new (NULL);
       
   113 	context2 = g_option_context_new (NULL);
       
   114 	g_option_context_add_main_entries (context, entries, NULL);
       
   115 	g_option_context_add_main_entries (context2, entries, NULL);
       
   116   	
       
   117   	main_group = g_option_context_get_main_group (context);
       
   118   	//Testing g_option_context_set_main_group with another context-context1
       
   119 	g_option_group_add_entries (main_group,entries1);
       
   120 	g_option_context_set_main_group(context1,main_group);
       
   121 
       
   122 	//Testing g_option_context_set_help_enabled 
       
   123 	//and g_option_context_get_help_enabled
       
   124 	//and g_option_context_get_ignore_unknown_options
       
   125 	g_option_context_set_help_enabled(context,TRUE);
       
   126 	g_assert(g_option_context_get_help_enabled(context));
       
   127 	g_assert(!g_option_context_get_ignore_unknown_options(context));
       
   128 	
       
   129 	/* Now try parsing on context1*/
       
   130 	argv = split_string ("program --try 20 --try 30", &argc);
       
   131 	  
       
   132 	retval = g_option_context_parse (context1, &argc, &argv, &error);
       
   133 	g_assert (retval);
       
   134 
       
   135 	/* Last arg specified is the one that should be stored */
       
   136 	g_assert (arg_test1_int == 30);
       
   137 
       
   138 	g_option_group_set_error_hook (main_group, error_func);
       
   139 	argv = split_string ("program --none 20 --none 30", &argc);
       
   140 	retval = g_option_context_parse (context1, &argc, &argv, &error);
       
   141 		
       
   142 	g_strfreev (argv);
       
   143 	g_option_context_free (context);
       
   144 	  
       
   145 }
       
   146 
       
   147 void g_option_context_add_group_test()
       
   148 {
       
   149 
       
   150 	GOptionContext *context;
       
   151 	GOptionEntry entries [] =
       
   152 		{ { "test", 0, 0, G_OPTION_ARG_INT, &arg_test2_int, "test_description", "test_arg" },
       
   153 	    { NULL } };
       
   154 	GOptionGroup *group;
       
   155 	gboolean retval;
       
   156 	GError *error = NULL;
       
   157 	gchar **argv;
       
   158 	int argc;
       
   159 	
       
   160 	group = g_option_group_new ("test","test_description","help_desc",NULL,NULL);
       
   161 	g_option_group_add_entries (group, entries);
       
   162 	
       
   163 	context = g_option_context_new (NULL);
       
   164 	
       
   165 	//Testing this:g_option_context_add_group
       
   166 	g_option_context_add_group (context, group);
       
   167 	
       
   168 	/* Now try parsing on context1*/
       
   169 	argv = split_string ("program --test 20 --test 30", &argc);
       
   170 	  
       
   171 	retval = g_option_context_parse (context, &argc, &argv, &error);
       
   172 	g_assert (retval);
       
   173 
       
   174 	/* Last arg specified is the one that should be stored */
       
   175 	g_assert (arg_test2_int == 30);
       
   176 	g_strfreev (argv);
       
   177 	g_option_context_free (context);
       
   178 }
       
   179 
       
   180 
       
   181 int main (int argc,char *argv[])
       
   182 {
       
   183 
       
   184 	#ifdef __SYMBIAN32__
       
   185  
       
   186  	g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO |  G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
   187  	#endif /*__SYMBIAN32__*/
       
   188  	
       
   189  	tg_option_tests();
       
   190  	g_option_context_add_group_test();
       
   191  
       
   192 #ifdef __SYMBIAN32__
       
   193   testResultXml("toption");
       
   194 #endif /* EMULATOR */
       
   195  	return 0;
       
   196 }