os/ossrv/glib/tsrc/BC/src/toption.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     3
*
sl@0
     4
* This library is free software; you can redistribute it and/or
sl@0
     5
* modify it under the terms of the GNU Lesser General Public
sl@0
     6
* License as published by the Free Software Foundation; either
sl@0
     7
* version 2 of the License, or (at your option) any later version.
sl@0
     8
*
sl@0
     9
* This library is distributed in the hope that it will be useful,
sl@0
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
* Lesser General Public License for more details.
sl@0
    13
*
sl@0
    14
* You should have received a copy of the GNU Lesser General Public
sl@0
    15
* License along with this library; if not, write to the
sl@0
    16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    17
* Boston, MA 02111-1307, USA.
sl@0
    18
*
sl@0
    19
* Description:  ?Description
sl@0
    20
*
sl@0
    21
*/
sl@0
    22
sl@0
    23
sl@0
    24
#undef G_DISABLE_ASSERT
sl@0
    25
#undef G_LOG_DOMAIN
sl@0
    26
sl@0
    27
sl@0
    28
#include <stdio.h>
sl@0
    29
#include <string.h>
sl@0
    30
#include <glib.h>
sl@0
    31
#include <fcntl.h>
sl@0
    32
#include <goption.h>
sl@0
    33
sl@0
    34
#ifdef SYMBIAN
sl@0
    35
#include "mrt2_glib2_test.h"
sl@0
    36
#endif /*SYMBIAN*/
sl@0
    37
sl@0
    38
#define	C2P(c)		((gpointer) ((long) (c)))
sl@0
    39
#define GINT_TO_POINTER(i)	((gpointer)  (i))
sl@0
    40
#define GPOINTER_TO_INT(p)	((gint)   (p))
sl@0
    41
#define TESTPASS	1
sl@0
    42
#define TESTFAIL	0
sl@0
    43
sl@0
    44
int arg_test1_int;
sl@0
    45
int arg_test2_int;
sl@0
    46
sl@0
    47
static gboolean error_test1_pre_parse (GOptionContext *context,
sl@0
    48
				GOptionGroup   *group,
sl@0
    49
		       gpointer	       data,
sl@0
    50
		       GError        **error)
sl@0
    51
{
sl@0
    52
  g_assert (arg_test1_int == 0x12345678);
sl@0
    53
sl@0
    54
  return TRUE;
sl@0
    55
}
sl@0
    56
sl@0
    57
static gboolean error_test1_post_parse (GOptionContext *context,
sl@0
    58
			GOptionGroup   *group,
sl@0
    59
			gpointer	  data,
sl@0
    60
			GError        **error)
sl@0
    61
{
sl@0
    62
  g_assert (arg_test1_int == 20);
sl@0
    63
sl@0
    64
  /* Set an error in the post hook */
sl@0
    65
  g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
sl@0
    66
sl@0
    67
  return FALSE;
sl@0
    68
}
sl@0
    69
sl@0
    70
gchar** split_string (const char *str, int *argc)
sl@0
    71
{
sl@0
    72
  gchar **argv;
sl@0
    73
  int len;
sl@0
    74
  
sl@0
    75
  argv = g_strsplit (str, " ", 0);
sl@0
    76
sl@0
    77
  for (len = 0; argv[len] != NULL; len++);
sl@0
    78
sl@0
    79
  if (argc)
sl@0
    80
    *argc = len;
sl@0
    81
    
sl@0
    82
  return argv;
sl@0
    83
}
sl@0
    84
sl@0
    85
sl@0
    86
void error_func (GOptionContext *context,GOptionGroup *group, gpointer data,GError **error)
sl@0
    87
{
sl@0
    88
	
sl@0
    89
}
sl@0
    90
sl@0
    91
void tg_option_tests()
sl@0
    92
{
sl@0
    93
sl@0
    94
	GOptionContext *context;
sl@0
    95
	GOptionContext *context1;//for testing set main group
sl@0
    96
	GOptionContext *context2;//for testing add group
sl@0
    97
	gboolean retval;
sl@0
    98
	GError *error = NULL;
sl@0
    99
	gchar **argv;
sl@0
   100
	int argc;
sl@0
   101
	gchar **argv1;
sl@0
   102
	int argc1;
sl@0
   103
	GOptionEntry entries [] =
sl@0
   104
	    { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "test_description", "test_arg" },
sl@0
   105
	      { NULL } };
sl@0
   106
	GOptionEntry entries1 [] =
sl@0
   107
	    { { "try", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "try_description", "try_arg" },
sl@0
   108
	      { NULL } };
sl@0
   109
	GOptionEntry entries2 [] =
