Debian Codegraph @

glib/girepository/tests/repository.c

raw
1 /*
2 * Copyright 2023 GNOME Foundation, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 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 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Philip Withnall <[email protected]>
20 */
21
22 #include "glib.h"
23 #include "girepository.h"
24
25 static GIRepository *
26 load_typelib_from_builddir (const char *name,
27 const char *version)
28 {
29 GIRepository *repository;
30 char *gobject_typelib_dir = NULL;
31 GITypelib *typelib = NULL;
32 GError *local_error = NULL;
33
34 gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "introspection", NULL);
35 g_test_message ("Using GI_TYPELIB_DIR = %s", gobject_typelib_dir);
36 gi_repository_prepend_search_path (gobject_typelib_dir);
37 g_free (gobject_typelib_dir);
38
39 repository = gi_repository_new ();
40 g_assert_nonnull (repository);
41
42 typelib = gi_repository_require (repository, name, version, 0, &local_error);
43 g_assert_no_error (local_error);
44 g_assert_nonnull (typelib);
45
46 return g_steal_pointer (&repository);
47 }
48
49 static void
50 test_repository_basic (void)
51 {
52 GIRepository *repository;
53 char *gobject_typelib_dir = NULL;
54 const char * const * search_paths;
55 GITypelib *typelib = NULL;
56 char **namespaces = NULL;
57 const char *expected_namespaces[] = { "GLib", NULL };
58 GError *local_error = NULL;
59 char **versions;
60 size_t n_versions;
61
62 g_test_summary ("Test basic opening of a repository and requiring a typelib");
63
64 gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "introspection", NULL);
65 g_test_message ("Using GI_TYPELIB_DIR = %s", gobject_typelib_dir);
66 gi_repository_prepend_search_path (gobject_typelib_dir);
67
68 repository = gi_repository_new ();
69 g_assert_nonnull (repository);
70
71 versions = gi_repository_enumerate_versions (repository, "SomeInvalidNamespace", &n_versions);
72 g_assert_nonnull (versions);
73 g_assert_cmpstrv (versions, ((char *[]){NULL}));
74 g_assert_cmpuint (n_versions, ==, 0);
75 g_clear_pointer (&versions, g_strfreev);
76
77 versions = gi_repository_enumerate_versions (repository, "GLib", NULL);
78 g_assert_nonnull (versions);
79 g_assert_cmpstrv (versions, ((char *[]){"2.0", NULL}));
80 g_clear_pointer (&versions, g_strfreev);
81
82 search_paths = gi_repository_get_search_path (NULL);
83 g_assert_nonnull (search_paths);
84 g_assert_cmpuint (g_strv_length ((char **) search_paths), >, 0);
85 g_assert_cmpstr (search_paths[0], ==, gobject_typelib_dir);
86
87 typelib = gi_repository_require (repository, "GLib", "2.0", 0, &local_error);
88 g_assert_no_error (local_error);
89 g_assert_nonnull (typelib);
90
91 namespaces = gi_repository_get_loaded_namespaces (repository);
92 g_assert_cmpstrv (namespaces, expected_namespaces);
93 g_strfreev (namespaces);
94
95 g_free (gobject_typelib_dir);
96 g_clear_object (&repository);
97 }
98
99 static void
100 test_repository_info (void)
101 {
102 GIRepository *repository;
103 char *gobject_typelib_dir = NULL;
104 GITypelib *typelib = NULL;
105 GIObjectInfo *object_info = NULL;
106 GISignalInfo *signal_info = NULL;
107 GIFunctionInfo *method_info = NULL;
108 GError *local_error = NULL;
109
110 g_test_summary ("Test retrieving some basic info blobs from a typelib");
111
112 gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "introspection", NULL);
113 g_test_message ("Using GI_TYPELIB_DIR = %s", gobject_typelib_dir);
114 gi_repository_prepend_search_path (gobject_typelib_dir);
115 g_free (gobject_typelib_dir);
116
117 repository = gi_repository_new ();
118 g_assert_nonnull (repository);
119
120 typelib = gi_repository_require (repository, "GObject", "2.0", 0, &local_error);
121 g_assert_no_error (local_error);
122 g_assert_nonnull (typelib);
123
124 object_info = (GIObjectInfo *) gi_repository_find_by_name (repository, "GObject", "Object");
125 g_assert_nonnull (object_info);
126
127 g_assert_cmpint (gi_base_info_get_info_type ((GIBaseInfo *) object_info), ==, GI_INFO_TYPE_OBJECT);
128 g_assert_cmpstr (gi_base_info_get_name ((GIBaseInfo *) object_info), ==, "Object");
129 g_assert_cmpstr (gi_base_info_get_namespace ((GIBaseInfo *) object_info), ==, "GObject");
130
131 signal_info = gi_object_info_find_signal (object_info, "notify");
132 g_assert_nonnull (signal_info);
133
134 g_assert_cmpint (gi_signal_info_get_flags (signal_info), ==,
135 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION);
136
137 g_assert_cmpuint (gi_object_info_get_n_methods (object_info), >, 2);
138
139 method_info = gi_object_info_find_method (object_info, "get_property");
140 g_assert_nonnull (method_info);
141 g_assert_true (gi_callable_info_is_method ((GICallableInfo *) method_info));
142 g_assert_cmpuint (gi_callable_info_get_n_args ((GICallableInfo *) method_info), ==, 2);
143 g_clear_pointer (&method_info, gi_base_info_unref);
144
145 method_info = gi_object_info_get_method (object_info,
146 gi_object_info_get_n_methods (object_info) - 1);
147 g_assert_true (gi_callable_info_is_method ((GICallableInfo *) method_info));
148 g_assert_cmpuint (gi_callable_info_get_n_args ((GICallableInfo *) method_info), >, 0);
149 g_clear_pointer (&method_info, gi_base_info_unref);
150
151 gi_base_info_unref (signal_info);
152 gi_base_info_unref (object_info);
153 g_clear_object (&repository);
154 }
155
156 static void
157 test_repository_dependencies (void)
158 {
159 GIRepository *repository;
160 GITypelib *typelib;
161 GError *error = NULL;
162 char *gobject_typelib_dir = NULL;
163 char **dependencies;
164
165 g_test_summary ("Test ensures namespace dependencies are correctly exposed");
166
167 gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "gobject", NULL);
168 g_test_message ("Using GI_TYPELIB_DIR = %s", gobject_typelib_dir);
169 gi_repository_prepend_search_path (gobject_typelib_dir);
170 g_free (gobject_typelib_dir);
171
172 repository = gi_repository_new ();
173 g_assert_nonnull (repository);
174
175 typelib = gi_repository_require (repository, "GObject", "2.0", 0, &error);
176 g_assert_no_error (error);
177 g_assert_nonnull (typelib);
178
179 dependencies = gi_repository_get_dependencies (repository, "GObject");
180 g_assert_cmpuint (g_strv_length (dependencies), ==, 1);
181 g_assert_true (g_strv_contains ((const char **) dependencies, "GLib-2.0"));
182
183 g_clear_error (&error);
184 g_clear_object (&repository);
185 g_clear_pointer (&dependencies, g_strfreev);
186 }
187
188 static void
189 test_repository_arg_info (void)
190 {
191 GIRepository *repository;
192 GIObjectInfo *object_info = NULL;
193 GIStructInfo *struct_info = NULL;
194 GIFunctionInfo *method_info = NULL;
195 GIArgInfo *arg_info = NULL;
196 GITypeInfo *type_info = NULL;
197 unsigned int idx;
198
199 g_test_summary ("Test retrieving GIArgInfos from a typelib");
200
201 repository = load_typelib_from_builddir ("GObject", "2.0");
202
203 /* Test all the methods of GIArgInfo. Here we’re looking at the
204 * `const char *property_name` argument of g_object_get_property(). (The
205 * ‘self’ argument is not exposed through gi_callable_info_get_arg().) */
206 object_info = (GIObjectInfo *) gi_repository_find_by_name (repository, "GObject", "Object");
207 g_assert_nonnull (object_info);
208
209 method_info = gi_object_info_find_method (object_info, "get_property");
210 g_assert_nonnull (method_info);
211
212 arg_info = gi_callable_info_get_arg (GI_CALLABLE_INFO (method_info), 0);
213 g_assert_nonnull (arg_info);
214
215 g_assert_cmpint (gi_arg_info_get_direction (arg_info), ==, GI_DIRECTION_IN);
216 g_assert_false (gi_arg_info_is_return_value (arg_info));
217 g_assert_false (gi_arg_info_is_optional (arg_info));
218 g_assert_false (gi_arg_info_is_caller_allocates (arg_info));
219 g_assert_false (gi_arg_info_may_be_null (arg_info));
220 g_assert_false (gi_arg_info_is_skip (arg_info));
221 g_assert_cmpint (gi_arg_info_get_ownership_transfer (arg_info), ==, GI_TRANSFER_NOTHING);
222 g_assert_cmpint (gi_arg_info_get_scope (arg_info), ==, GI_SCOPE_TYPE_INVALID);
223 g_assert_false (gi_arg_info_get_closure_index (arg_info, NULL));
224 g_assert_false (gi_arg_info_get_closure_index (arg_info, &idx));
225 g_assert_cmpuint (idx, ==, 0);
226 g_assert_false (gi_arg_info_get_destroy_index (arg_info, NULL));
227 g_assert_false (gi_arg_info_get_destroy_index (arg_info, &idx));
228 g_assert_cmpuint (idx, ==, 0);
229
230 type_info = gi_arg_info_get_type_info (arg_info);
231 g_assert_nonnull (type_info);
232 g_assert_true (gi_type_info_is_pointer (type_info));
233 g_assert_cmpint (gi_type_info_get_tag (type_info), ==, GI_TYPE_TAG_UTF8);
234
235 g_clear_pointer (&type_info, gi_base_info_unref);
236 g_clear_pointer (&arg_info, gi_base_info_unref);
237 g_clear_pointer (&method_info, gi_base_info_unref);
238 g_clear_pointer (&object_info, gi_base_info_unref);
239
240 /* Test an (out) argument. Here it’s the `guint *n_properties` from
241 * g_object_class_list_properties(). */
242 struct_info = (GIStructInfo *) gi_repository_find_by_name (repository, "GObject", "ObjectClass");
243 g_assert_nonnull (struct_info);
244
245 method_info = gi_struct_info_find_method (struct_info, "list_properties");
246 g_assert_nonnull (method_info);
247
248 arg_info = gi_callable_info_get_arg (GI_CALLABLE_INFO (method_info), 0);
249 g_assert_nonnull (arg_info);
250
251 g_assert_cmpint (gi_arg_info_get_direction (arg_info), ==, GI_DIRECTION_OUT);
252 g_assert_false (gi_arg_info_is_optional (arg_info));
253 g_assert_false (gi_arg_info_is_caller_allocates (arg_info));
254 g_assert_cmpint (gi_arg_info_get_ownership_transfer (arg_info), ==, GI_TRANSFER_EVERYTHING);
255
256 g_clear_pointer (&arg_info, gi_base_info_unref);
257 g_clear_pointer (&method_info, gi_base_info_unref);
258 g_clear_pointer (&struct_info, gi_base_info_unref);
259
260 g_clear_object (&repository);
261 }
262
263 static void
264 test_repository_boxed_info (void)
265 {
266 GIRepository *repository;
267 GIBoxedInfo *boxed_info = NULL;
268
269 g_test_summary ("Test retrieving GIBoxedInfos from a typelib");
270
271 repository = load_typelib_from_builddir ("GObject", "2.0");
272
273 /* Test all the methods of GIBoxedInfo. This is simple, because there are none. */
274 boxed_info = (GIBoxedInfo *) gi_repository_find_by_name (repository, "GObject", "BookmarkFile");
275 g_assert_nonnull (boxed_info);
276
277 g_clear_pointer (&boxed_info, gi_base_info_unref);
278
279 g_clear_object (&repository);
280 }
281
282 static void
283 test_repository_callable_info (void)
284 {
285 GIRepository *repository;
286 GIObjectInfo *object_info = NULL;
287 GIFunctionInfo *method_info = NULL;
288 GICallableInfo *callable_info;
289 GITypeInfo *type_info = NULL;
290 GIAttributeIter iter = GI_ATTRIBUTE_ITER_INIT;
291 const char *name, *value;
292 GIArgInfo *arg_info = NULL;
293
294 g_test_summary ("Test retrieving GICallableInfos from a typelib");
295
296 repository = load_typelib_from_builddir ("GObject", "2.0");
297
298 /* Test all the methods of GICallableInfo. Here we’re looking at
299 * g_object_get_qdata(). */
300 object_info = (GIObjectInfo *) gi_repository_find_by_name (repository, "GObject", "Object");
301 g_assert_nonnull (object_info);
302
303 method_info = gi_object_info_find_method (object_info, "get_qdata");
304 g_assert_nonnull (method_info);
305
306 callable_info = GI_CALLABLE_INFO (method_info);
307
308 g_assert_true (gi_callable_info_is_method (callable_info));
309 g_assert_false (gi_callable_info_can_throw_gerror (callable_info));
310
311 type_info = gi_callable_info_get_return_type (callable_info);
312 g_assert_nonnull (type_info);
313 g_assert_true (gi_type_info_is_pointer (type_info));
314 g_assert_cmpint (gi_type_info_get_tag (type_info), ==, GI_TYPE_TAG_VOID);
315 g_clear_pointer (&type_info, gi_base_info_unref);
316
317 /* This method has no attributes */
318 g_assert_false (gi_callable_info_iterate_return_attributes (callable_info, &iter, &name, &value));
319
320 g_assert_null (gi_callable_info_get_return_attribute (callable_info, "doesnt-exist"));
321
322 g_assert_false (gi_callable_info_get_caller_owns (callable_info));
323 g_assert_true (gi_callable_info_may_return_null (callable_info));
324 g_assert_false (gi_callable_info_skip_return (callable_info));
325
326 g_assert_cmpuint (gi_callable_info_get_n_args (callable_info), ==, 1);
327
328 arg_info = gi_callable_info_get_arg (callable_info, 0);
329 g_assert_nonnull (arg_info);
330 g_clear_pointer (&arg_info, gi_base_info_unref);
331
332 g_assert_cmpint (gi_callable_info_get_instance_ownership_transfer (callable_info), ==, GI_TRANSFER_NOTHING);
333
334 g_clear_pointer (&method_info, gi_base_info_unref);
335 g_clear_pointer (&object_info, gi_base_info_unref);
336
337 g_clear_object (&repository);
338 }
339
340 static void
341 test_repository_callback_info (void)
342 {
343 GIRepository *repository;
344 GICallbackInfo *callback_info = NULL;
345
346 g_test_summary ("Test retrieving GICallbackInfos from a typelib");
347
348 repository = load_typelib_from_builddir ("GObject", "2.0");
349
350 /* Test all the methods of GICallbackInfo. This is simple, because there are none. */
351 callback_info = (GICallbackInfo *) gi_repository_find_by_name (repository, "GObject", "ObjectFinalizeFunc");
352 g_assert_nonnull (callback_info);
353
354 g_clear_pointer (&callback_info, gi_base_info_unref);
355
356 g_clear_object (&repository);
357 }
358
359 int
360 main (int argc,
361 char *argv[])
362 {
363 /* Isolate from the system typelibs and GIRs. */
364 g_setenv ("GI_TYPELIB_PATH", "/dev/null", TRUE);
365 g_setenv ("GI_GIR_PATH", "/dev/null", TRUE);
366
367 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
368
369 g_test_add_func ("/repository/basic", test_repository_basic);
370 g_test_add_func ("/repository/info", test_repository_info);
371 g_test_add_func ("/repository/dependencies", test_repository_dependencies);
372 g_test_add_func ("/repository/arg-info", test_repository_arg_info);
373 g_test_add_func ("/repository/boxed-info", test_repository_boxed_info);
374 g_test_add_func ("/repository/callable-info", test_repository_callable_info);
375 g_test_add_func ("/repository/callback-info", test_repository_callback_info);
376
377 return g_test_run ();
378 }