1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/src/markup_test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,221 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.6 +*
1.7 +* This library is free software; you can redistribute it and/or
1.8 +* modify it under the terms of the GNU Lesser General Public
1.9 +* License as published by the Free Software Foundation; either
1.10 +* version 2 of the License, or (at your option) any later version.
1.11 +*
1.12 +* This library is distributed in the hope that it will be useful,
1.13 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 +* Lesser General Public License for more details.
1.16 +*
1.17 +* You should have received a copy of the GNU Lesser General Public
1.18 +* License along with this library; if not, write to the
1.19 +* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.20 +* Boston, MA 02111-1307, USA.
1.21 +*
1.22 +* Description:
1.23 +*
1.24 +*/
1.25 +
1.26 +
1.27 +
1.28 +/* This test case is dependent on the markup filr 1.gmarkup placed in the c dir.
1.29 + Make sure that the file is present before this test case is run. Export the file
1.30 + */
1.31 +
1.32 +#undef G_DISABLE_ASSERT
1.33 +#undef G_LOG_DOMAIN
1.34 +
1.35 +#include <stdio.h>
1.36 +#include <glib.h>
1.37 +
1.38 +#ifdef SYMBIAN
1.39 +#include "mrt2_glib2_test.h"
1.40 +#endif /*SYMBIAN*/
1.41 +
1.42 +static int depth = 0;
1.43 +
1.44 +
1.45 +static void
1.46 +start_element_handler (GMarkupParseContext *context,
1.47 + const gchar *element_name,
1.48 + const gchar **attribute_names,
1.49 + const gchar **attribute_values,
1.50 + gpointer user_data,
1.51 + GError **error)
1.52 +{
1.53 + ++depth;
1.54 +}
1.55 +
1.56 +static void
1.57 +end_element_handler (GMarkupParseContext *context,
1.58 + const gchar *element_name,
1.59 + gpointer user_data,
1.60 + GError **error)
1.61 +{
1.62 + --depth;
1.63 +}
1.64 +
1.65 +static void
1.66 +text_handler (GMarkupParseContext *context,
1.67 + const gchar *text,
1.68 + gsize text_len,
1.69 + gpointer user_data,
1.70 + GError **error)
1.71 +{
1.72 +
1.73 +}
1.74 +
1.75 +
1.76 +static void
1.77 +passthrough_handler (GMarkupParseContext *context,
1.78 + const gchar *passthrough_text,
1.79 + gsize text_len,
1.80 + gpointer user_data,
1.81 + GError **error)
1.82 +{
1.83 +}
1.84 +
1.85 +static void
1.86 +error_handler (GMarkupParseContext *context,
1.87 + GError *error,
1.88 + gpointer user_data)
1.89 +{
1.90 + g_assert(FALSE && "markup_test failed");
1.91 + g_print(" %s\n", error->message);
1.92 +}
1.93 +
1.94 +static GMarkupParser parser = {
1.95 + start_element_handler,
1.96 + end_element_handler,
1.97 + text_handler,
1.98 + passthrough_handler,
1.99 + error_handler
1.100 +};
1.101 +
1.102 +static int
1.103 +test_in_chunks (const gchar *contents,
1.104 + gint length,
1.105 + gint chunk_size)
1.106 +{
1.107 + GMarkupParseContext *context;
1.108 + int i = 0;
1.109 + int line_number,char_number;
1.110 +
1.111 + context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
1.112 +
1.113 + while (i < length)
1.114 + {
1.115 + int this_chunk = MIN (length - i, chunk_size);
1.116 +
1.117 + if (!g_markup_parse_context_parse (context,
1.118 + contents + i,
1.119 + this_chunk,
1.120 + NULL))
1.121 + {
1.122 + g_markup_parse_context_free (context);
1.123 + return 1;
1.124 +
1.125 + }
1.126 +
1.127 + g_assert(g_markup_parse_context_get_element(context) == NULL || !strcmp(g_markup_parse_context_get_element(context),"foobar"));
1.128 +
1.129 + g_markup_parse_context_get_position(context,&line_number,&char_number);
1.130 +
1.131 + g_assert(line_number == 1 && char_number == 25);
1.132 +
1.133 + i += this_chunk;
1.134 + }
1.135 +
1.136 + if (!g_markup_parse_context_end_parse (context, NULL))
1.137 + {
1.138 + g_markup_parse_context_free (context);
1.139 + return 1;
1.140 + }
1.141 +
1.142 + g_markup_parse_context_free (context);
1.143 +
1.144 + return 0;
1.145 +}
1.146 +
1.147 +static int
1.148 +test_file (const gchar *filename)
1.149 +{
1.150 + gchar *contents;
1.151 + gsize length;
1.152 + GError *error;
1.153 + GMarkupParseContext *context;
1.154 +
1.155 + error = NULL;
1.156 + if (!g_file_get_contents (filename,
1.157 + &contents,
1.158 + &length,
1.159 + &error))
1.160 + {
1.161 + g_assert(FALSE && "c:\\1.gmarkup not found");
1.162 + fprintf (stderr, "%s\n", error->message);
1.163 + g_error_free (error);
1.164 + return 1;
1.165 + }
1.166 +
1.167 + context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
1.168 +
1.169 + if (!g_markup_parse_context_parse (context, contents, length, NULL))
1.170 + {
1.171 + g_markup_parse_context_free (context);
1.172 + return 1;
1.173 + }
1.174 +
1.175 + if (!g_markup_parse_context_end_parse (context, NULL))
1.176 + {
1.177 + g_markup_parse_context_free (context);
1.178 + return 1;
1.179 + }
1.180 +
1.181 + g_markup_parse_context_free (context);
1.182 +
1.183 + if (test_in_chunks (contents, length, 24) != 0)
1.184 + return 1;
1.185 +
1.186 + return 0;
1.187 +}
1.188 +
1.189 +void g_markup_printf_escaped_test()
1.190 +{
1.191 + const char *store = "Fortnum & Mason";
1.192 + const char *item = "Tea";
1.193 + char *output;
1.194 +
1.195 + output = g_markup_printf_escaped ("<purchase>"
1.196 + "<store>%s</store>"
1.197 + "<item>%s</item>"
1.198 + "</purchase>",
1.199 + store, item);
1.200 +
1.201 + g_assert(!strcmp(output,"<purchase><store>Fortnum & Mason</store><item>Tea</item></purchase>"));
1.202 +}
1.203 +
1.204 +int
1.205 +main (int argc,
1.206 + char *argv[])
1.207 +{
1.208 +
1.209 + #ifdef SYMBIAN
1.210 +
1.211 + 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);
1.212 + g_set_print_handler(mrtPrintHandler);
1.213 + #endif /*SYMBIAN*/
1.214 +
1.215 + test_file ("c:\\1.gmarkup");
1.216 + g_markup_printf_escaped_test();
1.217 +
1.218 + #ifdef SYMBIAN
1.219 + testResultXml("markup_test");
1.220 + #endif /* EMULATOR */
1.221 +
1.222 + return 0;
1.223 +}
1.224 +