Update contrib.
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Description: ?Description
24 #undef G_DISABLE_ASSERT
35 #include "mrt2_glib2_test.h"
38 #define C2P(c) ((gpointer) ((long) (c)))
39 #define GINT_TO_POINTER(i) ((gpointer) (i))
40 #define GPOINTER_TO_INT(p) ((gint) (p))
47 static gboolean error_test1_pre_parse (GOptionContext *context,
52 g_assert (arg_test1_int == 0x12345678);
57 static gboolean error_test1_post_parse (GOptionContext *context,
62 g_assert (arg_test1_int == 20);
64 /* Set an error in the post hook */
65 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "");
70 gchar** split_string (const char *str, int *argc)
75 argv = g_strsplit (str, " ", 0);
77 for (len = 0; argv[len] != NULL; len++);
86 void error_func (GOptionContext *context,GOptionGroup *group, gpointer data,GError **error)
91 void tg_option_tests()
94 GOptionContext *context;
95 GOptionContext *context1;//for testing set main group
96 GOptionContext *context2;//for testing add group
103 GOptionEntry entries [] =
104 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "test_description", "test_arg" },
106 GOptionEntry entries1 [] =
107 { { "try", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "try_description", "try_arg" },
109 GOptionEntry entries2 [] =
110 { { "again", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, "again_description", "again_arg" },
113 GOptionGroup *main_group;
115 context = g_option_context_new (NULL);
116 context1 = g_option_context_new (NULL);
117 context2 = g_option_context_new (NULL);
118 g_option_context_add_main_entries (context, entries, NULL);
119 g_option_context_add_main_entries (context2, entries, NULL);
121 main_group = g_option_context_get_main_group (context);
122 //Testing g_option_context_set_main_group with another context-context1
123 g_option_group_add_entries (main_group,entries1);
124 g_option_context_set_main_group(context1,main_group);
126 //Testing g_option_context_set_help_enabled
127 //and g_option_context_get_help_enabled
128 //and g_option_context_get_ignore_unknown_options
129 g_option_context_set_help_enabled(context,TRUE);
130 g_assert(g_option_context_get_help_enabled(context));
131 g_assert(!g_option_context_get_ignore_unknown_options(context));
133 /* Now try parsing on context1*/
134 argv = split_string ("program --try 20 --try 30", &argc);
136 retval = g_option_context_parse (context1, &argc, &argv, &error);
139 /* Last arg specified is the one that should be stored */
140 g_assert (arg_test1_int == 30);
142 g_option_group_set_error_hook (main_group, error_func);
143 argv = split_string ("program --none 20 --none 30", &argc);
144 retval = g_option_context_parse (context1, &argc, &argv, &error);
147 g_option_context_free (context);
151 void g_option_context_add_group_test()
154 GOptionContext *context;
155 GOptionEntry entries [] =
156 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test2_int, "test_description", "test_arg" },
160 GError *error = NULL;
164 group = g_option_group_new ("test","test_description","help_desc",NULL,NULL);
165 g_option_group_add_entries (group, entries);
167 context = g_option_context_new (NULL);
169 //Testing this:g_option_context_add_group
170 g_option_context_add_group (context, group);
172 /* Now try parsing on context1*/
173 argv = split_string ("program --test 20 --test 30", &argc);
175 retval = g_option_context_parse (context, &argc, &argv, &error);
178 /* Last arg specified is the one that should be stored */
179 g_assert (arg_test2_int == 30);
181 g_option_context_free (context);
185 int main (int argc,char *argv[])
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);
194 g_option_context_add_group_test();
197 testResultXml("toption");
198 #endif /* EMULATOR */