2 * Copyright (C) 2011 Red Hat Inc.
5 * Benjamin Otte <otte@redhat.com>
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.
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.
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.
26 #include <glib/gstdio.h>
30 test_get_reference_file (const char *css_file)
32 GString *file = g_string_new (NULL);
34 if (g_str_has_suffix (css_file, ".css"))
35 g_string_append_len (file, css_file, strlen (css_file) - 4);
37 g_string_append (file, css_file);
39 g_string_append (file, ".ref.css");
41 if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
43 g_string_free (file, TRUE);
44 return g_strdup (css_file);
47 return g_string_free (file, FALSE);
51 diff_with_file (const char *file1,
56 const char *command[] = { "diff", "-u", file1, NULL, NULL };
65 /* write the text buffer to a temporary file */
66 fd = g_file_open_tmp (NULL, &tmpfile, error);
70 if (write (fd, text, len) != (int) len)
74 G_FILE_ERROR, G_FILE_ERROR_FAILED,
75 "Could not write data to temporary file '%s'", tmpfile);
81 /* run diff command */
99 test_css_file (GFile *file)
101 GtkCssProvider *provider;
103 char *css_file, *reference_file;
104 GError *error = NULL;
106 css_file = g_file_get_path (file);
108 provider = gtk_css_provider_new ();
109 gtk_css_provider_load_from_path (provider,
112 g_assert_no_error (error);
114 css = gtk_css_provider_to_string (provider);
116 g_assert_no_error (error);
118 reference_file = test_get_reference_file (css_file);
120 diff = diff_with_file (reference_file, css, -1, &error);
121 g_assert_no_error (error);
124 g_free (reference_file);
128 g_test_message ("%s", diff);
129 g_assert_not_reached ();
137 main (int argc, char **argv)
140 GError *error = NULL;
142 GFileEnumerator *enumerator;
145 gtk_test_init (&argc, &argv);
147 if (g_getenv ("srcdir"))
148 basedir = g_getenv ("srcdir");
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);
156 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
159 const char *filename;
161 filename = g_file_info_get_name (info);
163 if (!g_str_has_suffix (filename, ".css") ||
164 g_str_has_suffix (filename, ".out.css") ||
165 g_str_has_suffix (filename, ".ref.css"))
167 g_object_unref (info);
171 file = g_file_get_child (dir, filename);
173 g_test_add_vtable (g_file_get_path (file),
177 (GTestFixtureFunc) test_css_file,
178 (GTestFixtureFunc) g_object_unref);
180 g_object_unref (info);
183 g_assert_no_error (error);
184 g_object_unref (enumerator);
186 return g_test_run ();