Branch data Line data Source code
1 : : /*
2 : : * Copyright 2008 litl, LLC
3 : : * Copyright 2014 Simon Feltman <sfeltman@gnome.org>
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : */
20 : :
21 : : #include "config.h"
22 : :
23 : : #include "girepository.h"
24 : : #include "test-common.h"
25 : :
26 : : static void
27 : 1 : test_invoke_gerror (RepositoryFixture *fx,
28 : : const void *unused)
29 : : {
30 : 1 : GIFunctionInfo *func_info = NULL;
31 : : GIArgument in_arg[1];
32 : : GIArgument ret_arg;
33 : 1 : GError *error = NULL;
34 : : gboolean invoke_return;
35 : :
36 : 1 : g_test_summary ("Test invoking a function that throws a GError");
37 : :
38 : 1 : func_info = GI_FUNCTION_INFO (gi_repository_find_by_name (fx->repository, "GLib", "file_read_link"));
39 : 1 : g_assert_nonnull (func_info);
40 : 1 : g_assert_true (gi_callable_info_can_throw_gerror (GI_CALLABLE_INFO (func_info)));
41 : :
42 : 1 : in_arg[0].v_string = g_strdup ("non-existent-file/hope");
43 : 1 : invoke_return = gi_function_info_invoke (func_info, in_arg, 1, NULL, 0, &ret_arg, &error);
44 : 1 : g_clear_pointer (&in_arg[0].v_string, g_free);
45 : :
46 : 1 : g_assert_false (invoke_return);
47 : 1 : g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
48 : :
49 : 1 : g_clear_error (&error);
50 : 1 : g_clear_pointer (&func_info, gi_base_info_unref);
51 : 1 : }
52 : :
53 : : static void
54 : 1 : test_vfunc_can_throw_gerror (RepositoryFixture *fx,
55 : : const void *unused)
56 : : {
57 : 1 : GIInterfaceInfo *interface_info = NULL;
58 : 1 : GIFunctionInfo *invoker_info = NULL;
59 : 1 : GIVFuncInfo *vfunc_info = NULL;
60 : :
61 : 1 : g_test_summary ("Test gi_callable_info_can_throw_gerror() on a vfunc");
62 : :
63 : 1 : interface_info = GI_INTERFACE_INFO (gi_repository_find_by_name (fx->repository, "Gio", "AppInfo"));
64 : 1 : g_assert_nonnull (interface_info);
65 : :
66 : 1 : invoker_info = gi_interface_info_find_method (interface_info, "launch");
67 : 1 : g_assert_nonnull (invoker_info);
68 : 1 : g_assert_true (gi_callable_info_can_throw_gerror (GI_CALLABLE_INFO (invoker_info)));
69 : :
70 : 1 : vfunc_info = gi_interface_info_find_vfunc (interface_info, "launch");
71 : 1 : g_assert_nonnull (vfunc_info);
72 : 1 : g_assert_true (gi_callable_info_can_throw_gerror (GI_CALLABLE_INFO (vfunc_info)));
73 : :
74 : 1 : g_clear_pointer (&vfunc_info, gi_base_info_unref);
75 : 1 : g_clear_pointer (&invoker_info, gi_base_info_unref);
76 : 1 : g_clear_pointer (&interface_info, gi_base_info_unref);
77 : 1 : }
78 : :
79 : : static void
80 : 1 : test_callback_can_throw_gerror (RepositoryFixture *fx,
81 : : const void *unused)
82 : : {
83 : 1 : GIStructInfo *class_info = NULL;
84 : 1 : GIFieldInfo *field_info = NULL;
85 : 1 : GITypeInfo *field_type = NULL;
86 : 1 : GICallbackInfo *callback_info = NULL;
87 : :
88 : 1 : g_test_summary ("Test gi_callable_info_can_throw_gerror() on a callback");
89 : :
90 : 1 : class_info = GI_STRUCT_INFO (gi_repository_find_by_name (fx->repository, "Gio", "AppInfoIface"));
91 : 1 : g_assert_nonnull (class_info);
92 : :
93 : 1 : field_info = gi_struct_info_find_field (class_info, "launch");
94 : 1 : g_assert_nonnull (field_info);
95 : 1 : g_assert_true (GI_IS_FIELD_INFO (field_info));
96 : :
97 : 1 : field_type = gi_field_info_get_type_info (field_info);
98 : 1 : g_assert_nonnull (field_type);
99 : 1 : g_assert_true (GI_IS_TYPE_INFO (field_type));
100 : 1 : g_assert_cmpint (gi_type_info_get_tag (field_type), ==, GI_TYPE_TAG_INTERFACE);
101 : :
102 : 1 : callback_info = GI_CALLBACK_INFO (gi_type_info_get_interface (field_type));
103 : 1 : g_assert_nonnull (callback_info);
104 : 1 : g_assert (gi_callable_info_can_throw_gerror (GI_CALLABLE_INFO (callback_info)));
105 : :
106 : 1 : g_clear_pointer (&callback_info, gi_base_info_unref);
107 : 1 : g_clear_pointer (&field_type, gi_base_info_unref);
108 : 1 : g_clear_pointer (&field_info, gi_base_info_unref);
109 : 1 : g_clear_pointer (&class_info, gi_base_info_unref);
110 : 1 : }
111 : :
112 : : int
113 : 1 : main (int argc,
114 : : char *argv[])
115 : : {
116 : 1 : repository_init (&argc, &argv);
117 : :
118 : 1 : ADD_REPOSITORY_TEST ("/throws/invoke-gerror", test_invoke_gerror, &typelib_load_spec_glib);
119 : 1 : ADD_REPOSITORY_TEST ("/throws/vfunc-can-throw-gerror", test_vfunc_can_throw_gerror, &typelib_load_spec_gio);
120 : 1 : ADD_REPOSITORY_TEST ("/throws/callback-can-throw-gerror", test_callback_can_throw_gerror, &typelib_load_spec_gio);
121 : :
122 : 1 : return g_test_run ();
123 : : }
|