]> git.draconx.ca Git - gob-dx.git/blob - tests/general.at
Replace gnulib patch with new common helper macro.
[gob-dx.git] / tests / general.at
1 dnl Copyright © 2019-2023 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 AT_SETUP([test.gob compilation])
7
8 AT_CHECK([gob2 "$abs_top_srcdir/t/test.gob"])
9 AT_CHECK([$HAVE_GTK2 || exit 77])
10 TEST_COMPILE_GOBJECT([$GTK_CFLAGS test-object.c], [0], [], [ignore])
11
12 AT_CLEANUP
13
14 AT_SETUP([test.gob C++ compilation])
15
16 AT_CHECK([gob2 --for-cpp "$abs_top_srcdir/t/test.gob"])
17 AT_CHECK([$HAVE_GTK2 || exit 77])
18 TEST_COMPILEXX_GOBJECT([$GTK_CFLAGS test-object.cc], [0], [], [ignore])
19
20 AT_CLEANUP
21
22 AT_SETUP([str.gob])
23
24 # check if compiler supports format warnings
25 AT_DATA([fmt.c],
26 [[#include <config.h>
27 #include <glib.h>
28
29 void foo(const char *, ...) G_GNUC_PRINTF(1, 2);
30 void bar(void) {
31 #line 99 "VERIFY"
32 foo("%s", 0);
33 }
34 ]])
35 AT_DATA([fmt.awk],
36 [[BEGIN { pass=0; FS=":"; }
37 /%s/ && $1 == "VERIFY" && $2 == 99 { pass=1; }
38 END { exit(!pass); }
39 ]])
40
41 fmt_warnings=false
42 set x $CPPFLAGS $CFLAGS $LIBGOBJECT_CFLAGS; shift
43 AS_IF([$CC "$@" -c fmt.c 1>/dev/null 2>out && $AWK -f fmt.awk out],
44   [fmt_warnings=:],
45   [AS_IF([$CC "$@" -Wformat -c fmt.c 1>/dev/null 2>out && $AWK -f fmt.awk out],
46     [CFLAGS="$CFLAGS -Wformat" fmt_warnings=:])])
47
48 AT_DATA([main.c],
49 [[#include <config.h>
50 #include "str.h"
51
52 int main(void)
53 {
54   Str *test_good, *test_bad;
55   char *stupid_pointer = "ug";
56   int the_answer = 42;
57
58   g_type_init ();
59
60   /* This works fine. */
61   test_good = (Str *) (str_new ("%d", the_answer));
62   test_good = test_good;
63
64   /* This gets a warning thanks to our function attribute. */
65   test_bad = (Str *) (str_new ("%d", stupid_pointer));
66   test_bad = test_bad;
67
68   return 0;
69 }
70 ]])
71
72 str_gob=$abs_top_srcdir/t/str.gob
73 AT_CHECK([gob2 "$str_gob"])
74 AT_CHECK([$HAVE_GOBJECT_PRIVATES || exit 77])
75
76 TEST_COMPILE_GOBJECT([str.c], [0], [], [stderr])
77 mv stderr str_stderr
78 TEST_COMPILE_GOBJECT([main.c], [0], [], [stderr])
79 mv stderr main_stderr
80 TEST_LINK_GOBJECT([main], [str.o main.o])
81
82 AT_DATA([str.awk],
83 [[/want a string/ { lines[NR] = 1; }
84 END {
85   FS=":";
86   while ((rc = getline < "str_stderr") > 0) {
87     sub(/.*[\/]/, "", $1);
88     if (/%s/ && $1 == "str.gob" && $2 in lines)
89       lines[$2]--;
90   }
91
92   if (rc < 0)
93     exit(1);
94
95   count=0;
96   for (l in lines) {
97     if (lines[l])
98       exit(1);
99     count++;
100   }
101
102   exit(count != 2);
103 }
104 ]])
105
106 # Check for correct diagnostic messages on the target lines...
107 AT_CHECK([$fmt_warnings || exit 77
108 $AWK -f str.awk "$str_gob" || exit 1
109 $AWK -f - main_stderr <<'EOF'
110 BEGIN { pass=0; FS=":"; }
111 /%d/ && $1 == "main.c" && $2 == 17 { pass=1; }
112 END { exit(!pass); }
113 EOF])
114
115 AT_CLEANUP
116
117 dnl Check that dynamic types are accepted and compile OK...
118 AT_SETUP([dynamic types])
119 AT_KEYWORDS([dynamic])
120
121 AT_DATA([test.gob], [[%ctop{
122 #include <config.h>
123 %}
124 class :Test from G:Object (dynamic)
125 {
126   public void test(void)
127   {
128   }
129 }
130 ]])
131 AT_CHECK([gob2 test.gob])
132 TEST_COMPILE_GOBJECT([test.c], [0], [], [ignore])
133
134 AT_DATA([main.c],
135 [[#include <config.h>
136 #include "test.h"
137 int main(void)
138 {
139   g_type_init();
140   test_register_type(NULL);
141 }
142 ]])
143 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
144 TEST_LINK_GOBJECT([main], [test.o main.o])
145
146 AT_CLEANUP
147
148 dnl Dynamic types: simple test case which checks that we can instantiate a
149 dnl dynamic type after registration.
150 AT_SETUP([dynamic type registration])
151 AT_KEYWORDS([dynamic runtime])
152
153 AT_DATA([test.gob], [[%ctop{
154 #include <config.h>
155 %}
156 %{
157 #include <stdio.h>
158 %}
159 class :Test from G:Object (dynamic)
160 {
161   public gchar *s = { g_strdup("(nil)") };
162   property STRING s (link);
163
164   public void test(self)
165   {
166     printf("%s\n", self->s);
167   }
168 }
169 ]])
170 AT_CHECK([gob2 test.gob])
171 TEST_COMPILE_GOBJECT([test.c], [0], [], [ignore])
172
173 TEST_TYPE_MODULE([:Test])
174
175 AT_DATA([main.c],
176 [[#include <stdlib.h>
177 #include "test.h"
178 #include "test-mod.h"
179
180 void devnull(const char *a, GLogLevelFlags b, const char *c, gpointer d) { }
181
182 int main(void)
183 {
184   guint handler;
185   Test *t;
186
187   g_type_init();
188
189   /* should fail, suppress internal glib logging...  */
190   handler = g_log_set_handler("GLib-GObject", G_LOG_LEVEL_MASK, devnull, 0);
191   t = g_object_new(test_get_type(), NULL);
192   if (t != NULL)
193     return EXIT_FAILURE;
194   g_log_remove_handler("GLib-GObject", handler);
195
196   /* Register dynamic type */
197   g_type_module_use(g_object_new(test_mod_get_type(), NULL));
198
199   /* should work now */
200   t = g_object_new(test_get_type(), "s", "Hello, World", (char *)NULL);
201   if (!t)
202     return EXIT_FAILURE;
203
204   test_test(t);
205   return EXIT_SUCCESS;
206 }
207 ]])
208 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
209 TEST_LINK_GOBJECT([main], [test.o test-mod.o main.o])
210 AT_CHECK([./main], [0], [Hello, World
211 ])
212
213 AT_CLEANUP
214
215 AT_SETUP([GOB2_CHECK min-version test])
216
217 AT_DATA([configure.ac],
218 [[AC_PREREQ([2.62])
219 AC_INIT([test], [0])
220 AC_OUTPUT
221 ]])
222 AT_CHECK([$AUTOCONF && test -f configure || exit 77], [0], [ignore], [ignore])
223
224 m4_define([MYVER],
225   m4_bpatsubst(m4_dquote(m4_defn([AT_PACKAGE_VERSION])), [[^][0-9.]]))
226
227 m4_define([MYVER_P1], m4_dquote(m4_reverse(m4_unquote(
228   m4_split(m4_defn([MYVER]), [[.]])))))
229 m4_define([MYVER_P1], m4_join([.], m4_reverse(
230   m4_eval(m4_car(MYVER_P1)+1), m4_shift(MYVER_P1))))
231
232 AT_DATA([test.in], [[@GOB2@
233 ]])
234
235 cat >configure.ac <<EOF
236 [m4@&t@_include([$builddir/gob2.m4])]
237 [m4@&t@_pattern_forbid([^GOB2_])]
238 [m4@&t@_pattern_forbid([^DX_])]
239 [AC_INIT([gob2_check], [0])]
240 [GOB2_CHECK(]m4_dquote(m4_defn([MYVER]))[)]
241 GOB2=\`command -v \$GOB2\`
242 [AC_CONFIG_FILES([test])]
243 [AC_OUTPUT]
244 EOF
245 AT_CHECK([$AUTOCONF --force])
246 AT_CHECK([./configure], [0], [ignore])
247
248 command -v gob2 >expout
249 AT_CHECK([cat test], [0], [expout], [ignore])
250
251 sed '/GOB2_CHECK/c\
252 [GOB2_CHECK(]m4_dquote(m4_defn([MYVER_P1]))[)]' configure.ac >configure.new
253 mv -f configure.new configure.ac
254 AT_CHECK([$AUTOCONF --force])
255 AT_CHECK([./configure], [1], [ignore], [ignore])
256
257 AT_CLEANUP
258
259 AT_SETUP([private data members])
260
261 AT_DATA([test.gob], [[%ctop{
262 #include <config.h>
263 %}
264 class :Test from G:Object
265 {
266   private int x = 42;
267
268   public int get_x(G:Object *go) { return TEST(go)->_priv->x; }
269   public void set_x(G:Object *go, int val) { TEST(go)->_priv->x = val; }
270 }
271 ]])
272 AT_CHECK([gob2 test.gob])
273 AT_CHECK([$HAVE_GOBJECT_PRIVATES || exit 77])
274 TEST_COMPILE_GOBJECT([test.c], [0], [], [ignore])
275
276 AT_DATA([main.c], [[#include <config.h>
277 #include <stdio.h>
278 #include "test.h"
279
280 int main(void)
281 {
282   GObject *go;
283
284   g_type_init();
285   go = g_object_new(test_get_type(), NULL);
286
287   printf("%d\n", test_get_x(go));
288   printf("%d\n", (test_set_x(go, 123), test_get_x(go)));
289
290   return 0;
291 }
292 ]])
293 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
294 TEST_LINK_GOBJECT([main], [test.o main.o])
295 AT_CHECK([./main], [0], [42
296 123
297 ])
298
299 AT_CLEANUP
300
301 AT_SETUP([private data members (dynamic)])
302
303 AT_DATA([test.gob], [[%ctop{
304 #include <config.h>
305 %}
306 class :Test from G:Object (dynamic)
307 {
308   private int x = 54;
309
310   public int get_x(G:Object *go) { return TEST(go)->_priv->x; }
311   public void set_x(G:Object *go, int val) { TEST(go)->_priv->x = val; }
312 }
313 ]])
314 AT_CHECK([gob2 test.gob])
315 AT_CHECK([$HAVE_GOBJECT_PRIVATES || exit 77])
316 TEST_COMPILE_GOBJECT([test.c], [0], [], [ignore])
317
318 TEST_TYPE_MODULE([:Test])
319
320 AT_DATA([main.c], [[#include <config.h>
321 #include <stdio.h>
322 #include "test.h"
323 #include "test-mod.h"
324
325 int main(void)
326 {
327   GObject *go;
328
329   g_type_init();
330   g_type_module_use(g_object_new(test_mod_get_type(), NULL));
331   go = g_object_new(test_get_type(), NULL);
332
333   printf("%d\n", test_get_x(go));
334   printf("%d\n", (test_set_x(go, 123), test_get_x(go)));
335
336   return 0;
337 }
338 ]])
339 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
340 TEST_LINK_GOBJECT([main], [test.o test-mod.o main.o])
341 AT_CHECK([./main], [0], [54
342 123
343 ])
344
345 AT_CLEANUP