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