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
25static void
26test_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: &contents, length: &length, error: &error))
35 {
36 fprintf (stderr, format: "%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, buffer: contents, length, error: &error);
43 g_free (mem: contents);
44
45 if (ret)
46 {
47 g_assert_no_error (error);
48 g_string_append_printf (string, format: "SUCCESS\n");
49 }
50 else
51 {
52 g_string_append_printf (string, format: "ERROR: %s %d\n%s\n",
53 g_quark_to_string (quark: error->domain),
54 error->code,
55 error->message);
56 g_error_free (error);
57 }
58
59 g_object_unref (object: builder);
60}
61
62static char *
63get_expected_filename (const char *filename)
64{
65 char *f, *p, *expected;
66
67 f = g_strdup (str: filename);
68 p = strstr (haystack: f, needle: ".ui");
69 if (p)
70 *p = 0;
71 expected = g_strconcat (string1: f, ".expected", NULL);
72
73 g_free (mem: f);
74
75 return expected;
76}
77
78static void
79test_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 (format: "filename: %s", filename);
88 expected_file = get_expected_filename (filename);
89
90 string = g_string_sized_new (dfl_size: 0);
91
92 test_file (filename, string);
93
94 g_file_get_contents (filename: expected_file, contents: &expected, NULL, error: &error);
95 g_assert_no_error (error);
96 g_assert_cmpstr (string->str, ==, expected);
97 g_free (mem: expected);
98
99 g_string_free (string, TRUE);
100
101 g_free (mem: expected_file);
102}
103
104int
105main (int argc, char *argv[])
106{
107 GDir *dir;
108 GError *error = NULL;
109 const char *name;
110 char *path;
111
112 gtk_test_init (argcp: &argc, argvp: &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 (dfl_size: 0);
120 test_file (filename: argv[1], string);
121 g_print (format: "%s", string->str);
122
123 return 0;
124 }
125
126 path = g_test_build_filename (file_type: G_TEST_DIST, first_path: "ui", NULL);
127 dir = g_dir_open (path, flags: 0, error: &error);
128 g_free (mem: path);
129 g_assert_no_error (error);
130 while ((name = g_dir_read_name (dir)) != NULL)
131 {
132 if (!g_str_has_suffix (str: name, suffix: ".ui"))
133 continue;
134
135 path = g_strdup_printf (format: "/builder/parse/%s", name);
136 g_test_add_data_func_full (testpath: path, test_data: g_test_build_filename (file_type: G_TEST_DIST, first_path: "ui", name, NULL),
137 test_func: test_parse, data_free_func: g_free);
138 g_free (mem: path);
139 }
140 g_dir_close (dir);
141
142 return g_test_run ();
143}
144
145

source code of gtk/testsuite/gtk/builderparser.c