1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/src/toption.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,200 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.6 +*
1.7 +* This library is free software; you can redistribute it and/or
1.8 +* modify it under the terms of the GNU Lesser General Public
1.9 +* License as published by the Free Software Foundation; either
1.10 +* version 2 of the License, or (at your option) any later version.
1.11 +*
1.12 +* This library is distributed in the hope that it will be useful,
1.13 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 +* Lesser General Public License for more details.
1.16 +*
1.17 +* You should have received a copy of the GNU Lesser General Public
1.18 +* License along with this library; if not, write to the
1.19 +* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.20 +* Boston, MA 02111-1307, USA.
1.21 +*
1.22 +* Description: ?Description
1.23 +*
1.24 +*/
1.25 +
1.26 +
1.27 +#undef G_DISABLE_ASSERT
1.28 +#undef G_LOG_DOMAIN
1.29 +
1.30 +
1.31 +#include <stdio.h>
1.32 +#include <string.h>
1.33 +#include <glib.h>
1.34 +#include <fcntl.h>
1.35 +#include <goption.h>
1.36 +
1.37 +#ifdef SYMBIAN
1.38 +#include "mrt2_glib2_test.h"
1.39 +#endif /*SYMBIAN*/
1.40 +
1.41 +#define C2P(c) ((gpointer) ((long) (c)))
1.42 +#define GINT_TO_POINTER(i) ((gpointer) (i))
1.43 +#define GPOINTER_TO_INT(p) ((gint) (p))
1.44 +#define TESTPASS 1
1.45 +#define TESTFAIL 0
1.46 +
1.47 +int arg_test1_int;
1.48 +int arg_test2_int;
1.49 +
1.50 +static gboolean error_test1_pre_parse (GOptionContext *context,
1.51 + GOptionGroup *group,
1.52 + gpointer data,
1.53 + GError **error)
1.54 +{
1.55 + g_assert (arg_test1_int == 0x12345678);
1.56 +
1.57 + return TRUE;
1.58 +}
1.59 +
1.60 +static gboolean error_test1_post_parse (GOptionContext *context,
1.61 + GOptionGroup *group,
1.62 + gpointer data,
1.63 + GError **error)
1.64 +{
1.65 + g_assert (arg_test1_int == 20);
1.66 +
1.67 + /* Set an error in the post hook */
1.68 + g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
1.69 +
1.70 + return FALSE;
1.71 +}
1.72 +
1.73 +gchar** split_string (const char *str, int *argc)
1.74 +{
1.75 + gchar **argv;
1.76 + int len;
1.77 +
1.78 + argv = g_strsplit (str, " ", 0);
1.79 +
1.80 + for (len = 0; argv[len] != NULL; len++);
1.81 +
1.82 + if (argc)
1.83 + *argc = len;
1.84 +
1.85 + return argv;
1.86 +}
1.87 +
1.88 +
1.89 +void error_func (GOptionContext *context,GOptionGroup *group, gpointer data,GError **error)
1.90 +{
1.91 +
1.92 +}
1.93 +
1.94 +void tg_option_tests()
1.95 +{
1.96 +
1.97 + GOptionContext *context;
1.98 + GOptionContext *context1;//for testing set main group
1.99 + GOptionContext *context2;//for testing add group
1.100 + gboolean retval;
1.101 + GError *error = NULL;
1.102 + gchar **argv;
1.103 + int argc;
1.104 + gchar **argv1;
1.105 + int argc1;
1.106 + GOptionEntry entries [] =
1.107 + { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "test_description", "test_arg" },
1.108 + { NULL } };
1.109 + GOptionEntry entries1 [] =
1.110 + { { "try", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "try_description", "try_arg" },
1.111 + { NULL } };
1.112 + GOptionEntry entries2 [] =
1.113 + { { "again", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "again_description", "again_arg" },
1.114 + { NULL } };
1.115 +
1.116 + GOptionGroup *main_group;
1.117 +
1.118 + context = g_option_context_new (NULL);
1.119 + context1 = g_option_context_new (NULL);
1.120 + context2 = g_option_context_new (NULL);
1.121 + g_option_context_add_main_entries (context, entries, NULL);
1.122 + g_option_context_add_main_entries (context2, entries, NULL);
1.123 +
1.124 + main_group = g_option_context_get_main_group (context);
1.125 + //Testing g_option_context_set_main_group with another context-context1
1.126 + g_option_group_add_entries (main_group,entries1);
1.127 + g_option_context_set_main_group(context1,main_group);
1.128 +
1.129 + //Testing g_option_context_set_help_enabled
1.130 + //and g_option_context_get_help_enabled
1.131 + //and g_option_context_get_ignore_unknown_options
1.132 + g_option_context_set_help_enabled(context,TRUE);
1.133 + g_assert(g_option_context_get_help_enabled(context));
1.134 + g_assert(!g_option_context_get_ignore_unknown_options(context));
1.135 +
1.136 + /* Now try parsing on context1*/
1.137 + argv = split_string ("program --try 20 --try 30", &argc);
1.138 +
1.139 + retval = g_option_context_parse (context1, &argc, &argv, &error);
1.140 + g_assert (retval);
1.141 +
1.142 + /* Last arg specified is the one that should be stored */
1.143 + g_assert (arg_test1_int == 30);
1.144 +
1.145 + g_option_group_set_error_hook (main_group, error_func);
1.146 + argv = split_string ("program --none 20 --none 30", &argc);
1.147 + retval = g_option_context_parse (context1, &argc, &argv, &error);
1.148 +
1.149 + g_strfreev (argv);
1.150 + g_option_context_free (context);
1.151 +
1.152 +}
1.153 +
1.154 +void g_option_context_add_group_test()
1.155 +{
1.156 +
1.157 + GOptionContext *context;
1.158 + GOptionEntry entries [] =
1.159 + { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test2_int, "test_description", "test_arg" },
1.160 + { NULL } };
1.161 + GOptionGroup *group;
1.162 + gboolean retval;
1.163 + GError *error = NULL;
1.164 + gchar **argv;
1.165 + int argc;
1.166 +
1.167 + group = g_option_group_new ("test","test_description","help_desc",NULL,NULL);
1.168 + g_option_group_add_entries (group, entries);
1.169 +
1.170 + context = g_option_context_new (NULL);
1.171 +
1.172 + //Testing this:g_option_context_add_group
1.173 + g_option_context_add_group (context, group);
1.174 +
1.175 + /* Now try parsing on context1*/
1.176 + argv = split_string ("program --test 20 --test 30", &argc);
1.177 +
1.178 + retval = g_option_context_parse (context, &argc, &argv, &error);
1.179 + g_assert (retval);
1.180 +
1.181 + /* Last arg specified is the one that should be stored */
1.182 + g_assert (arg_test2_int == 30);
1.183 + g_strfreev (argv);
1.184 + g_option_context_free (context);
1.185 +}
1.186 +
1.187 +
1.188 +int main (int argc,char *argv[])
1.189 +{
1.190 +
1.191 + #ifdef SYMBIAN
1.192 +
1.193 + 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);
1.194 + #endif /*SYMBIAN*/
1.195 +
1.196 + tg_option_tests();
1.197 + g_option_context_add_group_test();
1.198 +
1.199 +#ifdef SYMBIAN
1.200 + testResultXml("toption");
1.201 +#endif /* EMULATOR */
1.202 + return 0;
1.203 +}
1.204 \ No newline at end of file