]> git.draconx.ca Git - gob-dx.git/blob - tests/interface.at
Add missing g_type_init calls to test cases.
[gob-dx.git] / tests / interface.at
1 dnl Copyright © 2020 Nick Bowler
2 dnl License GPLv2+: GNU General Public License version 2 or any later version.
3 dnl This is free software: you are free to change and redistribute it.
4 dnl There is NO WARRANTY, to the extent permitted by law.
5
6 dnl Create the Test:Fooable interface with the following interface method:
7 dnl
8 dnl   int foo(G:Object *obj);
9 dnl
10 dnl Link in test-fooable.o
11 m4_define([TEST_FOOABLE_IFACE],
12 [AT_KEYWORDS([interface])dnl
13 AT_CHECK([cp $srcdir/t/test-fooable.c $srcdir/t/test-fooable.h .])
14 TEST_COMPILE_GOBJECT([test-fooable.c], [0], [], [ignore])])
15
16 dnl TEST_FOOABLE_IMPL(Class:Name, Parent:Class, foo_body)
17 dnl TEST_FOOABLE_IMPL_DYN(Class:Name, Parent:Class, foo_body)
18 m4_define([TEST_FOOABLE_IMPL],
19   [TEST_FOOABLE_IMPL_([$1], TEST_CLASSNAME_REPLACE_SEP([$1], [-]),
20                       [$2], TEST_CLASSNAME_REPLACE_SEP([$2], [-]),
21                       [$3], [$4])])
22
23 m4_define([TEST_FOOABLE_IMPL_DYN],
24 [TEST_FOOABLE_IMPL([$1], [$2], [$3], [(dynamic)])
25 TEST_TYPE_MODULE([$1])])
26
27 m4_define([TEST_FOOABLE_IMPL_],
28 [AT_DATA([$2.gob], [[%ctop{
29 #include <config.h>
30 %}
31 %{
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "test-fooable.h"
35 %}
36 ]m4_if([$4], [g-object], [], [[
37 %h{
38 #include "$4.h"
39 %}
40 ]])[
41 class $1 from $3]m4_default_nblank([
42   $6])[
43   (interface Test:Fooable)
44 {
45   interface Test:Fooable private int foo(G:Object *go)
46     onerror 88
47   {
48     $5
49     abort();
50   }
51 }
52 ]])
53 AT_CHECK([gob2 $2.gob])
54 TEST_COMPILE_GOBJECT([$2.c], [0], [], [ignore])])
55
56 dnl Test that a static type can implement an interface.
57 AT_SETUP([interface implementation])
58 AT_KEYWORDS([runtime])dnl
59
60 TEST_FOOABLE_IFACE()
61 TEST_FOOABLE_IMPL([Test:A], [G:Object], [return 42;])
62
63 AT_DATA([main.c],
64 [[#include <config.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include "test-fooable.h"
68 #include "test-a.h"
69
70 int main(void)
71 {
72   int rc;
73
74   g_type_init();
75   rc = test_foo(g_object_new(TEST_TYPE_A, NULL));
76   printf("%d\n", rc);
77   if (rc < 0)
78     return EXIT_FAILURE;
79   return 0;
80 }
81 ]])
82 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
83
84 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
85   test-a.o test-fooable.o main.o])
86 AT_CHECK([./main], [0], [42
87 ])
88
89 AT_CLEANUP
90
91 dnl Test that a dynamic type can implement an interface.
92 AT_SETUP([interface implementation (dynamic)])
93 AT_KEYWORDS([runtime])dnl
94
95 TEST_FOOABLE_IFACE()
96 TEST_FOOABLE_IMPL_DYN([Test:A], [G:Object], [return 54;])
97
98 AT_DATA([main.c],
99 [[#include <config.h>
100 #include <stdio.h>
101 #include <stdlib.h>
102 #include "test-fooable.h"
103 #include "test-a.h"
104 #include "test-a-mod.h"
105
106 int main(void)
107 {
108   int rc;
109
110   g_type_init();
111   g_type_module_use(g_object_new(TEST_TYPE_A_MOD, NULL));
112
113   rc = test_foo(g_object_new(TEST_TYPE_A, NULL));
114   printf("%d\n", rc);
115   if (rc < 0)
116     return EXIT_FAILURE;
117   return 0;
118 }
119 ]])
120 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
121
122 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
123   test-a.o test-a-mod.o test-fooable.o main.o])
124 AT_CHECK([./main], [0], [54
125 ])
126
127 AT_CLEANUP
128
129 dnl Test that a static type can override the interface implementation
130 dnl in the parent type.
131 AT_SETUP([interface method override])
132 AT_KEYWORDS([runtime])dnl
133
134 TEST_FOOABLE_IFACE()
135 TEST_FOOABLE_IMPL([Test:A], [G:Object],
136   [puts("Test:A foo called"); return 42;])
137 TEST_FOOABLE_IMPL([Test:B], [Test:A],
138   [puts("Test:B foo called"); return 54;])
139
140 AT_DATA([main.c],
141 [[#include <config.h>
142 #include <stdio.h>
143 #include <stdlib.h>
144 #include "test-fooable.h"
145 #include "test-a.h"
146 #include "test-b.h"
147
148 int main(void)
149 {
150   int rc;
151
152   g_type_init();
153   rc = test_foo(g_object_new(TEST_TYPE_A, NULL));
154   printf("%d\n", rc);
155   if (rc < 0)
156     return EXIT_FAILURE;
157
158   rc = test_foo(g_object_new(TEST_TYPE_B, NULL));
159   printf("%d\n", rc);
160   if (rc < 0)
161     return EXIT_FAILURE;
162
163   return 0;
164 }
165 ]])
166 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
167
168 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
169   test-a.o test-b.o test-fooable.o main.o])
170 AT_CHECK([./main], [0], [Test:A foo called
171 42
172 Test:B foo called
173 54
174 ])
175
176 AT_CLEANUP
177
178 dnl Test that a dynamic type can override the interface implementation of
179 dnl a static parent type.
180 AT_SETUP([interface method override (dynamic)])
181 AT_KEYWORDS([runtime])dnl
182
183 TEST_FOOABLE_IFACE()
184 TEST_FOOABLE_IMPL([Test:A], [G:Object],
185   [puts("Test:A foo called"); return 42;])
186 TEST_FOOABLE_IMPL_DYN([Test:B], [Test:A],
187   [puts("Test:B foo called"); return 54;])
188
189 AT_DATA([main.c],
190 [[#include <config.h>
191 #include <stdio.h>
192 #include <stdlib.h>
193 #include "test-fooable.h"
194 #include "test-a.h"
195 #include "test-b.h"
196 #include "test-b-mod.h"
197
198 int main(void)
199 {
200   int rc;
201
202   g_type_init();
203   g_type_module_use(g_object_new(TEST_TYPE_B_MOD, NULL));
204
205   rc = test_foo(g_object_new(TEST_TYPE_A, NULL));
206   printf("%d\n", rc);
207   if (rc < 0)
208     return EXIT_FAILURE;
209
210   rc = test_foo(g_object_new(TEST_TYPE_B, NULL));
211   printf("%d\n", rc);
212   if (rc < 0)
213     return EXIT_FAILURE;
214
215   return 0;
216 }
217 ]])
218 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
219
220 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
221   test-a.o test-b.o test-b-mod.o test-fooable.o main.o])
222
223 AT_CHECK([./main], [0], [Test:A foo called
224 42
225 Test:B foo called
226 54
227 ])
228
229 AT_CLEANUP
230
231 dnl Test that a dynamic type can override the interface implementation of a
232 dnl dynamic parent type.
233 AT_SETUP([interface method override (dynamic) #2])
234 AT_KEYWORDS([runtime])dnl
235
236 TEST_FOOABLE_IFACE()
237 TEST_FOOABLE_IMPL_DYN([Test:A], [G:Object],
238   [puts("Test:A foo called"); return 42;])
239 TEST_FOOABLE_IMPL_DYN([Test:B], [Test:A],
240   [puts("Test:B foo called"); return 54;])
241
242 AT_DATA([main.c],
243 [[#include <config.h>
244 #include <stdio.h>
245 #include <stdlib.h>
246 #include "test-fooable.h"
247 #include "test-a.h"
248 #include "test-a-mod.h"
249 #include "test-b.h"
250 #include "test-b-mod.h"
251
252 int main(void)
253 {
254   int rc;
255
256   g_type_init();
257   g_type_module_use(g_object_new(TEST_TYPE_A_MOD, NULL));
258   g_type_module_use(g_object_new(TEST_TYPE_B_MOD, NULL));
259
260   rc = test_foo(g_object_new(TEST_TYPE_A, NULL));
261   printf("%d\n", rc);
262   if (rc < 0)
263     return EXIT_FAILURE;
264
265   rc = test_foo(g_object_new(TEST_TYPE_B, NULL));
266   printf("%d\n", rc);
267   if (rc < 0)
268     return EXIT_FAILURE;
269
270   return 0;
271 }
272 ]])
273 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
274
275 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
276   test-a.o test-a-mod.o test-b.o test-b-mod.o test-fooable.o main.o])
277
278 AT_CHECK([./main], [0], [Test:A foo called
279 42
280 Test:B foo called
281 54
282 ])
283
284 AT_CLEANUP
285
286 AT_SETUP([interface method chaining])
287 AT_KEYWORDS([runtime])dnl
288
289 TEST_FOOABLE_IFACE()
290 TEST_FOOABLE_IMPL([Test:A], [G:Object],
291   [puts("Test:A foo called"); return PARENT_HANDLER(go);])
292 TEST_FOOABLE_IMPL([Test:B], [Test:A],
293   [puts("Test:B foo called"); return PARENT_HANDLER(go);])
294 TEST_FOOABLE_IMPL([Test:C], [Test:B],
295   [puts("Test:C foo called"); return PARENT_HANDLER(go);])
296
297 AT_DATA([main.c],
298 [[#include <config.h>
299 #include <stdio.h>
300 #include <stdlib.h>
301 #include "test-fooable.h"
302 #include "test-a.h"
303 #include "test-b.h"
304 #include "test-c.h"
305
306 int main(void)
307 {
308   int rc;
309
310   g_type_init();
311   rc = test_foo(g_object_new(TEST_TYPE_C, NULL));
312   printf("%d\n", rc);
313   if (rc < 0)
314     return EXIT_FAILURE;
315
316   return 0;
317 }
318 ]])
319
320 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
321
322 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
323   test-a.o test-b.o test-c.o test-fooable.o main.o])
324 AT_CHECK([./main], [0], [Test:C foo called
325 Test:B foo called
326 Test:A foo called
327 88
328 ])
329
330 AT_CLEANUP
331
332 AT_SETUP([interface method chaining (dynamic)])
333 AT_KEYWORDS([runtime])dnl
334
335 TEST_FOOABLE_IFACE()
336 TEST_FOOABLE_IMPL_DYN([Test:A], [G:Object],
337   [puts("Test:A foo called"); return PARENT_HANDLER(go);])
338 TEST_FOOABLE_IMPL_DYN([Test:B], [Test:A],
339   [puts("Test:B foo called"); return PARENT_HANDLER(go);])
340 TEST_FOOABLE_IMPL_DYN([Test:C], [Test:B],
341   [puts("Test:C foo called"); return PARENT_HANDLER(go);])
342
343 AT_DATA([main.c],
344 [[#include <config.h>
345 #include <stdio.h>
346 #include <stdlib.h>
347 #include "test-fooable.h"
348 #include "test-a.h"
349 #include "test-a-mod.h"
350 #include "test-b.h"
351 #include "test-b-mod.h"
352 #include "test-c.h"
353 #include "test-c-mod.h"
354
355 int main(void)
356 {
357   int rc;
358
359   g_type_init();
360   g_type_module_use(g_object_new(TEST_TYPE_A_MOD, NULL));
361   g_type_module_use(g_object_new(TEST_TYPE_B_MOD, NULL));
362   g_type_module_use(g_object_new(TEST_TYPE_C_MOD, NULL));
363
364   rc = test_foo(g_object_new(TEST_TYPE_C, NULL));
365   printf("%d\n", rc);
366   if (rc < 0)
367     return EXIT_FAILURE;
368
369   return 0;
370 }
371 ]])
372
373 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
374
375 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
376   test-a.o test-a-mod.o test-b.o test-b-mod.o test-c.o test-c-mod.o \
377   test-fooable.o main.o])
378 AT_CHECK([./main], [0], [Test:C foo called
379 Test:B foo called
380 Test:A foo called
381 88
382 ])
383
384 AT_CLEANUP