os/ossrv/glib/tests/markup-collect.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* 
sl@0
     2
 * Copyright © 2007 Ryan Lortie
sl@0
     3
 * Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.
sl@0
     4
 * This program is free software: you can redistribute it and/or modify
sl@0
     5
 * it under the terms of the GNU Lesser General Public License as
sl@0
     6
 * published by the Free Software Foundation; either version 2 of the
sl@0
     7
 * License, or (at your option) any later version.
sl@0
     8
 * 
sl@0
     9
 * See the included COPYING file for more information.
sl@0
    10
 */
sl@0
    11
sl@0
    12
#include <string.h>
sl@0
    13
#include <glib.h>
sl@0
    14
#ifdef __SYMBIAN32__
sl@0
    15
#include "mrt2_glib2_test.h"
sl@0
    16
#endif /*__SYMBIAN32__*/
sl@0
    17
static void
sl@0
    18
start (GMarkupParseContext  *context,
sl@0
    19
       const char           *element_name,
sl@0
    20
       const char          **attribute_names,
sl@0
    21
       const char          **attribute_values,
sl@0
    22
       gpointer              user_data,
sl@0
    23
       GError              **error)
sl@0
    24
{
sl@0
    25
  GString *string = user_data;
sl@0
    26
  gboolean result;
sl@0
    27
#ifndef __SYMBIAN32__
sl@0
    28
#define collect(...) \
sl@0
    29
  g_markup_collect_attributes (element_name, attribute_names, \
sl@0
    30
                               attribute_values, error, __VA_ARGS__, \
sl@0
    31
                               G_MARKUP_COLLECT_INVALID)
sl@0
    32
#else
sl@0
    33
#define collect(args...) \
sl@0
    34
  g_markup_collect_attributes (element_name, attribute_names, \
sl@0
    35
                               attribute_values, error, args, \
sl@0
    36
                               G_MARKUP_COLLECT_INVALID)
sl@0
    37
#endif//__SYMBIAN32__							   
sl@0
    38
#define BOOL    G_MARKUP_COLLECT_BOOLEAN
sl@0
    39
#define OPTBOOL G_MARKUP_COLLECT_BOOLEAN | G_MARKUP_COLLECT_OPTIONAL
sl@0
    40
#define TRI     G_MARKUP_COLLECT_TRISTATE
sl@0
    41
#define STR     G_MARKUP_COLLECT_STRING
sl@0
    42
#define STRDUP  G_MARKUP_COLLECT_STRDUP
sl@0
    43
#define OPTSTR  G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL
sl@0
    44
#define OPTDUP  G_MARKUP_COLLECT_STRDUP | G_MARKUP_COLLECT_OPTIONAL
sl@0
    45
#define n(x)    ((x)?(x):"(null)")
sl@0
    46
sl@0
    47
  if (strcmp (element_name, "bool") == 0)
sl@0
    48
    {
sl@0
    49
      gboolean mb = 2, ob = 2, tri = 2;
sl@0
    50
sl@0
    51
      result = collect (BOOL,    "mb", &mb,
sl@0
    52
                        OPTBOOL, "ob", &ob,
sl@0
    53
                        TRI,     "tri", &tri);
sl@0
    54
sl@0
    55
      g_assert (result ||
sl@0
    56
                (mb == FALSE && ob == FALSE && tri != TRUE && tri != FALSE));
sl@0
    57
sl@0
    58
      if (tri != FALSE && tri != TRUE)
sl@0
    59
        tri = -1;
sl@0
    60
sl@0
    61
      g_string_append_printf (string, "<bool(%d) %d %d %d>",
sl@0
    62
                              result, mb, ob, tri);
sl@0
    63
    }
sl@0
    64
sl@0
    65
  else if (strcmp (element_name, "str") == 0)
sl@0
    66
    {
sl@0
    67
      const char *cm, *co;
sl@0
    68
      char *am, *ao;
sl@0
    69
sl@0
    70
      result = collect (STR,    "cm", &cm,
sl@0
    71
                        STRDUP, "am", &am,
sl@0
    72
                        OPTDUP, "ao", &ao,
sl@0
    73
                        OPTSTR, "co", &co);
sl@0
    74
sl@0
    75
      g_assert (result ||
sl@0
    76
                (cm == NULL && am == NULL && ao == NULL && co == NULL));
sl@0
    77
sl@0
    78
      g_string_append_printf (string, "<str(%d) %s %s %s %s>",
sl@0
    79
                              result, n (cm), n (am), n (ao), n (co));
sl@0
    80
sl@0
    81
      g_free (am);
sl@0
    82
      g_free (ao);
sl@0
    83
    }
sl@0
    84
}
sl@0
    85
