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.
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 test_get_output_file (const char *css_file)
53 GString *file = g_string_new (NULL);
55 if (g_str_has_suffix (css_file, ".css"))
56 g_string_append_len (file, css_file, strlen (css_file) - 4);
58 g_string_append (file, css_file);
60 g_string_append (file, ".out.css");
62 return g_string_free (file, FALSE);
66 diff_files (const char *file1,
70 const char *command[] = { "diff", "-u", file1, file2, NULL };
73 /* run diff command */
74 if (!g_spawn_sync (NULL, (char **) command, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
75 &diff, NULL, NULL, error)) {
83 test_css_file (GFile *file)
85 GtkCssProvider *provider;
87 char *css_file, *output_file, *reference_file;
90 css_file = g_file_get_path (file);
92 provider = gtk_css_provider_new ();
93 gtk_css_provider_load_from_path (provider,
96 g_assert_no_error (error);
98 css = gtk_css_provider_to_string (provider);
99 output_file = test_get_output_file (css_file);
101 g_file_set_contents (output_file, css, -1, &error);
102 g_assert_no_error (error);
106 reference_file = test_get_reference_file (css_file);
108 diff = diff_files (reference_file, output_file, &error);
109 g_assert_no_error (error);
111 g_free (reference_file);
112 g_free (output_file);
116 g_test_message ("%s", diff);
117 g_assert_not_reached ();
125 main (int argc, char **argv)
128 GError *error = NULL;
130 GFileEnumerator *enumerator;
133 gtk_test_init (&argc, &argv);
135 if (g_getenv ("srcdir"))
136 basedir = g_getenv ("srcdir");
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);
144 while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
147 const char *filename;
149 filename = g_file_info_get_name (info);
151 if (!g_str_has_suffix (filename, ".css") ||
152 g_str_has_suffix (filename, ".out.css") ||
153 g_str_has_suffix (filename, ".ref.css"))
155 g_object_unref (info);
159 file = g_file_get_child (dir, filename);
161 g_test_add_vtable (g_file_get_path (file),
165 (GTestFixtureFunc) test_css_file,
166 (GTestFixtureFunc) g_object_unref);
168 g_object_unref (info);
171 g_assert_no_error (error);
172 g_object_unref (enumerator);
174 return g_test_run ();