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