option-context.c 72.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Unit tests for GOptionContext
 * Copyright (C) 2007 Openismus GmbH
 * Authors: Mathias Hasselmann
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 */
22 23

#include <glib.h>
24

25
#include <stdlib.h>
Matthias Clasen's avatar
Matthias Clasen committed
26 27 28
#include <stdio.h>
#include <string.h>
#include <locale.h>
29

30 31
#include "gstrfuncsprivate.h"

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static GOptionEntry main_entries[] = {
  { "main-switch", 0, 0,
    G_OPTION_ARG_NONE, NULL,
    "A switch that is in the main group", NULL },
  { NULL }
};

static GOptionEntry group_entries[] = {
  { "test-switch", 0, 0,
    G_OPTION_ARG_NONE, NULL,
    "A switch that is in the test group", NULL },
  { NULL }
};

static GOptionContext *
make_options (int test_number)
{
  GOptionContext *options;
  GOptionGroup   *group = NULL;
  gboolean have_main_entries = (0 != (test_number & 1));
  gboolean have_test_entries = (0 != (test_number & 2));

  options = g_option_context_new (NULL);

  if (have_main_entries)
    g_option_context_add_main_entries (options, main_entries, NULL);
  if (have_test_entries)
    {
      group = g_option_group_new ("test", "Test Options",
                                  "Show all test options",
                                  NULL, NULL);
      g_option_context_add_group (options, group);
      g_option_group_add_entries (group, group_entries);
    }

  return options;
}

70
static void
71
print_help (GOptionContext *options, gchar **argv)
72
{
73 74
  gint    argc = 3;
  GError *error = NULL;
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89
  g_option_context_parse (options, &argc, &argv, &error);
  g_option_context_free (options);
  exit(0);
}

static void
test_group_captions_help (gconstpointer test_number)
{
  GOptionContext *options;
  gchar *argv[] = { __FILE__, "--help", NULL };

  options = make_options (GPOINTER_TO_INT (test_number));
  print_help (options, argv);
}
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static void
test_group_captions_help_all (gconstpointer test_number)
{
  GOptionContext *options;
  gchar *argv[] = { __FILE__, "--help-all", NULL };

  options = make_options (GPOINTER_TO_INT (test_number));
  print_help (options, argv);
}

static void
test_group_captions_help_test (gconstpointer test_number)
{
  GOptionContext *options;
  gchar *argv[] = { __FILE__, "--help-test", NULL };

  options = make_options (GPOINTER_TO_INT (test_number));
  print_help (options, argv);
}

