Bug 556186 – gpoll.h breaks gmain.h inclusion
[platform/upstream/glib.git] / glib / tests / option-context.c
1 /* Unit tests for GOptionContext
2  * Copyright (C) 2007 Openismus GmbH
3  * Authors: Mathias Hasselmann
4  *
5  * This work is provided "as is"; redistribution and modification
6  * in whole or in part, in any medium, physical or electronic is
7  * permitted without restriction.
8  *
9  * This work 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.
12  *
13  * In no event shall the authors or contributors be liable for any
14  * direct, indirect, incidental, special, exemplary, or consequential
15  * damages (including, but not limited to, procurement of substitute
16  * goods or services; loss of use, data, or profits; or business
17  * interruption) however caused and on any theory of liability, whether
18  * in contract, strict liability, or tort (including negligence or
19  * otherwise) arising in any way out of the use of this software, even
20  * if advised of the possibility of such damage.
21  */
22
23 #include <glib.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <locale.h>
29
30 static void
31 group_captions (void)
32 {
33   gchar *help_variants[] = { "--help", "--help-all", "--help-test" };
34
35   GOptionEntry main_entries[] = {
36     { "main-switch", 0,
37       G_OPTION_FLAG_NO_ARG,
38       G_OPTION_ARG_NONE, NULL,
39       "A switch that is in the main group", NULL },
40     { NULL }
41   };
42
43   GOptionEntry group_entries[] = {
44     { "test-switch", 0,
45       G_OPTION_FLAG_NO_ARG,
46       G_OPTION_ARG_NONE, NULL,
47       "A switch that is in the test group", NULL },
48     { NULL }
49   };
50
51   gint i, j;
52
53   g_test_bug ("504142");
54
55   for (i = 0; i < 4; ++i)
56     {
57       gboolean have_main_entries = (0 != (i & 1));
58       gboolean have_test_entries = (0 != (i & 2));
59
60       GOptionContext *options;
61       GOptionGroup   *group = NULL;
62
63       options = g_option_context_new (NULL);
64
65       if (have_main_entries)
66         g_option_context_add_main_entries (options, main_entries, NULL);
67       if (have_test_entries)
68         {
69           group = g_option_group_new ("test", "Test Options",
70                                       "Show all test options",
71                                       NULL, NULL);
72           g_option_context_add_group (options, group);
73           g_option_group_add_entries (group, group_entries);
74         }
75
76       for (j = 0; j < G_N_ELEMENTS (help_variants); ++j)
77         {
78           GTestTrapFlags trap_flags = 0;
79           gchar *args[3];
80
81           args[0] = __FILE__;
82           args[1] = help_variants[j];
83           args[2] = NULL;
84
85           if (!g_test_verbose ())
86             trap_flags |= G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR;
87
88           g_test_message ("test setup: args='%s', main-entries=%d, test-entries=%d",
89                           args[1], have_main_entries, have_test_entries);
90
91           if (g_test_trap_fork (0, trap_flags))
92             {
93               gchar **argv = args;
94               gint    argc = 2;
95               GError *error = NULL;
96
97               g_setenv ("LANG", "C", TRUE);
98
99               g_option_context_parse (options, &argc, &argv, &error);
100               exit(0);
101             }
102           else
103             {
104               gboolean expect_main_description = FALSE;
105               gboolean expect_main_switch      = FALSE;
106
107               gboolean expect_test_description = FALSE;
108               gboolean expect_test_switch      = FALSE;
109               gboolean expect_test_group       = FALSE;
110
111               g_test_trap_assert_passed ();
112               g_test_trap_assert_stderr ("");
113
114               switch (j)
115                 {
116                   case 0:
117                     g_assert_cmpstr ("--help", ==, args[1]);
118                     expect_main_switch = have_main_entries;
119                     expect_test_group  = have_test_entries;
120                     break;
121
122                   case 1:
123                     g_assert_cmpstr ("--help-all", ==, args[1]);
124                     expect_main_switch = have_main_entries;
125                     expect_test_switch = have_test_entries;
126                     expect_test_group  = have_test_entries;
127                     break;
128
129                   case 2:
130                     g_assert_cmpstr ("--help-test", ==, args[1]);
131                     expect_test_switch = have_test_entries;
132                     break;
133
134                   default:
135                     g_assert_not_reached ();
136                     break;
137                 }
138
139               expect_main_description |= expect_main_switch;
140               expect_test_description |= expect_test_switch;
141
142               if (expect_main_description)
143                 g_test_trap_assert_stdout           ("*Application Options*");
144               else
145                 g_test_trap_assert_stdout_unmatched ("*Application Options*");
146               if (expect_main_switch)
147                 g_test_trap_assert_stdout           ("*--main-switch*");
148               else
149                 g_test_trap_assert_stdout_unmatched ("*--main-switch*");
150
151               if (expect_test_description)
152                 g_test_trap_assert_stdout           ("*Test Options*");
153               else
154                 g_test_trap_assert_stdout_unmatched ("*Test Options*");
155               if (expect_test_switch)
156                 g_test_trap_assert_stdout           ("*--test-switch*");
157               else
158                 g_test_trap_assert_stdout_unmatched ("*--test-switch*");
159
160               if (expect_test_group)
161                 g_test_trap_assert_stdout           ("*--help-test*");
162               else
163                 g_test_trap_assert_stdout_unmatched ("*--help-test*");
164             }
165         }
166     }
167 }
168
169 int error_test1_int;
170 char *error_test2_string;
171 gboolean error_test3_boolean;
172
173 int arg_test1_int;
174 gchar *arg_test2_string;
175 gchar *arg_test3_filename;
176 gdouble arg_test4_double;
177 gdouble arg_test5_double;
178 gint64 arg_test6_int64;
179 gint64 arg_test6_int64_2;
180
181 gchar *callback_test1_string;
182 int callback_test2_int;
183
184 gchar *callback_test_optional_string;
185 gboolean callback_test_optional_boolean;
186
187 gchar **array_test1_array;
188
189 gboolean ignore_test1_boolean;
190 gboolean ignore_test2_boolean;
191 gchar *ignore_test3_string;
192
193 gchar **
194 split_string (const char *str, int *argc)
195 {
196   gchar **argv;
197   int len;
198   
199   argv = g_strsplit (str, " ", 0);
200
201   for (len = 0; argv[len] != NULL; len++);
202
203   if (argc)
204     *argc = len;
205     
206   return argv;
207 }
208
209 gchar *
210 join_stringv (int argc, char **argv)
211 {
212   int i;
213   GString *str;
214
215   str = g_string_new (NULL);
216
217   for (i = 0; i < argc; i++)
218     {
219       g_string_append (str, argv[i]);
220
221       if (i < argc - 1)
222         g_string_append_c (str, ' ');
223     }
224
225   return g_string_free (str, FALSE);
226 }
227
228 /* Performs a shallow copy */
229 char **
230 copy_stringv (char **argv, int argc)
231 {
232   return g_memdup (argv, sizeof (char *) * (argc + 1));
233 }
234
235
236 static gboolean
237 error_test1_pre_parse (GOptionContext *context,
238                        GOptionGroup   *group,
239                        gpointer        data,
240                        GError        **error)
241 {
242   g_assert (error_test1_int == 0x12345678);
243
244   return TRUE;
245 }
246
247 static gboolean
248 error_test1_post_parse (GOptionContext *context,
249                         GOptionGroup   *group,
250                         gpointer          data,
251                         GError        **error)
252 {
253   g_assert (error_test1_int == 20);
254
255   /* Set an error in the post hook */
256   g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
257
258   return FALSE;
259 }
260
261 void
262 error_test1 (void)
263 {
264   GOptionContext *context;
265   gboolean retval;
266   GError *error = NULL;
267   gchar **argv;
268   int argc;
269   GOptionGroup *main_group;
270   GOptionEntry entries [] =
271     { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
272       { NULL } };
273   
274   error_test1_int = 0x12345678;
275
276   context = g_option_context_new (NULL);
277   g_option_context_add_main_entries (context, entries, NULL);
278
279   /* Set pre and post parse hooks */
280   main_group = g_option_context_get_main_group (context);
281   g_option_group_set_parse_hooks (main_group,
282                                   error_test1_pre_parse, error_test1_post_parse);
283   
284   /* Now try parsing */
285   argv = split_string ("program --test 20", &argc);
286
287   retval = g_option_context_parse (context, &argc, &argv, &error);
288   g_assert (retval == FALSE);
289
290   /* On failure, values should be reset */
291   g_assert (error_test1_int == 0x12345678);
292   
293   g_strfreev (argv);
294   g_option_context_free (context);
295 }
296
297 static gboolean
298 error_test2_pre_parse (GOptionContext *context,
299                        GOptionGroup   *group,
300                        gpointer   data,
301                        GError        **error)
302 {
303   g_assert (strcmp (error_test2_string, "foo") == 0);
304
305   return TRUE;
306 }
307
308 static gboolean
309 error_test2_post_parse (GOptionContext *context,
310                         GOptionGroup   *group,
311                         gpointer          data,
312                         GError        **error)
313 {
314   g_assert (strcmp (error_test2_string, "bar") == 0);
315
316   /* Set an error in the post hook */
317   g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
318
319   return FALSE;
320 }
321
322 void
323 error_test2 (void)
324 {
325   GOptionContext *context;
326   gboolean retval;
327   GError *error = NULL;
328   gchar **argv;
329   int argc;
330   GOptionGroup *main_group;
331   GOptionEntry entries [] =
332     { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
333       { NULL } };
334
335   error_test2_string = "foo";
336
337   context = g_option_context_new (NULL);
338   g_option_context_add_main_entries (context, entries, NULL);
339
340   /* Set pre and post parse hooks */
341   main_group = g_option_context_get_main_group (context);
342   g_option_group_set_parse_hooks (main_group,
343                                   error_test2_pre_parse, error_test2_post_parse);
344   
345   /* Now try parsing */
346   argv = split_string ("program --test bar", &argc);
347   retval = g_option_context_parse (context, &argc, &argv, &error);
348
349   g_error_free (error);
350   g_assert (retval == FALSE);
351
352   g_assert (strcmp (error_test2_string, "foo") == 0);
353   
354   g_strfreev (argv);
355   g_option_context_free (context);
356 }
357
358 static gboolean
359 error_test3_pre_parse (GOptionContext *context,
360                        GOptionGroup   *group,
361                        gpointer   data,
362                        GError        **error)
363 {
364   g_assert (!error_test3_boolean);
365
366   return TRUE;
367 }
368
369 static gboolean
370 error_test3_post_parse (GOptionContext *context,
371                         GOptionGroup   *group,
372                         gpointer          data,
373                         GError        **error)
374 {
375   g_assert (error_test3_boolean);
376
377   /* Set an error in the post hook */
378   g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");
379
380   return FALSE;
381 }
382
383 void
384 error_test3 (void)
385 {
386   GOptionContext *context;
387   gboolean retval;
388   GError *error = NULL;
389   gchar **argv;
390   int argc;
391   GOptionGroup *main_group;
392   GOptionEntry entries [] =
393     { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
394       { NULL } };
395
396   error_test3_boolean = FALSE;
397
398   context = g_option_context_new (NULL);
399   g_option_context_add_main_entries (context, entries, NULL);
400
401   /* Set pre and post parse hooks */
402   main_group = g_option_context_get_main_group (context);
403   g_option_group_set_parse_hooks (main_group,
404                                   error_test3_pre_parse, error_test3_post_parse);
405   
406   /* Now try parsing */
407   argv = split_string ("program --test", &argc);
408   retval = g_option_context_parse (context, &argc, &argv, &error);
409
410   g_error_free (error);
411   g_assert (retval == FALSE);
412
413   g_assert (!error_test3_boolean);
414   
415   g_strfreev (argv);
416   g_option_context_free (context);
417 }
418
419 static void
420 assert_no_error (GError *error)
421 {
422   if (error) 
423     {
424       fprintf (stderr, "unexpected error: %s, %d, %s\n", g_quark_to_string (error->domain), error->code, error->message);
425       exit (1);
426     }
427 }
428
429 void
430 arg_test1 (void)
431 {
432   GOptionContext *context;
433   gboolean retval;
434   GError *error = NULL;
435   gchar **argv;
436   int argc;
437   GOptionEntry entries [] =
438     { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
439       { NULL } };
440
441   context = g_option_context_new (NULL);
442   g_option_context_add_main_entries (context, entries, NULL);
443
444   /* Now try parsing */
445   argv = split_string ("program --test 20 --test 30", &argc);
446
447   retval = g_option_context_parse (context, &argc, &argv, &error);
448   assert_no_error (error);
449   g_assert (retval);
450
451   /* Last arg specified is the one that should be stored */
452   g_assert (arg_test1_int == 30);
453
454   g_strfreev (argv);
455   g_option_context_free (context);
456 }
457
458 void
459 arg_test2 (void)
460 {
461   GOptionContext *context;
462   gboolean retval;
463   GError *error = NULL;
464   gchar **argv;
465   int argc;
466   GOptionEntry entries [] =
467     { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
468       { NULL } };
469   
470   context = g_option_context_new (NULL);
471   g_option_context_add_main_entries (context, entries, NULL);
472
473   /* Now try parsing */
474   argv = split_string ("program --test foo --test bar", &argc);
475   
476   retval = g_option_context_parse (context, &argc, &argv, &error);
477   assert_no_error (error);
478   g_assert (retval);
479
480   /* Last arg specified is the one that should be stored */
481   g_assert (strcmp (arg_test2_string, "bar") == 0);
482
483   g_free (arg_test2_string);
484   
485   g_strfreev (argv);
486   g_option_context_free (context);
487 }
488
489 void
490 arg_test3 (void)
491 {
492   GOptionContext *context;
493   gboolean retval;
494   GError *error = NULL;
495   gchar **argv;
496   int argc;
497   GOptionEntry entries [] =
498     { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
499       { NULL } };
500   
501   context = g_option_context_new (NULL);
502   g_option_context_add_main_entries (context, entries, NULL);
503
504   /* Now try parsing */
505   argv = split_string ("program --test foo.txt", &argc);
506   
507   retval = g_option_context_parse (context, &argc, &argv, &error);
508   assert_no_error (error);
509   g_assert (retval);
510
511   /* Last arg specified is the one that should be stored */
512   g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);
513
514   g_free (arg_test3_filename);
515   
516   g_strfreev (argv);
517   g_option_context_free (context);
518 }
519
520
521 void
522 arg_test4 (void)
523 {
524   GOptionContext *context;
525   gboolean retval;
526   GError *error = NULL;
527   gchar **argv;
528   int argc;
529   GOptionEntry entries [] =
530     { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, NULL, NULL },
531       { NULL } };
532
533   context = g_option_context_new (NULL);
534   g_option_context_add_main_entries (context, entries, NULL);
535
536   /* Now try parsing */
537   argv = split_string ("program --test 20.0 --test 30.03", &argc);
538
539   retval = g_option_context_parse (context, &argc, &argv, &error);
540   assert_no_error (error);
541   g_assert (retval);
542
543   /* Last arg specified is the one that should be stored */
544   g_assert (arg_test4_double == 30.03);
545
546   g_strfreev (argv);
547   g_option_context_free (context);
548 }
549
550 void
551 arg_test5 (void)
552 {
553   GOptionContext *context;
554   gboolean retval;
555   GError *error = NULL;
556   gchar **argv;
557   int argc;
558   char *old_locale, *current_locale;
559   const char *locale = "de_DE";
560   GOptionEntry entries [] =
561     { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test5_double, NULL, NULL },
562       { NULL } };
563
564   context = g_option_context_new (NULL);
565   g_option_context_add_main_entries (context, entries, NULL);
566
567   /* Now try parsing */
568   argv = split_string ("program --test 20,0 --test 30,03", &argc);
569
570   /* set it to some locale that uses commas instead of decimal points */
571   
572   old_locale = g_strdup (setlocale (LC_NUMERIC, locale));
573   current_locale = setlocale (LC_NUMERIC, NULL);
574   if (strcmp (current_locale, locale) != 0)
575     {
576       fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
577       goto cleanup; 
578     }
579
580   retval = g_option_context_parse (context, &argc, &argv, &error);
581   assert_no_error (error);
582   g_assert (retval);
583
584   /* Last arg specified is the one that should be stored */
585   g_assert (arg_test5_double == 30.03);
586
587  cleanup:
588   setlocale (LC_NUMERIC, old_locale);
589   g_free (old_locale);
590
591   g_strfreev (argv);
592   g_option_context_free (context);
593 }
594
595 void
596 arg_test6 (void)
597 {
598   GOptionContext *context;
599   gboolean retval;
600   GError *error = NULL;
601   gchar **argv;
602   int argc;
603   GOptionEntry entries [] =
604     { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL },
605       { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, NULL, NULL },
606       { NULL } };
607
608   context = g_option_context_new (NULL);
609   g_option_context_add_main_entries (context, entries, NULL);
610
611   /* Now try parsing */
612   argv = split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc);
613
614   retval = g_option_context_parse (context, &argc, &argv, &error);
615   assert_no_error (error);
616   g_assert (retval);
617
618   /* Last arg specified is the one that should be stored */
619   g_assert (arg_test6_int64 == G_GINT64_CONSTANT(4294967296));
620   g_assert (arg_test6_int64_2 == G_GINT64_CONSTANT(0xfffffffff));
621
622   g_strfreev (argv);
623   g_option_context_free (context);
624 }
625
626 static gboolean
627 callback_parse1 (const gchar *option_name, const gchar *value,
628                  gpointer data, GError **error)
629 {
630         callback_test1_string = g_strdup (value);
631         return TRUE;
632 }
633
634 void
635 callback_test1 (void)
636 {
637   GOptionContext *context;
638   gboolean retval;
639   GError *error = NULL;
640   gchar **argv;
641   int argc;
642   GOptionEntry entries [] =
643     { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL },
644       { NULL } };
645   
646   context = g_option_context_new (NULL);
647   g_option_context_add_main_entries (context, entries, NULL);
648
649   /* Now try parsing */
650   argv = split_string ("program --test foo.txt", &argc);
651   
652   retval = g_option_context_parse (context, &argc, &argv, &error);
653   assert_no_error (error);
654   g_assert (retval);
655
656   g_assert (strcmp (callback_test1_string, "foo.txt") == 0);
657
658   g_free (callback_test1_string);
659   
660   g_strfreev (argv);
661   g_option_context_free (context);
662 }
663
664 static gboolean
665 callback_parse2 (const gchar *option_name, const gchar *value,
666                  gpointer data, GError **error)
667 {
668         callback_test2_int++;
669         return TRUE;
670 }
671
672 void
673 callback_test2 (void)
674 {
675   GOptionContext *context;
676   gboolean retval;
677   GError *error = NULL;
678   gchar **argv;
679   int argc;
680   GOptionEntry entries [] =
681     { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL },
682       { NULL } };
683   
684   context = g_option_context_new (NULL);
685   g_option_context_add_main_entries (context, entries, NULL);
686
687   /* Now try parsing */
688   argv = split_string ("program --test --test", &argc);
689   
690   retval = g_option_context_parse (context, &argc, &argv, &error);
691   assert_no_error (error);
692   g_assert (retval);
693
694   g_assert (callback_test2_int == 2);
695   
696   g_strfreev (argv);
697   g_option_context_free (context);
698 }
699
700 static gboolean
701 callback_parse_optional (const gchar *option_name, const gchar *value,
702                  gpointer data, GError **error)
703 {
704         callback_test_optional_boolean = TRUE;
705         if (value)
706                 callback_test_optional_string = g_strdup (value);
707         else
708                 callback_test_optional_string = NULL;
709         return TRUE;
710 }
711
712 void
713 callback_test_optional_1 (void)
714 {
715   GOptionContext *context;
716   gboolean retval;
717   GError *error = NULL;
718   gchar **argv;
719   int argc;
720   GOptionEntry entries [] =
721     { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
722         callback_parse_optional, NULL, NULL },
723       { NULL } };
724   
725   context = g_option_context_new (NULL);
726   g_option_context_add_main_entries (context, entries, NULL);
727
728   /* Now try parsing */
729   argv = split_string ("program --test foo.txt", &argc);
730   
731   retval = g_option_context_parse (context, &argc, &argv, &error);
732   assert_no_error (error);
733   g_assert (retval);
734
735   g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
736   
737   g_assert (callback_test_optional_boolean);
738
739   g_free (callback_test_optional_string);
740   
741   g_strfreev (argv);
742   g_option_context_free (context);
743 }
744
745 void
746 callback_test_optional_2 (void)
747 {
748   GOptionContext *context;
749   gboolean retval;
750   GError *error = NULL;
751   gchar **argv;
752   int argc;
753   GOptionEntry entries [] =
754     { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
755         callback_parse_optional, NULL, NULL },
756       { NULL } };
757   
758   context = g_option_context_new (NULL);
759   g_option_context_add_main_entries (context, entries, NULL);
760
761   /* Now try parsing */
762   argv = split_string ("program --test", &argc);
763   
764   retval = g_option_context_parse (context, &argc, &argv, &error);
765   assert_no_error (error);
766   g_assert (retval);
767
768   g_assert (callback_test_optional_string == NULL);
769   
770   g_assert (callback_test_optional_boolean);
771
772   g_free (callback_test_optional_string);
773   
774   g_strfreev (argv);
775   g_option_context_free (context);
776 }
777
778 void
779 callback_test_optional_3 (void)
780 {
781   GOptionContext *context;
782   gboolean retval;
783   GError *error = NULL;
784   gchar **argv;
785   int argc;
786   GOptionEntry entries [] =
787     { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
788         callback_parse_optional, NULL, NULL },
789       { NULL } };
790   
791   context = g_option_context_new (NULL);
792   g_option_context_add_main_entries (context, entries, NULL);
793
794   /* Now try parsing */
795   argv = split_string ("program -t foo.txt", &argc);
796   
797   retval = g_option_context_parse (context, &argc, &argv, &error);
798   assert_no_error (error);
799   g_assert (retval);
800
801   g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
802   
803   g_assert (callback_test_optional_boolean);
804
805   g_free (callback_test_optional_string);
806   
807   g_strfreev (argv);
808   g_option_context_free (context);
809 }
810
811
812 void
813 callback_test_optional_4 (void)
814 {
815   GOptionContext *context;
816   gboolean retval;
817   GError *error = NULL;
818   gchar **argv;
819   int argc;
820   GOptionEntry entries [] =
821     { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
822         callback_parse_optional, NULL, NULL },
823       { NULL } };
824   
825   context = g_option_context_new (NULL);
826   g_option_context_add_main_entries (context, entries, NULL);
827
828   /* Now try parsing */
829   argv = split_string ("program -t", &argc);
830   
831   retval = g_option_context_parse (context, &argc, &argv, &error);
832   assert_no_error (error);
833   g_assert (retval);
834
835   g_assert (callback_test_optional_string == NULL);
836   
837   g_assert (callback_test_optional_boolean);
838
839   g_free (callback_test_optional_string);
840   
841   g_strfreev (argv);
842   g_option_context_free (context);
843 }
844
845 void
846 callback_test_optional_5 (void)
847 {
848   GOptionContext *context;
849   gboolean dummy;
850   gboolean retval;
851   GError *error = NULL;
852   gchar **argv;
853   int argc;
854   GOptionEntry entries [] =
855     { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
856       { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
857         callback_parse_optional, NULL, NULL },
858       { NULL } };
859   
860   context = g_option_context_new (NULL);
861   g_option_context_add_main_entries (context, entries, NULL);
862
863   /* Now try parsing */
864   argv = split_string ("program --test --dummy", &argc);
865   
866   retval = g_option_context_parse (context, &argc, &argv, &error);
867   assert_no_error (error);
868   g_assert (retval);
869
870   g_assert (callback_test_optional_string == NULL);
871   
872   g_assert (callback_test_optional_boolean);
873
874   g_free (callback_test_optional_string);
875   
876   g_strfreev (argv);
877   g_option_context_free (context);
878 }
879
880 void
881 callback_test_optional_6 (void)
882 {
883   GOptionContext *context;
884   gboolean dummy;
885   gboolean retval;
886   GError *error = NULL;
887   gchar **argv;
888   int argc;
889   GOptionEntry entries [] =
890     { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
891       { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
892         callback_parse_optional, NULL, NULL },
893       { NULL } };
894   
895   context = g_option_context_new (NULL);
896   g_option_context_add_main_entries (context, entries, NULL);
897
898   /* Now try parsing */
899   argv = split_string ("program -t -d", &argc);
900   
901   retval = g_option_context_parse (context, &argc, &argv, &error);
902   assert_no_error (error);
903   g_assert (retval);
904
905   g_assert (callback_test_optional_string == NULL);
906   
907   g_assert (callback_test_optional_boolean);
908
909   g_free (callback_test_optional_string);
910   
911   g_strfreev (argv);
912   g_option_context_free (context);
913 }
914
915 void
916 callback_test_optional_7 (void)
917 {
918   GOptionContext *context;
919   gboolean dummy;
920   gboolean retval;
921   GError *error = NULL;
922   gchar **argv;
923   int argc;
924   GOptionEntry entries [] =
925     { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
926       { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
927         callback_parse_optional, NULL, NULL },
928       { NULL } };
929   
930   context = g_option_context_new (NULL);
931   g_option_context_add_main_entries (context, entries, NULL);
932
933   /* Now try parsing */
934   argv = split_string ("program -td", &argc);
935   
936   retval = g_option_context_parse (context, &argc, &argv, &error);
937   assert_no_error (error);
938   g_assert (retval);
939
940   g_assert (callback_test_optional_string == NULL);
941   
942   g_assert (callback_test_optional_boolean);
943
944   g_free (callback_test_optional_string);
945   
946   g_strfreev (argv);
947   g_option_context_free (context);
948 }
949
950 void
951 callback_test_optional_8 (void)
952 {
953   GOptionContext *context;
954   gboolean dummy;
955   gboolean retval;
956   GError *error = NULL;
957   gchar **argv;
958   int argc;
959   GOptionEntry entries [] =
960     { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
961       { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
962         callback_parse_optional, NULL, NULL },
963       { NULL } };
964   
965   context = g_option_context_new (NULL);
966   g_option_context_add_main_entries (context, entries, NULL);
967
968   /* Now try parsing */
969   argv = split_string ("program -dt foo.txt", &argc);
970   
971   retval = g_option_context_parse (context, &argc, &argv, &error);
972   assert_no_error (error);
973   g_assert (retval);
974
975   g_assert (callback_test_optional_string);
976   
977   g_assert (callback_test_optional_boolean);
978
979   g_free (callback_test_optional_string);
980   
981   g_strfreev (argv);
982   g_option_context_free (context);
983 }
984
985 static GPtrArray *callback_remaining_args;
986 static gboolean
987 callback_remaining_test1_callback (const gchar *option_name, const gchar *value,
988                          gpointer data, GError **error)
989 {
990         g_ptr_array_add (callback_remaining_args, g_strdup (value));
991         return TRUE;
992 }
993
994 void
995 callback_remaining_test1 (void)
996 {
997   GOptionContext *context;
998   gboolean retval;
999   GError *error = NULL;
1000   gchar **argv;
1001   int argc;
1002   GOptionEntry entries [] =
1003     { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL },
1004       { NULL } };
1005   
1006   callback_remaining_args = g_ptr_array_new ();
1007   context = g_option_context_new (NULL);
1008   g_option_context_add_main_entries (context, entries, NULL);
1009
1010   /* Now try parsing */
1011   argv = split_string ("program foo.txt blah.txt", &argc);
1012   
1013   retval = g_option_context_parse (context, &argc, &argv, &error);
1014   assert_no_error (error);
1015   g_assert (retval);
1016
1017   g_assert (callback_remaining_args->len == 2);
1018   g_assert (strcmp (callback_remaining_args->pdata[0], "foo.txt") == 0);
1019   g_assert (strcmp (callback_remaining_args->pdata[1], "blah.txt") == 0);
1020
1021   g_ptr_array_foreach (callback_remaining_args, (GFunc) g_free, NULL);
1022   g_ptr_array_free (callback_remaining_args, TRUE);
1023   
1024   g_strfreev (argv);
1025   g_option_context_free (context);
1026 }
1027
1028 void
1029 ignore_test1 (void)
1030 {
1031   GOptionContext *context;
1032   gboolean retval;
1033   GError *error = NULL;
1034   gchar **argv, **argv_copy;
1035   int argc;
1036   gchar *arg;
1037   GOptionEntry entries [] =
1038     { { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1039       { NULL } };
1040
1041   context = g_option_context_new (NULL);
1042   g_option_context_set_ignore_unknown_options (context, TRUE);
1043   g_option_context_add_main_entries (context, entries, NULL);
1044
1045   /* Now try parsing */
1046   argv = split_string ("program --test --hello", &argc);
1047   argv_copy = copy_stringv (argv, argc);
1048   
1049   retval = g_option_context_parse (context, &argc, &argv, &error);
1050   assert_no_error (error);
1051   g_assert (retval);
1052
1053   /* Check array */
1054   arg = join_stringv (argc, argv);
1055   g_assert (strcmp (arg, "program --hello") == 0);
1056
1057   g_free (arg);
1058   g_strfreev (argv_copy);
1059   g_free (argv);
1060   g_option_context_free (context);
1061 }
1062
1063 void
1064 ignore_test2 (void)
1065 {
1066   GOptionContext *context;
1067   gboolean retval;
1068   GError *error = NULL;
1069   gchar **argv;
1070   int argc;
1071   gchar *arg;
1072   GOptionEntry entries [] =
1073     { { "test", 't', 0, G_OPTION_ARG_NONE, &ignore_test2_boolean, NULL, NULL },
1074       { NULL } };
1075
1076   context = g_option_context_new (NULL);
1077   g_option_context_set_ignore_unknown_options (context, TRUE);
1078   g_option_context_add_main_entries (context, entries, NULL);
1079
1080   /* Now try parsing */
1081   argv = split_string ("program -test", &argc);
1082   
1083   retval = g_option_context_parse (context, &argc, &argv, &error);
1084   assert_no_error (error);
1085   g_assert (retval);
1086
1087   /* Check array */
1088   arg = join_stringv (argc, argv);
1089   g_assert (strcmp (arg, "program -es") == 0);
1090
1091   g_free (arg);
1092   g_strfreev (argv);
1093   g_option_context_free (context);
1094 }
1095
1096 void
1097 ignore_test3 (void)
1098 {
1099   GOptionContext *context;
1100   gboolean retval;
1101   GError *error = NULL;
1102   gchar **argv, **argv_copy;
1103   int argc;
1104   gchar *arg;
1105   GOptionEntry entries [] =
1106     { { "test", 0, 0, G_OPTION_ARG_STRING, &ignore_test3_string, NULL, NULL },
1107       { NULL } };
1108
1109   context = g_option_context_new (NULL);
1110   g_option_context_set_ignore_unknown_options (context, TRUE);
1111   g_option_context_add_main_entries (context, entries, NULL);
1112
1113   /* Now try parsing */
1114   argv = split_string ("program --test foo --hello", &argc);
1115   argv_copy = copy_stringv (argv, argc);
1116   
1117   retval = g_option_context_parse (context, &argc, &argv, &error);
1118   assert_no_error (error);
1119   g_assert (retval);
1120
1121   /* Check array */
1122   arg = join_stringv (argc, argv);
1123   g_assert (strcmp (arg, "program --hello") == 0);
1124
1125   g_assert (strcmp (ignore_test3_string, "foo") == 0);
1126   g_free (ignore_test3_string);
1127
1128   g_free (arg);
1129   g_strfreev (argv_copy);
1130   g_free (argv);
1131   g_option_context_free (context);
1132 }
1133
1134 void
1135 array_test1 (void)
1136 {
1137   GOptionContext *context;
1138   gboolean retval;
1139   GError *error = NULL;
1140   gchar **argv;
1141   int argc;
1142   GOptionEntry entries [] =
1143     { { "test", 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1144       { NULL } };
1145         
1146   context = g_option_context_new (NULL);
1147   g_option_context_add_main_entries (context, entries, NULL);
1148
1149   /* Now try parsing */
1150   argv = split_string ("program --test foo --test bar", &argc);
1151   
1152   retval = g_option_context_parse (context, &argc, &argv, &error);
1153   assert_no_error (error);
1154   g_assert (retval);
1155
1156   /* Check array */
1157   g_assert (strcmp (array_test1_array[0], "foo") == 0);
1158   g_assert (strcmp (array_test1_array[1], "bar") == 0);
1159   g_assert (array_test1_array[2] == NULL);
1160
1161   g_strfreev (array_test1_array);
1162   
1163   g_strfreev (argv);
1164   g_option_context_free (context);
1165 }
1166
1167 void
1168 add_test1 (void)
1169 {
1170   GOptionContext *context;
1171
1172   GOptionEntry entries1 [] =
1173     { { "test1", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1174       { NULL } };
1175   GOptionEntry entries2 [] =
1176     { { "test2", 0, 0, G_OPTION_ARG_STRING_ARRAY, NULL, NULL, NULL },
1177       { NULL } };
1178
1179   context = g_option_context_new (NULL);
1180   g_option_context_add_main_entries (context, entries1, NULL);
1181   g_option_context_add_main_entries (context, entries2, NULL);
1182
1183   g_option_context_free (context);
1184 }
1185
1186 void
1187 empty_test1 (void)
1188 {
1189   GOptionContext *context;
1190   GOptionEntry entries [] =
1191     { { NULL } };
1192   char *prgname;
1193
1194   g_set_prgname (NULL);
1195   context = g_option_context_new (NULL);
1196
1197   g_option_context_add_main_entries (context, entries, NULL);
1198   
1199   g_option_context_parse (context, NULL, NULL, NULL);
1200
1201   prgname = g_get_prgname ();
1202   g_assert (prgname && strcmp (prgname, "<unknown>") == 0);
1203   
1204   g_option_context_free (context);
1205 }
1206
1207 void
1208 empty_test2 (void)
1209 {
1210   GOptionContext *context;
1211
1212   context = g_option_context_new (NULL);
1213   g_option_context_parse (context, NULL, NULL, NULL);
1214   
1215   g_option_context_free (context);
1216 }
1217
1218 void
1219 empty_test3 (void)
1220 {
1221   GOptionContext *context;
1222   gint argc;
1223   gchar **argv;
1224
1225   argc = 0;
1226   argv = NULL;
1227
1228   context = g_option_context_new (NULL);
1229   g_option_context_parse (context, &argc, &argv, NULL);
1230   
1231   g_option_context_free (context);
1232 }
1233
1234 /* check that non-option arguments are left in argv by default */
1235 void
1236 rest_test1 (void)
1237 {
1238   GOptionContext *context;
1239   gboolean retval;
1240   GError *error = NULL;
1241   gchar **argv;
1242   int argc;
1243   GOptionEntry entries [] = { 
1244       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1245       { NULL } 
1246   };
1247         
1248   context = g_option_context_new (NULL);
1249   g_option_context_add_main_entries (context, entries, NULL);
1250
1251   /* Now try parsing */
1252   argv = split_string ("program foo --test bar", &argc);
1253   
1254   retval = g_option_context_parse (context, &argc, &argv, &error);
1255   assert_no_error (error);
1256   g_assert (retval);
1257
1258   /* Check array */
1259   g_assert (ignore_test1_boolean);
1260   g_assert (strcmp (argv[0], "program") == 0);
1261   g_assert (strcmp (argv[1], "foo") == 0);
1262   g_assert (strcmp (argv[2], "bar") == 0);
1263   g_assert (argv[3] == NULL);
1264
1265   g_strfreev (argv);
1266   g_option_context_free (context);
1267 }
1268
1269 /* check that -- works */
1270 void
1271 rest_test2 (void)
1272 {
1273   GOptionContext *context;
1274   gboolean retval;
1275   GError *error = NULL;
1276   gchar **argv;
1277   int argc;
1278   GOptionEntry entries [] = { 
1279       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1280       { NULL } 
1281   };
1282         
1283   context = g_option_context_new (NULL);
1284   g_option_context_add_main_entries (context, entries, NULL);
1285
1286   /* Now try parsing */
1287   argv = split_string ("program foo --test -- -bar", &argc);
1288   
1289   retval = g_option_context_parse (context, &argc, &argv, &error);
1290   assert_no_error (error);
1291   g_assert (retval);
1292
1293   /* Check array */
1294   g_assert (ignore_test1_boolean);
1295   g_assert (strcmp (argv[0], "program") == 0);
1296   g_assert (strcmp (argv[1], "foo") == 0);
1297   g_assert (strcmp (argv[2], "--") == 0);
1298   g_assert (strcmp (argv[3], "-bar") == 0);
1299   g_assert (argv[4] == NULL);
1300
1301   g_strfreev (argv);
1302   g_option_context_free (context);
1303 }
1304
1305 /* check that -- stripping works */
1306 void
1307 rest_test2a (void)
1308 {
1309   GOptionContext *context;
1310   gboolean retval;
1311   GError *error = NULL;
1312   gchar **argv;
1313   int argc;
1314   GOptionEntry entries [] = { 
1315       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1316       { NULL } 
1317   };
1318         
1319   context = g_option_context_new (NULL);
1320   g_option_context_add_main_entries (context, entries, NULL);
1321
1322   /* Now try parsing */
1323   argv = split_string ("program foo --test -- bar", &argc);
1324   
1325   retval = g_option_context_parse (context, &argc, &argv, &error);
1326   assert_no_error (error);
1327   g_assert (retval);
1328
1329   /* Check array */
1330   g_assert (ignore_test1_boolean);
1331   g_assert (strcmp (argv[0], "program") == 0);
1332   g_assert (strcmp (argv[1], "foo") == 0);
1333   g_assert (strcmp (argv[2], "bar") == 0);
1334   g_assert (argv[3] == NULL);
1335
1336   g_strfreev (argv);
1337   g_option_context_free (context);
1338 }
1339
1340 void
1341 rest_test2b (void)
1342 {
1343   GOptionContext *context;
1344   gboolean retval;
1345   GError *error = NULL;
1346   gchar **argv;
1347   int argc;
1348   GOptionEntry entries [] = { 
1349       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1350       { NULL } 
1351   };
1352         
1353   context = g_option_context_new (NULL);
1354   g_option_context_set_ignore_unknown_options (context, TRUE);
1355   g_option_context_add_main_entries (context, entries, NULL);
1356
1357   /* Now try parsing */
1358   argv = split_string ("program foo --test -bar --", &argc);
1359   
1360   retval = g_option_context_parse (context, &argc, &argv, &error);
1361   assert_no_error (error);
1362   g_assert (retval);
1363
1364   /* Check array */
1365   g_assert (ignore_test1_boolean);
1366   g_assert (strcmp (argv[0], "program") == 0);
1367   g_assert (strcmp (argv[1], "foo") == 0);
1368   g_assert (strcmp (argv[2], "-bar") == 0);
1369   g_assert (argv[3] == NULL);
1370
1371   g_strfreev (argv);
1372   g_option_context_free (context);
1373 }
1374
1375 void
1376 rest_test2c (void)
1377 {
1378   GOptionContext *context;
1379   gboolean retval;
1380   GError *error = NULL;
1381   gchar **argv;
1382   int argc;
1383   GOptionEntry entries [] = { 
1384       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1385       { NULL } 
1386   };
1387         
1388   context = g_option_context_new (NULL);
1389   g_option_context_add_main_entries (context, entries, NULL);
1390
1391   /* Now try parsing */
1392   argv = split_string ("program --test foo -- bar", &argc);
1393   
1394   retval = g_option_context_parse (context, &argc, &argv, &error);
1395   assert_no_error (error);
1396   g_assert (retval);
1397
1398   /* Check array */
1399   g_assert (ignore_test1_boolean);
1400   g_assert (strcmp (argv[0], "program") == 0);
1401   g_assert (strcmp (argv[1], "foo") == 0);
1402   g_assert (strcmp (argv[2], "bar") == 0);
1403   g_assert (argv[3] == NULL);
1404
1405   g_strfreev (argv);
1406   g_option_context_free (context);
1407 }
1408
1409 void
1410 rest_test2d (void)
1411 {
1412   GOptionContext *context;
1413   gboolean retval;
1414   GError *error = NULL;
1415   gchar **argv;
1416   int argc;
1417   GOptionEntry entries [] = { 
1418       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1419       { NULL } 
1420   };
1421         
1422   context = g_option_context_new (NULL);
1423   g_option_context_add_main_entries (context, entries, NULL);
1424
1425   /* Now try parsing */
1426   argv = split_string ("program --test -- -bar", &argc);
1427   
1428   retval = g_option_context_parse (context, &argc, &argv, &error);
1429   assert_no_error (error);
1430   g_assert (retval);
1431
1432   /* Check array */
1433   g_assert (ignore_test1_boolean);
1434   g_assert (strcmp (argv[0], "program") == 0);
1435   g_assert (strcmp (argv[1], "--") == 0);
1436   g_assert (strcmp (argv[2], "-bar") == 0);
1437   g_assert (argv[3] == NULL);
1438
1439   g_strfreev (argv);
1440   g_option_context_free (context);
1441 }
1442
1443
1444 /* check that G_OPTION_REMAINING collects non-option arguments */
1445 void
1446 rest_test3 (void)
1447 {
1448   GOptionContext *context;
1449   gboolean retval;
1450   GError *error = NULL;
1451   gchar **argv;
1452   int argc;
1453   GOptionEntry entries [] = { 
1454       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1455       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1456       { NULL } 
1457   };
1458         
1459   context = g_option_context_new (NULL);
1460   g_option_context_add_main_entries (context, entries, NULL);
1461
1462   /* Now try parsing */
1463   argv = split_string ("program foo --test bar", &argc);
1464   
1465   retval = g_option_context_parse (context, &argc, &argv, &error);
1466   assert_no_error (error);
1467   g_assert (retval);
1468
1469   /* Check array */
1470   g_assert (ignore_test1_boolean);
1471   g_assert (strcmp (array_test1_array[0], "foo") == 0);
1472   g_assert (strcmp (array_test1_array[1], "bar") == 0);
1473   g_assert (array_test1_array[2] == NULL);
1474
1475   g_strfreev (array_test1_array);
1476   
1477   g_strfreev (argv);
1478   g_option_context_free (context);
1479 }
1480
1481
1482 /* check that G_OPTION_REMAINING and -- work together */
1483 void
1484 rest_test4 (void)
1485 {
1486   GOptionContext *context;
1487   gboolean retval;
1488   GError *error = NULL;
1489   gchar **argv;
1490   int argc;
1491   GOptionEntry entries [] = { 
1492       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1493       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &array_test1_array, NULL, NULL },
1494       { NULL } 
1495   };
1496         
1497   context = g_option_context_new (NULL);
1498   g_option_context_add_main_entries (context, entries, NULL);
1499
1500   /* Now try parsing */
1501   argv = split_string ("program foo --test -- -bar", &argc);
1502   
1503   retval = g_option_context_parse (context, &argc, &argv, &error);
1504   assert_no_error (error);
1505   g_assert (retval);
1506
1507   /* Check array */
1508   g_assert (ignore_test1_boolean);
1509   g_assert (strcmp (array_test1_array[0], "foo") == 0);
1510   g_assert (strcmp (array_test1_array[1], "-bar") == 0);
1511   g_assert (array_test1_array[2] == NULL);
1512
1513   g_strfreev (array_test1_array);
1514   
1515   g_strfreev (argv);
1516   g_option_context_free (context);
1517 }
1518
1519 /* test that G_OPTION_REMAINING works with G_OPTION_ARG_FILENAME_ARRAY */
1520 void
1521 rest_test5 (void)
1522 {
1523   GOptionContext *context;
1524   gboolean retval;
1525   GError *error = NULL;
1526   gchar **argv;
1527   int argc;
1528   GOptionEntry entries [] = { 
1529       { "test", 0, 0, G_OPTION_ARG_NONE, &ignore_test1_boolean, NULL, NULL },
1530       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &array_test1_array, NULL, NULL },
1531       { NULL } 
1532   };
1533         
1534   context = g_option_context_new (NULL);
1535   g_option_context_add_main_entries (context, entries, NULL);
1536
1537   /* Now try parsing */
1538   argv = split_string ("program foo --test bar", &argc);
1539   
1540   retval = g_option_context_parse (context, &argc, &argv, &error);
1541   assert_no_error (error);
1542   g_assert (retval);
1543
1544   /* Check array */
1545   g_assert (ignore_test1_boolean);
1546   g_assert (strcmp (array_test1_array[0], "foo") == 0);
1547   g_assert (strcmp (array_test1_array[1], "bar") == 0);
1548   g_assert (array_test1_array[2] == NULL);
1549
1550   g_strfreev (array_test1_array);
1551   
1552   g_strfreev (argv);
1553   g_option_context_free (context);
1554 }
1555
1556 void
1557 unknown_short_test (void)
1558 {
1559   GOptionContext *context;
1560   gboolean retval;
1561   GError *error = NULL;
1562   gchar **argv;
1563   int argc;
1564   GOptionEntry entries [] = { { NULL } };
1565
1566   g_test_bug ("166609");
1567
1568   context = g_option_context_new (NULL);
1569   g_option_context_add_main_entries (context, entries, NULL);
1570
1571   /* Now try parsing */
1572   argv = split_string ("program -0", &argc);
1573
1574   retval = g_option_context_parse (context, &argc, &argv, &error);
1575   g_assert (!retval);
1576
1577   g_strfreev (argv);
1578   g_option_context_free (context);
1579 }
1580
1581 /* test that lone dashes are treated as non-options */
1582 void lonely_dash_test (void)
1583 {
1584   GOptionContext *context;
1585   gboolean retval;
1586   GError *error = NULL;
1587   gchar **argv;
1588   int argc;
1589
1590   g_test_bug ("168008");
1591
1592   context = g_option_context_new (NULL);
1593
1594   /* Now try parsing */
1595   argv = split_string ("program -", &argc);
1596
1597   retval = g_option_context_parse (context, &argc, &argv, &error);
1598   assert_no_error (error);
1599   g_assert (retval);
1600
1601   g_assert (argv[1] && strcmp (argv[1], "-") == 0);
1602
1603   g_strfreev (argv);
1604   g_option_context_free (context);
1605 }
1606
1607 void
1608 missing_arg_test (void)
1609 {
1610   GOptionContext *context;
1611   gboolean retval;
1612   GError *error = NULL;
1613   gchar **argv;
1614   int argc;
1615   gchar *arg = NULL;
1616   GOptionEntry entries [] =
1617     { { "test", 't', 0, G_OPTION_ARG_STRING, &arg, NULL, NULL },
1618       { NULL } };
1619
1620   g_test_bug ("305576");
1621
1622   context = g_option_context_new (NULL);
1623   g_option_context_add_main_entries (context, entries, NULL);
1624
1625   /* Now try parsing */
1626   argv = split_string ("program --test", &argc);
1627
1628   retval = g_option_context_parse (context, &argc, &argv, &error);
1629   g_assert (retval == FALSE);
1630   g_clear_error (&error);
1631
1632   g_strfreev (argv);
1633
1634   /* Try parsing again */
1635   argv = split_string ("program --t", &argc);
1636
1637   retval = g_option_context_parse (context, &argc, &argv, &error);
1638   g_assert (retval == FALSE);
1639
1640   g_strfreev (argv);
1641   g_option_context_free (context);
1642 }
1643
1644 int
1645 main (int   argc,
1646       char *argv[])
1647 {
1648   g_test_init (&argc, &argv, NULL);
1649
1650   g_test_bug_base ("http://bugzilla.gnome.org/");
1651   g_test_add_func ("/group/captions", group_captions);
1652
1653   /* Test that restoration on failure works */
1654   g_test_add_func ("/restoration/int", error_test1);
1655   g_test_add_func ("/restoration/string", error_test2);
1656   g_test_add_func ("/restoration/boolean", error_test3);
1657   
1658   /* Test that special argument parsing works */
1659   g_test_add_func ("/arg/repetition/int", arg_test1);
1660   g_test_add_func ("/arg/repetition/string", arg_test2);
1661   g_test_add_func ("/arg/repetition/filename", arg_test3);
1662   g_test_add_func ("/arg/repetition/double", arg_test4);
1663   g_test_add_func ("/arg/repetition/locale", arg_test5);
1664   g_test_add_func ("/arg/repetition/int64", arg_test6);
1665
1666   /* Test string arrays */
1667   g_test_add_func ("/arg/array/string", array_test1);
1668
1669   /* Test callback args */
1670   g_test_add_func ("/arg/callback/string", callback_test1);
1671   g_test_add_func ("/arg/callback/count", callback_test2);
1672
1673   /* Test optional arg flag for callback */
1674   g_test_add_func ("/arg/callback/optional1", callback_test_optional_1);
1675   g_test_add_func ("/arg/callback/optional2", callback_test_optional_2);
1676   g_test_add_func ("/arg/callback/optional3", callback_test_optional_3);
1677   g_test_add_func ("/arg/callback/optional4", callback_test_optional_4);
1678   g_test_add_func ("/arg/callback/optional5", callback_test_optional_5);
1679   g_test_add_func ("/arg/callback/optional6", callback_test_optional_6);
1680   g_test_add_func ("/arg/callback/optional7", callback_test_optional_7);
1681   g_test_add_func ("/arg/callback/optional8", callback_test_optional_8);
1682
1683   /* Test callback with G_OPTION_REMAINING */
1684   g_test_add_func ("/arg/remaining/callback", callback_remaining_test1);
1685   
1686   /* Test ignoring options */
1687   g_test_add_func ("/arg/ignore/long", ignore_test1);
1688   g_test_add_func ("/arg/ignore/short", ignore_test2);
1689   g_test_add_func ("/arg/ignore/arg", ignore_test3);
1690
1691   g_test_add_func ("/context/add", add_test1);
1692
1693   /* Test parsing empty args */
1694   g_test_add_func ("/context/empty1", empty_test1);
1695   g_test_add_func ("/context/empty2", empty_test2);
1696   g_test_add_func ("/context/empty3", empty_test3);
1697
1698   /* Test handling of rest args */
1699   g_test_add_func ("/arg/rest/non-option", rest_test1);
1700   g_test_add_func ("/arg/rest/separator1", rest_test2);
1701   g_test_add_func ("/arg/rest/separator2", rest_test2a);
1702   g_test_add_func ("/arg/rest/separator3", rest_test2b);
1703   g_test_add_func ("/arg/rest/separator4", rest_test2c);
1704   g_test_add_func ("/arg/rest/separator5", rest_test2d);
1705   g_test_add_func ("/arg/remaining/non-option", rest_test3);
1706   g_test_add_func ("/arg/remaining/separator", rest_test4);
1707   g_test_add_func ("/arg/remaining/array", rest_test5);
1708
1709   /* regression tests for individual bugs */
1710   g_test_add_func ("/bug/unknown-short", unknown_short_test);
1711   g_test_add_func ("/bug/lonely-dash", lonely_dash_test);
1712   g_test_add_func ("/bug/missing-arg", missing_arg_test);
1713
1714   return g_test_run();
1715 }