]> git.draconx.ca Git - gob-dx.git/blob - tests/general.at
Improve str.gob testcase on non-GNU compilers.
[gob-dx.git] / tests / general.at
1 dnl Copyright © 2019-2022 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 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o 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
145 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main test.o main.o])
146
147 AT_CLEANUP
148
149 dnl Dynamic types: simple test case which checks that we can instantiate a
150 dnl dynamic type after registration.
151 AT_SETUP([dynamic type registration])
152 AT_KEYWORDS([dynamic runtime])
153
154 AT_DATA([test.gob], [[%ctop{
155 #include <config.h>
156 %}
157 %{
158 #include <stdio.h>
159 %}
160 class :Test from G:Object (dynamic)
161 {
162   public gchar *s = { g_strdup("(nil)") };
163   property STRING s (link);
164
165   public void test(self)
166   {
167     printf("%s\n", self->s);
168   }
169 }
170 ]])
171 AT_CHECK([gob2 test.gob])
172 TEST_COMPILE_GOBJECT([test.c], [0], [], [ignore])
173
174 TEST_TYPE_MODULE([:Test])
175
176 AT_DATA([main.c],
177 [[#include <stdlib.h>
178 #include "test.h"
179 #include "test-mod.h"
180
181 void devnull(const char *a, GLogLevelFlags b, const char *c, gpointer d) { }
182
183 int main(void)
184 {
185   guint handler;
186   Test *t;
187
188   g_type_init();
189
190   /* should fail, suppress internal glib logging...  */
191   handler = g_log_set_handler("GLib-GObject", G_LOG_LEVEL_MASK, devnull, 0);
192   t = g_object_new(test_get_type(), NULL);
193   if (t != NULL)
194     return EXIT_FAILURE;
195   g_log_remove_handler("GLib-GObject", handler);
196
197   /* Register dynamic type */
198   g_type_module_use(g_object_new(test_mod_get_type(), NULL));
199
200   /* should work now */
201   t = g_object_new(test_get_type(), "s", "Hello, World", (char *)NULL);
202   if (!t)
203     return EXIT_FAILURE;
204
205   test_test(t);
206   return EXIT_SUCCESS;
207 }
208 ]])
209 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
210
211 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
212   test.o test-mod.o main.o])
213 AT_CHECK([./main], [0], [Hello, World
214 ])
215
216 AT_CLEANUP
217
218 AT_SETUP([GOB2_CHECK min-version test])
219
220 AT_DATA([configure.ac],
221 [[AC_PREREQ([2.62])
222 AC_INIT([test], [0])
223 AC_OUTPUT
224 ]])
225 AT_CHECK([$AUTOCONF && test -f configure || exit 77], [0], [ignore], [ignore])
226
227 m4_define([MYVER],
228   m4_bpatsubst(m4_dquote(m4_defn([AT_PACKAGE_VERSION])), [[^][0-9.]]))
229
230 m4_define([MYVER_P1], m4_dquote(m4_reverse(m4_unquote(
231   m4_split(m4_defn([MYVER]), [[.]])))))
232 m4_define([MYVER_P1], m4_join([.], m4_reverse(
233   m4_eval(m4_car(MYVER_P1)+1), m4_shift(MYVER_P1))))
234
235 AT_DATA([test.in], [[@GOB2@
236 ]])
237
238 cat >configure.ac <<EOF
239 [m4@&t@_include([$builddir/gob2.m4])]
240 [m4@&t@_pattern_forbid([^GOB2_])]
241 [m4@&t@_pattern_forbid([^DX_])]
242 [AC_INIT([gob2_check], [0])]
243 [GOB2_CHECK(]m4_dquote(m4_defn([MYVER]))[)]
244 GOB2=\`command -v \$GOB2\`
245 [AC_CONFIG_FILES([test])]
246 [AC_OUTPUT]
247 EOF
248 AT_CHECK([$AUTOCONF --force])
249 AT_CHECK([./configure], [0], [ignore])
250
251 command -v gob2 >expout
252 AT_CHECK([cat test], [0], [expout], [ignore])
253
254 sed '/GOB2_CHECK/c\
255 [GOB2_CHECK(]m4_dquote(m4_defn([MYVER_P1]))[)]' configure.ac >configure.new
256 mv -f configure.new configure.ac
257 AT_CHECK([$AUTOCONF --force])
258 AT_CHECK([./configure], [1], [ignore], [ignore])
259
260 AT_CLEANUP
261
262 AT_SETUP([private data members])
263
264 AT_DATA([test.gob], [[%ctop{
265 #include <config.h>
266 %}
267 class :Test from G:Object
268 {
269   private int x = 42;
270
271   public int get_x(G:Object *go) { return TEST(go)->_priv->x; }
272   public void set_x(G:Object *go, int val) { TEST(go)->_priv->x = val; }
273 }
274 ]])
275 AT_CHECK([gob2 test.gob])
276 AT_CHECK([$HAVE_GOBJECT_PRIVATES || exit 77])
277 TEST_COMPILE_GOBJECT([test.c], [0], [], [ignore])
278
279 AT_DATA([main.c], [[#include <config.h>
280 #include <stdio.h>
281 #include "test.h"
282
283 int main(void)
284 {
285   GObject *go;
286
287   g_type_init();
288   go = g_object_new(test_get_type(), NULL);
289
290   printf("%d\n", test_get_x(go));
291   printf("%d\n", (test_set_x(go, 123), test_get_x(go)));
292
293   return 0;
294 }
295 ]])
296 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
297
298 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
299   test.o main.o])
300 AT_CHECK([./main], [0], [42
301 123
302 ])
303
304 AT_CLEANUP
305
306 AT_SETUP([private data members (dynamic)])
307
308 AT_DATA([test.gob], [[%ctop{
309 #include <config.h>
310 %}
311 class :Test from G:Object (dynamic)
312 {
313   private int x = 54;
314
315   public int get_x(G:Object *go) { return TEST(go)->_priv->x; }
316   public void set_x(G:Object *go, int val) { TEST(go)->_priv->x = val; }
317 }
318 ]])
319 AT_CHECK([gob2 test.gob])
320 AT_CHECK([$HAVE_GOBJECT_PRIVATES || exit 77])
321 TEST_COMPILE_GOBJECT([test.c], [0], [], [ignore])
322
323 TEST_TYPE_MODULE([:Test])
324
325 AT_DATA([main.c], [[#include <config.h>
326 #include <stdio.h>
327 #include "test.h"
328 #include "test-mod.h"
329
330 int main(void)
331 {
332   GObject *go;
333
334   g_type_init();
335   g_type_module_use(g_object_new(test_mod_get_type(), NULL));
336   go = g_object_new(test_get_type(), NULL);
337
338   printf("%d\n", test_get_x(go));
339   printf("%d\n", (test_set_x(go, 123), test_get_x(go)));
340
341   return 0;
342 }
343 ]])
344 TEST_COMPILE_GOBJECT([main.c], [0], [], [ignore])
345
346 AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main \
347   test.o test-mod.o main.o])
348 AT_CHECK([./main], [0], [54
349 123
350 ])
351
352 AT_CLEANUP