⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 option-context.c

📁 this is a glib for c language
💻 C
📖 第 1 页 / 共 4 页
字号:
/* 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. */#include <glib.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <locale.h>static voidgroup_captions (void){  gchar *help_variants[] = { "--help", "--help-all", "--help-test" };  GOptionEntry main_entries[] = {    { "main-switch", 0,      G_OPTION_FLAG_NO_ARG,      G_OPTION_ARG_NONE, NULL,      "A switch that is in the main group", NULL },    { NULL }  };  GOptionEntry group_entries[] = {    { "test-switch", 0,      G_OPTION_FLAG_NO_ARG,      G_OPTION_ARG_NONE, NULL,      "A switch that is in the test group", NULL },    { NULL }  };  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));      GOptionContext *options;      GOptionGroup   *group = NULL;      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);        }      for (j = 0; j < G_N_ELEMENTS (help_variants); ++j)        {          GTestTrapFlags trap_flags = 0;          gchar *args[3];          args[0] = __FILE__;          args[1] = help_variants[j];          args[2] = NULL;          if (!g_test_verbose ())            trap_flags |= G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR;          g_test_message ("test setup: args='%s', main-entries=%d, test-entries=%d",                          args[1], have_main_entries, have_test_entries);          if (g_test_trap_fork (0, trap_flags))            {              gchar **argv = args;              gint    argc = 2;              GError *error = NULL;              g_setenv ("LANG", "C", TRUE);              g_option_context_parse (options, &argc, &argv, &error);              exit(0);            }          else            {              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;              g_test_trap_assert_passed ();              g_test_trap_assert_stderr ("");              switch (j)                {                  case 0:                    g_assert_cmpstr ("--help", ==, args[1]);                    expect_main_switch = have_main_entries;                    expect_test_group  = have_test_entries;                    break;                  case 1:                    g_assert_cmpstr ("--help-all", ==, args[1]);                    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", ==, args[1]);                    expect_test_switch = have_test_entries;                    break;                  default:                    g_assert_not_reached ();                    break;                }              expect_main_description |= expect_main_switch;              expect_test_description |= expect_test_switch;              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*");              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*");              if (expect_test_group)                g_test_trap_assert_stdout           ("*--help-test*");              else                g_test_trap_assert_stdout_unmatched ("*--help-test*");            }        }    }}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;gchar **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;}gchar *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 */char **copy_stringv (char **argv, int argc){  return g_memdup (argv, sizeof (char *) * (argc + 1));}static gbooleanerror_test1_pre_parse (GOptionContext *context,		       GOptionGroup   *group,		       gpointer	       data,		       GError        **error){  g_assert (error_test1_int == 0x12345678);  return TRUE;}static gbooleanerror_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;}voiderror_test1 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  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);  retval = g_option_context_parse (context, &argc, &argv, &error);  g_assert (retval == FALSE);  /* On failure, values should be reset */  g_assert (error_test1_int == 0x12345678);    g_strfreev (argv);  g_option_context_free (context);}static gbooleanerror_test2_pre_parse (GOptionContext *context,		       GOptionGroup   *group,		       gpointer	  data,		       GError        **error){  g_assert (strcmp (error_test2_string, "foo") == 0);  return TRUE;}static gbooleanerror_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;}voiderror_test2 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  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);  retval = g_option_context_parse (context, &argc, &argv, &error);  g_error_free (error);  g_assert (retval == FALSE);  g_assert (strcmp (error_test2_string, "foo") == 0);    g_strfreev (argv);  g_option_context_free (context);}static gbooleanerror_test3_pre_parse (GOptionContext *context,		       GOptionGroup   *group,		       gpointer	  data,		       GError        **error){  g_assert (!error_test3_boolean);  return TRUE;}static gbooleanerror_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;}voiderror_test3 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  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);  retval = g_option_context_parse (context, &argc, &argv, &error);  g_error_free (error);  g_assert (retval == FALSE);  g_assert (!error_test3_boolean);    g_strfreev (argv);  g_option_context_free (context);}static voidassert_no_error (GError *error){  if (error)     {      fprintf (stderr, "unexpected error: %s, %d, %s\n", g_quark_to_string (error->domain), error->code, error->message);      exit (1);    }}static voidassert_error (GError *error,               GQuark *domain,               gint    code){  g_assert (error && error->domain == domain && error->code == code);}voidarg_test1 (void){  GOptionContext *context;  gboolean retval;  GError *error = NULL;  gchar **argv;  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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -