sl@0: /* sl@0: * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: * sl@0: * Description: ?Description sl@0: * sl@0: */ sl@0: sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: #define C2P(c) ((gpointer) ((long) (c))) sl@0: #define GINT_TO_POINTER(i) ((gpointer) (i)) sl@0: #define GPOINTER_TO_INT(p) ((gint) (p)) sl@0: #define TESTPASS 1 sl@0: #define TESTFAIL 0 sl@0: sl@0: int arg_test1_int; sl@0: int arg_test2_int; sl@0: sl@0: static gboolean error_test1_pre_parse (GOptionContext *context, sl@0: GOptionGroup *group, sl@0: gpointer data, sl@0: GError **error) sl@0: { sl@0: g_assert (arg_test1_int == 0x12345678); sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: static gboolean error_test1_post_parse (GOptionContext *context, sl@0: GOptionGroup *group, sl@0: gpointer data, sl@0: GError **error) sl@0: { sl@0: g_assert (arg_test1_int == 20); sl@0: sl@0: /* Set an error in the post hook */ sl@0: g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, ""); sl@0: sl@0: return FALSE; sl@0: } sl@0: sl@0: gchar** split_string (const char *str, int *argc) sl@0: { sl@0: gchar **argv; sl@0: int len; sl@0: sl@0: argv = g_strsplit (str, " ", 0); sl@0: sl@0: for (len = 0; argv[len] != NULL; len++); sl@0: sl@0: if (argc) sl@0: *argc = len; sl@0: sl@0: return argv; sl@0: } sl@0: sl@0: sl@0: void error_func (GOptionContext *context,GOptionGroup *group, gpointer data,GError **error) sl@0: { sl@0: sl@0: } sl@0: sl@0: void tg_option_tests() sl@0: { sl@0: sl@0: GOptionContext *context; sl@0: GOptionContext *context1;//for testing set main group sl@0: GOptionContext *context2;//for testing add group sl@0: gboolean retval; sl@0: GError *error = NULL; sl@0: gchar **argv; sl@0: int argc; sl@0: gchar **argv1; sl@0: int argc1; sl@0: GOptionEntry entries [] = sl@0: { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "test_description", "test_arg" }, sl@0: { NULL } }; sl@0: GOptionEntry entries1 [] = sl@0: { { "try", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "try_description", "try_arg" }, sl@0: { NULL } }; sl@0: GOptionEntry entries2 [] = sl@0: { { "again", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "again_description", "again_arg" }, sl@0: { NULL } }; sl@0: sl@0: GOptionGroup *main_group; sl@0: sl@0: context = g_option_context_new (NULL); sl@0: context1 = g_option_context_new (NULL); sl@0: context2 = g_option_context_new (NULL); sl@0: g_option_context_add_main_entries (context, entries, NULL); sl@0: g_option_context_add_main_entries (context2, entries, NULL); sl@0: sl@0: main_group = g_option_context_get_main_group (context); sl@0: //Testing g_option_context_set_main_group with another context-context1 sl@0: g_option_group_add_entries (main_group,entries1); sl@0: g_option_context_set_main_group(context1,main_group); sl@0: sl@0: //Testing g_option_context_set_help_enabled sl@0: //and g_option_context_get_help_enabled sl@0: //and g_option_context_get_ignore_unknown_options sl@0: g_option_context_set_help_enabled(context,TRUE); sl@0: g_assert(g_option_context_get_help_enabled(context)); sl@0: g_assert(!g_option_context_get_ignore_unknown_options(context)); sl@0: sl@0: /* Now try parsing on context1*/ sl@0: argv = split_string ("program --try 20 --try 30", &argc); sl@0: sl@0: retval = g_option_context_parse (context1, &argc, &argv, &error); sl@0: g_assert (retval); sl@0: sl@0: /* Last arg specified is the one that should be stored */ sl@0: g_assert (arg_test1_int == 30); sl@0: sl@0: g_option_group_set_error_hook (main_group, error_func); sl@0: argv = split_string ("program --none 20 --none 30", &argc); sl@0: retval = g_option_context_parse (context1, &argc, &argv, &error); sl@0: sl@0: g_strfreev (argv); sl@0: g_option_context_free (context); sl@0: sl@0: } sl@0: sl@0: void g_option_context_add_group_test() sl@0: { sl@0: sl@0: GOptionContext *context; sl@0: GOptionEntry entries [] = sl@0: { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test2_int, "test_description", "test_arg" }, sl@0: { NULL } }; sl@0: GOptionGroup *group; sl@0: gboolean retval; sl@0: GError *error = NULL; sl@0: gchar **argv; sl@0: int argc; sl@0: sl@0: group = g_option_group_new ("test","test_description","help_desc",NULL,NULL); sl@0: g_option_group_add_entries (group, entries); sl@0: sl@0: context = g_option_context_new (NULL); sl@0: sl@0: //Testing this:g_option_context_add_group sl@0: g_option_context_add_group (context, group); sl@0: sl@0: /* Now try parsing on context1*/ sl@0: argv = split_string ("program --test 20 --test 30", &argc); sl@0: sl@0: retval = g_option_context_parse (context, &argc, &argv, &error); sl@0: g_assert (retval); sl@0: sl@0: /* Last arg specified is the one that should be stored */ sl@0: g_assert (arg_test2_int == 30); sl@0: g_strfreev (argv); sl@0: g_option_context_free (context); sl@0: } sl@0: sl@0: sl@0: int main (int argc,char *argv[]) sl@0: { sl@0: sl@0: #ifdef SYMBIAN sl@0: sl@0: 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: #endif /*SYMBIAN*/ sl@0: sl@0: tg_option_tests(); sl@0: g_option_context_add_group_test(); sl@0: sl@0: #ifdef SYMBIAN sl@0: testResultXml("toption"); sl@0: #endif /* EMULATOR */ sl@0: return 0; sl@0: }