sl@0
    86
static GMarkupParser parser = { start };
sl@0
    87
sl@0
    88
struct test
sl@0
    89
{
sl@0
    90
  const char   *document;
sl@0
    91
  const char   *result;
sl@0
    92
  GMarkupError  error_code;
sl@0
    93
  const char   *error_info;
sl@0
    94
};
sl@0
    95
sl@0
    96
static struct test tests[] =
sl@0
    97
{
sl@0
    98
  { "<bool mb='y'>", "<bool(1) 1 0 -1>",
sl@0
    99
    G_MARKUP_ERROR_PARSE, "'bool'" },
sl@0
   100
sl@0
   101
  { "<bool mb='false'/>", "<bool(1) 0 0 -1>" },
sl@0
   102
  { "<bool mb='true'/>", "<bool(1) 1 0 -1>" },
sl@0
   103
  { "<bool mb='t' ob='f' tri='1'/>", "<bool(1) 1 0 1>" },
sl@0
   104
  { "<bool mb='y' ob='n' tri='0'/>", "<bool(1) 1 0 0>" },
sl@0
   105
sl@0
   106
  { "<bool ob='y'/>", "<bool(0) 0 0 -1>",
sl@0
   107
    G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'mb'" },
sl@0
   108
sl@0
   109
  { "<bool mb='y' mb='y'/>", "<bool(0) 0 0 -1>",
sl@0
   110
    G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" },
sl@0
   111
sl@0
   112
  { "<bool mb='y' tri='y' tri='n'/>", "<bool(0) 0 0 -1>",
sl@0
   113
    G_MARKUP_ERROR_INVALID_CONTENT, "'tri'" },
sl@0
   114
sl@0
   115
  { "<str cm='x' am='y'/>", "<str(1) x y (null) (null)>" },
sl@0
   116
sl@0
   117
  { "<str am='x' co='y'/>", "<str(0) (null) (null) (null) (null)>",
sl@0
   118
    G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" },
sl@0
   119
sl@0
   120
  { "<str am='x'/>", "<str(0) (null) (null) (null) (null)>",
sl@0
   121
    G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" },
sl@0
   122
sl@0
   123
  { "<str am='x' cm='x' am='y'/>", "<str(0) (null) (null) (null) (null)>",
sl@0
   124
    G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
sl@0
   125
sl@0
   126
  { "<str am='x' qm='y' cm='x'/>", "<str(0) (null) (null) (null) (null)>",
sl@0
   127
    G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, "'qm'" },
sl@0
   128
sl@0
   129
  { "<str am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' cm='x'/>", "<str(0) (null) (null) (null) (null)>",
sl@0
   130
    G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
sl@0
   131
sl@0
   132
  { "<str cm='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x'/>", "<str(0) (null) (null) (null) (null)>",
sl@0
   133
    G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
sl@0
   134
sl@0
   135
  { "<str a='x' b='x' c='x' d='x' e='x' f='x' g='x' h='x' i='x' j='x' k='x' l='x' m='x' n='x' o='x' p='x' q='x' r='x' s='x' t='x' u='x' v='x' w='x' x='x' y='x' z='x' aa='x' bb='x' cc='x' dd='x' ee='x' ff='x' gg='x' hh='x' ii='x' jj='x' kk='x' ll='x' mm='x' nn='x' oo='x' pp='x' qq='x' rr='x' ss='x' tt='x' uu='x' vv='x' ww='x' xx='x' yy='x' zz='x' am='x' cm='x'/>",
sl@0
   136
    "<str(0) (null) (null) (null) (null)>",
sl@0
   137
    G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, "'a'" },
sl@0
   138
sl@0
   139
  { "<bool mb='ja'/>", "<bool(0) 0 0 -1>",
sl@0
   140
    G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" },
sl@0
   141
sl@0
   142
  { "<bool mb='nein'/>", "<bool(0) 0 0 -1>",
sl@0
   143
    G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" }
sl@0
   144
};
sl@0
   145
sl@0
   146
int
sl@0
   147
main (int argc, char **argv)
sl@0
   148
{
sl@0
   149
  gboolean verbose = FALSE;
sl@0
   150
  int i;
sl@0
   151
#ifdef __SYMBIAN32__
sl@0
   152
  g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
sl@0
   153
  g_set_print_handler(mrtPrintHandler);
sl@0
   154
#endif /*__SYMBIAN32__*/
sl@0
   155
  if (argc > 1)
sl@0
   156
    {
sl@0
   157
      if (argc != 2 || strcmp (argv[1], "-v") != 0)
sl@0
   158
        {
sl@0
   159
          g_print ("error: call with no arguments or '-v' for verbose\n");
sl@0
   160
          return 1;
sl@0
   161
        }
sl@0
   162
sl@0
   163
      verbose = TRUE;
sl@0
   164
    }
sl@0
   165
sl@0
   166
  for (i = 0; i < G_N_ELEMENTS (tests); i++)
sl@0
   167
    {
sl@0
   168
      GMarkupParseContext *ctx;
sl@0
   169
      GError *error = NULL;
sl@0
   170
      GString *string;
sl@0
   171
      gboolean result;
sl@0
   172
sl@0
   173
      string = g_string_new ("");
sl@0
   174
      ctx = g_markup_parse_context_new (&parser, 0, string, NULL);
sl@0
   175
      result = g_markup_parse_context_parse (ctx,
sl@0
   176
                                             tests[i].document,
sl@0
   177
                                             -1, &error);
sl@0
   178
      if (result)
sl@0
   179
        result = g_markup_parse_context_end_parse (ctx, &error);
sl@0
   180
sl@0
   181
      if (verbose)
sl@0
   182
        g_print ("%d: %s:\n  (error %d, \"%s\")\n  %s\n\n",
sl@0
   183
                 i, tests[i].document,
sl@0
   184
                 error ? error->code : 0,
sl@0
   185
                 error ? error->message : "(no error)",
sl@0
   186
                 string->str);
sl@0
   187
sl@0
   188
      if (result)
sl@0
   189
        {
sl@0
   190
          if (error != NULL)
sl@0
   191
            g_error ("parser successful but error is set: "
sl@0
   192
                     "%s(%d) '%s'", g_quark_to_string (error->domain),
sl@0
   193
                     error->code, error->message);
sl@0
   194
sl@0
   195
          if (tests[i].error_code != 0)
sl@0
   196
            g_error ("parser succeeded on test %d ('%s') but "
sl@0
   197
                     "we expected a failure with code %d\n", i,
sl@0
   198
                     tests[i].document, tests[i].error_code);
sl@0
   199
        }
sl@0
   200
      else
sl@0
   201
        {
sl@0
   202
          if (error->domain != G_MARKUP_ERROR)
sl@0
   203
            g_error ("error occured on test %d ('%s') but is not in "
sl@0
   204
                     "the GMarkupError domain, but rather '%s'", i,
sl@0
   205
                     tests[i].document, g_quark_to_string (error->domain));
sl@0
   206
sl@0
   207
          if (error->code != tests[i].error_code)
sl@0
   208
            g_error ("failure expected with test %d ('%s') but it "
sl@0
   209
                    "has error code %d (we expected code %d)", i,
sl@0
   210
                    tests[i].document, error->code, tests[i].error_code);
sl@0
   211
sl@0
   212
          if (strstr (error->message, tests[i].error_info) == NULL)
sl@0
   213
            g_error ("failure message on test %d ('%s') fails "
sl@0
   214
                     "to mention '%s' in the error message", i,
sl@0
   215
                     tests[i].document, tests[i].error_info);
sl@0
   216
        }
sl@0
   217
sl@0
   218
      if (strcmp (tests[i].result, string->str) != 0)
sl@0
   219
        g_error ("result on test %d ('%s') expected to be '%s' "
sl@0
   220
                 "but came out as '%s'", i, tests[i].document,
sl@0
   221
                 tests[i].result, string->str);
sl@0
   222
sl@0
   223
      g_markup_parse_context_free (ctx);
sl@0
   224
      g_string_free (string, TRUE);
sl@0
   225
      g_clear_error (&error);
sl@0
   226
    }
sl@0
   227
sl@0
   228
  if (verbose)
sl@0
   229
    g_print ("\n*** all tests passed ***\n\n");
sl@0
   230
#ifdef __SYMBIAN32__
sl@0
   231
    testResultXml("markup-collect");
sl@0
   232
#endif /* EMULATOR */
sl@0
   233
  return 0;
sl@0
   234
}