1 /*
2 * builderparser.c: Test GtkBuilder parser
3 *
4 * Copyright (C) 2014 Red Hat, Inc
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 #include <string.h>
23 #include <gtk/gtk.h>
24
25 static void
test_file(const char * filename,GString * string)26 test_file (const char *filename, GString *string)
27 {
28 char *contents;
29 gsize length;
30 GError *error = NULL;
31 gboolean ret;
32 GtkBuilder *builder;
33
34 if (!g_file_get_contents (filename, &contents, &length, &error))
35 {
36 fprintf (stderr, "%s\n", error->message);
37 g_error_free (error);
38 return;
39 }
40
41 builder = gtk_builder_new ();
42 ret = gtk_builder_add_from_string (builder, contents, length, &error);
43 g_free (contents);
44
45 if (ret)
46 {
47 g_assert_no_error (error);
48 g_string_append_printf (string, "SUCCESS\n");
49 }
50 else
51 {
52 g_string_append_printf (string, "ERROR: %s %d\n%s\n",
53 g_quark_to_string (error->domain),
54 error->code,
55 error->message);
56 g_error_free (error);
57 }
58
59 g_object_unref (builder);
60 }
61
62 static char *
get_expected_filename(const char * filename)63 get_expected_filename (const char *filename)
64 {
65 char *f, *p, *expected;
66
67 f = g_strdup (filename);
68 p = strstr (f, ".ui");
69 if (p)
70 *p = 0;
71 expected = g_strconcat (f, ".expected", NULL);
72
73 g_free (f);
74
75 return expected;
76 }
77
78 static void
test_parse(gconstpointer d)79 test_parse (gconstpointer d)
80 {
81 const char *filename = d;
82 char *expected_file;
83 char *expected;
84 GError *error = NULL;
85 GString *string;
86
87 g_test_message ("filename: %s", filename);
88 expected_file = get_expected_filename (filename);
89
90 string = g_string_sized_new (0);
91
92 test_file (filename, string);
93
94 g_file_get_contents (expected_file, &expected, NULL, &error);
95 g_assert_no_error (error);
96 g_assert_cmpstr (string->str, ==, expected);
97 g_free (expected);
98
99 g_string_free (string, TRUE);
100
101 g_free (expected_file);
102 }
103
104 int
main(int argc,char * argv[])105 main (int argc, char *argv[])
106 {
107 GDir *dir;
108 GError *error = NULL;
109 const char *name;
110 char *path;
111
112 gtk_test_init (&argc, &argv, NULL);
113
114 /* allow to easily generate expected output for new test cases */
115 if (argc > 1)
116 {
117 GString *string;
118
119 string = g_string_sized_new (0);
120 test_file (argv[1], string);
121 g_print ("%s", string->str);
122
123 return 0;
124 }
125
126 path = g_test_build_filename (G_TEST_DIST, "ui", NULL);
127 dir = g_dir_open (path, 0, &error);
128 g_free (path);
129 g_assert_no_error (error);
130 while ((name = g_dir_read_name (dir)) != NULL)
131 {
132 if (!g_str_has_suffix (name, ".ui"))
133 continue;
134
135 path = g_strdup_printf ("/builder/parse/%s", name);
136 g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "ui", name, NULL),
137 test_parse, g_free);
138 g_free (path);
139 }
140 g_dir_close (dir);
141
142 return g_test_run ();
143 }
144
145