1 #undef G_DISABLE_ASSERT
14 static GString *string;
22 g_string_append (string, " ");
28 start_element_handler (GMarkupParseContext *context,
29 const gchar *element_name,
30 const gchar **attribute_names,
31 const gchar **attribute_values,
38 g_string_append_printf (string, "ELEMENT '%s'\n", element_name);
41 while (attribute_names[i] != NULL)
45 g_string_append_printf (string, "%s=\"%s\"\n",
56 end_element_handler (GMarkupParseContext *context,
57 const gchar *element_name,
63 g_string_append_printf (string, "END '%s'\n", element_name);
67 text_handler (GMarkupParseContext *context,
74 g_string_append_printf (string, "TEXT '%.*s'\n", (int)text_len, text);
79 passthrough_handler (GMarkupParseContext *context,
80 const gchar *passthrough_text,
87 g_string_append_printf (string, "PASS '%.*s'\n", (int)text_len, passthrough_text);
91 error_handler (GMarkupParseContext *context,
95 g_string_append_printf (string, "ERROR %s\n", error->message);
98 static const GMarkupParser parser = {
99 start_element_handler,
106 static const GMarkupParser silent_parser = {
115 test_in_chunks (const gchar *contents,
119 GMarkupParseContext *context;
122 context = g_markup_parse_context_new (&silent_parser, 0, NULL, NULL);
126 int this_chunk = MIN (length - i, chunk_size);
128 if (!g_markup_parse_context_parse (context,
133 g_markup_parse_context_free (context);
140 if (!g_markup_parse_context_end_parse (context, NULL))
142 g_markup_parse_context_free (context);
146 g_markup_parse_context_free (context);
152 test_file (const gchar *filename)
157 GMarkupParseContext *context;
161 if (!g_file_get_contents (filename,
166 fprintf (stderr, "%s\n", error->message);
167 g_error_free (error);
171 context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
172 g_assert (g_markup_parse_context_get_user_data (context) == NULL);
173 g_markup_parse_context_get_position (context, &line, &col);
174 g_assert (line == 1 && col == 1);
176 if (!g_markup_parse_context_parse (context, contents, length, NULL))
178 g_markup_parse_context_free (context);
183 if (!g_markup_parse_context_end_parse (context, NULL))
185 g_markup_parse_context_free (context);
190 g_markup_parse_context_free (context);
192 /* A byte at a time */
193 if (test_in_chunks (contents, length, 1) != 0)
200 if (test_in_chunks (contents, length, 2) != 0)
207 if (test_in_chunks (contents, length, 5) != 0)
214 if (test_in_chunks (contents, length, 12) != 0)
221 if (test_in_chunks (contents, length, 1024) != 0)
233 get_expected_filename (const gchar *filename)
235 gchar *f, *p, *expected;
237 f = g_strdup (filename);
238 p = strstr (f, ".gmarkup");
241 expected = g_strconcat (f, ".expected", NULL);
248 test_parse (gconstpointer d)
250 const gchar *filename = d;
251 gchar *expected_file;
256 string = g_string_sized_new (0);
258 res = test_file (filename);
260 if (strstr (filename, "valid"))
261 g_assert_cmpint (res, ==, 0);
263 g_assert_cmpint (res, ==, 1);
265 expected_file = get_expected_filename (filename);
266 if (g_file_get_contents (expected_file, &expected, NULL, NULL))
268 g_assert_cmpstr (string->str, ==, expected);
271 g_free (expected_file);
273 g_string_free (string, TRUE);
277 main (int argc, char *argv[])
284 setlocale (LC_ALL, "");
286 g_test_init (&argc, &argv, NULL);
288 /* allow to easily generate expected output for new test cases */
291 string = g_string_sized_new (0);
293 g_print ("%s", string->str);
298 dir = g_dir_open (SRCDIR "/markups", 0, &error);
299 g_assert_no_error (error);
300 while ((name = g_dir_read_name (dir)) != NULL)
302 if (strstr (name, "expected"))
305 path = g_strdup_printf ("/markup/parse/%s", name);
306 g_test_add_data_func (path, g_build_filename (SRCDIR, "markups", name, NULL), test_parse);
311 return g_test_run ();