Imported Upstream version 2.50.2
[platform/upstream/glib.git] / glib / tests / markup-parse.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include <locale.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <glib.h>
8
9 static int depth = 0;
10 static GString *string;
11
12 static void
13 indent (int extra)
14 {
15   int i = 0;
16   while (i < depth)
17     {
18       g_string_append (string, "  ");
19       ++i;
20     }
21 }
22
23 static void
24 start_element_handler  (GMarkupParseContext *context,
25                         const gchar         *element_name,
26                         const gchar        **attribute_names,
27                         const gchar        **attribute_values,
28                         gpointer             user_data,
29                         GError             **error)
30 {
31   int i;
32   
33   indent (0);
34   g_string_append_printf (string, "ELEMENT '%s'\n", element_name);
35
36   i = 0;
37   while (attribute_names[i] != NULL)
38     {
39       indent (1);
40
41       g_string_append_printf (string, "%s=\"%s\"\n",
42                               attribute_names[i],
43                               attribute_values[i]);
44       
45       ++i;
46     }
47   
48   ++depth;
49 }
50
51 static void
52 end_element_handler (GMarkupParseContext *context,
53                      const gchar         *element_name,
54                      gpointer             user_data,
55                      GError             **error)
56 {
57   --depth;
58   indent (0);
59   g_string_append_printf (string, "END '%s'\n", element_name);
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   indent (0);
70   g_string_append_printf (string, "TEXT '%.*s'\n", (int)text_len, text);
71 }
72
73
74 static void
75 passthrough_handler (GMarkupParseContext *context,
76                      const gchar         *passthrough_text,
77                      gsize                text_len,
78                      gpointer             user_data,
79                      GError             **error)
80 {
81   indent (0);
82
83   g_string_append_printf (string, "PASS '%.*s'\n", (int)text_len, passthrough_text);
84 }
85
86 static void
87 error_handler (GMarkupParseContext *context,
88                GError              *error,
89                gpointer             user_data)
90 {
91   g_string_append_printf (string, "ERROR %s\n", error->message);
92 }
93
94 static const GMarkupParser parser = {
95   start_element_handler,
96   end_element_handler,
97   text_handler,
98   passthrough_handler,
99   error_handler
100 };
101
102 static const GMarkupParser silent_parser = {
103   NULL,
104   NULL,
105   NULL,
106   NULL,
107   error_handler
108 };
109
110 static int
111 test_in_chunks (const gchar       *contents,
112                 gint               length,
113                 gint               chunk_size,
114                 GMarkupParseFlags  flags)
115 {
116   GMarkupParseContext *context;
117   int i = 0;
118   
119   context = g_markup_parse_context_new (&silent_parser, flags, NULL, NULL);
120
121   while (i < length)
122     {
123       int this_chunk = MIN (length - i, chunk_size);
124
125       if (!g_markup_parse_context_parse (context,
126                                          contents + i,
127                                          this_chunk,
128                                          NULL))
129         {
130           g_markup_parse_context_free (context);
131           return 1;
132         }
133
134       i += this_chunk;
135     }
136       
137   if (!g_markup_parse_context_end_parse (context, NULL))
138     {
139       g_markup_parse_context_free (context);
140       return 1;
141     }
142
143   g_markup_parse_context_free (context);
144
145   return 0;
146 }
147
148 static int
149 test_file (const gchar *filename, GMarkupParseFlags flags)
150 {
151   gchar *contents;
152   gsize  length;
153   GError *error;
154   GMarkupParseContext *context;
155   gint line, col;
156
157   error = NULL;
158   if (!g_file_get_contents (filename,
159                             &contents,
160                             &length,
161                             &error))
162     {
163       fprintf (stderr, "%s\n", error->message);
164       g_error_free (error);
165       return 1;
166     }
167
168   context = g_markup_parse_context_new (&parser, flags, NULL, NULL);
169   g_assert (g_markup_parse_context_get_user_data (context) == NULL);
170   g_markup_parse_context_get_position (context, &line, &col);
171   g_assert (line == 1 && col == 1);
172
173   if (!g_markup_parse_context_parse (context, contents, length, NULL))
174     {
175       g_markup_parse_context_free (context);
176       g_free (contents);
177       return 1;
178     }
179
180   if (!g_markup_parse_context_end_parse (context, NULL))
181     {
182       g_markup_parse_context_free (context);
183       g_free (contents);
184       return 1;
185     }
186
187   g_markup_parse_context_free (context);
188
189   /* A byte at a time */
190   if (test_in_chunks (contents, length, 1, flags) != 0)
191     {
192       g_free (contents);
193       return 1;
194     }
195
196   /* 2 bytes */
197   if (test_in_chunks (contents, length, 2, flags) != 0)
198     {
199       g_free (contents);
200       return 1;
201     }
202
203   /* 5 bytes */
204   if (test_in_chunks (contents, length, 5, flags) != 0)
205     {
206       g_free (contents);
207       return 1;
208     }
209
210   /* 12 bytes */
211   if (test_in_chunks (contents, length, 12, flags) != 0)
212     {
213       g_free (contents);
214       return 1;
215     }
216
217   /* 1024 bytes */
218   if (test_in_chunks (contents, length, 1024, flags) != 0)
219     {
220       g_free (contents);
221       return 1;
222     }
223
224   g_free (contents);
225
226   return 0;
227 }
228
229 static gchar *
230 get_expected_filename (const gchar       *filename,
231                        GMarkupParseFlags  flags)
232 {
233   gchar *f, *p, *expected;
234
235   f = g_strdup (filename);
236   p = strstr (f, ".gmarkup");
237   if (p)
238     *p = 0;
239   if (flags == 0)
240     expected = g_strconcat (f, ".expected", NULL);
241   else if (flags == G_MARKUP_TREAT_CDATA_AS_TEXT)
242     expected = g_strconcat (f, ".cdata-as-text", NULL);
243   else
244     g_assert_not_reached ();
245
246   g_free (f);
247
248   return expected;
249 }
250
251 static void
252 test_parse (gconstpointer d)
253 {
254   const gchar *filename = d;
255   gchar *expected_file;
256   gchar *expected;
257   gboolean valid_input;
258   GError *error = NULL;
259   gint res;
260
261   valid_input = strstr (filename, "valid") != NULL;
262   expected_file = get_expected_filename (filename, 0);
263
264   depth = 0;
265   string = g_string_sized_new (0);
266
267   res = test_file (filename, 0);
268   g_assert_cmpint (res, ==, valid_input ? 0 : 1);
269
270   g_file_get_contents (expected_file, &expected, NULL, &error);
271   g_assert_no_error (error);
272   g_assert_cmpstr (string->str, ==, expected);
273   g_free (expected);
274
275   g_string_free (string, TRUE);
276
277   g_free (expected_file);
278
279   expected_file = get_expected_filename (filename, G_MARKUP_TREAT_CDATA_AS_TEXT);
280   if (g_file_test (expected_file, G_FILE_TEST_EXISTS))
281     {
282       depth = 0;
283       string = g_string_sized_new (0);
284
285       res = test_file (filename, G_MARKUP_TREAT_CDATA_AS_TEXT);
286       g_assert_cmpint (res, ==, valid_input ? 0 : 1);
287
288       g_file_get_contents (expected_file, &expected, NULL, &error);
289       g_assert_no_error (error);
290       g_assert_cmpstr (string->str, ==, expected);
291       g_free (expected);
292
293       g_string_free (string, TRUE);
294     }
295
296   g_free (expected_file);
297 }
298
299 int
300 main (int argc, char *argv[])
301 {
302   GDir *dir;
303   GError *error;
304   const gchar *name;
305   gchar *path;
306
307   g_setenv ("LC_ALL", "C", TRUE);
308   setlocale (LC_ALL, "");
309
310   g_test_init (&argc, &argv, NULL);
311
312   /* allow to easily generate expected output for new test cases */
313   if (argc > 1)
314     {
315       gint arg = 1;
316       GMarkupParseFlags flags = 0;
317
318       if (strcmp (argv[1], "--cdata-as-text") == 0)
319         {
320           flags = G_MARKUP_TREAT_CDATA_AS_TEXT;
321           arg = 2;
322         }
323       string = g_string_sized_new (0);
324       test_file (argv[arg], flags);
325       g_print ("%s", string->str);
326       return 0;
327     }
328
329   error = NULL;
330   path = g_test_build_filename (G_TEST_DIST, "markups", NULL);
331   dir = g_dir_open (path, 0, &error);
332   g_free (path);
333   g_assert_no_error (error);
334   while ((name = g_dir_read_name (dir)) != NULL)
335     {
336       if (!strstr (name, "gmarkup"))
337         continue;
338
339       path = g_strdup_printf ("/markup/parse/%s", name);
340       g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "markups", name, NULL),
341                                  test_parse, g_free);
342       g_free (path);
343     }
344   g_dir_close (dir);
345
346   return g_test_run ();
347 }
348