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