static void
test_group_captions (void)
{
  const gchar *test_name_base[] = { "help", "help-all", "help-test" };
  gchar *test_name;
116 117 118 119 120 121 122 123 124
  gint i, j;

  g_test_bug ("504142");

  for (i = 0; i < 4; ++i)
    {
      gboolean have_main_entries = (0 != (i & 1));
      gboolean have_test_entries = (0 != (i & 2));

125
      for (j = 0; j < G_N_ELEMENTS (test_name_base); ++j)
126
        {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
          GTestSubprocessFlags trap_flags = 0;
          gboolean expect_main_description = FALSE;
          gboolean expect_main_switch      = FALSE;
          gboolean expect_test_description = FALSE;
          gboolean expect_test_switch      = FALSE;
          gboolean expect_test_group       = FALSE;

          if (g_test_verbose ())
            trap_flags |= G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR;

          test_name = g_strdup_printf ("/option/group/captions/subprocess/%s-%d",
                                       test_name_base[j], i);
          g_test_trap_subprocess (test_name, 0, trap_flags);
          g_free (test_name);
          g_test_trap_assert_passed ();
          g_test_trap_assert_stderr ("");

          switch (j)
            {
            case 0:
              g_assert_cmpstr ("help", ==, test_name_base[j]);
              expect_main_switch = have_main_entries;
              expect_test_group  = have_test_entries;
              break;

            case 1:
              g_assert_cmpstr ("help-all", ==, test_name_base[j]);
              expect_main_switch = have_main_entries;
              expect_test_switch = have_test_entries;
              expect_test_group  = have_test_entries;
              break;

            case 2:
              g_assert_cmpstr ("help-test", ==, test_name_base[j]);
              expect_test_switch = have_test_entries;
              break;

            default:
              g_assert_not_reached ();
              break;
            }
168

169 170
          expect_main_description |= expect_main_switch;
          expect_test_description |= expect_test_switch;
171

172 173 174 175 176 177 178 179
          if (expect_main_description)
            g_test_trap_assert_stdout           ("*Application Options*");
          else
            g_test_trap_assert_stdout_unmatched ("*Application Options*");
          if (expect_main_switch)
            g_test_trap_assert_stdout           ("*--main-switch*");
          else
            g_test_trap_assert_stdout_unmatched ("*--main-switch*");
180

181 182 183 184 185 186 187 188
          if (expect_test_description)
            g_test_trap_assert_stdout           ("*Test Options*");
          else
            g_test_trap_assert_stdout_unmatched ("*Test Options*");
          if (expect_test_switch)
            g_test_trap_assert_stdout           ("*--test-switch*");
          else
            g_test_trap_assert_stdout_unmatched ("*--test-switch*");
189

190 191
          if (expect_test_group)
            g_test_trap_assert_stdout           ("*--help-test*");
192
          else
193
            g_test_trap_assert_stdout_unmatched ("*--help-test*");
194
        }
195 196 197
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
int error_test1_int;
char *error_test2_string;
gboolean error_test3_boolean;

int arg_test1_int;
gchar *arg_test2_string;
gchar *arg_test3_filename;
gdouble arg_test4_double;
gdouble arg_test5_double;
gint64 arg_test6_int64;
gint64 arg_test6_int64_2;

gchar *callback_test1_string;
int callback_test2_int;

gchar *callback_test_optional_string;
gboolean callback_test_optional_boolean;

gchar **array_test1_array;

gboolean ignore_test1_boolean;
gboolean ignore_test2_boolean;
gchar *ignore_test3_string;

222
static gchar **
Matthias Clasen's avatar
Matthias Clasen committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
split_string (const char *str, int *argc)
{
  gchar **argv;
  int len;
  
  argv = g_strsplit (str, " ", 0);

  for (len = 0; argv[len] != NULL; len++);

  if (argc)
    *argc = len;
    
  return argv;
}

238
static gchar *
Matthias Clasen's avatar
Matthias Clasen committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
join_stringv (int argc, char **argv)
{
  int i;
  GString *str;

  str = g_string_new (NULL);

  for (i = 0; i < argc; i++)
    {
      g_string_append (str, argv[i]);

      if (i < argc - 1)
	g_string_append_c (str, ' ');
    }

  return g_string_free (str, FALSE);
}

/* Performs a shallow copy */
258
static char **
Matthias Clasen's avatar
Matthias Clasen committed
259 260
copy_stringv (char **argv, int argc)
{
261
  return g_memdup2 (argv, sizeof (char *) * (argc + 1));
Matthias Clasen's avatar
Matthias Clasen committed
262 263
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
static void
check_identical_stringv (gchar **before, gchar **after)
{
  guint i;

  /* Not only is it the same string... */
  for (i = 0; before[i] != NULL; i++)
    g_assert_cmpstr (before[i], ==, after[i]);

  /* ... it is actually the same pointer */
  for (i = 0; before[i] != NULL; i++)
    g_assert (before[i] == after[i]);

  g_assert (after[i] == NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

static gboolean
error_test1_pre_parse (GOptionContext *context,
		       GOptionGroup   *group,
		       gpointer	       data,
		       GError        **error)
{
  g_assert (error_test1_int == 0x12345678);

  return TRUE;
}

static gboolean
error_test1_post_parse (GOptionContext *context,
			GOptionGroup   *group,
			gpointer	  data,
			GError        **error)
{
  g_assert (error_test1_int == 20);

  /* Set an error in the post hook */
  g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");

  return FALSE;
}

306
static void
Matthias Clasen's avatar
Matthias Clasen committed
307 308 309 310 311 312
error_test1 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
313
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  int argc;
  GOptionGroup *main_group;
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_INT, &error_test1_int, NULL, NULL },
      { NULL } };
  
  error_test1_int = 0x12345678;

  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Set pre and post parse hooks */
  main_group = g_option_context_get_main_group (context);
  g_option_group_set_parse_hooks (main_group,
				  error_test1_pre_parse, error_test1_post_parse);
  
  /* Now try parsing */
  argv = split_string ("program --test 20", &argc);
332
  argv_copy = copy_stringv (argv, argc);
Matthias Clasen's avatar
Matthias Clasen committed
333 334 335

  retval = g_option_context_parse (context, &argc, &argv, &error);
  g_assert (retval == FALSE);
336 337 338 339
  g_assert (error != NULL);
  /* An error occurred, so argv has not been changed */
  check_identical_stringv (argv_copy, argv);
  g_clear_error (&error);
Matthias Clasen's avatar
Matthias Clasen committed
340 341 342

  /* On failure, values should be reset */
  g_assert (error_test1_int == 0x12345678);
343 344 345

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  g_option_context_free (context);
}

static gboolean
error_test2_pre_parse (GOptionContext *context,
		       GOptionGroup   *group,
		       gpointer	  data,
		       GError        **error)
{
  g_assert (strcmp (error_test2_string, "foo") == 0);

  return TRUE;
}

static gboolean
error_test2_post_parse (GOptionContext *context,
			GOptionGroup   *group,
			gpointer	  data,
			GError        **error)
{
  g_assert (strcmp (error_test2_string, "bar") == 0);

  /* Set an error in the post hook */
  g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");

  return FALSE;
}

374
static void
Matthias Clasen's avatar
Matthias Clasen committed
375 376 377 378 379 380
error_test2 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
381
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  int argc;
  GOptionGroup *main_group;
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_STRING, &error_test2_string, NULL, NULL },
      { NULL } };

  error_test2_string = "foo";

  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Set pre and post parse hooks */
  main_group = g_option_context_get_main_group (context);
  g_option_group_set_parse_hooks (main_group,
				  error_test2_pre_parse, error_test2_post_parse);
  
  /* Now try parsing */
  argv = split_string ("program --test bar", &argc);
