1 #undef G_DISABLE_ASSERT
10 static GString *string;
18 g_string_append (string, " ");
24 start_element_handler (GMarkupParseContext *context,
25 const gchar *element_name,
26 const gchar **attribute_names,
27 const gchar **attribute_values,
34 g_string_append_printf (string, "ELEMENT '%s'\n", element_name);
37 while (attribute_names[i] != NULL)
41 g_string_append_printf (string, "%s=\"%s\"\n",
52 end_element_handler (GMarkupParseContext *context,
53 const gchar *element_name,
59 g_string_append_printf (string, "END '%s'\n", element_name);
63 text_handler (GMarkupParseContext *context,
70 g_string_append_printf (string, "TEXT '%.*s'\n", (int)text_len, text);
75 passthrough_handler (GMarkupParseContext *context,
76 const gchar *passthrough_text,
83 g_string_append_printf (string, "PASS '%.*s'\n", (int)text_len, passthrough_text);
87 error_handler (GMarkupParseContext *context,
91 g_string_append_printf (string, "ERROR %s\n", error->message);
94 static const GMarkupParser parser = {
95 start_element_handler,
102 static const GMarkupParser silent_parser = {
111 test_in_chunks (const gchar *contents,
114 GMarkupParseFlags flags)
116 GMarkupParseContext *context;
119 context = g_markup_parse_context_new (&silent_parser, flags, NULL, NULL);
123 int this_chunk = MIN (length - i, chunk_size);
125 if (!g_markup_parse_context_parse (context,
130 g_markup_parse_context_free (context);
137 if (!g_markup_parse_context_end_parse (context, NULL))
139 g_markup_parse_context_free (context);
143 g_markup_parse_context_free (context);
149 test_file (const gchar *filename, GMarkupParseFlags flags)
154 GMarkupParseContext *context;
158 if (!g_file_get_contents (filename,
163 fprintf (stderr, "%s\n", error->message);
164 g_error_free (error);
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);
173 if (!g_markup_parse_context_parse (context, contents, length, NULL))
175 g_markup_parse_context_free (context);
180 if (!g_markup_parse_context_end_parse (context, NULL))
182 g_markup_parse_context_free (context);
187 g_markup_parse_context_free (context);
189 /* A byte at a time */
190 if (test_in_chunks (contents, length, 1, flags) != 0)
197 if (test_in_chunks (contents, length, 2, flags) != 0)
204 if (test_in_chunks (contents, length, 5, flags) != 0)
211 if (test_in_chunks (contents, length, 12, flags) != 0)
218 if (test_in_chunks (contents, length, 1024, flags) != 0)
230 get_expected_filename (const gchar *filename,
231 GMarkupParseFlags flags)
233 gchar *f, *p, *expected;
235 f = g_strdup (filename);
236 p = strstr (f, ".gmarkup");
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);
250 test_parse (gconstpointer d)
252 const gchar *filename = d;
253 gchar *expected_file;
255 gboolean valid_input;
256 GError *error = NULL;
259 valid_input = strstr (filename, "valid") != NULL;
260 expected_file = get_expected_filename (filename, 0);
263 string = g_string_sized_new (0);
265 res = test_file (filename, 0);
266 g_assert_cmpint (res, ==, valid_input ? 0 : 1);
268 g_file_get_contents (expected_file, &expected, NULL, &error);
269 g_assert_no_error (error);
270 g_assert_cmpstr (string->str, ==, expected);
273 g_string_free (string, TRUE);
275 g_free (expected_file);
277 expected_file = get_expected_filename (filename, G_MARKUP_TREAT_CDATA_AS_TEXT);
278 if (g_file_test (expected_file, G_FILE_TEST_EXISTS))
281 string = g_string_sized_new (0);
283 res = test_file (filename, G_MARKUP_TREAT_CDATA_AS_TEXT);
284 g_assert_cmpint (res, ==, valid_input ? 0 : 1);
286 g_file_get_contents (expected_file, &expected, NULL, &error);
287 g_assert_no_error (error);
288 g_assert_cmpstr (string->str, ==, expected);
291 g_string_free (string, TRUE);
294 g_free (expected_file);
298 main (int argc, char *argv[])
305 g_setenv ("LC_ALL", "C", TRUE);
306 setlocale (LC_ALL, "");
308 g_test_init (&argc, &argv, NULL);
310 /* allow to easily generate expected output for new test cases */
314 GMarkupParseFlags flags = 0;
316 if (strcmp (argv[1], "--cdata-as-text") == 0)
318 flags = G_MARKUP_TREAT_CDATA_AS_TEXT;
321 string = g_string_sized_new (0);
322 test_file (argv[arg], flags);
323 g_print ("%s", string->str);
328 path = g_test_build_filename (G_TEST_DIST, "markups", NULL);
329 dir = g_dir_open (path, 0, &error);
331 g_assert_no_error (error);
332 while ((name = g_dir_read_name (dir)) != NULL)
334 if (!strstr (name, "gmarkup"))
337 path = g_strdup_printf ("/markup/parse/%s", name);
338 g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "markups", name, NULL),
344 return g_test_run ();