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