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