sl@0
   110
	    { { "again", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "again_description", "again_arg" },
sl@0
   111
	      { NULL } };
sl@0
   112
	
sl@0
   113
	GOptionGroup *main_group;
sl@0
   114
		
sl@0
   115
	context = g_option_context_new (NULL);
sl@0
   116
	context1 = g_option_context_new (NULL);
sl@0
   117
	context2 = g_option_context_new (NULL);
sl@0
   118
	g_option_context_add_main_entries (context, entries, NULL);
sl@0
   119
	g_option_context_add_main_entries (context2, entries, NULL);
sl@0
   120
  	
sl@0
   121
  	main_group = g_option_context_get_main_group (context);
sl@0
   122
  	//Testing g_option_context_set_main_group with another context-context1
sl@0
   123
	g_option_group_add_entries (main_group,entries1);
sl@0
   124
	g_option_context_set_main_group(context1,main_group);
sl@0
   125
sl@0
   126
	//Testing g_option_context_set_help_enabled 
sl@0
   127
	//and g_option_context_get_help_enabled
sl@0
   128
	//and g_option_context_get_ignore_unknown_options
sl@0
   129
	g_option_context_set_help_enabled(context,TRUE);
sl@0
   130
	g_assert(g_option_context_get_help_enabled(context));
sl@0
   131
	g_assert(!g_option_context_get_ignore_unknown_options(context));
sl@0
   132
	
sl@0
   133
	/* Now try parsing on context1*/
sl@0
   134
	argv = split_string ("program --try 20 --try 30", &argc);
sl@0
   135
	  
sl@0
   136
	retval = g_option_context_parse (context1, &argc, &argv, &error);
sl@0
   137
	g_assert (retval);
sl@0
   138
sl@0
   139
	/* Last arg specified is the one that should be stored */
sl@0
   140
	g_assert (arg_test1_int == 30);
sl@0
   141
sl@0
   142
	g_option_group_set_error_hook (main_group, error_func);
sl@0
   143
	argv = split_string ("program --none 20 --none 30", &argc);
sl@0
   144
	retval = g_option_context_parse (context1, &argc, &argv, &error);
sl@0
   145
		
sl@0
   146
	g_strfreev (argv);
sl@0
   147
	g_option_context_free (context);
sl@0
   148
	  
sl@0
   149
}
sl@0
   150
sl@0
   151
void g_option_context_add_group_test()
sl@0
   152
{
sl@0
   153
sl@0
   154
	GOptionContext *context;
sl@0
   155
	GOptionEntry entries [] =
sl@0
   156
		{ { "test", 0, 0, G_OPTION_ARG_INT, &arg_test2_int, "test_description", "test_arg" },
sl@0
   157
	    { NULL } };
sl@0
   158
	GOptionGroup *group;
sl@0
   159
	gboolean retval;
sl@0
   160
	GError *error = NULL;
sl@0
   161
	gchar **argv;
sl@0
   162
	int argc;
sl@0
   163
	
sl@0
   164
	group = g_option_group_new ("test","test_description","help_desc",NULL,NULL);
sl@0
   165
	g_option_group_add_entries (group, entries);
sl@0
   166
	
sl@0
   167
	context = g_option_context_new (NULL);
sl@0
   168
	
sl@0
   169
	//Testing this:g_option_context_add_group
sl@0
   170
	g_option_context_add_group (context, group);
sl@0
   171
	
sl@0
   172
	/* Now try parsing on context1*/
sl@0
   173
	argv = split_string ("program --test 20 --test 30", &argc);
sl@0
   174
	  
sl@0
   175
	retval = g_option_context_parse (context, &argc, &argv, &error);
sl@0
   176
	g_assert (retval);
sl@0
   177
sl@0
   178
	/* Last arg specified is the one that should be stored */
sl@0
   179
	g_assert (arg_test2_int == 30);
sl@0
   180
	g_strfreev (argv);
sl@0
   181
	g_option_context_free (context);
sl@0
   182
}
sl@0
   183
sl@0
   184
sl@0
   185
int main (int argc,char *argv[])
sl@0
   186
{
sl@0
   187
sl@0
   188
	#ifdef SYMBIAN
sl@0
   189
 
sl@0
   190
 	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);
sl@0
   191
 	#endif /*SYMBIAN*/
sl@0
   192
 	
sl@0
   193
 	tg_option_tests();
sl@0
   194
 	g_option_context_add_group_test();
sl@0
   195
 
sl@0
   196
#ifdef SYMBIAN
sl@0
   197
  testResultXml("toption");
sl@0
   198
#endif /* EMULATOR */
sl@0
   199
 	return 0;
sl@0
   200
}