Fix g_autoptr_cleanup_gstring_free( ) argument to avoid of confliting arument name
[platform/upstream/glib.git] / gobject / tests / deprecated-properties.c
1 /*
2  * Copyright 2022 Simon McVittie
3  * SPDX-License-Identifier: LGPL-2.1-or-later
4  */
5
6 #include <glib-object.h>
7 #include <glib.h>
8
9 typedef struct
10 {
11   GObject parent;
12   int normal;
13   int normal_construct;
14   int deprecated;
15   int deprecated_construct;
16 } MyObject;
17
18 typedef struct
19 {
20   GObjectClass parent;
21 } MyObjectClass;
22
23 typedef enum
24 {
25   PROP_0,
26   PROP_NORMAL,
27   PROP_NORMAL_CONSTRUCT,
28   PROP_DEPRECATED,
29   PROP_DEPRECATED_CONSTRUCT,
30   N_PROPS
31 } Property;
32
33 static GParamSpec *props[N_PROPS] = { NULL };
34
35 static GType my_object_get_type (void);
36
37 G_DEFINE_TYPE (MyObject, my_object, G_TYPE_OBJECT);
38
39 static void
40 my_object_init (MyObject *self)
41 {
42 }
43
44 static void
45 my_object_set_property (GObject *object,
46                         guint prop_id,
47                         const GValue *value,
48                         GParamSpec *param_spec)
49 {
50   MyObject *self = (MyObject *) object;
51
52   switch ((Property) prop_id)
53     {
54     case PROP_NORMAL:
55       self->normal = g_value_get_int (value);
56       break;
57
58     case PROP_NORMAL_CONSTRUCT:
59       self->normal_construct = g_value_get_int (value);
60       break;
61
62     case PROP_DEPRECATED:
63       self->deprecated = g_value_get_int (value);
64       break;
65
66     case PROP_DEPRECATED_CONSTRUCT:
67       self->deprecated_construct = g_value_get_int (value);
68       break;
69
70     case PROP_0:
71     case N_PROPS:
72     default:
73       g_assert_not_reached ();
74     }
75 }
76
77 static void
78 my_object_get_property (GObject *object,
79                         guint prop_id,
80                         GValue *value,
81                         GParamSpec *param_spec)
82 {
83   MyObject *self = (MyObject *) object;
84
85   switch ((Property) prop_id)
86     {
87     case PROP_NORMAL:
88       g_value_set_int (value, self->normal);
89       break;
90
91     case PROP_NORMAL_CONSTRUCT:
92       g_value_set_int (value, self->normal_construct);
93       break;
94
95     case PROP_DEPRECATED:
96       g_value_set_int (value, self->deprecated);
97       break;
98
99     case PROP_DEPRECATED_CONSTRUCT:
100       g_value_set_int (value, self->deprecated_construct);
101       break;
102
103     case PROP_0:
104     case N_PROPS:
105     default:
106       g_assert_not_reached ();
107     }
108 }
109
110 static void
111 my_object_class_init (MyObjectClass *cls)
112 {
113   GObjectClass *object_class = G_OBJECT_CLASS (cls);
114
115   props[PROP_NORMAL] = g_param_spec_int ("normal", NULL, NULL,
116                                          G_MININT, G_MAXINT, -1,
117                                          (G_PARAM_READWRITE |
118                                           G_PARAM_STATIC_STRINGS));
119   props[PROP_NORMAL_CONSTRUCT] = g_param_spec_int ("normal-construct", NULL, NULL,
120                                                    G_MININT, G_MAXINT, -1,
121                                                    (G_PARAM_READWRITE |
122                                                     G_PARAM_STATIC_STRINGS |
123                                                     G_PARAM_CONSTRUCT));
124   props[PROP_DEPRECATED] = g_param_spec_int ("deprecated", NULL, NULL,
125                                              G_MININT, G_MAXINT, -1,
126                                              (G_PARAM_READWRITE |
127                                               G_PARAM_STATIC_STRINGS |
128                                               G_PARAM_DEPRECATED));
129   props[PROP_DEPRECATED_CONSTRUCT] = g_param_spec_int ("deprecated-construct", NULL, NULL,
130                                                        G_MININT, G_MAXINT, -1,
131                                                        (G_PARAM_READWRITE |
132                                                         G_PARAM_STATIC_STRINGS |
133                                                         G_PARAM_CONSTRUCT |
134                                                         G_PARAM_DEPRECATED));
135   object_class->get_property = my_object_get_property;
136   object_class->set_property = my_object_set_property;
137   g_object_class_install_properties (object_class, N_PROPS, props);
138 }
139
140 static void
141 test_construct (void)
142 {
143   if (g_test_subprocess ())
144     {
145       MyObject *o;
146
147       /* Don't crash on deprecation warnings, so we can see all of them */
148       g_log_set_always_fatal (G_LOG_FATAL_MASK);
149       g_log_set_fatal_mask ("GLib-GObject", G_LOG_FATAL_MASK);
150
151       o = g_object_new (my_object_get_type (),
152                         "normal", 1,
153                         "normal-construct", 2,
154                         "deprecated", 3,
155                         "deprecated-construct", 4,
156                         NULL);
157       g_printerr ("Constructed object");
158       g_assert_cmpint (o->normal, ==, 1);
159       g_assert_cmpint (o->normal_construct, ==, 2);
160       g_assert_cmpint (o->deprecated, ==, 3);
161       g_assert_cmpint (o->deprecated_construct, ==, 4);
162       g_clear_object (&o);
163       return;
164     }
165
166   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
167   g_test_trap_assert_stderr ("*The property MyObject:deprecated-construct is deprecated*");
168   g_test_trap_assert_stderr ("*The property MyObject:deprecated is deprecated*");
169   g_test_trap_assert_stderr_unmatched ("*The property MyObject:normal*");
170   g_test_trap_assert_passed ();
171 }
172
173 static void
174 test_def_construct (void)
175 {
176   if (g_test_subprocess ())
177     {
178       MyObject *o;
179
180       /* Don't crash on deprecation warnings, so we can see all of them */
181       g_log_set_always_fatal (G_LOG_FATAL_MASK);
182       g_log_set_fatal_mask ("GLib-GObject", G_LOG_FATAL_MASK);
183
184       o = g_object_new (my_object_get_type (),
185                         NULL);
186       g_printerr ("Constructed object");
187       g_assert_cmpint (o->normal, ==, 0);
188       g_assert_cmpint (o->normal_construct, ==, -1);
189       g_assert_cmpint (o->deprecated, ==, 0);
190       g_assert_cmpint (o->deprecated_construct, ==, -1);
191       g_clear_object (&o);
192       return;
193     }
194
195   g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2748");
196   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
197   g_test_trap_assert_stderr_unmatched ("*The property MyObject:deprecated*");
198   g_test_trap_assert_stderr_unmatched ("*The property MyObject:normal*");
199   g_test_trap_assert_passed ();
200 }
201
202 static void
203 test_set (void)
204 {
205   if (g_test_subprocess ())
206     {
207       MyObject *o;
208
209       /* Don't crash on deprecation warnings, so we can see all of them */
210       g_log_set_always_fatal (G_LOG_FATAL_MASK);
211       g_log_set_fatal_mask ("GLib-GObject", G_LOG_FATAL_MASK);
212
213       o = g_object_new (my_object_get_type (),
214                         NULL);
215       g_printerr ("Constructed object");
216       g_assert_cmpint (o->normal, ==, 0);
217       g_assert_cmpint (o->normal_construct, ==, -1);
218       g_assert_cmpint (o->deprecated, ==, 0);
219       g_assert_cmpint (o->deprecated_construct, ==, -1);
220
221       g_object_set (o,
222                     "normal", 1,
223                     "normal-construct", 2,
224                     "deprecated", 3,
225                     "deprecated-construct", 4,
226                     NULL);
227       g_printerr ("Set properties");
228       g_assert_cmpint (o->normal, ==, 1);
229       g_assert_cmpint (o->normal_construct, ==, 2);
230       g_assert_cmpint (o->deprecated, ==, 3);
231       g_assert_cmpint (o->deprecated_construct, ==, 4);
232
233       g_clear_object (&o);
234       return;
235     }
236
237   g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2748");
238   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
239   g_test_trap_assert_stderr ("*The property MyObject:deprecated-construct is deprecated*");
240   g_test_trap_assert_stderr ("*The property MyObject:deprecated is deprecated*");
241   g_test_trap_assert_stderr_unmatched ("*The property MyObject:normal*");
242   g_test_trap_assert_passed ();
243 }
244
245 int
246 main (int argc, char *argv[])
247 {
248   g_test_init (&argc, &argv, NULL);
249
250   g_setenv ("G_ENABLE_DIAGNOSTIC", "1", TRUE);
251
252   g_test_set_nonfatal_assertions ();
253   g_test_add_func ("/deprecated-properties/construct", test_construct);
254   g_test_add_func ("/deprecated-properties/default-construct", test_def_construct);
255   g_test_add_func ("/deprecated-properties/set", test_set);
256   return g_test_run ();
257 }