Add --enable-installed-tests configure option
[platform/upstream/glib.git] / glib / tests / markup-parse.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include <locale.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <glib.h>
8
9 static int depth = 0;
10 static GString *string;
11 static const gchar *datapath;
12
13 static void
14 indent (int extra)
15 {
16   int i = 0;
17   while (i < depth)
18     {
19       g_string_append (string, "  ");
20       ++i;
21     }
22 }
23
24 static void
25 start_element_handler  (GMarkupParseContext *context,
26                         const gchar         *element_name,
27                         const gchar        **attribute_names,
28                         const gchar        **attribute_values,
29                         gpointer             user_data,
30                         GError             **error)
31 {
32   int i;
33   
34   indent (0);
35   g_string_append_printf (string, "ELEMENT '%s'\n", element_name);
36
37   i = 0;
38   while (attribute_names[i] != NULL)
39     {
40       indent (1);
41
42       g_string_append_printf (string, "%s=\"%s\"\n",
43                               attribute_names[i],
44                               attribute_values[i]);
45       
46       ++i;
47     }
48   
49   ++depth;
50 }
51
52 static void
53 end_element_handler (GMarkupParseContext *context,
54                      const gchar         *element_name,
55                      gpointer             user_data,
56                      GError             **error)
57 {
58   --depth;
59   indent (0);
60   g_string_append_printf (string, "END '%s'\n", element_name);
61   }
62
63 static void
64 text_handler (GMarkupParseContext *context,
65               const gchar         *text,
66               gsize                text_len,
67               gpointer             user_data,
68               GError             **error)
69 {
70   indent (0);
71   g_string_append_printf (string, "TEXT '%.*s'\n", (int)text_len, text);
72 }
73
74
75 static void
76 passthrough_handler (GMarkupParseContext *context,
77                      const gchar         *passthrough_text,
78                      gsize                text_len,
79                      gpointer             user_data,
80                      GError             **error)
81 {
82   indent (0);
83
84   g_string_append_printf (string, "PASS '%.*s'\n", (int)text_len, passthrough_text);
85 }
86
87 static void
88 error_handler (GMarkupParseContext *context,
89                GError              *error,
90                gpointer             user_data)
91 {
92   g_string_append_printf (string, "ERROR %s\n", error->message);
93 }
94
95 static const GMarkupParser parser = {
96   start_element_handler,
97   end_element_handler,
98   text_handler,
99   passthrough_handler,
100   error_handler
101 };
102
103 static const GMarkupParser silent_parser = {
104   NULL,
105   NULL,
106   NULL,
107   NULL,
108   error_handler
109 };
110
111 static int
112 test_in_chunks (const gchar *contents,
113                 gint         length,
114                 gint         chunk_size)
115 {
116   GMarkupParseContext *context;
117   int i = 0;
118   
119   context = g_markup_parse_context_new (&silent_parser, 0, NULL, NULL);
120
121   while (i < length)
122     {
123       int this_chunk = MIN (length - i, chunk_size);
124
125       if (!g_markup_parse_context_parse (context,
126                                          contents + i,
127                                          this_chunk,
128                                          NULL))
129         {
130           g_markup_parse_context_free (context);
131           return 1;
132         }
133
134       i += this_chunk;
135     }
136       
137   if (!g_markup_parse_context_end_parse (context, NULL))
138     {
139       g_markup_parse_context_free (context);
140       return 1;
141     }
142
143   g_markup_parse_context_free (context);
144
145   return 0;
146 }
147
148 static int
149 test_file (const gchar *filename)
150 {
151   gchar *contents;
152   gsize  length;
153   GError *error;
154   GMarkupParseContext *context;
155   gint line, col;
156
157   error = NULL;
158   if (!g_file_get_contents (filename,
159                             &contents,
160                             &length,
161                             &error))
162     {
163       fprintf (stderr, "%s\n", error->message);
164       g_error_free (error);
165       return 1;
166     }
167
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);
172
173   if (!g_markup_parse_context_parse (context, contents, length, NULL))
174     {
175       g_markup_parse_context_free (context);
176       g_free (contents);
177       return 1;
178     }
179
180   if (!g_markup_parse_context_end_parse (context, NULL))
181     {
182       g_markup_parse_context_free (context);
183       g_free (contents);
184       return 1;
185     }
186
187   g_markup_parse_context_free (context);
188
189   /* A byte at a time */
190   if (test_in_chunks (contents, length, 1) != 0)
191     {
192       g_free (contents);
193       return 1;
194     }
195
196   /* 2 bytes */
197   if (test_in_chunks (contents, length, 2) != 0)
198     {
199       g_free (contents);
200       return 1;
201     }
202
203   /*5 bytes */
204   if (test_in_chunks (contents, length, 5) != 0)
205     {
206       g_free (contents);
207       return 1;
208     }
209
210   /* 12 bytes */
211   if (test_in_chunks (contents, length, 12) != 0)
212     {
213       g_free (contents);
214       return 1;
215     }
216
217   /* 1024 bytes */
218   if (test_in_chunks (contents, length, 1024) != 0)
219     {
220       g_free (contents);
221       return 1;
222     }
223
224   g_free (contents);
225
226   return 0;
227 }
228
229 static gchar *
230 get_expected_filename (const gchar *filename)
231 {
232   gchar *f, *p, *expected;
233
234   f = g_strdup (filename);
235   p = strstr (f, ".gmarkup");
236   if (p)
237     *p = 0;
238   expected = g_strconcat (f, ".expected", NULL);
239   g_free (f);
240
241   return expected;
242 }
243
244 static void
245 test_parse (gconstpointer d)
246 {
247   const gchar *filename = d;
248   gchar *expected_file;
249   gchar *expected;
250   GError *error = NULL;
251   gint res;
252
253   depth = 0;
254   string = g_string_sized_new (0);
255
256   res = test_file (filename);
257
258   if (strstr (filename, "valid"))
259     g_assert_cmpint (res, ==, 0);
260   else
261     g_assert_cmpint (res, ==, 1);
262
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);
267   g_free (expected);
268   g_free (expected_file);
269
270   g_string_free (string, TRUE);
271 }
272
273 int
274 main (int argc, char *argv[])
275 {
276   GDir *dir;
277   GError *error;
278   const gchar *name;
279   gchar *path;
280
281   if (g_getenv ("G_TEST_DATA"))
282     datapath = g_getenv ("G_TEST_DATA");
283   else
284     datapath = SRCDIR;
285
286   g_setenv ("LANG", "en_US.utf-8", TRUE);
287   setlocale (LC_ALL, "");
288
289   g_test_init (&argc, &argv, NULL);
290
291   /* allow to easily generate expected output for new test cases */
292   if (argc > 1)
293     {
294       string = g_string_sized_new (0);
295       test_file (argv[1]);
296       g_print ("%s", string->str);
297       return 0;
298     }
299
300   error = NULL;
301   path = g_build_filename (datapath, "markups", NULL);
302   dir = g_dir_open (path, 0, &error);
303   g_free (path);
304   g_assert_no_error (error);
305   while ((name = g_dir_read_name (dir)) != NULL)
306     {
307       if (strstr (name, "expected"))
308         continue;
309
310       path = g_strdup_printf ("/markup/parse/%s", name);
311       g_test_add_data_func_full (path, g_build_filename (datapath, "markups", name, NULL),
312                                  test_parse, g_free);
313       g_free (path);
314     }
315   g_dir_close (dir);
316
317   return g_test_run ();
318 }
319