|
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */ |
|
2 #include <glib.h> |
|
3 #include <string.h> |
|
4 |
|
5 #ifdef SYMBIAN |
|
6 #include <glib_global.h> |
|
7 #include "mrt2_glib2_test.h" |
|
8 #endif /*SYMBIAN*/ |
|
9 int error_test1_int; |
|
10 char *error_test2_string; |
|
11 gboolean error_test3_boolean; |
|
12 |
|
13 int arg_test1_int; |
|
14 gchar *arg_test2_string; |
|
15 gchar *arg_test3_filename; |
|
16 |
|
17 gchar *callback_test1_string; |
|
18 gboolean callback_test2_int; |
|
19 |
|
20 gchar *callback_test_optional_string; |
|
21 gboolean callback_test_optional_boolean; |
|
22 |
|
23 gchar **array_test1_array; |
|
24 |
|
25 gboolean ignore_test1_boolean; |
|
26 gboolean ignore_test2_boolean; |
|
27 gchar *ignore_test3_string; |
|
28 |
|
29 gchar ** |
|
30 split_string (const char *str, int *argc) |
|
31 { |
|
32 gchar **argv; |
|
33 int len; |
|
34 |
|
35 argv = g_strsplit (str, " ", 0); |
|
36 |
|
37 for (len = 0; argv[len] != NULL; len++); |
|
38 |
|
39 if (argc) |
|
40 *argc = len; |
|
41 |
|
42 return argv; |
|
43 } |
|
44 |
|
45 gchar * |
|
46 join_stringv (int argc, char **argv) |
|
47 { |
|
48 int i; |
|
49 GString *str; |
|
50 |
|
51 str = g_string_new (NULL); |
|
52 |
|
53 for (i = 0; i < argc; i++) |
|
54 { |
|
55 g_string_append (str, argv[i]); |
|
56 |
|
57 if (i < argc - 1) |
|
58 g_string_append_c (str, ' '); |
|
59 } |
|
60 |
|
61 return g_string_free (str, FALSE); |
|
62 } |
|
63 |
|
64 /* Performs a shallow copy */ |
|
65 char ** |
|
66 copy_stringv (char **argv, int argc) |
|
67 { |
|
68 return g_memdup (argv, sizeof (char *) * (argc + 1)); |
|
69 } |
|
70 |
|
71 |
|
72 static gboolean |
|
73 error_test1_pre_parse (GOptionContext *context, |
|
74 GOptionGroup *group, |
|
75 gpointer data, |
|
76 GError **error) |
|
77 { |
|
78 g_assert (error_test1_int == 0x12345678); |
|
79 |
|
80 return TRUE; |
|
81 } |
|
82 |
|
83 static gboolean |
|
84 error_test1_post_parse (GOptionContext *context, |
|
85 GOptionGroup *group, |
|
86 gpointer data, |
|
87 GError **error) |
|
88 { |
|
89 g_assert (error_test1_int == 20); |
|
90 |
|
91 /* Set an error in the post hook */ |
|
92 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, ""); |
|
93 |
|
94 return FALSE; |
|
95 } |
|
96 |
|
97 void |
|
98 error_test1 (void) |
|
99 { |
|
100 GOptionContext *context; |
|
101 gboolean retval; |
|
102 GError *error = NULL; |
|
103 gchar **argv; |
|
104 int argc; |
|
105 GOptionGroup *main_group; |
|
106 #ifndef SYMBIAN |
|
107 GOptionEntry entries [] = |
|
108 { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL }, |
|
109 { NULL } }; |
|
110 #else |
|
111 |
|
112 GOptionEntry entries [2]; |
|
113 |
|
114 entries[0].long_name = "test"; |
|
115 entries[0].short_name = 0; |
|
116 entries[0].flags = 0; |
|
117 entries[0].arg = G_OPTION_ARG_INT; |
|
118 entries[0].arg_data = (gpointer)&error_test1_int; |
|
119 entries[0].description = NULL; |
|
120 entries[0].arg_description = NULL; |
|
121 |
|
122 entries[1].long_name = NULL; |
|
123 entries[1].short_name = 0; |
|
124 entries[1].arg_data = NULL; |
|
125 entries[1].description = NULL; |
|
126 entries[1].arg_description = NULL; |
|
127 #endif |
|
128 |
|
129 context = g_option_context_new (NULL); |
|
130 g_option_context_add_main_entries (context, entries, NULL); |
|
131 |
|
132 /* Set pre and post parse hooks */ |
|
133 main_group = g_option_context_get_main_group (context); |
|
134 g_option_group_set_parse_hooks (main_group, |
|
135 error_test1_pre_parse, error_test1_post_parse); |
|
136 |
|
137 /* Now try parsing */ |
|
138 argv = split_string ("program --test 20", &argc); |
|
139 |
|
140 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
141 g_assert (retval == FALSE); |
|
142 |
|
143 /* On failure, values should be reset */ |
|
144 g_assert (error_test1_int == 0x12345678); |
|
145 |
|
146 g_strfreev (argv); |
|
147 g_option_context_free (context); |
|
148 |
|
149 } |
|
150 |
|
151 static gboolean |
|
152 error_test2_pre_parse (GOptionContext *context, |
|
153 GOptionGroup *group, |
|
154 gpointer data, |
|
155 GError **error) |
|
156 { |
|
157 g_assert (strcmp (error_test2_string, "foo") == 0); |
|
158 |
|
159 return TRUE; |
|
160 } |
|
161 |
|
162 static gboolean |
|
163 error_test2_post_parse (GOptionContext *context, |
|
164 GOptionGroup *group, |
|
165 gpointer data, |
|
166 GError **error) |
|
167 { |
|
168 g_assert (strcmp (error_test2_string, "bar") == 0); |
|
169 |
|
170 /* Set an error in the post hook */ |
|
171 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, ""); |
|
172 |
|
173 return FALSE; |
|
174 } |
|
175 |
|
176 void |
|
177 error_test2 (void) |
|
178 { |
|
179 GOptionContext *context; |
|
180 gboolean retval; |
|
181 GError *error = NULL; |
|
182 gchar **argv; |
|
183 int argc; |
|
184 GOptionGroup *main_group; |
|
185 #ifndef SYMBIAN |
|
186 GOptionEntry entries [] = |
|
187 { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL }, |
|
188 { NULL } }; |
|
189 #else |
|
190 GOptionEntry entries [2]; |
|
191 |
|
192 entries[0].long_name = "test"; |
|
193 entries[0].short_name = 0; |
|
194 entries[0].flags = 0; |
|
195 entries[0].arg = G_OPTION_ARG_STRING; |
|
196 entries[0].arg_data = (gpointer)&error_test2_string; |
|
197 entries[0].description = NULL; |
|
198 entries[0].arg_description = NULL; |
|
199 |
|
200 entries[1].long_name = NULL; |
|
201 entries[1].short_name = 0; |
|
202 entries[1].arg_data = NULL; |
|
203 entries[1].description = NULL; |
|
204 entries[1].arg_description = NULL; |
|
205 #endif |
|
206 |
|
207 |
|
208 context = g_option_context_new (NULL); |
|
209 g_option_context_add_main_entries (context, entries, NULL); |
|
210 |
|
211 /* Set pre and post parse hooks */ |
|
212 main_group = g_option_context_get_main_group (context); |
|
213 g_option_group_set_parse_hooks (main_group, |
|
214 error_test2_pre_parse, error_test2_post_parse); |
|
215 |
|
216 /* Now try parsing */ |
|
217 argv = split_string ("program --test bar", &argc); |
|
218 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
219 |
|
220 g_error_free (error); |
|
221 g_assert (retval == FALSE); |
|
222 |
|
223 g_assert (strcmp (error_test2_string, "foo") == 0); |
|
224 |
|
225 g_strfreev (argv); |
|
226 g_option_context_free (context); |
|
227 } |
|
228 |
|
229 static gboolean |
|
230 error_test3_pre_parse (GOptionContext *context, |
|
231 GOptionGroup *group, |
|
232 gpointer data, |
|
233 GError **error) |
|
234 { |
|
235 g_assert (!error_test3_boolean); |
|
236 |
|
237 return TRUE; |
|
238 } |
|
239 |
|
240 static gboolean |
|
241 error_test3_post_parse (GOptionContext *context, |
|
242 GOptionGroup *group, |
|
243 gpointer data, |
|
244 GError **error) |
|
245 { |
|
246 g_assert (error_test3_boolean); |
|
247 |
|
248 /* Set an error in the post hook */ |
|
249 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, ""); |
|
250 |
|
251 return FALSE; |
|
252 } |
|
253 |
|
254 void |
|
255 error_test3 (void) |
|
256 { |
|
257 GOptionContext *context; |
|
258 gboolean retval; |
|
259 GError *error = NULL; |
|
260 gchar **argv; |
|
261 int argc; |
|
262 GOptionGroup *main_group; |
|
263 |
|
264 #ifndef SYMBIAN |
|
265 GOptionEntry entries [] = |
|
266 { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL }, |
|
267 { NULL } }; |
|
268 #else |
|
269 GOptionEntry entries [2]; |
|
270 |
|
271 entries[0].long_name = "test"; |
|
272 entries[0].short_name = 0; |
|
273 entries[0].flags = 0; |
|
274 entries[0].arg = G_OPTION_ARG_NONE; |
|
275 entries[0].arg_data = (gpointer)&error_test3_boolean; |
|
276 entries[0].description = NULL; |
|
277 entries[0].arg_description = NULL; |
|
278 |
|
279 entries[1].long_name = NULL; |
|
280 entries[1].short_name = 0; |
|
281 entries[1].arg_data = NULL; |
|
282 entries[1].description = NULL; |
|
283 entries[1].arg_description = NULL; |
|
284 #endif |
|
285 |
|
286 |
|
287 context = g_option_context_new (NULL); |
|
288 g_option_context_add_main_entries (context, entries, NULL); |
|
289 |
|
290 /* Set pre and post parse hooks */ |
|
291 main_group = g_option_context_get_main_group (context); |
|
292 g_option_group_set_parse_hooks (main_group, |
|
293 error_test3_pre_parse, error_test3_post_parse); |
|
294 |
|
295 /* Now try parsing */ |
|
296 argv = split_string ("program --test", &argc); |
|
297 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
298 |
|
299 g_error_free (error); |
|
300 g_assert (retval == FALSE); |
|
301 |
|
302 g_assert (!error_test3_boolean); |
|
303 |
|
304 g_strfreev (argv); |
|
305 g_option_context_free (context); |
|
306 } |
|
307 |
|
308 void |
|
309 arg_test1 (void) |
|
310 { |
|
311 GOptionContext *context; |
|
312 gboolean retval; |
|
313 GError *error = NULL; |
|
314 gchar **argv; |
|
315 int argc; |
|
316 #ifndef SYMBIAN |
|
317 GOptionEntry entries [] = |
|
318 { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL }, |
|
319 { NULL } }; |
|
320 #else |
|
321 GOptionEntry entries [2]; |
|
322 |
|
323 entries[0].long_name = "test"; |
|
324 entries[0].short_name = 0; |
|
325 entries[0].flags = 0; |
|
326 entries[0].arg = G_OPTION_ARG_INT; |
|
327 entries[0].arg_data = (gpointer)&arg_test1_int; |
|
328 entries[0].description = NULL; |
|
329 entries[0].arg_description = NULL; |
|
330 |
|
331 entries[1].long_name = NULL; |
|
332 entries[1].short_name = 0; |
|
333 entries[1].arg_data = NULL; |
|
334 entries[1].description = NULL; |
|
335 entries[1].arg_description = NULL; |
|
336 #endif |
|
337 |
|
338 context = g_option_context_new (NULL); |
|
339 g_option_context_add_main_entries (context, entries, NULL); |
|
340 |
|
341 /* Now try parsing */ |
|
342 argv = split_string ("program --test 20 --test 30", &argc); |
|
343 |
|
344 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
345 g_assert (retval); |
|
346 |
|
347 /* Last arg specified is the one that should be stored */ |
|
348 g_assert (arg_test1_int == 30); |
|
349 |
|
350 g_strfreev (argv); |
|
351 g_option_context_free (context); |
|
352 } |
|
353 |
|
354 void |
|
355 arg_test2 (void) |
|
356 { |
|
357 GOptionContext *context; |
|
358 gboolean retval; |
|
359 GError *error = NULL; |
|
360 gchar **argv; |
|
361 int argc; |
|
362 #ifndef SYMBIAN |
|
363 GOptionEntry entries [] = |
|
364 { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL }, |
|
365 { NULL } }; |
|
366 #else |
|
367 GOptionEntry entries [2]; |
|
368 |
|
369 entries[0].long_name = "test"; |
|
370 entries[0].short_name = 0; |
|
371 entries[0].flags = 0; |
|
372 entries[0].arg = G_OPTION_ARG_STRING; |
|
373 entries[0].arg_data = (gpointer)&arg_test2_string; |
|
374 entries[0].description = NULL; |
|
375 entries[0].arg_description = NULL; |
|
376 |
|
377 entries[1].long_name = NULL; |
|
378 entries[1].short_name = 0; |
|
379 entries[1].arg_data = NULL; |
|
380 entries[1].description = NULL; |
|
381 entries[1].arg_description = NULL; |
|
382 #endif |
|
383 |
|
384 context = g_option_context_new (NULL); |
|
385 g_option_context_add_main_entries (context, entries, NULL); |
|
386 |
|
387 /* Now try parsing */ |
|
388 argv = split_string ("program --test foo --test bar", &argc); |
|
389 |
|
390 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
391 g_assert (retval); |
|
392 |
|
393 /* Last arg specified is the one that should be stored */ |
|
394 g_assert (strcmp (arg_test2_string, "bar") == 0); |
|
395 |
|
396 g_free (arg_test2_string); |
|
397 |
|
398 g_strfreev (argv); |
|
399 g_option_context_free (context); |
|
400 } |
|
401 |
|
402 void |
|
403 arg_test3 (void) |
|
404 { |
|
405 GOptionContext *context; |
|
406 gboolean retval; |
|
407 GError *error = NULL; |
|
408 gchar **argv; |
|
409 int argc; |
|
410 #ifndef SYMBIAN |
|
411 GOptionEntry entries [] = |
|
412 { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL }, |
|
413 { NULL } }; |
|
414 #else |
|
415 GOptionEntry entries [2]; |
|
416 |
|
417 entries[0].long_name = "test"; |
|
418 entries[0].short_name = 0; |
|
419 entries[0].flags = 0; |
|
420 entries[0].arg = G_OPTION_ARG_FILENAME; |
|
421 entries[0].arg_data = (gpointer)&arg_test3_filename; |
|
422 entries[0].description = NULL; |
|
423 entries[0].arg_description = NULL; |
|
424 |
|
425 entries[1].long_name = NULL; |
|
426 entries[1].short_name = 0; |
|
427 entries[1].arg_data = NULL; |
|
428 entries[1].description = NULL; |
|
429 entries[1].arg_description = NULL; |
|
430 #endif |
|
431 |
|
432 context = g_option_context_new (NULL); |
|
433 g_option_context_add_main_entries (context, entries, NULL); |
|
434 |
|
435 /* Now try parsing */ |
|
436 argv = split_string ("program --test foo.txt", &argc); |
|
437 |
|
438 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
439 g_assert (retval); |
|
440 |
|
441 /* Last arg specified is the one that should be stored */ |
|
442 g_assert (strcmp (arg_test3_filename, "foo.txt") == 0); |
|
443 |
|
444 g_free (arg_test3_filename); |
|
445 |
|
446 g_strfreev (argv); |
|
447 g_option_context_free (context); |
|
448 } |
|
449 |
|
450 static gboolean |
|
451 callback_parse1 (const gchar *option_name, const gchar *value, |
|
452 gpointer data, GError **error) |
|
453 { |
|
454 callback_test1_string = g_strdup (value); |
|
455 return TRUE; |
|
456 } |
|
457 |
|
458 void |
|
459 callback_test1 (void) |
|
460 { |
|
461 GOptionContext *context; |
|
462 gboolean retval; |
|
463 GError *error = NULL; |
|
464 gchar **argv; |
|
465 int argc; |
|
466 #ifndef SYMBIAN |
|
467 GOptionEntry entries [] = |
|
468 { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL }, |
|
469 { NULL } }; |
|
470 #else |
|
471 GOptionEntry entries [2]; |
|
472 |
|
473 entries[0].long_name = "test"; |
|
474 entries[0].short_name = 0; |
|
475 entries[0].flags = 0; |
|
476 entries[0].arg = G_OPTION_ARG_CALLBACK; |
|
477 entries[0].arg_data = (gpointer)callback_parse1; |
|
478 entries[0].description = NULL; |
|
479 entries[0].arg_description = NULL; |
|
480 |
|
481 entries[1].long_name = NULL; |
|
482 entries[1].short_name = 0; |
|
483 entries[1].arg_data = NULL; |
|
484 entries[1].description = NULL; |
|
485 entries[1].arg_description = NULL; |
|
486 #endif |
|
487 |
|
488 context = g_option_context_new (NULL); |
|
489 g_option_context_add_main_entries (context, entries, NULL); |
|
490 |
|
491 /* Now try parsing */ |
|
492 argv = split_string ("program --test foo.txt", &argc); |
|
493 |
|
494 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
495 g_assert (retval); |
|
496 |
|
497 g_assert (strcmp (callback_test1_string, "foo.txt") == 0); |
|
498 |
|
499 g_free (callback_test1_string); |
|
500 |
|
501 g_strfreev (argv); |
|
502 g_option_context_free (context); |
|
503 } |
|
504 |
|
505 static gboolean |
|
506 callback_parse2 (const gchar *option_name, const gchar *value, |
|
507 gpointer data, GError **error) |
|
508 { |
|
509 callback_test2_int++; |
|
510 return TRUE; |
|
511 } |
|
512 |
|
513 void |
|
514 callback_test2 (void) |
|
515 { |
|
516 GOptionContext *context; |
|
517 gboolean retval; |
|
518 GError *error = NULL; |
|
519 gchar **argv; |
|
520 int argc; |
|
521 |
|
522 #ifndef SYMBIAN |
|
523 GOptionEntry entries [] = |
|
524 { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL }, |
|
525 { NULL } }; |
|
526 #else |
|
527 GOptionEntry entries [2]; |
|
528 |
|
529 entries[0].long_name = "test"; |
|
530 entries[0].short_name = 0; |
|
531 entries[0].flags = G_OPTION_FLAG_NO_ARG; |
|
532 entries[0].arg = G_OPTION_ARG_CALLBACK; |
|
533 entries[0].arg_data = (gpointer)callback_parse2; |
|
534 entries[0].description = NULL; |
|
535 entries[0].arg_description = NULL; |
|
536 |
|
537 entries[1].long_name = NULL; |
|
538 entries[1].short_name = 0; |
|
539 entries[1].arg_data = NULL; |
|
540 entries[1].description = NULL; |
|
541 entries[1].arg_description = NULL; |
|
542 #endif |
|
543 |
|
544 context = g_option_context_new (NULL); |
|
545 g_option_context_add_main_entries (context, entries, NULL); |
|
546 |
|
547 /* Now try parsing */ |
|
548 argv = split_string ("program --test --test", &argc); |
|
549 |
|
550 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
551 g_assert (retval); |
|
552 |
|
553 g_assert (callback_test2_int == 2); |
|
554 |
|
555 g_strfreev (argv); |
|
556 g_option_context_free (context); |
|
557 } |
|
558 |
|
559 static gboolean |
|
560 callback_parse_optional (const gchar *option_name, const gchar *value, |
|
561 gpointer data, GError **error) |
|
562 { |
|
563 callback_test_optional_boolean = TRUE; |
|
564 if (value) |
|
565 callback_test_optional_string = g_strdup (value); |
|
566 else |
|
567 callback_test_optional_string = NULL; |
|
568 return TRUE; |
|
569 } |
|
570 |
|
571 void |
|
572 callback_test_optional_1 (void) |
|
573 { |
|
574 GOptionContext *context; |
|
575 gboolean retval; |
|
576 GError *error = NULL; |
|
577 gchar **argv; |
|
578 int argc; |
|
579 #ifndef SYMBIAN |
|
580 GOptionEntry entries [] = |
|
581 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, |
|
582 callback_parse_optional, NULL, NULL }, |
|
583 { NULL } }; |
|
584 #else |
|
585 GOptionEntry entries [2]; |
|
586 |
|
587 entries[0].long_name = "test"; |
|
588 entries[0].short_name = 0; |
|
589 entries[0].flags = G_OPTION_FLAG_OPTIONAL_ARG; |
|
590 entries[0].arg = G_OPTION_ARG_CALLBACK; |
|
591 entries[0].arg_data = (gpointer)callback_parse_optional; |
|
592 entries[0].description = NULL; |
|
593 entries[0].arg_description = NULL; |
|
594 |
|
595 entries[1].long_name = NULL; |
|
596 entries[1].short_name = 0; |
|
597 entries[1].arg_data = NULL; |
|
598 entries[1].description = NULL; |
|
599 entries[1].arg_description = NULL; |
|
600 #endif |
|
601 |
|
602 context = g_option_context_new (NULL); |
|
603 g_option_context_add_main_entries (context, entries, NULL); |
|
604 |
|
605 /* Now try parsing */ |
|
606 argv = split_string ("program --test foo.txt", &argc); |
|
607 |
|
608 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
609 g_assert (retval); |
|
610 |
|
611 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0); |
|
612 |
|
613 g_assert (callback_test_optional_boolean); |
|
614 |
|
615 g_free (callback_test_optional_string); |
|
616 |
|
617 g_strfreev (argv); |
|
618 g_option_context_free (context); |
|
619 } |
|
620 |
|
621 void |
|
622 callback_test_optional_2 (void) |
|
623 { |
|
624 GOptionContext *context; |
|
625 gboolean retval; |
|
626 GError *error = NULL; |
|
627 gchar **argv; |
|
628 int argc; |
|
629 #ifndef SYMBIAN |
|
630 GOptionEntry entries [] = |
|
631 { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, |
|
632 callback_parse_optional, NULL, NULL }, |
|
633 { NULL } }; |
|
634 #else |
|
635 GOptionEntry entries [2]; |
|
636 |
|
637 entries[0].long_name = "test"; |
|
638 entries[0].short_name = 0; |
|
639 entries[0].flags = G_OPTION_FLAG_OPTIONAL_ARG; |
|
640 entries[0].arg = G_OPTION_ARG_CALLBACK; |
|
641 entries[0].arg_data = (gpointer)callback_parse_optional; |
|
642 entries[0].description = NULL; |
|
643 entries[0].arg_description = NULL; |
|
644 |
|
645 entries[1].long_name = NULL; |
|
646 entries[1].short_name = 0; |
|
647 entries[1].arg_data = NULL; |
|
648 entries[1].description = NULL; |
|
649 entries[1].arg_description = NULL; |
|
650 #endif |
|
651 |
|
652 context = g_option_context_new (NULL); |
|
653 g_option_context_add_main_entries (context, entries, NULL); |
|
654 |
|
655 /* Now try parsing */ |
|
656 argv = split_string ("program --test", &argc); |
|
657 |
|
658 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
659 g_assert (retval); |
|
660 |
|
661 g_assert (callback_test_optional_string == NULL); |
|
662 |
|
663 g_assert (callback_test_optional_boolean); |
|
664 |
|
665 g_free (callback_test_optional_string); |
|
666 |
|
667 g_strfreev (argv); |
|
668 g_option_context_free (context); |
|
669 } |
|
670 |
|
671 void |
|
672 callback_test_optional_3 (void) |
|
673 { |
|
674 GOptionContext *context; |
|
675 gboolean retval; |
|
676 GError *error = NULL; |
|
677 gchar **argv; |
|
678 int argc; |
|
679 #ifndef SYMBIAN |
|
680 GOptionEntry entries [] = |
|
681 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, |
|
682 callback_parse_optional, NULL, NULL }, |
|
683 { NULL } }; |
|
684 #else |
|
685 GOptionEntry entries [2]; |
|
686 |
|
687 entries[0].long_name = "test"; |
|
688 entries[0].short_name = 't'; |
|
689 entries[0].flags = G_OPTION_FLAG_OPTIONAL_ARG; |
|
690 entries[0].arg = G_OPTION_ARG_CALLBACK; |
|
691 entries[0].arg_data = (gpointer)callback_parse_optional; |
|
692 entries[0].description = NULL; |
|
693 entries[0].arg_description = NULL; |
|
694 |
|
695 entries[1].long_name = NULL; |
|
696 entries[1].short_name = 0; |
|
697 entries[1].arg_data = NULL; |
|
698 entries[1].description = NULL; |
|
699 entries[1].arg_description = NULL; |
|
700 #endif |
|
701 |
|
702 context = g_option_context_new (NULL); |
|
703 g_option_context_add_main_entries (context, entries, NULL); |
|
704 |
|
705 /* Now try parsing */ |
|
706 argv = split_string ("program -t foo.txt", &argc); |
|
707 |
|
708 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
709 g_assert (retval); |
|
710 |
|
711 g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0); |
|
712 |
|
713 g_assert (callback_test_optional_boolean); |
|
714 |
|
715 g_free (callback_test_optional_string); |
|
716 |
|
717 g_strfreev (argv); |
|
718 g_option_context_free (context); |
|
719 } |
|
720 |
|
721 |
|
722 void |
|
723 callback_test_optional_4 (void) |
|
724 { |
|
725 GOptionContext *context; |
|
726 gboolean retval; |
|
727 GError *error = NULL; |
|
728 gchar **argv; |
|
729 int argc; |
|
730 #ifndef SYMBIAN |
|
731 GOptionEntry entries [] = |
|
732 { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, |
|
733 callback_parse_optional, NULL, NULL }, |
|
734 { NULL } }; |
|
735 #else |
|
736 GOptionEntry entries [2]; |
|
737 |
|
738 entries[0].long_name = "test"; |
|
739 entries[0].short_name = 't'; |
|
740 entries[0].flags = G_OPTION_FLAG_OPTIONAL_ARG; |
|
741 entries[0].arg = G_OPTION_ARG_CALLBACK; |
|
742 entries[0].arg_data = (gpointer)callback_parse_optional; |
|
743 entries[0].description = NULL; |
|
744 entries[0].arg_description = NULL; |
|
745 |
|
746 entries[1].long_name = NULL; |
|
747 entries[1].short_name = 0; |
|
748 entries[1].arg_data = NULL; |
|
749 entries[1].description = NULL; |
|
750 entries[1].arg_description = NULL; |
|
751 #endif |
|
752 |
|
753 context = g_option_context_new (NULL); |
|
754 g_option_context_add_main_entries (context, entries, NULL); |
|
755 |
|
756 /* Now try parsing */ |
|
757 argv = split_string ("program -t", &argc); |
|
758 |
|
759 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
760 g_assert (retval); |
|
761 |
|
762 g_assert (callback_test_optional_string == NULL); |
|
763 |
|
764 g_assert (callback_test_optional_boolean); |
|
765 |
|
766 g_free (callback_test_optional_string); |
|
767 |
|
768 g_strfreev (argv); |
|
769 g_option_context_free (context); |
|
770 } |
|
771 |
|
772 void |
|
773 callback_test_optional_5 (void) |
|
774 { |
|
775 GOptionContext *context; |
|
776 gboolean dummy; |
|
777 gboolean retval; |
|
778 GError *error = NULL; |
|
779 gchar **argv; |
|
780 int argc; |
|
781 #ifndef SYMBIAN |
|
782 GOptionEntry entries [] = |
|
783 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL }, |
|
784 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, |
|
785 callback_parse_optional, NULL, NULL }, |
|
786 { NULL } }; |
|
787 #else |
|
788 GOptionEntry entries [3]; |
|
789 |
|
790 entries[0].long_name = "dummy"; |
|
791 entries[0].short_name = 'd'; |
|
792 entries[0].flags = 0; |
|
793 entries[0].arg = G_OPTION_ARG_NONE; |
|
794 entries[0].arg_data = (gpointer)&dummy; |
|
795 entries[0].description = NULL; |
|
796 |
|
797 entries[1].long_name = "test"; |
|
798 entries[1].short_name = 't'; |
|
799 entries[1].flags = G_OPTION_FLAG_OPTIONAL_ARG; |
|
800 entries[1].arg = G_OPTION_ARG_CALLBACK; |
|
801 entries[1].arg_data = (gpointer)callback_parse_optional; |
|
802 entries[1].description = NULL; |
|
803 entries[1].arg_description = NULL; |
|
804 |
|
805 |
|
806 entries[2].long_name = NULL; |
|
807 entries[2].short_name = 0; |
|
808 entries[2].arg_data = NULL; |
|
809 entries[2].description = NULL; |
|
810 entries[2].arg_description = NULL; |
|
811 #endif |
|
812 |
|
813 context = g_option_context_new (NULL); |
|
814 g_option_context_add_main_entries (context, entries, NULL); |
|
815 |
|
816 /* Now try parsing */ |
|
817 argv = split_string ("program --test --dummy", &argc); |
|
818 |
|
819 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
820 g_assert (retval); |
|
821 |
|
822 g_assert (callback_test_optional_string == NULL); |
|
823 |
|
824 g_assert (callback_test_optional_boolean); |
|
825 |
|
826 g_free (callback_test_optional_string); |
|
827 |
|
828 g_strfreev (argv); |
|
829 g_option_context_free (context); |
|
830 } |
|
831 |
|
832 void |
|
833 callback_test_optional_6 (void) |
|
834 { |
|
835 GOptionContext *context; |
|
836 gboolean dummy; |
|
837 gboolean retval; |
|
838 GError *error = NULL; |
|
839 gchar **argv; |
|
840 int argc; |
|
841 #ifndef SYMBIAN |
|
842 GOptionEntry entries [] = |
|
843 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL }, |
|
844 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, |
|
845 callback_parse_optional, NULL, NULL }, |
|
846 { NULL } }; |
|
847 #else |
|
848 GOptionEntry entries [3]; |
|
849 |
|
850 entries[0].long_name = "dummy"; |
|
851 entries[0].short_name = 'd'; |
|
852 entries[0].flags = 0; |
|
853 entries[0].arg = G_OPTION_ARG_NONE; |
|
854 entries[0].arg_data = (gpointer)&dummy; |
|
855 entries[0].description = NULL; |
|
856 |
|
857 entries[1].long_name = "test"; |
|
858 entries[1].short_name = 't'; |
|
859 entries[1].flags = G_OPTION_FLAG_OPTIONAL_ARG; |
|
860 entries[1].arg = G_OPTION_ARG_CALLBACK; |
|
861 entries[1].arg_data = (gpointer)callback_parse_optional; |
|
862 entries[1].description = NULL; |
|
863 entries[1].arg_description = NULL; |
|
864 |
|
865 |
|
866 entries[2].long_name = NULL; |
|
867 entries[2].short_name = 0; |
|
868 entries[2].arg_data = NULL; |
|
869 entries[2].description = NULL; |
|
870 entries[2].arg_description = NULL; |
|
871 #endif |
|
872 |
|
873 |
|
874 context = g_option_context_new (NULL); |
|
875 g_option_context_add_main_entries (context, entries, NULL); |
|
876 |
|
877 /* Now try parsing */ |
|
878 argv = split_string ("program -t -d", &argc); |
|
879 |
|
880 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
881 g_assert (retval); |
|
882 |
|
883 g_assert (callback_test_optional_string == NULL); |
|
884 |
|
885 g_assert (callback_test_optional_boolean); |
|
886 |
|
887 g_free (callback_test_optional_string); |
|
888 |
|
889 g_strfreev (argv); |
|
890 g_option_context_free (context); |
|
891 } |
|
892 |
|
893 void |
|
894 callback_test_optional_7 (void) |
|
895 { |
|
896 GOptionContext *context; |
|
897 gboolean dummy; |
|
898 gboolean retval; |
|
899 GError *error = NULL; |
|
900 gchar **argv; |
|
901 int argc; |
|
902 #ifndef SYMBIAN |
|
903 GOptionEntry entries [] = |
|
904 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL }, |
|
905 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, |
|
906 callback_parse_optional, NULL, NULL }, |
|
907 { NULL } }; |
|
908 #else |
|
909 GOptionEntry entries [3]; |
|
910 |
|
911 entries[0].long_name = "dummy"; |
|
912 entries[0].short_name = 'd'; |
|
913 entries[0].flags = 0; |
|
914 entries[0].arg = G_OPTION_ARG_NONE; |
|
915 entries[0].arg_data = (gpointer)&dummy; |
|
916 entries[0].description = NULL; |
|
917 |
|
918 entries[1].long_name = "test"; |
|
919 entries[1].short_name = 't'; |
|
920 entries[1].flags = G_OPTION_FLAG_OPTIONAL_ARG; |
|
921 entries[1].arg = G_OPTION_ARG_CALLBACK; |
|
922 entries[1].arg_data = (gpointer)callback_parse_optional; |
|
923 entries[1].description = NULL; |
|
924 entries[1].arg_description = NULL; |
|
925 |
|
926 |
|
927 entries[2].long_name = NULL; |
|
928 entries[2].short_name = 0; |
|
929 entries[2].arg_data = NULL; |
|
930 entries[2].description = NULL; |
|
931 entries[2].arg_description = NULL; |
|
932 #endif |
|
933 |
|
934 |
|
935 context = g_option_context_new (NULL); |
|
936 g_option_context_add_main_entries (context, entries, NULL); |
|
937 |
|
938 /* Now try parsing */ |
|
939 argv = split_string ("program -td", &argc); |
|
940 |
|
941 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
942 g_assert (retval); |
|
943 |
|
944 g_assert (callback_test_optional_string == NULL); |
|
945 |
|
946 g_assert (callback_test_optional_boolean); |
|
947 |
|
948 g_free (callback_test_optional_string); |
|
949 |
|
950 g_strfreev (argv); |
|
951 g_option_context_free (context); |
|
952 } |
|
953 |
|
954 void |
|
955 callback_test_optional_8 (void) |
|
956 { |
|
957 GOptionContext *context; |
|
958 gboolean dummy; |
|
959 gboolean retval; |
|
960 GError *error = NULL; |
|
961 gchar **argv; |
|
962 int argc; |
|
963 #ifndef SYMBIAN |
|
964 GOptionEntry entries [] = |
|
965 { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL }, |
|
966 { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, |
|
967 callback_parse_optional, NULL, NULL }, |
|
968 { NULL } }; |
|
969 #else |
|
970 GOptionEntry entries [3]; |
|
971 |
|
972 entries[0].long_name = "dummy"; |
|
973 entries[0].short_name = 'd'; |
|
974 entries[0].flags = 0; |
|
975 entries[0].arg = G_OPTION_ARG_NONE; |
|
976 entries[0].arg_data = (gpointer)&dummy; |
|
977 entries[0].description = NULL; |
|
978 |
|
979 entries[1].long_name = "test"; |
|
980 entries[1].short_name = 't'; |
|
981 entries[1].flags = G_OPTION_FLAG_OPTIONAL_ARG; |
|
982 entries[1].arg = G_OPTION_ARG_CALLBACK; |
|
983 entries[1].arg_data = (gpointer)callback_parse_optional; |
|
984 entries[1].description = NULL; |
|
985 entries[1].arg_description = NULL; |
|
986 |
|
987 |
|
988 entries[2].long_name = NULL; |
|
989 entries[2].short_name = 0; |
|
990 entries[2].arg_data = NULL; |
|
991 entries[2].description = NULL; |
|
992 entries[2].arg_description = NULL; |
|
993 #endif |
|
994 |
|
995 context = g_option_context_new (NULL); |
|
996 g_option_context_add_main_entries (context, entries, NULL); |
|
997 |
|
998 /* Now try parsing */ |
|
999 argv = split_string ("program -dt foo.txt", &argc); |
|
1000 |
|
1001 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1002 g_assert (retval); |
|
1003 |
|
1004 g_assert (callback_test_optional_string); |
|
1005 |
|
1006 g_assert (callback_test_optional_boolean); |
|
1007 |
|
1008 g_free (callback_test_optional_string); |
|
1009 |
|
1010 g_strfreev (argv); |
|
1011 g_option_context_free (context); |
|
1012 } |
|
1013 |
|
1014 void |
|
1015 ignore_test1 (void) |
|
1016 { |
|
1017 GOptionContext *context; |
|
1018 gboolean retval; |
|
1019 GError *error = NULL; |
|
1020 gchar **argv, **argv_copy; |
|
1021 int argc; |
|
1022 gchar *arg; |
|
1023 #ifndef SYMBIAN |
|
1024 GOptionEntry entries [] = |
|
1025 { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1026 { NULL } }; |
|
1027 #else |
|
1028 GOptionEntry entries [2]; |
|
1029 |
|
1030 entries[0].long_name = "test"; |
|
1031 entries[0].short_name = 0; |
|
1032 entries[0].flags = 0; |
|
1033 entries[0].arg = G_OPTION_ARG_NONE; |
|
1034 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1035 entries[0].description = NULL; |
|
1036 entries[0].arg_description = NULL; |
|
1037 |
|
1038 entries[1].long_name = NULL; |
|
1039 entries[1].short_name = 0; |
|
1040 entries[1].arg_data = NULL; |
|
1041 entries[1].description = NULL; |
|
1042 entries[1].arg_description = NULL; |
|
1043 #endif |
|
1044 |
|
1045 context = g_option_context_new (NULL); |
|
1046 g_option_context_set_ignore_unknown_options (context, TRUE); |
|
1047 g_option_context_add_main_entries (context, entries, NULL); |
|
1048 |
|
1049 /* Now try parsing */ |
|
1050 argv = split_string ("program --test --hello", &argc); |
|
1051 argv_copy = copy_stringv (argv, argc); |
|
1052 |
|
1053 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1054 g_assert (retval); |
|
1055 |
|
1056 /* Check array */ |
|
1057 arg = join_stringv (argc, argv); |
|
1058 g_assert (strcmp (arg, "program --hello") == 0); |
|
1059 |
|
1060 g_free (arg); |
|
1061 g_strfreev (argv_copy); |
|
1062 g_free (argv); |
|
1063 g_option_context_free (context); |
|
1064 } |
|
1065 |
|
1066 void |
|
1067 ignore_test2 (void) |
|
1068 { |
|
1069 GOptionContext *context; |
|
1070 gboolean retval; |
|
1071 GError *error = NULL; |
|
1072 gchar **argv; |
|
1073 int argc; |
|
1074 gchar *arg; |
|
1075 #ifndef SYMBIAN |
|
1076 GOptionEntry entries [] = |
|
1077 { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL }, |
|
1078 { NULL } }; |
|
1079 #else |
|
1080 GOptionEntry entries [2]; |
|
1081 |
|
1082 entries[0].long_name = "test"; |
|
1083 entries[0].short_name = 't'; |
|
1084 entries[0].flags = 0; |
|
1085 entries[0].arg = G_OPTION_ARG_NONE; |
|
1086 entries[0].arg_data = (gpointer)&ignore_test2_boolean; |
|
1087 entries[0].description = NULL; |
|
1088 entries[0].arg_description = NULL; |
|
1089 |
|
1090 entries[1].long_name = NULL; |
|
1091 entries[1].short_name = 0; |
|
1092 entries[1].arg_data = NULL; |
|
1093 entries[1].description = NULL; |
|
1094 entries[1].arg_description = NULL; |
|
1095 #endif |
|
1096 |
|
1097 context = g_option_context_new (NULL); |
|
1098 g_option_context_set_ignore_unknown_options (context, TRUE); |
|
1099 g_option_context_add_main_entries (context, entries, NULL); |
|
1100 |
|
1101 /* Now try parsing */ |
|
1102 argv = split_string ("program -test", &argc); |
|
1103 |
|
1104 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1105 g_assert (retval); |
|
1106 |
|
1107 /* Check array */ |
|
1108 arg = join_stringv (argc, argv); |
|
1109 g_assert (strcmp (arg, "program -es") == 0); |
|
1110 |
|
1111 g_free (arg); |
|
1112 g_strfreev (argv); |
|
1113 g_option_context_free (context); |
|
1114 } |
|
1115 |
|
1116 void |
|
1117 ignore_test3 (void) |
|
1118 { |
|
1119 GOptionContext *context; |
|
1120 gboolean retval; |
|
1121 GError *error = NULL; |
|
1122 gchar **argv, **argv_copy; |
|
1123 int argc; |
|
1124 gchar *arg; |
|
1125 #ifndef SYMBAIN |
|
1126 GOptionEntry entries [] = |
|
1127 { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL }, |
|
1128 { NULL } }; |
|
1129 #else |
|
1130 GOptionEntry entries [2]; |
|
1131 |
|
1132 entries[0].long_name = "test"; |
|
1133 entries[0].short_name = 0; |
|
1134 entries[0].flags = 0; |
|
1135 entries[0].arg = G_OPTION_ARG_STRING; |
|
1136 entries[0].arg_data = (gpointer)&ignore_test3_string; |
|
1137 entries[0].description = NULL; |
|
1138 entries[0].arg_description = NULL; |
|
1139 |
|
1140 entries[1].long_name = NULL; |
|
1141 entries[1].short_name = 0; |
|
1142 entries[1].arg_data = NULL; |
|
1143 entries[1].description = NULL; |
|
1144 entries[1].arg_description = NULL; |
|
1145 #endif |
|
1146 |
|
1147 context = g_option_context_new (NULL); |
|
1148 g_option_context_set_ignore_unknown_options (context, TRUE); |
|
1149 g_option_context_add_main_entries (context, entries, NULL); |
|
1150 |
|
1151 /* Now try parsing */ |
|
1152 argv = split_string ("program --test foo --hello", &argc); |
|
1153 argv_copy = copy_stringv (argv, argc); |
|
1154 |
|
1155 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1156 g_assert (retval); |
|
1157 |
|
1158 /* Check array */ |
|
1159 arg = join_stringv (argc, argv); |
|
1160 g_assert (strcmp (arg, "program --hello") == 0); |
|
1161 |
|
1162 g_assert (strcmp (ignore_test3_string, "foo") == 0); |
|
1163 g_free (ignore_test3_string); |
|
1164 |
|
1165 g_free (arg); |
|
1166 g_strfreev (argv_copy); |
|
1167 g_free (argv); |
|
1168 g_option_context_free (context); |
|
1169 } |
|
1170 |
|
1171 void |
|
1172 array_test1 (void) |
|
1173 { |
|
1174 GOptionContext *context; |
|
1175 gboolean retval; |
|
1176 GError *error = NULL; |
|
1177 gchar **argv; |
|
1178 int argc; |
|
1179 #ifndef SYMBIAN |
|
1180 GOptionEntry entries [] = |
|
1181 { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL }, |
|
1182 { NULL } }; |
|
1183 #else |
|
1184 GOptionEntry entries [2]; |
|
1185 |
|
1186 entries[0].long_name = "test"; |
|
1187 entries[0].short_name = 0; |
|
1188 entries[0].flags = 0; |
|
1189 entries[0].arg = G_OPTION_ARG_STRING_ARRAY; |
|
1190 entries[0].arg_data = (gpointer)&array_test1_array; |
|
1191 entries[0].description = NULL; |
|
1192 entries[0].arg_description = NULL; |
|
1193 |
|
1194 entries[1].long_name = NULL; |
|
1195 entries[1].short_name = 0; |
|
1196 entries[1].arg_data = NULL; |
|
1197 entries[1].description = NULL; |
|
1198 entries[1].arg_description = NULL; |
|
1199 #endif |
|
1200 |
|
1201 context = g_option_context_new (NULL); |
|
1202 g_option_context_add_main_entries (context, entries, NULL); |
|
1203 |
|
1204 /* Now try parsing */ |
|
1205 argv = split_string ("program --test foo --test bar", &argc); |
|
1206 |
|
1207 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1208 g_assert (retval); |
|
1209 |
|
1210 /* Check array */ |
|
1211 g_assert (strcmp (array_test1_array[0], "foo") == 0); |
|
1212 g_assert (strcmp (array_test1_array[1], "bar") == 0); |
|
1213 g_assert (array_test1_array[2] == NULL); |
|
1214 |
|
1215 g_strfreev (array_test1_array); |
|
1216 |
|
1217 g_strfreev (argv); |
|
1218 g_option_context_free (context); |
|
1219 } |
|
1220 |
|
1221 void |
|
1222 add_test1 (void) |
|
1223 { |
|
1224 GOptionContext *context; |
|
1225 |
|
1226 #ifndef SYMBIAN |
|
1227 GOptionEntry entries1 [] = |
|
1228 { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL }, |
|
1229 { NULL } }; |
|
1230 GOptionEntry entries2 [] = |
|
1231 { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL }, |
|
1232 { NULL } }; |
|
1233 #else |
|
1234 GOptionEntry entries1 [2]; |
|
1235 GOptionEntry entries2 [2]; |
|
1236 |
|
1237 entries1[0].long_name = "test1"; |
|
1238 entries1[0].short_name = 0; |
|
1239 entries1[0].flags = 0; |
|
1240 entries1[0].arg = G_OPTION_ARG_STRING_ARRAY; |
|
1241 entries1[0].arg_data = NULL; |
|
1242 entries1[0].description = NULL; |
|
1243 entries1[0].arg_description = NULL; |
|
1244 |
|
1245 entries1[1].long_name = NULL; |
|
1246 entries1[1].short_name = 0; |
|
1247 entries1[1].arg_data = NULL; |
|
1248 entries1[1].description = NULL; |
|
1249 entries1[1].arg_description = NULL; |
|
1250 |
|
1251 |
|
1252 entries2[0].long_name = "test2"; |
|
1253 entries2[0].short_name = 0; |
|
1254 entries2[0].flags = 0; |
|
1255 entries2[0].arg = G_OPTION_ARG_STRING_ARRAY; |
|
1256 entries2[0].arg_data = NULL; |
|
1257 entries2[0].description = NULL; |
|
1258 entries2[0].arg_description = NULL; |
|
1259 |
|
1260 entries2[1].long_name = NULL; |
|
1261 entries2[1].short_name = 0; |
|
1262 entries2[1].arg_data = NULL; |
|
1263 entries2[1].description = NULL; |
|
1264 entries2[1].arg_description = NULL; |
|
1265 #endif |
|
1266 |
|
1267 context = g_option_context_new (NULL); |
|
1268 g_option_context_add_main_entries (context, entries1, NULL); |
|
1269 g_option_context_add_main_entries (context, entries2, NULL); |
|
1270 |
|
1271 g_option_context_free (context); |
|
1272 } |
|
1273 |
|
1274 void |
|
1275 empty_test1 (void) |
|
1276 { |
|
1277 GOptionContext *context; |
|
1278 GOptionEntry entries [] = |
|
1279 { { NULL } }; |
|
1280 |
|
1281 g_set_prgname (NULL); |
|
1282 |
|
1283 context = g_option_context_new (NULL); |
|
1284 |
|
1285 g_option_context_add_main_entries (context, entries, NULL); |
|
1286 |
|
1287 g_option_context_parse (context, NULL, NULL, NULL); |
|
1288 |
|
1289 g_assert (strcmp (g_get_prgname (), "<unknown>") == 0); |
|
1290 |
|
1291 g_option_context_free (context); |
|
1292 } |
|
1293 |
|
1294 void |
|
1295 empty_test2 (void) |
|
1296 { |
|
1297 GOptionContext *context; |
|
1298 |
|
1299 context = g_option_context_new (NULL); |
|
1300 g_option_context_parse (context, NULL, NULL, NULL); |
|
1301 |
|
1302 g_option_context_free (context); |
|
1303 } |
|
1304 |
|
1305 void |
|
1306 empty_test3 (void) |
|
1307 { |
|
1308 GOptionContext *context; |
|
1309 gint argc; |
|
1310 gchar **argv; |
|
1311 |
|
1312 argc = 0; |
|
1313 argv = NULL; |
|
1314 |
|
1315 context = g_option_context_new (NULL); |
|
1316 g_option_context_parse (context, &argc, &argv, NULL); |
|
1317 |
|
1318 g_option_context_free (context); |
|
1319 } |
|
1320 |
|
1321 /* check that non-option arguments are left in argv by default */ |
|
1322 void |
|
1323 rest_test1 (void) |
|
1324 { |
|
1325 GOptionContext *context; |
|
1326 gboolean retval; |
|
1327 GError *error = NULL; |
|
1328 gchar **argv; |
|
1329 int argc; |
|
1330 #ifndef SYMBIAN |
|
1331 GOptionEntry entries [] = { |
|
1332 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1333 { NULL } |
|
1334 }; |
|
1335 #else |
|
1336 GOptionEntry entries [2]; |
|
1337 |
|
1338 entries[0].long_name = "test"; |
|
1339 entries[0].short_name = 0; |
|
1340 entries[0].flags = 0; |
|
1341 entries[0].arg = G_OPTION_ARG_NONE; |
|
1342 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1343 entries[0].description = NULL; |
|
1344 entries[0].arg_description = NULL; |
|
1345 |
|
1346 entries[1].long_name = NULL; |
|
1347 entries[1].short_name = 0; |
|
1348 entries[1].arg_data = NULL; |
|
1349 entries[1].description = NULL; |
|
1350 entries[1].arg_description = NULL; |
|
1351 #endif |
|
1352 |
|
1353 context = g_option_context_new (NULL); |
|
1354 g_option_context_add_main_entries (context, entries, NULL); |
|
1355 |
|
1356 /* Now try parsing */ |
|
1357 argv = split_string ("program foo --test bar", &argc); |
|
1358 |
|
1359 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1360 g_assert (retval); |
|
1361 |
|
1362 /* Check array */ |
|
1363 g_assert (ignore_test1_boolean); |
|
1364 g_assert (strcmp (argv[0], "program") == 0); |
|
1365 g_assert (strcmp (argv[1], "foo") == 0); |
|
1366 g_assert (strcmp (argv[2], "bar") == 0); |
|
1367 g_assert (argv[3] == NULL); |
|
1368 |
|
1369 g_strfreev (argv); |
|
1370 g_option_context_free (context); |
|
1371 } |
|
1372 |
|
1373 /* check that -- works */ |
|
1374 void |
|
1375 rest_test2 (void) |
|
1376 { |
|
1377 GOptionContext *context; |
|
1378 gboolean retval; |
|
1379 GError *error = NULL; |
|
1380 gchar **argv; |
|
1381 int argc; |
|
1382 #ifndef SYMBIAN |
|
1383 GOptionEntry entries [] = { |
|
1384 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1385 { NULL } |
|
1386 }; |
|
1387 #else |
|
1388 GOptionEntry entries [2]; |
|
1389 |
|
1390 entries[0].long_name = "test"; |
|
1391 entries[0].short_name = 0; |
|
1392 entries[0].flags = 0; |
|
1393 entries[0].arg = G_OPTION_ARG_NONE; |
|
1394 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1395 entries[0].description = NULL; |
|
1396 entries[0].arg_description = NULL; |
|
1397 |
|
1398 entries[1].long_name = NULL; |
|
1399 entries[1].short_name = 0; |
|
1400 entries[1].arg_data = NULL; |
|
1401 entries[1].description = NULL; |
|
1402 entries[1].arg_description = NULL; |
|
1403 #endif |
|
1404 |
|
1405 context = g_option_context_new (NULL); |
|
1406 g_option_context_add_main_entries (context, entries, NULL); |
|
1407 |
|
1408 /* Now try parsing */ |
|
1409 argv = split_string ("program foo --test -- -bar", &argc); |
|
1410 |
|
1411 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1412 g_assert (retval); |
|
1413 |
|
1414 /* Check array */ |
|
1415 g_assert (ignore_test1_boolean); |
|
1416 g_assert (strcmp (argv[0], "program") == 0); |
|
1417 g_assert (strcmp (argv[1], "foo") == 0); |
|
1418 g_assert (strcmp (argv[2], "--") == 0); |
|
1419 g_assert (strcmp (argv[3], "-bar") == 0); |
|
1420 g_assert (argv[4] == NULL); |
|
1421 |
|
1422 g_strfreev (argv); |
|
1423 g_option_context_free (context); |
|
1424 } |
|
1425 |
|
1426 /* check that -- stripping works */ |
|
1427 void |
|
1428 rest_test2a (void) |
|
1429 { |
|
1430 GOptionContext *context; |
|
1431 gboolean retval; |
|
1432 GError *error = NULL; |
|
1433 gchar **argv; |
|
1434 int argc; |
|
1435 #ifndef SYMBIAN |
|
1436 GOptionEntry entries [] = { |
|
1437 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1438 { NULL } |
|
1439 }; |
|
1440 #else |
|
1441 GOptionEntry entries [2]; |
|
1442 |
|
1443 entries[0].long_name = "test"; |
|
1444 entries[0].short_name = 0; |
|
1445 entries[0].flags = 0; |
|
1446 entries[0].arg = G_OPTION_ARG_NONE; |
|
1447 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1448 entries[0].description = NULL; |
|
1449 entries[0].arg_description = NULL; |
|
1450 |
|
1451 entries[1].long_name = NULL; |
|
1452 entries[1].short_name = 0; |
|
1453 entries[1].arg_data = NULL; |
|
1454 entries[1].description = NULL; |
|
1455 entries[1].arg_description = NULL; |
|
1456 #endif |
|
1457 |
|
1458 context = g_option_context_new (NULL); |
|
1459 g_option_context_add_main_entries (context, entries, NULL); |
|
1460 |
|
1461 /* Now try parsing */ |
|
1462 argv = split_string ("program foo --test -- bar", &argc); |
|
1463 |
|
1464 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1465 g_assert (retval); |
|
1466 |
|
1467 /* Check array */ |
|
1468 g_assert (ignore_test1_boolean); |
|
1469 g_assert (strcmp (argv[0], "program") == 0); |
|
1470 g_assert (strcmp (argv[1], "foo") == 0); |
|
1471 g_assert (strcmp (argv[2], "bar") == 0); |
|
1472 g_assert (argv[3] == NULL); |
|
1473 |
|
1474 g_strfreev (argv); |
|
1475 g_option_context_free (context); |
|
1476 } |
|
1477 |
|
1478 void |
|
1479 rest_test2b (void) |
|
1480 { |
|
1481 GOptionContext *context; |
|
1482 gboolean retval; |
|
1483 GError *error = NULL; |
|
1484 gchar **argv; |
|
1485 int argc; |
|
1486 #ifndef SYMBIAN |
|
1487 GOptionEntry entries [] = { |
|
1488 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1489 { NULL } |
|
1490 }; |
|
1491 #else |
|
1492 GOptionEntry entries [2]; |
|
1493 |
|
1494 entries[0].long_name = "test"; |
|
1495 entries[0].short_name = 0; |
|
1496 entries[0].flags = 0; |
|
1497 entries[0].arg = G_OPTION_ARG_NONE; |
|
1498 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1499 entries[0].description = NULL; |
|
1500 entries[0].arg_description = NULL; |
|
1501 |
|
1502 entries[1].long_name = NULL; |
|
1503 entries[1].short_name = 0; |
|
1504 entries[1].arg_data = NULL; |
|
1505 entries[1].description = NULL; |
|
1506 entries[1].arg_description = NULL; |
|
1507 #endif |
|
1508 |
|
1509 context = g_option_context_new (NULL); |
|
1510 g_option_context_set_ignore_unknown_options (context, TRUE); |
|
1511 g_option_context_add_main_entries (context, entries, NULL); |
|
1512 |
|
1513 /* Now try parsing */ |
|
1514 argv = split_string ("program foo --test -bar --", &argc); |
|
1515 |
|
1516 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1517 g_assert (retval); |
|
1518 |
|
1519 /* Check array */ |
|
1520 g_assert (ignore_test1_boolean); |
|
1521 g_assert (strcmp (argv[0], "program") == 0); |
|
1522 g_assert (strcmp (argv[1], "foo") == 0); |
|
1523 g_assert (strcmp (argv[2], "-bar") == 0); |
|
1524 g_assert (argv[3] == NULL); |
|
1525 |
|
1526 g_strfreev (argv); |
|
1527 g_option_context_free (context); |
|
1528 } |
|
1529 |
|
1530 void |
|
1531 rest_test2c (void) |
|
1532 { |
|
1533 GOptionContext *context; |
|
1534 gboolean retval; |
|
1535 GError *error = NULL; |
|
1536 gchar **argv; |
|
1537 int argc; |
|
1538 #ifndef SYMBIAN |
|
1539 GOptionEntry entries [] = { |
|
1540 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1541 { NULL } |
|
1542 }; |
|
1543 #else |
|
1544 GOptionEntry entries [2]; |
|
1545 |
|
1546 entries[0].long_name = "test"; |
|
1547 entries[0].short_name = 0; |
|
1548 entries[0].flags = 0; |
|
1549 entries[0].arg = G_OPTION_ARG_NONE; |
|
1550 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1551 entries[0].description = NULL; |
|
1552 entries[0].arg_description = NULL; |
|
1553 |
|
1554 entries[1].long_name = NULL; |
|
1555 entries[1].short_name = 0; |
|
1556 entries[1].arg_data = NULL; |
|
1557 entries[1].description = NULL; |
|
1558 entries[1].arg_description = NULL; |
|
1559 #endif |
|
1560 |
|
1561 context = g_option_context_new (NULL); |
|
1562 g_option_context_add_main_entries (context, entries, NULL); |
|
1563 |
|
1564 /* Now try parsing */ |
|
1565 argv = split_string ("program --test foo -- bar", &argc); |
|
1566 |
|
1567 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1568 g_assert (retval); |
|
1569 |
|
1570 /* Check array */ |
|
1571 g_assert (ignore_test1_boolean); |
|
1572 g_assert (strcmp (argv[0], "program") == 0); |
|
1573 g_assert (strcmp (argv[1], "foo") == 0); |
|
1574 g_assert (strcmp (argv[2], "bar") == 0); |
|
1575 g_assert (argv[3] == NULL); |
|
1576 |
|
1577 g_strfreev (argv); |
|
1578 g_option_context_free (context); |
|
1579 } |
|
1580 |
|
1581 void |
|
1582 rest_test2d (void) |
|
1583 { |
|
1584 GOptionContext *context; |
|
1585 gboolean retval; |
|
1586 GError *error = NULL; |
|
1587 gchar **argv; |
|
1588 int argc; |
|
1589 #ifndef SYMBIAN |
|
1590 GOptionEntry entries [] = { |
|
1591 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1592 { NULL } |
|
1593 }; |
|
1594 #else |
|
1595 GOptionEntry entries [2]; |
|
1596 |
|
1597 entries[0].long_name = "test"; |
|
1598 entries[0].short_name = 0; |
|
1599 entries[0].flags = 0; |
|
1600 entries[0].arg = G_OPTION_ARG_NONE; |
|
1601 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1602 entries[0].description = NULL; |
|
1603 entries[0].arg_description = NULL; |
|
1604 |
|
1605 entries[1].long_name = NULL; |
|
1606 entries[1].short_name = 0; |
|
1607 entries[1].arg_data = NULL; |
|
1608 entries[1].description = NULL; |
|
1609 entries[1].arg_description = NULL; |
|
1610 #endif |
|
1611 |
|
1612 context = g_option_context_new (NULL); |
|
1613 g_option_context_add_main_entries (context, entries, NULL); |
|
1614 |
|
1615 /* Now try parsing */ |
|
1616 argv = split_string ("program --test -- -bar", &argc); |
|
1617 |
|
1618 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1619 g_assert (retval); |
|
1620 |
|
1621 /* Check array */ |
|
1622 g_assert (ignore_test1_boolean); |
|
1623 g_assert (strcmp (argv[0], "program") == 0); |
|
1624 g_assert (strcmp (argv[1], "--") == 0); |
|
1625 g_assert (strcmp (argv[2], "-bar") == 0); |
|
1626 g_assert (argv[3] == NULL); |
|
1627 |
|
1628 g_strfreev (argv); |
|
1629 g_option_context_free (context); |
|
1630 } |
|
1631 |
|
1632 |
|
1633 /* check that G_OPTION_REMAINING collects non-option arguments */ |
|
1634 void |
|
1635 rest_test3 (void) |
|
1636 { |
|
1637 GOptionContext *context; |
|
1638 gboolean retval; |
|
1639 GError *error = NULL; |
|
1640 gchar **argv; |
|
1641 int argc; |
|
1642 #ifndef SYMBIAN |
|
1643 GOptionEntry entries [] = { |
|
1644 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1645 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL }, |
|
1646 { NULL } |
|
1647 }; |
|
1648 #else |
|
1649 GOptionEntry entries [3]; |
|
1650 |
|
1651 entries[0].long_name = "test"; |
|
1652 entries[0].short_name = 0; |
|
1653 entries[0].flags = 0; |
|
1654 entries[0].arg = G_OPTION_ARG_NONE; |
|
1655 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1656 entries[0].description = NULL; |
|
1657 entries[0].arg_description = NULL; |
|
1658 |
|
1659 |
|
1660 entries[1].long_name = G_OPTION_REMAINING; |
|
1661 entries[1].short_name = 0; |
|
1662 entries[1].flags = 0; |
|
1663 entries[1].arg = G_OPTION_ARG_STRING_ARRAY; |
|
1664 entries[1].arg_data = (gpointer)&array_test1_array; |
|
1665 entries[1].description = NULL; |
|
1666 entries[1].arg_description = NULL; |
|
1667 |
|
1668 entries[2].long_name = NULL; |
|
1669 entries[2].short_name = 0; |
|
1670 entries[2].arg_data = NULL; |
|
1671 entries[2].description = NULL; |
|
1672 entries[2].arg_description = NULL; |
|
1673 #endif |
|
1674 |
|
1675 context = g_option_context_new (NULL); |
|
1676 g_option_context_add_main_entries (context, entries, NULL); |
|
1677 |
|
1678 /* Now try parsing */ |
|
1679 argv = split_string ("program foo --test bar", &argc); |
|
1680 |
|
1681 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1682 g_assert (retval); |
|
1683 |
|
1684 /* Check array */ |
|
1685 g_assert (ignore_test1_boolean); |
|
1686 g_assert (strcmp (array_test1_array[0], "foo") == 0); |
|
1687 g_assert (strcmp (array_test1_array[1], "bar") == 0); |
|
1688 g_assert (array_test1_array[2] == NULL); |
|
1689 |
|
1690 g_strfreev (array_test1_array); |
|
1691 |
|
1692 g_strfreev (argv); |
|
1693 g_option_context_free (context); |
|
1694 } |
|
1695 |
|
1696 |
|
1697 /* check that G_OPTION_REMAINING and -- work together */ |
|
1698 void |
|
1699 rest_test4 (void) |
|
1700 { |
|
1701 GOptionContext *context; |
|
1702 gboolean retval; |
|
1703 GError *error = NULL; |
|
1704 gchar **argv; |
|
1705 int argc; |
|
1706 #ifndef SYMBIAN |
|
1707 GOptionEntry entries [] = { |
|
1708 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1709 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL }, |
|
1710 { NULL } |
|
1711 }; |
|
1712 #else |
|
1713 GOptionEntry entries [3]; |
|
1714 |
|
1715 entries[0].long_name = "test"; |
|
1716 entries[0].short_name = 0; |
|
1717 entries[0].flags = 0; |
|
1718 entries[0].arg = G_OPTION_ARG_NONE; |
|
1719 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1720 entries[0].description = NULL; |
|
1721 entries[0].arg_description = NULL; |
|
1722 |
|
1723 |
|
1724 entries[1].long_name = G_OPTION_REMAINING; |
|
1725 entries[1].short_name = 0; |
|
1726 entries[1].flags = 0; |
|
1727 entries[1].arg = G_OPTION_ARG_STRING_ARRAY; |
|
1728 entries[1].arg_data = (gpointer)&array_test1_array; |
|
1729 entries[1].description = NULL; |
|
1730 entries[1].arg_description = NULL; |
|
1731 |
|
1732 entries[2].long_name = NULL; |
|
1733 entries[2].short_name = 0; |
|
1734 entries[2].arg_data = NULL; |
|
1735 entries[2].description = NULL; |
|
1736 entries[2].arg_description = NULL; |
|
1737 #endif |
|
1738 |
|
1739 context = g_option_context_new (NULL); |
|
1740 g_option_context_add_main_entries (context, entries, NULL); |
|
1741 |
|
1742 /* Now try parsing */ |
|
1743 argv = split_string ("program foo --test -- -bar", &argc); |
|
1744 |
|
1745 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1746 g_assert (retval); |
|
1747 |
|
1748 /* Check array */ |
|
1749 g_assert (ignore_test1_boolean); |
|
1750 g_assert (strcmp (array_test1_array[0], "foo") == 0); |
|
1751 g_assert (strcmp (array_test1_array[1], "-bar") == 0); |
|
1752 g_assert (array_test1_array[2] == NULL); |
|
1753 |
|
1754 g_strfreev (array_test1_array); |
|
1755 |
|
1756 g_strfreev (argv); |
|
1757 g_option_context_free (context); |
|
1758 } |
|
1759 |
|
1760 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */ |
|
1761 void |
|
1762 rest_test5 (void) |
|
1763 { |
|
1764 GOptionContext *context; |
|
1765 gboolean retval; |
|
1766 GError *error = NULL; |
|
1767 gchar **argv; |
|
1768 int argc; |
|
1769 |
|
1770 #ifndef SYMBIAN |
|
1771 GOptionEntry entries [] = { |
|
1772 { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL }, |
|
1773 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL }, |
|
1774 { NULL } |
|
1775 }; |
|
1776 #else |
|
1777 GOptionEntry entries [3]; |
|
1778 |
|
1779 entries[0].long_name = "test"; |
|
1780 entries[0].short_name = 0; |
|
1781 entries[0].flags = 0; |
|
1782 entries[0].arg = G_OPTION_ARG_NONE; |
|
1783 entries[0].arg_data = (gpointer)&ignore_test1_boolean; |
|
1784 entries[0].description = NULL; |
|
1785 entries[0].arg_description = NULL; |
|
1786 |
|
1787 |
|
1788 entries[1].long_name = G_OPTION_REMAINING; |
|
1789 entries[1].short_name = 0; |
|
1790 entries[1].flags = 0; |
|
1791 entries[1].arg = G_OPTION_ARG_FILENAME_ARRAY; |
|
1792 entries[1].arg_data = (gpointer)&array_test1_array; |
|
1793 entries[1].description = NULL; |
|
1794 entries[1].arg_description = NULL; |
|
1795 |
|
1796 entries[2].long_name = NULL; |
|
1797 entries[2].short_name = 0; |
|
1798 entries[2].arg_data = NULL; |
|
1799 entries[2].description = NULL; |
|
1800 entries[2].arg_description = NULL; |
|
1801 #endif |
|
1802 |
|
1803 context = g_option_context_new (NULL); |
|
1804 g_option_context_add_main_entries (context, entries, NULL); |
|
1805 |
|
1806 /* Now try parsing */ |
|
1807 argv = split_string ("program foo --test bar", &argc); |
|
1808 |
|
1809 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1810 g_assert (retval); |
|
1811 |
|
1812 /* Check array */ |
|
1813 g_assert (ignore_test1_boolean); |
|
1814 g_assert (strcmp (array_test1_array[0], "foo") == 0); |
|
1815 g_assert (strcmp (array_test1_array[1], "bar") == 0); |
|
1816 g_assert (array_test1_array[2] == NULL); |
|
1817 |
|
1818 g_strfreev (array_test1_array); |
|
1819 |
|
1820 g_strfreev (argv); |
|
1821 g_option_context_free (context); |
|
1822 } |
|
1823 |
|
1824 void |
|
1825 unknown_short_test (void) |
|
1826 { |
|
1827 GOptionContext *context; |
|
1828 gboolean retval; |
|
1829 GError *error = NULL; |
|
1830 gchar **argv; |
|
1831 int argc; |
|
1832 #ifndef SYMBIAN |
|
1833 GOptionEntry entries [] = { { NULL } }; |
|
1834 #else |
|
1835 GOptionEntry entries [1]; |
|
1836 |
|
1837 entries[0].long_name = NULL; |
|
1838 entries[0].short_name = 0; |
|
1839 entries[0].arg_data = NULL; |
|
1840 entries[0].description = NULL; |
|
1841 entries[0].arg_description = NULL; |
|
1842 #endif |
|
1843 |
|
1844 context = g_option_context_new (NULL); |
|
1845 g_option_context_add_main_entries (context, entries, NULL); |
|
1846 |
|
1847 /* Now try parsing */ |
|
1848 argv = split_string ("program -0", &argc); |
|
1849 |
|
1850 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1851 g_assert (!retval); |
|
1852 |
|
1853 g_strfreev (argv); |
|
1854 g_option_context_free (context); |
|
1855 } |
|
1856 |
|
1857 /* test that lone dashes are treated as non-options */ |
|
1858 void lonely_dash_test (void) |
|
1859 { |
|
1860 GOptionContext *context; |
|
1861 gboolean retval; |
|
1862 GError *error = NULL; |
|
1863 gchar **argv; |
|
1864 int argc; |
|
1865 |
|
1866 context = g_option_context_new (NULL); |
|
1867 |
|
1868 /* Now try parsing */ |
|
1869 argv = split_string ("program -", &argc); |
|
1870 |
|
1871 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1872 |
|
1873 g_assert (retval); |
|
1874 |
|
1875 g_assert (argv[1] && strcmp (argv[1], "-") == 0); |
|
1876 |
|
1877 g_strfreev (argv); |
|
1878 g_option_context_free (context); |
|
1879 } |
|
1880 |
|
1881 void |
|
1882 missing_arg_test (void) |
|
1883 { |
|
1884 GOptionContext *context; |
|
1885 gboolean retval; |
|
1886 GError *error = NULL; |
|
1887 gchar **argv; |
|
1888 int argc; |
|
1889 gchar *arg = NULL; |
|
1890 #ifndef SYMBIAN |
|
1891 GOptionEntry entries [] = |
|
1892 { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL }, |
|
1893 { NULL } }; |
|
1894 #else |
|
1895 GOptionEntry entries [2]; |
|
1896 |
|
1897 entries[0].long_name = "test"; |
|
1898 entries[0].short_name = 't'; |
|
1899 entries[0].flags = 0; |
|
1900 entries[0].arg = G_OPTION_ARG_STRING; |
|
1901 entries[0].arg_data = (gpointer)&arg; |
|
1902 entries[0].description = NULL; |
|
1903 entries[0].arg_description = NULL; |
|
1904 |
|
1905 entries[1].long_name = NULL; |
|
1906 entries[1].short_name = 0; |
|
1907 entries[1].arg_data = NULL; |
|
1908 entries[1].description = NULL; |
|
1909 entries[1].arg_description = NULL; |
|
1910 #endif |
|
1911 |
|
1912 context = g_option_context_new (NULL); |
|
1913 g_option_context_add_main_entries (context, entries, NULL); |
|
1914 |
|
1915 /* Now try parsing */ |
|
1916 argv = split_string ("program --test", &argc); |
|
1917 |
|
1918 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1919 g_assert (retval == FALSE); |
|
1920 g_clear_error (&error); |
|
1921 |
|
1922 g_strfreev (argv); |
|
1923 |
|
1924 /* Try parsing again */ |
|
1925 argv = split_string ("program --t", &argc); |
|
1926 |
|
1927 retval = g_option_context_parse (context, &argc, &argv, &error); |
|
1928 g_assert (retval == FALSE); |
|
1929 |
|
1930 g_strfreev (argv); |
|
1931 g_option_context_free (context); |
|
1932 } |
|
1933 |
|
1934 int |
|
1935 main (int argc, char **argv) |
|
1936 { |
|
1937 #ifdef SYMBIAN |
|
1938 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); |
|
1939 g_set_print_handler(mrtPrintHandler); |
|
1940 #endif /*SYMBIAN*/ |
|
1941 /* Test that restoration on failure works */ |
|
1942 error_test1_int = 0x12345678; |
|
1943 error_test1 (); |
|
1944 error_test2_string = "foo"; |
|
1945 error_test2 (); |
|
1946 error_test3_boolean = FALSE; |
|
1947 error_test3 (); |
|
1948 |
|
1949 /* Test that special argument parsing works */ |
|
1950 arg_test1 (); |
|
1951 arg_test2 (); |
|
1952 arg_test3 (); |
|
1953 |
|
1954 /* Test string arrays */ |
|
1955 array_test1 (); |
|
1956 |
|
1957 /* Test callback args */ |
|
1958 callback_test1 (); |
|
1959 callback_test2 (); |
|
1960 |
|
1961 /* Test optional arg flag for callback */ |
|
1962 callback_test_optional_1 (); |
|
1963 callback_test_optional_2 (); |
|
1964 callback_test_optional_3 (); |
|
1965 callback_test_optional_4 (); |
|
1966 callback_test_optional_5 (); |
|
1967 callback_test_optional_6 (); |
|
1968 callback_test_optional_7 (); |
|
1969 callback_test_optional_8 (); |
|
1970 |
|
1971 /* Test ignoring options */ |
|
1972 ignore_test1 (); |
|
1973 ignore_test2 (); |
|
1974 ignore_test3 (); |
|
1975 |
|
1976 add_test1 (); |
|
1977 |
|
1978 /* Test parsing empty args */ |
|
1979 empty_test1 (); |
|
1980 empty_test2 (); |
|
1981 empty_test3 (); |
|
1982 |
|
1983 /* Test handling of rest args */ |
|
1984 rest_test1 (); |
|
1985 rest_test2 (); |
|
1986 rest_test2a (); |
|
1987 rest_test2b (); |
|
1988 rest_test2c (); |
|
1989 rest_test2d (); |
|
1990 rest_test3 (); |
|
1991 rest_test4 (); |
|
1992 rest_test5 (); |
|
1993 |
|
1994 /* test for bug 166609 */ |
|
1995 unknown_short_test (); |
|
1996 |
|
1997 /* test for bug 168008 */ |
|
1998 lonely_dash_test (); |
|
1999 |
|
2000 /* test for bug 305576 */ |
|
2001 missing_arg_test (); |
|
2002 |
|
2003 #ifdef SYMBIAN |
|
2004 testResultXml("option-test"); |
|
2005 #endif /* EMULATOR */ |
|
2006 return 0; |
|
2007 } |