400
  argv_copy = copy_stringv (argv, argc);
Matthias Clasen's avatar
Matthias Clasen committed
401 402 403
  retval = g_option_context_parse (context, &argc, &argv, &error);

  g_assert (retval == FALSE);
404 405 406
  g_assert (error != NULL);
  check_identical_stringv (argv_copy, argv);
  g_clear_error (&error);
Matthias Clasen's avatar
Matthias Clasen committed
407 408

  g_assert (strcmp (error_test2_string, "foo") == 0);
409 410 411

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  g_option_context_free (context);
}

static gboolean
error_test3_pre_parse (GOptionContext *context,
		       GOptionGroup   *group,
		       gpointer	  data,
		       GError        **error)
{
  g_assert (!error_test3_boolean);

  return TRUE;
}

static gboolean
error_test3_post_parse (GOptionContext *context,
			GOptionGroup   *group,
			gpointer	  data,
			GError        **error)
{
  g_assert (error_test3_boolean);

  /* Set an error in the post hook */
  g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, " ");

  return FALSE;
}

440
static void
Matthias Clasen's avatar
Matthias Clasen committed
441 442 443 444 445 446
error_test3 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
447
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
  int argc;
  GOptionGroup *main_group;
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_NONE, &error_test3_boolean, NULL, NULL },
      { NULL } };

  error_test3_boolean = FALSE;

  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Set pre and post parse hooks */
  main_group = g_option_context_get_main_group (context);
  g_option_group_set_parse_hooks (main_group,
				  error_test3_pre_parse, error_test3_post_parse);
  
  /* Now try parsing */
  argv = split_string ("program --test", &argc);
466
  argv_copy = copy_stringv (argv, argc);
Matthias Clasen's avatar
Matthias Clasen committed
467 468 469
  retval = g_option_context_parse (context, &argc, &argv, &error);

  g_assert (retval == FALSE);
470 471 472
  g_assert (error != NULL);
  check_identical_stringv (argv_copy, argv);
  g_clear_error (&error);
Matthias Clasen's avatar
Matthias Clasen committed
473 474

  g_assert (!error_test3_boolean);
475 476 477

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
478 479 480
  g_option_context_free (context);
}

