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 ! */
22 static char *
type_name_mangle(const char * name,gboolean split_first_cap)23 type_name_mangle (const char *name,
24                   gboolean     split_first_cap)
25 {
26   GString *symbol_name = g_string_new ("");
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 (name[i]) &&
33              ((i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
34               (i == 1 && name[0] == g_ascii_toupper (name[0]) && split_first_cap))) ||
35            (i > 2 && name[i]  == g_ascii_toupper (name[i]) &&
36            name[i-1] == g_ascii_toupper (name[i-1]) &&
37            name[i-2] == g_ascii_toupper (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 (symbol_name, "_get_type");
42 
43   return g_string_free (symbol_name, FALSE);
44 }
45 
46 static void
check(const char * TN,const char * gtf,const char * gtf_splitcap)47 check (const char *TN, const char *gtf, const char *gtf_splitcap)
48 {
49   char *symbol;
50 
51   symbol = type_name_mangle (TN, FALSE);
52   g_assert_cmpstr (symbol, ==, gtf);
53   g_free (symbol);
54 
55   symbol = type_name_mangle (TN, TRUE);
56   g_assert_cmpstr (symbol, ==, gtf_splitcap ? gtf_splitcap : gtf);
57   g_free (symbol);
58 }
59 
test_GtkWindow(void)60 static void test_GtkWindow (void)    { check ("GtkWindow", "gtk_window_get_type", NULL); }
test_GtkHBox(void)61 static void test_GtkHBox (void)      { check ("GtkHBox", "gtk_hbox_get_type", NULL); }
test_GtkUIManager(void)62 static void test_GtkUIManager (void) { check ("GtkUIManager", "gtk_ui_manager_get_type", NULL); }
test_GtkCList(void)63 static void test_GtkCList (void)     { check ("GtkCList", "gtk_clist_get_type", NULL); }
test_GtkIMContext(void)64 static void test_GtkIMContext (void) { check ("GtkIMContext", "gtk_im_context_get_type", NULL); }
test_Me2Shell(void)65 static void test_Me2Shell (void)     { check ("Me2Shell", "me_2shell_get_type", NULL); }
test_GWeather(void)66 static void test_GWeather (void)     { check ("GWeatherLocation", "gweather_location_get_type", "g_weather_location_get_type"); }
test_GThemedIcon(void)67 static void test_GThemedIcon (void)     { check ("GThemedIcon", "gthemed_icon_get_type", "g_themed_icon_get_type"); }
68 
69 int
main(int argc,char * argv[])70 main (int argc, char *argv[])
71 {
72   (g_test_init) (&argc, &argv, NULL);
73 
74   g_test_add_func ("/builder/get-type/GtkWindow",    test_GtkWindow);
75   g_test_add_func ("/builder/get-type/GtkHBox",      test_GtkHBox);
76   g_test_add_func ("/builder/get-type/GtkUIManager", test_GtkUIManager);
77   g_test_add_func ("/builder/get-type/GtkCList",     test_GtkCList);
78   g_test_add_func ("/builder/get-type/GtkIMContext", test_GtkIMContext);
79   g_test_add_func ("/builder/get-type/Me2Shell",     test_Me2Shell);
80   g_test_add_func ("/builder/get-type/GWeather",     test_GWeather);
81   g_test_add_func ("/builder/get-type/GThemedIcon",     test_GThemedIcon);
82 
83   return g_test_run ();
84 }
85