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