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