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 gchar *
type_name_mangle(const gchar * name)23 type_name_mangle (const gchar *name)
24 {
25   GString *symbol_name = g_string_new ("");
26   gint i;
27 
28   for (i = 0; name[i] != '\0'; i++)
29     {
30       /* skip if uppercase, first or previous is uppercase */
31       if ((name[i] == g_ascii_toupper (name[i]) &&
32            i > 0 && name[i-1] != g_ascii_toupper (name[i-1])) ||
33            (i > 2 && name[i]   == g_ascii_toupper (name[i]) &&
34            name[i-1] == g_ascii_toupper (name[i-1]) &&
35            name[i-2] == g_ascii_toupper (name[i-2])))
36         g_string_append_c (symbol_name, '_');
37       g_string_append_c (symbol_name, g_ascii_tolower (name[i]));
38     }
39   g_string_append (symbol_name, "_get_type");
40 
41   return g_string_free (symbol_name, FALSE);
42 }
43 
44 static void
check(const gchar * TN,const gchar * gtf)45 check (const gchar *TN, const gchar *gtf)
46 {
47   gchar *symbol;
48 
49   symbol = type_name_mangle (TN);
50   g_assert_cmpstr (symbol, ==, gtf);
51   g_free (symbol);
52 }
53 
test_GtkWindow(void)54 static void test_GtkWindow (void)    { check ("GtkWindow", "gtk_window_get_type"); }
test_GtkHBox(void)55 static void test_GtkHBox (void)      { check ("GtkHBox", "gtk_hbox_get_type"); }
test_GtkUIManager(void)56 static void test_GtkUIManager (void) { check ("GtkUIManager", "gtk_ui_manager_get_type"); }
test_GtkCList(void)57 static void test_GtkCList (void)     { check ("GtkCList", "gtk_clist_get_type"); }
test_GtkIMContext(void)58 static void test_GtkIMContext (void) { check ("GtkIMContext", "gtk_im_context_get_type"); }
test_Me2Shell(void)59 static void test_Me2Shell (void)     { check ("Me2Shell", "me_2shell_get_type"); }
test_GWeather(void)60 static void test_GWeather (void)     { check ("GWeatherLocation", "gweather_location_get_type"); }
61 
62 int
main(int argc,char * argv[])63 main (int argc, char *argv[])
64 {
65   g_test_init (&argc, &argv, NULL);
66 
67   g_test_add_func ("/builder/get-type/GtkWindow",    test_GtkWindow);
68   g_test_add_func ("/builder/get-type/GtkHBox",      test_GtkHBox);
69   g_test_add_func ("/builder/get-type/GtkUIManager", test_GtkUIManager);
70   g_test_add_func ("/builder/get-type/GtkCList",     test_GtkCList);
71   g_test_add_func ("/builder/get-type/GtkIMContext", test_GtkIMContext);
72   g_test_add_func ("/builder/get-type/Me2Shell",     test_Me2Shell);
73   g_test_add_func ("/builder/get-type/GWeather",     test_GWeather);
74 
75   return g_test_run ();
76 }
77