1 #undef G_DISABLE_ASSERT
13 static GString *string;
21 g_string_append (string, " ");
27 start_element_handler (GMarkupParseContext *context,
28 const gchar *element_name,
29 const gchar **attribute_names,
30 const gchar **attribute_values,
37 g_string_append_printf (string, "ELEMENT '%s'\n", element_name);
40 while (attribute_names[i] != NULL)
44 g_string_append_printf (string, "%s=\"%s\"\n",
55 end_element_handler (GMarkupParseContext *context,
56 const gchar *element_name,
62 g_string_append_printf (string, "END '%s'\n", element_name);
66 text_handler (GMarkupParseContext *context,
73 g_string_append_printf (string, "TEXT '%.*s'\n", (int)text_len, text);
78 passthrough_handler (GMarkupParseContext *context,
79 const gchar *passthrough_text,
86 g_string_append_printf (string, "PASS '%.*s'\n", (int)text_len, passthrough_text);
90 error_handler (GMarkupParseContext *context,
94 g_string_append_printf (string, "ERROR %s\n", error->message);
97 static const GMarkupParser parser = {
98 start_element_handler,
105 static const GMarkupParser silent_parser = {
114 test_in_chunks (const gchar *contents,
118 GMarkupParseContext *context;
121 context = g_markup_parse_context_new (&silent_parser, 0, NULL, NULL);
125 int this_chunk = MIN (length - i, chunk_size);
127 if (!g_markup_parse_context_parse (context,
132 g_markup_parse_context_free (context);
139 if (!g_markup_parse_context_end_parse (context, NULL))
141 g_markup_parse_context_free (context);
145 g_markup_parse_context_free (context);
151 test_file (const gchar *filename)
156 GMarkupParseContext *context;
160 if (!g_file_get_contents (filename,
165 fprintf (stderr, "%s\n", error->message);
166 g_error_free (error);
170 context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
171 g_assert (g_markup_parse_context_get_user_data (context) == NULL);
172 g_markup_parse_context_get_position (context, &line, &col);
173 g_assert (line == 1 && col == 1);
175 if (!g_markup_parse_context_parse (context, contents, length, NULL))
177 g_markup_parse_context_free (context);
181 if (!g_markup_parse_context_end_parse (context, NULL))
183 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) != 0)
194 if (test_in_chunks (contents, length, 2) != 0)
198 if (test_in_chunks (contents, length, 5) != 0)
202 if (test_in_chunks (contents, length, 12) != 0)
206 if (test_in_chunks (contents, length, 1024) != 0)
213 get_expected_filename (const gchar *filename)
215 gchar *f, *p, *expected;
217 f = g_strdup (filename);
218 p = strstr (f, ".gmarkup");
221 expected = g_strconcat (f, ".expected", NULL);
228 test_parse (gconstpointer d)
230 const gchar *filename = d;
231 gchar *expected_file;
236 string = g_string_sized_new (0);
238 res = test_file (filename);
240 if (strstr (filename, "valid"))
241 g_assert_cmpint (res, ==, 0);
243 g_assert_cmpint (res, ==, 1);
245 expected_file = get_expected_filename (filename);
246 if (g_file_get_contents (expected_file, &expected, NULL, NULL))
248 g_assert_cmpstr (string->str, ==, expected);
251 g_free (expected_file);
253 g_string_free (string, TRUE);
257 main (int argc, char *argv[])
264 g_test_init (&argc, &argv, NULL);
266 /* allow to easily generate expected output for new test cases */
269 string = g_string_sized_new (0);
271 g_print ("%s", string->str);
276 dir = g_dir_open (SRCDIR "/markups", 0, &error);
277 g_assert_no_error (error);
278 while ((name = g_dir_read_name (dir)) != NULL)
280 if (strstr (name, "expected"))
283 path = g_strdup_printf ("/markup/parse/%s", name);
284 g_test_add_data_func (path, g_build_filename (SRCDIR, "markups", name, NULL), test_parse);
289 return g_test_run ();