1 #undef G_DISABLE_ASSERT
10 static GString *string;
11 static const gchar *datapath;
19 g_string_append (string, " ");
25 start_element_handler (GMarkupParseContext *context,
26 const gchar *element_name,
27 const gchar **attribute_names,
28 const gchar **attribute_values,
35 g_string_append_printf (string, "ELEMENT '%s'\n", element_name);
38 while (attribute_names[i] != NULL)
42 g_string_append_printf (string, "%s=\"%s\"\n",
53 end_element_handler (GMarkupParseContext *context,
54 const gchar *element_name,
60 g_string_append_printf (string, "END '%s'\n", element_name);
64 text_handler (GMarkupParseContext *context,
71 g_string_append_printf (string, "TEXT '%.*s'\n", (int)text_len, text);
76 passthrough_handler (GMarkupParseContext *context,
77 const gchar *passthrough_text,
84 g_string_append_printf (string, "PASS '%.*s'\n", (int)text_len, passthrough_text);
88 error_handler (GMarkupParseContext *context,
92 g_string_append_printf (string, "ERROR %s\n", error->message);
95 static const GMarkupParser parser = {
96 start_element_handler,
103 static const GMarkupParser silent_parser = {
112 test_in_chunks (const gchar *contents,
116 GMarkupParseContext *context;
119 context = g_markup_parse_context_new (&silent_parser, 0, 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)
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, 0, 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) != 0)
197 if (test_in_chunks (contents, length, 2) != 0)
204 if (test_in_chunks (contents, length, 5) != 0)
211 if (test_in_chunks (contents, length, 12) != 0)
218 if (test_in_chunks (contents, length, 1024) != 0)
230 get_expected_filename (const gchar *filename)
232 gchar *f, *p, *expected;
234 f = g_strdup (filename);
235 p = strstr (f, ".gmarkup");
238 expected = g_strconcat (f, ".expected", NULL);
245 test_parse (gconstpointer d)
247 const gchar *filename = d;
248 gchar *expected_file;
250 GError *error = NULL;
254 string = g_string_sized_new (0);
256 res = test_file (filename);
258 if (strstr (filename, "valid"))
259 g_assert_cmpint (res, ==, 0);
261 g_assert_cmpint (res, ==, 1);
263 expected_file = get_expected_filename (filename);
264 g_file_get_contents (expected_file, &expected, NULL, &error);
265 g_assert_no_error (error);
266 g_assert_cmpstr (string->str, ==, expected);
268 g_free (expected_file);
270 g_string_free (string, TRUE);
274 main (int argc, char *argv[])
281 if (g_getenv ("G_TEST_DATA"))
282 datapath = g_getenv ("G_TEST_DATA");
286 g_setenv ("LANG", "en_US.utf-8", TRUE);
287 setlocale (LC_ALL, "");
289 g_test_init (&argc, &argv, NULL);
291 /* allow to easily generate expected output for new test cases */
294 string = g_string_sized_new (0);
296 g_print ("%s", string->str);
301 path = g_build_filename (datapath, "markups", NULL);
302 dir = g_dir_open (path, 0, &error);
304 g_assert_no_error (error);
305 while ((name = g_dir_read_name (dir)) != NULL)
307 if (strstr (name, "expected"))
310 path = g_strdup_printf ("/markup/parse/%s", name);
311 g_test_add_data_func_full (path, g_build_filename (datapath, "markups", name, NULL),
317 return g_test_run ();