1/* typename.c: Test for GtkBuilder's type-name-mangling heuristics
2 * Copyright (C) 2014 Red Hat, Inc.
3 * Authors: Matthias Clasen
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <glib.h>
20
21/* Keep in sync with gtkbuilder.c ! */
22static char *
23type_name_mangle (const char *name,
24 gboolean split_first_cap)
25{
26 GString *symbol_name = g_string_new (init: "");
27 int i;
28
29 for (i = 0; name[i] != '\0'; i++)
30 {
31 /* skip if uppercase, first or previous is uppercase */
32 if ((name[i] == g_ascii_toupper (c: name[i]) &&
33 ((i > 0 && name[i-1] != g_ascii_toupper (c: name[i-1])) ||
34 (i == 1 && name[0] == g_ascii_toupper (c: name[0]) && split_first_cap))) ||
35 (i > 2 && name[i] == g_ascii_toupper (c: name[i]) &&
36 name[i-1] == g_ascii_toupper (c: name[i-1]) &&
37 name[i-2] == g_ascii_toupper (c: name[i-2])))
38 g_string_append_c (symbol_name, '_');
39 g_string_append_c (symbol_name, g_ascii_tolower (name[i]));
40 }
41 g_string_append (string: symbol_name, val: "_get_type");
42
43 return g_string_free (string: symbol_name, FALSE);
44}
45
46static void
47check (const char *TN, const char *gtf, const char *gtf_splitcap)
48{
49 char *symbol;
50
51 symbol = type_name_mangle (name: TN, FALSE);
52 g_assert_cmpstr (symbol, ==, gtf);
53 g_free (mem: symbol);
54
55 symbol = type_name_mangle (name: TN, TRUE);
56 g_assert_cmpstr (symbol, ==, gtf_splitcap ? gtf_splitcap : gtf);
57 g_free (mem: symbol);
58}
59
60static void test_GtkWindow (void) { check (TN: "GtkWindow", gtf: "gtk_window_get_type", NULL); }
61static void test_GtkHBox (void) { check (TN: "GtkHBox", gtf: "gtk_hbox_get_type", NULL); }
62static void test_GtkUIManager (void) { check (TN: "GtkUIManager", gtf: "gtk_ui_manager_get_type", NULL); }
63static void test_GtkCList (void) { check (TN: "GtkCList", gtf: "gtk_clist_get_type", NULL); }
64static void test_GtkIMContext (void) { check (TN: "GtkIMContext", gtf: "gtk_im_context_get_type", NULL); }
65static void test_Me2Shell (void) { check (TN: "Me2Shell", gtf: "me_2shell_get_type", NULL); }
66static void test_GWeather (void) { check (TN: "GWeatherLocation", gtf: "gweather_location_get_type", gtf_splitcap: "g_weather_location_get_type"); }
67static void test_GThemedIcon (void) { check (TN: "GThemedIcon", gtf: "gthemed_icon_get_type", gtf_splitcap: "g_themed_icon_get_type"); }
68
69int
70main (int argc, char *argv[])
71{
72 (g_test_init) (argc: &argc, argv: &argv, NULL);
73
74 g_test_add_func (testpath: "/builder/get-type/GtkWindow", test_func: test_GtkWindow);
75 g_test_add_func (testpath: "/builder/get-type/GtkHBox", test_func: test_GtkHBox);
76 g_test_add_func (testpath: "/builder/get-type/GtkUIManager", test_func: test_GtkUIManager);
77 g_test_add_func (testpath: "/builder/get-type/GtkCList", test_func: test_GtkCList);
78 g_test_add_func (testpath: "/builder/get-type/GtkIMContext", test_func: test_GtkIMContext);
79 g_test_add_func (testpath: "/builder/get-type/Me2Shell", test_func: test_Me2Shell);
80 g_test_add_func (testpath: "/builder/get-type/GWeather", test_func: test_GWeather);
81 g_test_add_func (testpath: "/builder/get-type/GThemedIcon", test_func: test_GThemedIcon);
82
83 return g_test_run ();
84}
85

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