481
static void
Matthias Clasen's avatar
Matthias Clasen committed
482 483 484 485 486 487
arg_test1 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
488
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
489 490 491 492 493 494 495 496 497 498
  int argc;
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_INT, &arg_test1_int, NULL, NULL },
      { NULL } };

  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test 20 --test 30", &argc);
499
  argv_copy = copy_stringv (argv, argc);
Matthias Clasen's avatar
Matthias Clasen committed
500 501

  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
502
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
503 504 505 506 507
  g_assert (retval);

  /* Last arg specified is the one that should be stored */
  g_assert (arg_test1_int == 30);

508 509 510 511 512
  /* We free all of the strings in a copy of argv, because now argv is a
   * subset - some have been removed in-place
   */
  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
513 514 515
  g_option_context_free (context);
}

516
static void
Matthias Clasen's avatar
Matthias Clasen committed
517 518 519 520 521 522
arg_test2 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
523
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
524 525 526 527 528 529 530 531 532 533
  int argc;
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_STRING, &arg_test2_string, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test foo --test bar", &argc);
534 535
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
536
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
537
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
538 539 540 541 542 543
  g_assert (retval);

  /* Last arg specified is the one that should be stored */
  g_assert (strcmp (arg_test2_string, "bar") == 0);

  g_free (arg_test2_string);
544 545 546

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
547 548 549
  g_option_context_free (context);
}

550
static void
Matthias Clasen's avatar
Matthias Clasen committed
551 552 553 554 555 556
arg_test3 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
557
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
558 559 560 561 562 563 564 565 566 567
  int argc;
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_FILENAME, &arg_test3_filename, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test foo.txt", &argc);
568 569
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
570
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
571
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
572 573 574 575 576 577
  g_assert (retval);

  /* Last arg specified is the one that should be stored */
  g_assert (strcmp (arg_test3_filename, "foo.txt") == 0);

  g_free (arg_test3_filename);
578 579 580

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
581 582 583 584
  g_option_context_free (context);
}


585
static void
Matthias Clasen's avatar
Matthias Clasen committed
586 587 588 589 590
arg_test4 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
591
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
592 593 594 595 596 597 598 599 600 601 602
  gchar **argv;
  int argc;
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test4_double, NULL, NULL },
      { NULL } };

  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test 20.0 --test 30.03", &argc);
603
  argv_copy = copy_stringv (argv, argc);
Matthias Clasen's avatar
Matthias Clasen committed
604 605

  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
606
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
607 608 609 610 611
  g_assert (retval);

  /* Last arg specified is the one that should be stored */
  g_assert (arg_test4_double == 30.03);

612 613
  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
614 615 616
  g_option_context_free (context);
}

617
static void
Matthias Clasen's avatar
Matthias Clasen committed
618 619 620 621 622 623
arg_test5 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
624
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
625 626
  int argc;
  char *old_locale, *current_locale;
627
  const char *locale = "de_DE.UTF-8";
Matthias Clasen's avatar
Matthias Clasen committed
628 629 630 631 632 633 634 635 636
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_DOUBLE, &arg_test5_double, NULL, NULL },
      { NULL } };

  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test 20,0 --test 30,03", &argc);
637
  argv_copy = copy_stringv (argv, argc);
Matthias Clasen's avatar
Matthias Clasen committed
638 639 640 641 642 643 644 645 646 647 648 649

  /* set it to some locale that uses commas instead of decimal points */
  
  old_locale = g_strdup (setlocale (LC_NUMERIC, locale));
  current_locale = setlocale (LC_NUMERIC, NULL);
  if (strcmp (current_locale, locale) != 0)
    {
      fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
      goto cleanup; 
    }

  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
650
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
651 652 653 654 655 656 657 658 659
  g_assert (retval);

  /* Last arg specified is the one that should be stored */
  g_assert (arg_test5_double == 30.03);

 cleanup:
  setlocale (LC_NUMERIC, old_locale);
  g_free (old_locale);

660 661
  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
662 663 664
  g_option_context_free (context);
}

665
static void
Matthias Clasen's avatar
Matthias Clasen committed
666 667 668 669 670 671
arg_test6 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
672
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
673 674 675 676 677 678 679 680 681 682 683
  int argc;
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL },
      { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, NULL, NULL },
      { NULL } };

  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc);
684
  argv_copy = copy_stringv (argv, argc);
