]> Pileus Git - ~andy/gtk/blob - tests/css/parser/test-css-parser.c
tests: Change the way the code does diffs
[~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 #include <glib/gstdio.h>
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 diff_with_file (const char  *file1,
52                 char        *text,
53                 gssize       len,
54                 GError     **error)
55 {
56   const char *command[] = { "diff", "-u", file1, NULL, NULL };
57   char *diff, *tmpfile;
58   int fd;
59
60   diff = NULL;
61
62   if (len < 0)
63     len = strlen (text);
64   
65   /* write the text buffer to a temporary file */
66   fd = g_file_open_tmp (NULL, &tmpfile, error);
67   if (fd < 0)
68     return NULL;
69
70   if (write (fd, text, len) != (int) len)
71     {
72       close (fd);
73       g_set_error (error,
74                    G_FILE_ERROR, G_FILE_ERROR_FAILED,
75                    "Could not write data to temporary file '%s'", tmpfile);
76       goto done;
77     }
78   close (fd);
79   command[3] = tmpfile;
80
81   /* run diff command */
82   g_spawn_sync (NULL, 
83                 (char **) command,
84                 NULL,
85                 G_SPAWN_SEARCH_PATH,
86                 NULL, NULL,
87                 &diff,
88                 NULL, NULL,
89                 error);
90
91 done:
92   g_unlink (tmpfile);
93   g_free (tmpfile);
94
95   return diff;
96 }
97
98 static void
99 test_css_file (GFile *file)
100 {
101   GtkCssProvider *provider;
102   char *css, *diff;
103   char *css_file, *reference_file;
104   GError *error = NULL;
105
106   css_file = g_file_get_path (file);
107
108   provider = gtk_css_provider_new ();
109   gtk_css_provider_load_from_path (provider,
110                                    css_file,
111                                    &error);
112   g_assert_no_error (error);
113
114   css = gtk_css_provider_to_string (provider);
115
116   g_assert_no_error (error);
117
118   reference_file = test_get_reference_file (css_file);
119
120   diff = diff_with_file (reference_file, css, -1, &error);
121   g_assert_no_error (error);
122
123   g_free (css);
124   g_free (reference_file);
125
126   if (diff && diff[0])
127     {
128       g_test_message ("%s", diff);
129       g_assert_not_reached ();
130     }
131
132   g_free (diff);
133   g_free (css_file);
134 }
135
136 int
137 main (int argc, char **argv)
138 {
139   const char *basedir;
140   GError *error = NULL;
141   GFile *dir;
142   GFileEnumerator *enumerator;
143   GFileInfo *info;
144
145   gtk_test_init (&argc, &argv);
146
147   if (g_getenv ("srcdir"))
148     basedir = g_getenv ("srcdir");
149   else
150     basedir = ".";
151     
152   dir = g_file_new_for_path (basedir);
153   enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
154   g_assert_no_error (error);
155
156   while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
157     {
158       GFile *file;
159       const char *filename;
160
161       filename = g_file_info_get_name (info);
162
163       if (!g_str_has_suffix (filename, ".css") ||
164           g_str_has_suffix (filename, ".out.css") ||
165           g_str_has_suffix (filename, ".ref.css"))
166         {
167           g_object_unref (info);
168           continue;
169         }
170
171       file = g_file_get_child (dir, filename);
172
173       g_test_add_vtable (g_file_get_path (file),
174                          0,
175                          file,
176                          NULL,
177                          (GTestFixtureFunc) test_css_file,
178                          (GTestFixtureFunc) g_object_unref);
179
180       g_object_unref (info);
181     }
182   
183   g_assert_no_error (error);
184   g_object_unref (enumerator);
185
186   return g_test_run ();
187 }
188