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);
148 /* Load the given @filename and parse it multiple times with different chunking
149 * and length handling. All results should be equal. %TRUE is returned if the
150 * file was parsed successfully on every attempt; %FALSE if it failed to parse
151 * on every attempt. The test aborts if some attempts succeed and some fail. */
153 test_file (const gchar *filename,
154 GMarkupParseFlags flags)
156 gchar *contents = NULL, *contents_unterminated = NULL;
158 GError *local_error = NULL;
159 GMarkupParseContext *context;
161 guint n_failures = 0;
163 const gsize chunk_sizes_bytes[] = { 1, 2, 5, 12, 1024 };
165 GString *first_string = NULL;
167 g_file_get_contents (filename, &contents, &length_bytes, &local_error);
168 g_assert_no_error (local_error);
170 /* Make a copy of the contents with no trailing nul. */
171 contents_unterminated = g_malloc (length_bytes);
172 if (contents_unterminated != NULL)
173 memcpy (contents_unterminated, contents, length_bytes);
175 /* Test with nul termination. */
176 context = g_markup_parse_context_new (&parser, flags, NULL, NULL);
177 g_assert (g_markup_parse_context_get_user_data (context) == NULL);
178 g_markup_parse_context_get_position (context, &line, &col);
179 g_assert_cmpint (line, ==, 1);
180 g_assert_cmpint (col, ==, 1);
182 if (!g_markup_parse_context_parse (context, contents, -1, NULL) ||
183 !g_markup_parse_context_end_parse (context, NULL))
187 g_markup_parse_context_free (context);
189 /* FIXME: Swap out the error string so we only return one copy of it, not
190 * @n_tests copies. This should be fixed properly by eliminating the global
191 * state in this file. */
192 first_string = g_steal_pointer (&string);
193 string = g_string_new ("");
195 /* With the length specified explicitly and a nul terminator present (since
196 * g_file_get_contents() always adds one). */
197 if (test_in_chunks (contents, length_bytes, length_bytes, flags) != 0)
201 /* With the length specified explicitly and no nul terminator present. */
202 if (test_in_chunks (contents_unterminated, length_bytes, length_bytes, flags) != 0)
206 /* In various sized chunks. */
207 for (i = 0; i < G_N_ELEMENTS (chunk_sizes_bytes); i++)
209 if (test_in_chunks (contents, length_bytes, chunk_sizes_bytes[i], flags) != 0)
215 g_free (contents_unterminated);
217 /* FIXME: Restore the error string. */
218 g_string_free (string, TRUE);
219 string = g_steal_pointer (&first_string);
221 /* We expect the file to either always be parsed successfully, or never be
222 * parsed successfully. There’s a bug in GMarkup if it sometimes parses
223 * successfully depending on how you chunk or terminate the input. */
225 g_assert_cmpint (n_failures, ==, n_tests);
227 return (n_failures == 0);
231 get_expected_filename (const gchar *filename,
232 GMarkupParseFlags flags)
234 gchar *f, *p, *expected;
236 f = g_strdup (filename);
237 p = strstr (f, ".gmarkup");
241 expected = g_strconcat (f, ".expected", NULL);
242 else if (flags == G_MARKUP_TREAT_CDATA_AS_TEXT)
243 expected = g_strconcat (f, ".cdata-as-text", NULL);
245 g_assert_not_reached ();
253 test_parse (gconstpointer d)
255 const gchar *filename = d;
256 gchar *expected_file;
258 gboolean valid_input;
259 GError *error = NULL;
262 valid_input = strstr (filename, "valid") != NULL;
263 expected_file = get_expected_filename (filename, 0);
266 string = g_string_sized_new (0);
268 res = test_file (filename, 0);
269 g_assert_cmpint (res, ==, valid_input);
271 g_file_get_contents (expected_file, &expected, NULL, &error);
272 g_assert_no_error (error);
273 g_assert_cmpstr (string->str, ==, expected);
276 g_string_free (string, TRUE);
278 g_free (expected_file);
280 expected_file = get_expected_filename (filename, G_MARKUP_TREAT_CDATA_AS_TEXT);
281 if (g_file_test (expected_file, G_FILE_TEST_EXISTS))
284 string = g_string_sized_new (0);
286 res = test_file (filename, G_MARKUP_TREAT_CDATA_AS_TEXT);
287 g_assert_cmpint (res, ==, valid_input);
289 g_file_get_contents (expected_file, &expected, NULL, &error);
290 g_assert_no_error (error);
291 g_assert_cmpstr (string->str, ==, expected);
294 g_string_free (string, TRUE);
297 g_free (expected_file);
301 main (int argc, char *argv[])
308 g_setenv ("LC_ALL", "C", TRUE);
309 setlocale (LC_ALL, "");
311 g_test_init (&argc, &argv, NULL);
313 /* allow to easily generate expected output for new test cases */
317 GMarkupParseFlags flags = 0;
319 if (strcmp (argv[1], "--cdata-as-text") == 0)
321 flags = G_MARKUP_TREAT_CDATA_AS_TEXT;
324 string = g_string_sized_new (0);
325 test_file (argv[arg], flags);
326 g_print ("%s", string->str);
331 path = g_test_build_filename (G_TEST_DIST, "markups", NULL);
332 dir = g_dir_open (path, 0, &error);
334 g_assert_no_error (error);
335 while ((name = g_dir_read_name (dir)) != NULL)
337 if (!strstr (name, "gmarkup"))
340 path = g_strdup_printf ("/markup/parse/%s", name);
341 g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "markups", name, NULL),
347 return g_test_run ();