Debian Codegraph @

testsuite/css/parser/test-css-parser.c

raw
1 /*
2 * Copyright (C) 2011 Red Hat Inc.
3 *
4 * Author:
5 * Benjamin Otte <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #undef GTK_DISABLE_DEPRECATED
24
25 #include <string.h>
26 #include <glib/gstdio.h>
27 #include <gtk/gtk.h>
28
29 #ifdef G_OS_WIN32
30 # include <io.h>
31 #endif
32
33 static char *
34 test_get_reference_file (const char *css_file)
35 {
36 GString *file = g_string_new (NULL);
37
38 if (g_str_has_suffix (css_file, ".css"))
39 g_string_append_len (file, css_file, strlen (css_file) - 4);
40 else
41 g_string_append (file, css_file);
42
43 g_string_append (file, ".ref.css");
44
45 if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
46 {
47 g_string_free (file, TRUE);
48 return g_strdup (css_file);
49 }
50
51 return g_string_free (file, FALSE);
52 }
53
54 static char *
55 test_get_errors_file (const char *css_file)
56 {
57 GString *file = g_string_new (NULL);
58
59 if (g_str_has_suffix (css_file, ".css"))
60 g_string_append_len (file, css_file, strlen (css_file) - 4);
61 else
62 g_string_append (file, css_file);
63
64 g_string_append (file, ".errors");
65
66 if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
67 {
68 g_string_free (file, TRUE);
69 return NULL;
70 }
71
72 return g_string_free (file, FALSE);
73 }
74
75 static char *
76 diff_with_file (const char *file1,
77 char *text,
78 gssize len,
79 GError **error)
80 {
81 const char *command[] = { "diff", "-u", file1, NULL, NULL };
82 char *diff, *tmpfile;
83 int fd;
84
85 diff = NULL;
86
87 if (len < 0)
88 len = strlen (text);
89
90 /* write the text buffer to a temporary file */
91 fd = g_file_open_tmp (NULL, &tmpfile, error);
92 if (fd < 0)
93 return NULL;
94
95 if (write (fd, text, len) != (int) len)
96 {
97 close (fd);
98 g_set_error (error,
99 G_FILE_ERROR, G_FILE_ERROR_FAILED,
100 "Could not write data to temporary file '%s'", tmpfile);
101 goto done;
102 }
103 close (fd);
104 command[3] = tmpfile;
105
106 /* run diff command */
107 g_spawn_sync (NULL,
108 (char **) command,
109 NULL,
110 G_SPAWN_SEARCH_PATH,
111 NULL, NULL,
112 &diff,
113 NULL, NULL,
114 error);
115
116 done:
117 g_unlink (tmpfile);
118 g_free (tmpfile);
119
120 return diff;
121 }
122
123 static void
124 append_error_value (GString *string,
125 GType enum_type,
126 guint value)
127 {
128 GEnumClass *enum_class;
129 GEnumValue *enum_value;
130
131 enum_class = g_type_class_ref (enum_type);
132 enum_value = g_enum_get_value (enum_class, value);
133
134 g_string_append (string, enum_value->value_name);
135
136 g_type_class_unref (enum_class);
137 }
138
139 static void
140 parsing_error_cb (GtkCssProvider *provider,
141 GtkCssSection *section,
142 const GError *error,
143 GString *errors)
144 {
145 char *basename;
146
147 basename = g_file_get_basename (gtk_css_section_get_file (section));
148 g_string_append_printf (errors,
149 "%s:%u: error: ",
150 basename, gtk_css_section_get_end_line (section) + 1);
151 g_free (basename);
152
153 if (error->domain == GTK_CSS_PROVIDER_ERROR)
154 append_error_value (errors, GTK_TYPE_CSS_PROVIDER_ERROR, error->code);
155 else
156 g_string_append_printf (errors,
157 "%s %u\n",
158 g_quark_to_string (error->domain),
159 error->code);
160
161 g_string_append_c (errors, '\n');
162 }
163
164 static void
165 parse_css_file (GFile *file, gboolean generate)
166 {
167 GtkCssProvider *provider;
168 char *css, *diff;
169 char *css_file, *reference_file, *errors_file;
170 GString *errors;
171 GError *error = NULL;
172
173 css_file = g_file_get_path (file);
174 errors = g_string_new ("");
175
176 provider = gtk_css_provider_new ();
177 g_signal_connect (provider,
178 "parsing-error",
179 G_CALLBACK (parsing_error_cb),
180 errors);
181 gtk_css_provider_load_from_path (provider, css_file, NULL);
182
183 css = gtk_css_provider_to_string (provider);
184
185 if (generate)
186 {
187 g_print ("%s", css);
188 goto out;
189 }
190
191 reference_file = test_get_reference_file (css_file);
192
193 diff = diff_with_file (reference_file, css, -1, &error);
194 g_assert_no_error (error);
195
196 if (diff && diff[0])
197 {
198 g_test_message ("Resulting CSS doesn't match reference:\n%s", diff);
199 g_test_fail ();
200 }
201 g_free (reference_file);
202
203 errors_file = test_get_errors_file (css_file);
204
205 if (errors_file)
206 {
207 diff = diff_with_file (errors_file, errors->str, errors->len, &error);
208 g_assert_no_error (error);
209
210 if (diff && diff[0])
211 {
212 g_test_message ("Errors don't match expected errors:\n%s", diff);
213 g_test_fail ();
214 }
215 }
216 else if (errors->str[0])
217 {
218 g_test_message ("Unexpected errors:\n%s", errors->str);
219 g_test_fail ();
220 }
221
222 g_free (errors_file);
223 g_string_free (errors, TRUE);
224
225 g_free (diff);
226
227 out:
228 g_free (css_file);
229 g_free (css);
230 }
231
232 static void
233 test_css_file (GFile *file)
234 {
235 parse_css_file (file, FALSE);
236 }
237
238 static void
239 add_test_for_file (GFile *file)
240 {
241 char *path;
242
243 path = g_file_get_path (file);
244
245 g_test_add_vtable (path,
246 0,
247 g_object_ref (file),
248 NULL,
249 (GTestFixtureFunc) test_css_file,
250 (GTestFixtureFunc) g_object_unref);
251
252 g_free (path);
253 }
254
255 static int
256 compare_files (gconstpointer a, gconstpointer b)
257 {
258 GFile *file1 = G_FILE (a);
259 GFile *file2 = G_FILE (b);
260 char *path1, *path2;
261 int result;
262
263 path1 = g_file_get_path (file1);
264 path2 = g_file_get_path (file2);
265
266 result = strcmp (path1, path2);
267
268 g_free (path1);
269 g_free (path2);
270
271 return result;
272 }
273
274 static void
275 add_tests_for_files_in_directory (GFile *dir)
276 {
277 GFileEnumerator *enumerator;
278 GFileInfo *info;
279 GList *files;
280 GError *error = NULL;
281
282 enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
283 g_assert_no_error (error);
284 files = NULL;
285
286 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
287 {
288 const char *filename;
289
290 filename = g_file_info_get_name (info);
291
292 if (!g_str_has_suffix (filename, ".css") ||
293 g_str_has_suffix (filename, ".out.css") ||
294 g_str_has_suffix (filename, ".ref.css"))
295 {
296 g_object_unref (info);
297 continue;
298 }
299
300 files = g_list_prepend (files, g_file_get_child (dir, filename));
301
302 g_object_unref (info);
303 }
304
305 g_assert_no_error (error);
306 g_object_unref (enumerator);
307
308 files = g_list_sort (files, compare_files);
309 g_list_foreach (files, (GFunc) add_test_for_file, NULL);
310 g_list_free_full (files, g_object_unref);
311 }
312
313 int
314 main (int argc, char **argv)
315 {
316 gtk_test_init (&argc, &argv);
317
318 if (argc < 2)
319 {
320 const char *basedir;
321 GFile *dir;
322
323 basedir = g_test_get_dir (G_TEST_DIST);
324 dir = g_file_new_for_path (basedir);
325 add_tests_for_files_in_directory (dir);
326
327 g_object_unref (dir);
328 }
329 else if (strcmp (argv[1], "--generate") == 0)
330 {
331 if (argc >= 3)
332 {
333 GFile *file = g_file_new_for_commandline_arg (argv[2]);
334
335 parse_css_file (file, TRUE);
336
337 g_object_unref (file);
338 }
339 }
340 else
341 {
342 guint i;
343
344 for (i = 1; i < argc; i++)
345 {
346 GFile *file = g_file_new_for_commandline_arg (argv[i]);
347
348 add_test_for_file (file);
349
350 g_object_unref (file);
351 }
352 }
353
354 return g_test_run ();
355 }
356