apicompatanamdw/bcdrivers/os/ossrv/glib/src/toption.c
changeset 2 0cb2248d0edc
equal deleted inserted replaced
1:61e9400fe245 2:0cb2248d0edc
       
     1 /*
       
     2 * Copyright (c) 2006 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 #undef G_DISABLE_ASSERT
       
    20 #undef G_LOG_DOMAIN
       
    21 
       
    22 
       
    23 #include <stdio.h>
       
    24 #include <string.h>
       
    25 #include <glib.h>
       
    26 #include <fcntl.h>
       
    27 #include <goption.h>
       
    28 
       
    29 #ifdef SYMBIAN
       
    30 #include "mrt2_glib2_test.h"
       
    31 #endif /*SYMBIAN*/
       
    32 
       
    33 #define	C2P(c)		((gpointer) ((long) (c)))
       
    34 #define GINT_TO_POINTER(i)	((gpointer)  (i))
       
    35 #define GPOINTER_TO_INT(p)	((gint)   (p))
       
    36 #define TESTPASS	1
       
    37 #define TESTFAIL	0
       
    38 
       
    39 int arg_test1_int;
       
    40 int arg_test2_int;
       
    41 
       
    42 static gboolean error_test1_pre_parse (GOptionContext *context,
       
    43 				GOptionGroup   *group,
       
    44 		       gpointer	       data,
       
    45 		       GError        **error)
       
    46 {
       
    47   g_assert (arg_test1_int == 0x12345678);
       
    48 
       
    49   return TRUE;
       
    50 }
       
    51 
       
    52 static gboolean error_test1_post_parse (GOptionContext *context,
       
    53 			GOptionGroup   *group,
       
    54 			gpointer	  data,
       
    55 			GError        **error)
       
    56 {
       
    57   g_assert (arg_test1_int == 20);
       
    58 
       
    59   /* Set an error in the post hook */
       
    60   g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
       
    61 
       
    62   return FALSE;
       
    63 }
       
    64 
       
    65 gchar** split_string (const char *str, int *argc)
       
    66 {
       
    67   gchar **argv;
       
    68   int len;
       
    69   
       
    70   argv = g_strsplit (str, " ", 0);
       
    71 
       
    72   for (len = 0; argv[len] != NULL; len++);
       
    73 
       
    74   if (argc)
       
    75     *argc = len;
       
    76     
       
    77   return argv;
       
    78 }
       
    79 
       
    80 
       
    81 void error_func (GOptionContext *context,GOptionGroup *group, gpointer data,GError **error)
       
    82 {
       
    83 	
       
    84 }
       
    85 
       
    86 void tg_option_tests()
       
    87 {
       
    88 
       
    89 	GOptionContext *context;
       
    90 	GOptionContext *context1;//for testing set main group
       
    91 	GOptionContext *context2;//for testing add group
       
    92 	gboolean retval;
       
    93 	GError *error = NULL;
       
    94 	gchar **argv;
       
    95 	int argc;
       
    96 	gchar **argv1;
       
    97 	int argc1;
       
    98 	GOptionEntry entries [] =
       
    99 	    { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "test_description", "test_arg" },
       
   100 	      { NULL } };
       
   101 	GOptionEntry entries1 [] =
       
   102 	    { { "try", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "try_description", "try_arg" },
       
   103 	      { NULL } };
       
   104 	GOptionEntry entries2 [] =
       
   105 	    { { "again", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "again_description", "again_arg" },
       
   106 	      { NULL } };
       
   107 	
       
   108 	GOptionGroup *main_group;
       
   109 		
       
   110 	context = g_option_context_new (NULL);
       
   111 	context1 = g_option_context_new (NULL);
       
   112 	context2 = g_option_context_new (NULL);
       
   113 	g_option_context_add_main_entries (context, entries, NULL);
       
   114 	g_option_context_add_main_entries (context2, entries, NULL);
       
   115   	
       
   116   	main_group = g_option_context_get_main_group (context);
       
   117   	//Testing g_option_context_set_main_group with another context-context1
       
   118 	g_option_group_add_entries (main_group,entries1);
       
   119 	g_option_context_set_main_group(context1,main_group);
       
   120 
       
   121 	//Testing g_option_context_set_help_enabled 
       
   122 	//and g_option_context_get_help_enabled
       
   123 	//and g_option_context_get_ignore_unknown_options
       
   124 	g_option_context_set_help_enabled(context,TRUE);
       
   125 	g_assert(g_option_context_get_help_enabled(context));
       
   126 	g_assert(!g_option_context_get_ignore_unknown_options(context));
       
   127 	
       
   128 	/* Now try parsing on context1*/
       
   129 	argv = split_string ("program --try 20 --try 30", &argc);
       
   130 	  
       
   131 	retval = g_option_context_parse (context1, &argc, &argv, &error);
       
   132 	g_assert (retval);
       
   133 
       
   134 	/* Last arg specified is the one that should be stored */
       
   135 	g_assert (arg_test1_int == 30);
       
   136 
       
   137 	g_option_group_set_error_hook (main_group, error_func);
       
   138 	argv = split_string ("program --none 20 --none 30", &argc);
       
   139 	retval = g_option_context_parse (context1, &argc, &argv, &error);
       
   140 		
       
   141 	g_strfreev (argv);
       
   142 	g_option_context_free (context);
       
   143 	  
       
   144 }
       
   145 
       
   146 void g_option_context_add_group_test()
       
   147 {
       
   148 
       
   149 	GOptionContext *context;
       
   150 	GOptionEntry entries [] =
       
   151 		{ { "test", 0, 0, G_OPTION_ARG_INT, &arg_test2_int, "test_description", "test_arg" },
       
   152 	    { NULL } };
       
   153 	GOptionGroup *group;
       
   154 	gboolean retval;
       
   155 	GError *error = NULL;
       
   156 	gchar **argv;
       
   157 	int argc;
       
   158 	
       
   159 	group = g_option_group_new ("test","test_description","help_desc",NULL,NULL);
       
   160 	g_option_group_add_entries (group, entries);
       
   161 	
       
   162 	context = g_option_context_new (NULL);
       
   163 	
       
   164 	//Testing this:g_option_context_add_group
       
   165 	g_option_context_add_group (context, group);
       
   166 	
       
   167 	/* Now try parsing on context1*/
       
   168 	argv = split_string ("program --test 20 --test 30", &argc);
       
   169 	  
       
   170 	retval = g_option_context_parse (context, &argc, &argv, &error);
       
   171 	g_assert (retval);
       
   172 
       
   173 	/* Last arg specified is the one that should be stored */
       
   174 	g_assert (arg_test2_int == 30);
       
   175 	g_strfreev (argv);
       
   176 	g_option_context_free (context);
       
   177 }
       
   178 
       
   179 
       
   180 int main (int argc,char *argv[])
       
   181 {
       
   182 
       
   183 	#ifdef SYMBIAN
       
   184  
       
   185  	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);
       
   186  	#endif /*SYMBIAN*/
       
   187  	
       
   188  	tg_option_tests();
       
   189  	g_option_context_add_group_test();
       
   190  
       
   191 #ifdef SYMBIAN
       
   192   testResultXml("toption");
       
   193 #endif /* EMULATOR */
       
   194  	return 0;
       
   195 }