glib/tsrc/BC/src/markup_test.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     3 *
       
     4 * This library is free software; you can redistribute it and/or
       
     5 * modify it under the terms of the GNU Lesser General Public
       
     6 * License as published by the Free Software Foundation; either
       
     7 * version 2 of the License, or (at your option) any later version.
       
     8 *
       
     9 * This library 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.  See the GNU
       
    12 * Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public
       
    15 * License along with this library; if not, write to the
       
    16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17 * Boston, MA 02111-1307, USA.
       
    18 *
       
    19 * Description:
       
    20 *
       
    21 */
       
    22 
       
    23 
       
    24 
       
    25 /* This test case is dependent on the markup filr 1.gmarkup placed in the c dir.
       
    26    Make sure that the file is present before this test case is run. Export the file
       
    27    */
       
    28 
       
    29 #undef G_DISABLE_ASSERT
       
    30 #undef G_LOG_DOMAIN
       
    31 
       
    32 #include <stdio.h>
       
    33 #include <glib.h>
       
    34 
       
    35 #ifdef SYMBIAN
       
    36 #include "mrt2_glib2_test.h"
       
    37 #endif /*SYMBIAN*/
       
    38 
       
    39 static int depth = 0;
       
    40 
       
    41 
       
    42 static void
       
    43 start_element_handler  (GMarkupParseContext *context,
       
    44                         const gchar         *element_name,
       
    45                         const gchar        **attribute_names,
       
    46                         const gchar        **attribute_values,
       
    47                         gpointer             user_data,
       
    48                         GError             **error)
       
    49 {
       
    50   ++depth;
       
    51 }
       
    52 
       
    53 static void
       
    54 end_element_handler    (GMarkupParseContext *context,
       
    55                         const gchar         *element_name,
       
    56                         gpointer             user_data,
       
    57                         GError             **error)
       
    58 {
       
    59   --depth;
       
    60 }
       
    61 
       
    62 static void
       
    63 text_handler           (GMarkupParseContext *context,
       
    64                         const gchar         *text,
       
    65                         gsize                text_len,
       
    66                         gpointer             user_data,
       
    67                         GError             **error)
       
    68 {
       
    69 
       
    70 }
       
    71 
       
    72 
       
    73 static void
       
    74 passthrough_handler    (GMarkupParseContext *context,
       
    75                         const gchar         *passthrough_text,
       
    76                         gsize                text_len,
       
    77                         gpointer             user_data,
       
    78                         GError             **error)
       
    79 {
       
    80 }
       
    81 
       
    82 static void
       
    83 error_handler          (GMarkupParseContext *context,
       
    84                         GError              *error,
       
    85                         gpointer             user_data)
       
    86 {
       
    87   g_assert(FALSE && "markup_test failed");
       
    88   g_print(" %s\n", error->message);
       
    89 }
       
    90 
       
    91 static GMarkupParser parser = {
       
    92   start_element_handler,
       
    93   end_element_handler,
       
    94   text_handler,
       
    95   passthrough_handler,
       
    96   error_handler
       
    97 };
       
    98 
       
    99 static int
       
   100 test_in_chunks (const gchar *contents,
       
   101                 gint         length,
       
   102                 gint         chunk_size)
       
   103 {
       
   104   GMarkupParseContext *context;
       
   105   int i = 0;
       
   106   int line_number,char_number;
       
   107   
       
   108   context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
       
   109 
       
   110   while (i < length)
       
   111     {
       
   112       int this_chunk = MIN (length - i, chunk_size);
       
   113 
       
   114       if (!g_markup_parse_context_parse (context,
       
   115                                          contents + i,
       
   116                                          this_chunk,
       
   117                                          NULL))
       
   118         {
       
   119           g_markup_parse_context_free (context);
       
   120           return 1;
       
   121      
       
   122         }
       
   123     
       
   124 	g_assert(g_markup_parse_context_get_element(context) == NULL || !strcmp(g_markup_parse_context_get_element(context),"foobar"));
       
   125 	
       
   126 	g_markup_parse_context_get_position(context,&line_number,&char_number);
       
   127 	
       
   128 	g_assert(line_number == 1 && char_number == 25);
       
   129     
       
   130     i += this_chunk;
       
   131     }
       
   132       
       
   133   if (!g_markup_parse_context_end_parse (context, NULL))
       
   134     {
       
   135       g_markup_parse_context_free (context);
       
   136       return 1;
       
   137     }
       
   138 
       
   139   g_markup_parse_context_free (context);
       
   140 
       
   141   return 0;
       
   142 }
       
   143 
       
   144 static int
       
   145 test_file (const gchar *filename)
       
   146 {
       
   147   gchar *contents;
       
   148   gsize  length;
       
   149   GError *error;
       
   150   GMarkupParseContext *context;
       
   151   
       
   152   error = NULL;
       
   153   if (!g_file_get_contents (filename,
       
   154                             &contents,
       
   155                             &length,
       
   156                             &error))
       
   157     {
       
   158       g_assert(FALSE && "c:\\1.gmarkup not found");
       
   159       fprintf (stderr, "%s\n", error->message);
       
   160       g_error_free (error);
       
   161       return 1;
       
   162     }
       
   163 
       
   164   context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
       
   165 
       
   166   if (!g_markup_parse_context_parse (context, contents, length, NULL))
       
   167     {
       
   168       g_markup_parse_context_free (context);
       
   169       return 1;
       
   170     }
       
   171 
       
   172   if (!g_markup_parse_context_end_parse (context, NULL))
       
   173     {
       
   174       g_markup_parse_context_free (context);
       
   175       return 1;
       
   176     }
       
   177 
       
   178   g_markup_parse_context_free (context);
       
   179 
       
   180   if (test_in_chunks (contents, length, 24) != 0)
       
   181     return 1;
       
   182   
       
   183   return 0;
       
   184 }
       
   185 
       
   186 void g_markup_printf_escaped_test()
       
   187 {
       
   188 	const char *store = "Fortnum & Mason";
       
   189 	const char *item = "Tea";
       
   190 	char *output;
       
   191  
       
   192 	output = g_markup_printf_escaped ("<purchase>"
       
   193                                   "<store>%s</store>"
       
   194                                   "<item>%s</item>"
       
   195                                   "</purchase>",
       
   196                                   store, item);
       
   197                                   
       
   198     g_assert(!strcmp(output,"<purchase><store>Fortnum &amp; Mason</store><item>Tea</item></purchase>"));
       
   199 }
       
   200 
       
   201 int
       
   202 main (int   argc,
       
   203       char *argv[])
       
   204 {
       
   205   
       
   206 	#ifdef SYMBIAN
       
   207 	
       
   208 	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);
       
   209 	g_set_print_handler(mrtPrintHandler);
       
   210 	#endif /*SYMBIAN*/
       
   211   
       
   212 	test_file ("c:\\1.gmarkup");
       
   213 	g_markup_printf_escaped_test();
       
   214 	
       
   215 	#ifdef SYMBIAN
       
   216   	testResultXml("markup_test");
       
   217   	#endif /* EMULATOR */
       
   218 	
       
   219 	return 0;
       
   220 }
       
   221