| 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 |
| 26 |
test_file (const gchar *filename, GString *string) |
| 27 |
{ |
| 28 |
gchar *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 gchar * |
| 63 |
get_expected_filename (const gchar *filename) |
| 64 |
{ |
| 65 |
gchar *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 |
| 79 |
test_parse (gconstpointer d) |
| 80 |
{ |
| 81 |
const gchar *filename = d; |
| 82 |
gchar *expected_file; |
| 83 |
gchar *expected; |
| 84 |
GError *error = NULL; |
| 85 |
GString *string; |
| 86 |
|
| 87 |
expected_file = get_expected_filename (filename); |
| 88 |
|
| 89 |
string = g_string_sized_new (0); |
| 90 |
|
| 91 |
test_file (filename, string); |
| 92 |
|
| 93 |
g_file_get_contents (expected_file, &expected, NULL, &error); |
| 94 |
g_assert_no_error (error); |
| 95 |
g_assert_cmpstr (string->str, ==, expected); |
| 96 |
g_free (expected); |
| 97 |
|
| 98 |
g_string_free (string, TRUE); |
| 99 |
|
| 100 |
g_free (expected_file); |
| 101 |
} |
| 102 |
|
| 103 |
int |
| 104 |
main (int argc, char *argv[]) |
| 105 |
{ |
| 106 |
GDir *dir; |
| 107 |
GError *error = NULL; |
| 108 |
const gchar *name; |
| 109 |
gchar *path; |
| 110 |
|
| 111 |
gtk_test_init (&argc, &argv, NULL); |
| 112 |
|
| 113 |
/* allow to easily generate expected output for new test cases */ |
| 114 |
if (argc > 1) |
| 115 |
{ |
| 116 |
GString *string; |
| 117 |
|
| 118 |
string = g_string_sized_new (0); |
| 119 |
test_file (argv[1], string); |
| 120 |
g_print ("%s", string->str); |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
path = g_test_build_filename (G_TEST_DIST, "ui", NULL); |
| 126 |
dir = g_dir_open (path, 0, &error); |
| 127 |
g_free (path); |
| 128 |
g_assert_no_error (error); |
| 129 |
while ((name = g_dir_read_name (dir)) != NULL) |
| 130 |
{ |
| 131 |
if (!g_str_has_suffix (name, ".ui")) |
| 132 |
continue; |
| 133 |
|
| 134 |
path = g_strdup_printf ("/builder/parse/%s", name); |
| 135 |
g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "ui", name, NULL), |
| 136 |
test_parse, g_free); |
| 137 |
g_free (path); |
| 138 |
} |
| 139 |
g_dir_close (dir); |
| 140 |
|
| 141 |
return g_test_run (); |
| 142 |
} |
| 143 |
|