]> Pileus Git - ~andy/gtk/blob - tests/css/parser/test-css-parser.c
tests: Add a test for the CSS parser
[~andy/gtk] / tests / css / parser / test-css-parser.c
1 /*
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * Author:
5  *      Benjamin Otte <otte@redhat.com>
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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include <gtk/gtk.h>
28
29 static char *
30 test_get_reference_file (const char *css_file)
31 {
32   GString *file = g_string_new (NULL);
33
34   if (g_str_has_suffix (css_file, ".css"))
35     g_string_append_len (file, css_file, strlen (css_file) - 4);
36   else
37     g_string_append (file, css_file);
38   
39   g_string_append (file, ".ref.css");
40
41   if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
42     {
43       g_string_free (file, TRUE);
44       return g_strdup (css_file);
45     }
46
47   return g_string_free (file, FALSE);
48 }
49
50 static char *
51 test_get_output_file (const char *css_file)
52 {
53   GString *file = g_string_new (NULL);
54
55   if (g_str_has_suffix (css_file, ".css"))
56     g_string_append_len (file, css_file, strlen (css_file) - 4);
57   else
58     g_string_append (file, css_file);
59   
60   g_string_append (file, ".out.css");
61
62   return g_string_free (file, FALSE);
63 }
64
65 static char *
66 diff_files (const char *file1,
67             const char *file2,
68             GError **   error)
69 {
70   const char *command[] = { "diff", "-u", file1, file2, NULL };
71   char *diff;
72
73   /* run diff command */
74   if (!g_spawn_sync (NULL, (char **) command, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
75         &diff, NULL, NULL, error)) {
76     return NULL;
77   }
78
79   return diff;
80 }
81
82 static void
83 test_css_file (GFile *file)
84 {
85   GtkCssProvider *provider;
86   char *css, *diff;
87   char *css_file, *output_file, *reference_file;
88   GError *error = NULL;
89
90   css_file = g_file_get_path (file);
91
92   provider = gtk_css_provider_new ();
93   gtk_css_provider_load_from_path (provider,
94                                    css_file,
95                                    &error);
96   g_assert_no_error (error);
97
98   css = gtk_css_provider_to_string (provider);
99   output_file = test_get_output_file (css_file);
100
101   g_file_set_contents (output_file, css, -1, &error);
102   g_assert_no_error (error);
103
104   g_free (css);
105
106   reference_file = test_get_reference_file (css_file);
107
108   diff = diff_files (reference_file, output_file, &error);
109   g_assert_no_error (error);
110
111   g_free (reference_file);
112   g_free (output_file);
113
114   if (diff && diff[0])
115     {
116       g_test_message ("%s", diff);
117       g_assert_not_reached ();
118     }
119
120   g_free (diff);
121   g_free (css_file);
122 }
123
124 int
125 main (int argc, char **argv)
126 {
127   const char *basedir;
128   GError *error = NULL;
129   GFile *dir;
130   GFileEnumerator *enumerator;
131   GFileInfo *info;
132
133   gtk_test_init (&argc, &argv);
134
135   if (g_getenv ("srcdir"))
136     basedir = g_getenv ("srcdir");
137   else
138     basedir = ".";
139     
140   dir = g_file_new_for_path (basedir);
141   enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
142   g_assert_no_error (error);
143
144   while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
145     {
146       GFile *file;
147       const char *filename;
148
149       filename = g_file_info_get_name (info);
150
151       if (!g_str_has_suffix (filename, ".css") ||
152           g_str_has_suffix (filename, ".out.css") ||
153           g_str_has_suffix (filename, ".ref.css"))
154         {
155           g_object_unref (info);
156           continue;
157         }
158
159       file = g_file_get_child (dir, filename);
160
161       g_test_add_vtable (g_file_get_path (file),
162                          0,
163                          file,
164                          NULL,
165                          (GTestFixtureFunc) test_css_file,
166                          (GTestFixtureFunc) g_object_unref);
167
168       g_object_unref (info);
169     }
170   
171   g_assert_no_error (error);
172   g_object_unref (enumerator);
173
174   return g_test_run ();
175 }
176