Matthias Clasen's avatar
Matthias Clasen committed
685 686

  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
687
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
688 689 690 691 692 693
  g_assert (retval);

  /* Last arg specified is the one that should be stored */
  g_assert (arg_test6_int64 == G_GINT64_CONSTANT(4294967296));
  g_assert (arg_test6_int64_2 == G_GINT64_CONSTANT(0xfffffffff));

694 695
  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
696 697 698 699 700 701 702 703 704 705 706
  g_option_context_free (context);
}

static gboolean
callback_parse1 (const gchar *option_name, const gchar *value,
		 gpointer data, GError **error)
{
	callback_test1_string = g_strdup (value);
	return TRUE;
}

707
static void
Matthias Clasen's avatar
Matthias Clasen committed
708 709 710 711 712 713
callback_test1 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
714
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
715 716 717 718 719 720 721 722 723 724
  int argc;
  GOptionEntry entries [] =
    { { "test", 0, 0, G_OPTION_ARG_CALLBACK, callback_parse1, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test foo.txt", &argc);
725 726
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
727
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
728
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
729 730 731 732 733
  g_assert (retval);

  g_assert (strcmp (callback_test1_string, "foo.txt") == 0);

  g_free (callback_test1_string);
734 735 736

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
737 738 739 740 741 742 743 744 745 746 747
  g_option_context_free (context);
}

static gboolean
callback_parse2 (const gchar *option_name, const gchar *value,
		 gpointer data, GError **error)
{
	callback_test2_int++;
	return TRUE;
}

748
static void
Matthias Clasen's avatar
Matthias Clasen committed
749 750 751 752 753 754
callback_test2 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
755
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
756 757 758 759 760 761 762 763 764 765
  int argc;
  GOptionEntry entries [] =
    { { "test", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_parse2, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test --test", &argc);
766 767
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
768
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
769
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
770 771 772
  g_assert (retval);

  g_assert (callback_test2_int == 2);
773 774 775

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
  g_option_context_free (context);
}

static gboolean
callback_parse_optional (const gchar *option_name, const gchar *value,
		 gpointer data, GError **error)
{
	callback_test_optional_boolean = TRUE;
	if (value)
		callback_test_optional_string = g_strdup (value);
	else
		callback_test_optional_string = NULL;
	return TRUE;
}

791
static void
Matthias Clasen's avatar
Matthias Clasen committed
792 793 794 795 796 797
callback_test_optional_1 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
798
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
799 800 801 802 803 804 805 806 807 808 809
  int argc;
  GOptionEntry entries [] =
    { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
	callback_parse_optional, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test foo.txt", &argc);
810 811
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
812
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
813
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
814 815 816 817 818 819 820
  g_assert (retval);

  g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
  
  g_assert (callback_test_optional_boolean);

  g_free (callback_test_optional_string);
821 822 823

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
824 825 826
  g_option_context_free (context);
}

827
static void
Matthias Clasen's avatar
Matthias Clasen committed
828 829 830 831 832 833
callback_test_optional_2 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
834
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
835 836 837 838 839 840 841 842 843 844 845
  int argc;
  GOptionEntry entries [] =
    { { "test", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
	callback_parse_optional, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test", &argc);
846 847
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
848
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
849
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
850 851 852 853 854 855 856
  g_assert (retval);

  g_assert (callback_test_optional_string == NULL);
  
  g_assert (callback_test_optional_boolean);

  g_free (callback_test_optional_string);
857 858 859

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
860 861 862
  g_option_context_free (context);
}

863
static void
Matthias Clasen's avatar
Matthias Clasen committed
864 865 866 867 868
callback_test_optional_3 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
869
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
870 871 872 873 874 875 876 877 878 879 880 881
  gchar **argv;
  int argc;
  GOptionEntry entries [] =
    { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
	callback_parse_optional, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program -t foo.txt", &argc);
882 883
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
884
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
885
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
886 887 888 889 890 891 892
  g_assert (retval);

  g_assert (strcmp (callback_test_optional_string, "foo.txt") == 0);
  
  g_assert (callback_test_optional_boolean);

  g_free (callback_test_optional_string);
893 894 895

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
896 897 898 899
  g_option_context_free (context);
}


900
static void
Matthias Clasen's avatar
Matthias Clasen committed
901 902 903 904 905 906
callback_test_optional_4 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
907
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
908 909 910 911 912 913 914 915 916 917 918
  int argc;
  GOptionEntry entries [] =
    { { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
	callback_parse_optional, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program -t", &argc);
919 920
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
921
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
922
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
923 924 925 926 927 928 929
  g_assert (retval);

  g_assert (callback_test_optional_string == NULL);
  
  g_assert (callback_test_optional_boolean);

  g_free (callback_test_optional_string);
930 931 932

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
933 934 935
  g_option_context_free (context);
}

936
static void
Matthias Clasen's avatar
Matthias Clasen committed
937 938 939 940 941 942 943
callback_test_optional_5 (void)
{
  GOptionContext *context;
  gboolean dummy;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
944
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
945 946 947 948 949 950 951 952 953 954 955 956
  int argc;
  GOptionEntry entries [] =
    { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
      { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
	callback_parse_optional, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --test --dummy", &argc);
957 958
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
959
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
960
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
961 962 963 964 965 966 967
  g_assert (retval);

  g_assert (callback_test_optional_string == NULL);
  
  g_assert (callback_test_optional_boolean);

  g_free (callback_test_optional_string);
968 969 970

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
971 972 973
  g_option_context_free (context);
}

974
static void
Matthias Clasen's avatar
Matthias Clasen committed
975 976 977 978 979 980 981
callback_test_optional_6 (void)
{
  GOptionContext *context;
  gboolean dummy;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
982
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
983 984 985 986 987 988 989 990 991 992 993 994
  int argc;
  GOptionEntry entries [] =
    { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
      { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
	callback_parse_optional, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program -t -d", &argc);
995 996
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
997
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
998
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
999 1000 1001 1002 1003 1004 1005
  g_assert (retval);

  g_assert (callback_test_optional_string == NULL);
  
  g_assert (callback_test_optional_boolean);

  g_free (callback_test_optional_string);
1006 1007 1008

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
1009 1010 1011
  g_option_context_free (context);
}

1012
static void
Matthias Clasen's avatar
Matthias Clasen committed
1013 1014 1015 1016 1017 1018 1019
callback_test_optional_7 (void)
{
  GOptionContext *context;
  gboolean dummy;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
1020
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
  int argc;
  GOptionEntry entries [] =
    { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
      { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
	callback_parse_optional, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program -td", &argc);
1033 1034
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
1035
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
1036
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
1037 1038 1039 1040 1041 1042 1043
  g_assert (retval);

  g_assert (callback_test_optional_string == NULL);
  
  g_assert (callback_test_optional_boolean);

  g_free (callback_test_optional_string);
1044 1045 1046

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
1047 1048 1049
  g_option_context_free (context);
}

1050
static void
Matthias Clasen's avatar
Matthias Clasen committed
1051 1052 1053 1054 1055 1056 1057
callback_test_optional_8 (void)
{
  GOptionContext *context;
  gboolean dummy;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
1058
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
  int argc;
  GOptionEntry entries [] =
    { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, NULL },
      { "test", 't', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, 
	callback_parse_optional, NULL, NULL },
      { NULL } };
  
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program -dt foo.txt", &argc);
1071 1072
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
1073
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
1074
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
1075 1076 1077 1078 1079 1080 1081
  g_assert (retval);

  g_assert (callback_test_optional_string);
  
  g_assert (callback_test_optional_boolean);

  g_free (callback_test_optional_string);
1082 1083 1084

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
  g_option_context_free (context);
}

static GPtrArray *callback_remaining_args;
static gboolean
callback_remaining_test1_callback (const gchar *option_name, const gchar *value,
		         gpointer data, GError **error)
{
	g_ptr_array_add (callback_remaining_args, g_strdup (value));
	return TRUE;
}

1097
static void
Matthias Clasen's avatar
Matthias Clasen committed
1098 1099 1100 1101 1102 1103
callback_remaining_test1 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
1104
  gchar **argv_copy;
Matthias Clasen's avatar
Matthias Clasen committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
  int argc;
  GOptionEntry entries [] =
    { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, callback_remaining_test1_callback, NULL, NULL },
      { NULL } };
  
  callback_remaining_args = g_ptr_array_new ();
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program foo.txt blah.txt", &argc);
1116 1117
  argv_copy = copy_stringv (argv, argc);

Matthias Clasen's avatar
Matthias Clasen committed
1118
  retval = g_option_context_parse (context, &argc, &argv, &error);
Matthias Clasen's avatar
Matthias Clasen committed
1119
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
1120 1121 1122 1123 1124 1125 1126 1127
  g_assert (retval);

  g_assert (callback_remaining_args->len == 2);
  g_assert (strcmp (callback_remaining_args->pdata[0], "foo.txt") == 0);
  g_assert (strcmp (callback_remaining_args->pdata[1], "blah.txt") == 0);

  g_ptr_array_foreach (callback_remaining_args, (GFunc) g_free, NULL);
  g_ptr_array_free (callback_remaining_args, TRUE);
1128 1129 1130

  g_strfreev (argv_copy);
  g_free (argv);
Matthias Clasen's avatar
Matthias Clasen committed
1131 1132 1133
  g_option_context_free (context);
}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
static gboolean
callback_error (const gchar *option_name, const gchar *value,
                gpointer data, GError **error)
{
  g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "42");
  return FALSE;
}

static void
callback_returns_false (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv;
1149
  gchar **argv_copy;
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  int argc;
  GOptionEntry entries [] =
    { { "error", 0, 0, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
      { "error-no-arg", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
      { "error-optional-arg", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, callback_error, NULL, NULL },
      { NULL } };

  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  /* Now try parsing */
  argv = split_string ("program --error value", &argc);
1162 1163
  argv_copy = copy_stringv (argv, argc);

1164 1165 1166
  retval = g_option_context_parse (context, &argc, &argv, &error);
  g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
  g_assert (retval == FALSE);
1167
  check_identical_stringv (argv_copy, argv);
1168 1169 1170

  g_option_context_free (context);
  g_clear_error (&error);
1171 1172
  g_strfreev (argv_copy);
  g_free (argv);
1173 1174 1175 1176 1177 1178

  /* And again, this time with a no-arg variant */
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  argv = split_string ("program --error-no-arg", &argc);
1179 1180
  argv_copy = copy_stringv (argv, argc);

1181 1182 1183
  retval = g_option_context_parse (context, &argc, &argv, &error);
  g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
  g_assert (retval == FALSE);
1184
  check_identical_stringv (argv_copy, argv);
1185 1186 1187

  g_option_context_free (context);
  g_clear_error (&error);
1188 1189
  g_strfreev (argv_copy);
  g_free (argv);
1190

1191
  /* And again, this time with an optional arg variant, with argument */
1192 1193 1194 1195
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  argv = split_string ("program --error-optional-arg value", &argc);
1196 1197
  argv_copy = copy_stringv (argv, argc);

1198 1199 1200
  retval = g_option_context_parse (context, &argc, &argv, &error);
  g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
  g_assert (retval == FALSE);
1201
  check_identical_stringv (argv_copy, argv);
1202 1203 1204

  g_option_context_free (context);
  g_clear_error (&error);
1205 1206
  g_strfreev (argv_copy);
  g_free (argv);
1207

1208
  /* And again, this time with an optional arg variant, without argument */
1209 1210 1211 1212
  context = g_option_context_new (NULL);
  g_option_context_add_main_entries (context, entries, NULL);

  argv = split_string ("program --error-optional-arg", &argc);
1213 1214
  argv_copy = copy_stringv (argv, argc);

1215 1216 1217
  retval = g_option_context_parse (context, &argc, &argv, &error);
  g_assert_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE);
  g_assert (retval == FALSE);
1218
  check_identical_stringv (argv_copy, argv);
1219 1220 1221

  g_option_context_free (context);
  g_clear_error (&error);
1222 1223
  g_strfreev (argv_copy);
  g_free (argv);
1224 1225 1226
}


1227
static void
Matthias Clasen's avatar
Matthias Clasen committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
ignore_test1 (void)
{
  GOptionContext *context;
  gboolean retval;
  GError *error = NULL;
  gchar **argv, **argv_copy;
  int argc;
  gchar *arg;
  GOptionEntry entries [] =
    { { "test"