Imported Upstream version 1.35.9
[platform/upstream/gobject-introspection.git] / tests / repository / gitypelibtest.c
1 /* -*- Mode: C; c-basic-offset: 2 -*-
2  * vim: shiftwidth=2 expandtab
3  */
4
5 #include "girepository.h"
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 static void
12 test_enum_and_flags_cidentifier (GIRepository * repo)
13 {
14   GITypelib *ret;
15   GError *error = NULL;
16   gint n_infos, i;
17
18   ret = g_irepository_require (repo, "GIMarshallingTests", NULL, 0, &error);
19   if (!ret)
20     g_error ("%s", error->message);
21
22   n_infos = g_irepository_get_n_infos (repo, "GIMarshallingTests");
23
24   for (i = 0; i < n_infos; i++)
25     {
26       GIBaseInfo *info;
27
28       info = g_irepository_get_info (repo, "GIMarshallingTests", i);
29
30       /* both GI_INFO_TYPE_ENUM and GI_INFO_TYPE_FLAGS use GIEnumInfo */
31       if (GI_IS_ENUM_INFO (info))
32         {
33           gint n_values, j;
34
35           n_values = g_enum_info_get_n_values ((GIEnumInfo *) info);
36           for (j = 0; j < n_values; j++)
37             {
38               GIValueInfo *value_info;
39               const gchar *c_identifier = NULL;
40
41               value_info = g_enum_info_get_value ((GIEnumInfo *) info, j);
42               c_identifier = g_base_info_get_attribute ((GIBaseInfo *) value_info, "c:identifier");
43
44               if (c_identifier == NULL)
45                 {
46                   g_error
47                     ("Error: no 'c:identifier' attribute on GIMarshallingTests.%s.%s\n",
48                      g_base_info_get_name (info), g_base_info_get_name ((GIBaseInfo *) value_info));
49                 }
50
51               g_base_info_unref ((GIBaseInfo *) value_info);
52             }
53         }
54
55       g_base_info_unref (info);
56     }
57 }
58
59 static void
60 _check_enum_methods (GIBaseInfo * info, const gchar * name, const gchar * prefix)
61 {
62   gint n_methods, i;
63
64   n_methods = g_enum_info_get_n_methods ((GIEnumInfo *) info);
65   if (n_methods <= 0)
66     g_error ("%s should have methods", name);
67
68   for (i = 0; i < n_methods; i += n_methods - 1)
69     {
70       GIBaseInfo *function_info;
71       GIFunctionInfoFlags flags;
72       const gchar *symbol;
73       function_info = g_enum_info_get_method ((GIEnumInfo *) info, i);
74       if (!function_info)
75         g_error ("Could not find %s method nr. %d", name, i + 1);
76       flags = g_function_info_get_flags ((GIFunctionInfo *) function_info);
77       if (flags != 0)
78         g_error ("%s methods should be static", name);
79       symbol = g_function_info_get_symbol ((GIFunctionInfo *) function_info);
80       if (!symbol || !g_str_has_prefix (symbol, prefix))
81         g_error ("Could not find valid function symbol");
82       g_base_info_unref (function_info);
83     }
84 }
85
86 static void
87 test_enum_and_flags_static_methods (GIRepository * repo)
88 {
89   GITypelib *ret;
90   GError *error = NULL;
91   GIBaseInfo *enum_info;
92
93   ret = g_irepository_require (repo, "GIMarshallingTests", NULL, 0, &error);
94   if (!ret)
95     g_error ("%s", error->message);
96
97   enum_info = g_irepository_find_by_name (repo, "GIMarshallingTests", "GEnum");
98   if (!enum_info)
99     g_error ("Could not find GIMarshallingTests.GEnum");
100   _check_enum_methods (enum_info, "GIMarshallingTests.GEnum", "gi_marshalling_tests_genum_");
101   g_base_info_unref (enum_info);
102
103   enum_info = g_irepository_find_by_name (repo, "GIMarshallingTests", "Flags");
104   if (!enum_info)
105     g_error ("Could not find GIMarshallingTests.Flags");
106   _check_enum_methods (enum_info, "GIMarshallingTests.Flags", "gi_marshalling_tests_flags_");
107   g_base_info_unref (enum_info);
108 }
109
110 static void
111 test_size_of_gvalue (GIRepository * repo)
112 {
113   GIBaseInfo *struct_info;
114
115   struct_info = g_irepository_find_by_name (repo, "GObject", "Value");
116   if (!struct_info)
117     g_error ("Could not find GObject.Value");
118   g_assert_cmpuint (g_struct_info_get_size (struct_info), ==, sizeof (GValue));
119   g_base_info_unref (struct_info);
120 }
121
122 static void
123 test_is_pointer_for_struct_arg (GIRepository * repo)
124 {
125   GITypelib *ret;
126   GError *error = NULL;
127   GIStructInfo *variant_info;
128   GIFunctionInfo *equal_info;
129   GIArgInfo *arg_info;
130   GITypeInfo *type_info;
131
132   ret = g_irepository_require (repo, "GLib", NULL, 0, &error);
133   if (!ret)
134     g_error ("%s", error->message);
135
136   variant_info = g_irepository_find_by_name (repo, "GLib", "Variant");
137   if (!variant_info)
138     g_error ("Could not find GLib.Variant");
139
140   equal_info = g_struct_info_find_method (variant_info, "equal");
141   if (!equal_info)
142     g_error ("Could not find GLib.Variant.equal()");
143
144   arg_info = g_callable_info_get_arg (equal_info, 0);
145   if (!arg_info)
146     g_error ("Could not find 1st arg of GLib.Variant.equal()");
147
148   type_info = g_arg_info_get_type (arg_info);
149   if (!type_info)
150     g_error ("Could not find typeinfo of 1st arg of GLib.Variant.equal()");
151
152   g_assert (g_type_info_is_pointer (type_info));
153
154   g_base_info_unref (type_info);
155   g_base_info_unref (arg_info);
156   g_base_info_unref (equal_info);
157   g_base_info_unref (variant_info);
158 }
159
160 static void
161 test_fundamental_get_ref_function_pointer (GIRepository * repo)
162 {
163   GIObjectInfo *info;
164
165   g_assert (g_irepository_require (repo, "Regress", NULL, 0, NULL));
166   info = g_irepository_find_by_name (repo, "Regress", "TestFundamentalObject");
167   g_object_info_get_ref_function_pointer (info);
168   g_base_info_unref (info);
169 }
170
171 static void
172 test_hash_with_cairo_typelib (GIRepository * repo)
173 {
174   GIBaseInfo *info;
175
176   g_assert (g_irepository_require (repo, "cairo", NULL, 0, NULL));
177   info = g_irepository_find_by_name (repo, "cairo", "region");
178   g_assert (info == NULL);
179 }
180
181 static GIPropertyInfo *
182 lookup_property (GIObjectInfo * info, const gchar * name)
183 {
184   gssize n_props;
185   gssize i;
186   GIPropertyInfo *property_info;
187
188   n_props = g_object_info_get_n_properties (info);
189   for (i = 0; i < n_props; i++)
190     {
191       property_info = g_object_info_get_property (info, i);
192       if (strcmp (name, g_base_info_get_name (property_info)) == 0)
193         return property_info;
194       g_base_info_unref (property_info);
195     }
196
197   return NULL;
198 }
199
200 static void
201 test_char_types (GIRepository * repo)
202 {
203   GITypelib *ret;
204   GError *error = NULL;
205   GIBaseInfo *prop_obj;
206   GIPropertyInfo *prop_info;
207   GITypeInfo *type_info;
208
209   ret = g_irepository_require (repo, "GIMarshallingTests", NULL, 0, &error);
210   if (!ret)
211     g_error ("%s", error->message);
212
213   prop_obj = g_irepository_find_by_name (repo, "GIMarshallingTests", "PropertiesObject");
214   g_assert (prop_obj != NULL);
215   g_assert (GI_IS_OBJECT_INFO (prop_obj));
216
217   /* unsigned char */
218   prop_info = lookup_property ((GIObjectInfo *) prop_obj, "some-uchar");
219   g_assert (prop_info != NULL);
220   type_info = g_property_info_get_type (prop_info);
221   g_assert_cmpuint (g_type_info_get_tag (type_info), ==, GI_TYPE_TAG_UINT8);
222   g_base_info_unref (type_info);
223   g_base_info_unref (prop_info);
224
225   /* signed char */
226   prop_info = lookup_property ((GIObjectInfo *) prop_obj, "some-char");
227   g_assert (prop_info != NULL);
228   type_info = g_property_info_get_type (prop_info);
229   g_assert_cmpuint (g_type_info_get_tag (type_info), ==, GI_TYPE_TAG_INT8);
230   g_base_info_unref (type_info);
231   g_base_info_unref (prop_info);
232
233   g_base_info_unref (prop_obj);
234 }
235
236 static void
237 test_signal_array_len (GIRepository * repo)
238 {
239   GIObjectInfo *testobj_info;
240   GISignalInfo *sig_info;
241   GIArgInfo arg_info;
242   GITypeInfo type_info;
243   int i;
244
245   g_assert (g_irepository_require (repo, "Regress", NULL, 0, NULL));
246   testobj_info = g_irepository_find_by_name (repo, "Regress", "TestObj");
247   g_assert (testobj_info != NULL);
248
249   /* find sig-with-array-len-prop signal */
250   for (i = g_object_info_get_n_signals (testobj_info) - 1; i >= 0; --i)
251     {
252       sig_info = g_object_info_get_signal (testobj_info, i);
253       g_assert (sig_info != NULL);
254       if (strcmp (g_base_info_get_name (sig_info), "sig-with-array-len-prop") == 0)
255         break;
256       g_base_info_unref (sig_info);
257     }
258   g_assert (i >= 0);
259
260   g_assert_cmpint (g_callable_info_get_n_args (sig_info), ==, 2);
261
262   /* verify array argument */
263   g_callable_info_load_arg (sig_info, 0, &arg_info);
264   g_assert_cmpstr (g_base_info_get_name (&arg_info), ==, "arr");
265   g_arg_info_load_type (&arg_info, &type_info);
266   g_assert_cmpint (g_type_info_get_tag (&type_info), ==, GI_TYPE_TAG_ARRAY);
267   g_assert_cmpint (g_type_info_get_array_type (&type_info), ==, GI_ARRAY_TYPE_C);
268   g_assert (!g_type_info_is_zero_terminated (&type_info));
269   g_assert_cmpint (g_type_info_get_array_length (&type_info), ==, 1);
270
271   /* verify array length argument */
272   g_callable_info_load_arg (sig_info, 1, &arg_info);
273   g_assert_cmpstr (g_base_info_get_name (&arg_info), ==, "len");
274
275   g_base_info_unref (sig_info);
276   g_base_info_unref (testobj_info);
277 }
278
279 int
280 main (int argc, char **argv)
281 {
282   GIRepository *repo;
283
284   repo = g_irepository_get_default ();
285
286   /* do tests */
287   test_enum_and_flags_cidentifier (repo);
288   test_enum_and_flags_static_methods (repo);
289   test_size_of_gvalue (repo);
290   test_is_pointer_for_struct_arg (repo);
291   test_fundamental_get_ref_function_pointer (repo);
292   test_hash_with_cairo_typelib (repo);
293   test_char_types (repo);
294   test_signal_array_len (repo);
295
296   exit (0);
297 }