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,
115 GMarkupParseContext *context;
118 context = g_markup_parse_context_new (&silent_parser, 0, NULL, NULL);
122 int this_chunk = MIN (length - i, chunk_size);
124 if (!g_markup_parse_context_parse (context,
129 g_markup_parse_context_free (context);
136 if (!g_markup_parse_context_end_parse (context, NULL))
138 g_markup_parse_context_free (context);
142 g_markup_parse_context_free (context);
148 test_file (const gchar *filename)
153 GMarkupParseContext *context;
157 if (!g_file_get_contents (filename,
162 fprintf (stderr, "%s\n", error->message);
163 g_error_free (error);
167 context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
168 g_assert (g_markup_parse_context_get_user_data (context) == NULL);
169 g_markup_parse_context_get_position (context, &line, &col);
170 g_assert (line == 1 && col == 1);
172 if (!g_markup_parse_context_parse (context, contents, length, NULL))
174 g_markup_parse_context_free (context);
179 if (!g_markup_parse_context_end_parse (context, NULL))
181 g_markup_parse_context_free (context);
186 g_markup_parse_context_free (context);
188 /* A byte at a time */
189 if (test_in_chunks (contents, length, 1) != 0)
196 if (test_in_chunks (contents, length, 2) != 0)
203 if (test_in_chunks (contents, length, 5) != 0)
210 if (test_in_chunks (contents, length, 12) != 0)
217 if (test_in_chunks (contents, length, 1024) != 0)
229 get_expected_filename (const gchar *filename)
231 gchar *f, *p, *expected;
233 f = g_strdup (filename);
234 p = strstr (f, ".gmarkup");
237 expected = g_strconcat (f, ".expected", NULL);
244 test_parse (gconstpointer d)
246 const gchar *filename = d;
247 gchar *expected_file;
249 GError *error = NULL;
253 string = g_string_sized_new (0);
255 res = test_file (filename);
257 if (strstr (filename, "valid"))
258 g_assert_cmpint (res, ==, 0);
260 g_assert_cmpint (res, ==, 1);
262 expected_file = get_expected_filename (filename);
263 g_file_get_contents (expected_file, &expected, NULL, &error);
264 g_assert_no_error (error);
265 g_assert_cmpstr (string->str, ==, expected);
267 g_free (expected_file);
269 g_string_free (string, TRUE);
273 main (int argc, char *argv[])
280 g_setenv ("LANG", "en_US.utf-8", TRUE);
281 setlocale (LC_ALL, "");
283 g_test_init (&argc, &argv, NULL);
285 /* allow to easily generate expected output for new test cases */
288 string = g_string_sized_new (0);
290 g_print ("%s", string->str);
295 path = g_test_build_filename (G_TEST_DISTED, "markups", NULL);
296 dir = g_dir_open (path, 0, &error);
298 g_assert_no_error (error);
299 while ((name = g_dir_read_name (dir)) != NULL)
301 if (strstr (name, "expected"))
304 path = g_strdup_printf ("/markup/parse/%s", name);
305 g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DISTED, "markups", name, NULL),
311 return g_test_run ();