]> git.draconx.ca Git - dxcommon.git/blob - tests/scripts.at
Add script for generating miscellaneous string tables.
[dxcommon.git] / tests / scripts.at
1 AT_BANNER([Script tests])
2
3 AT_SETUP([gen-options.awk])
4
5 AT_DATA([options.def],
6 [[--option-only
7 --option-with-val (5)
8 --option-with-flagval (&x, 5)
9 --option-with-arg=ARG
10 some "text" goes here
11 --option-with-optional-arg[=OPTIONAL]
12
13 hello
14 -a, --option-with-sopt
15
16 -b, --option-with-sopt-and-arg=SOPTARG
17 -c, --option-with-sopt-and-optional-arg[=SOPTOPTIONAL]
18 --option-with-arg-and-val=ARGVAL (42)
19 --option-with-arg-and-flagval=ARGFLAGVAL (&a[1], 'x')
20 --option-with-optional-arg-and-val[=OPTIONALARGVAL] (54)
21 --option-with-optional-arg-and-flagval[=OPTIONALFLAGVAL] (0, 0)
22 --with-sopt
23 Here is a help string
24     that has a line randomly indented
25 # with a comment
26     @&t@
27 and a blank line
28 --with-arg=ARG
29 do stuff with ARG
30 --flagval
31 ]])
32
33 AT_CHECK([$AWK -f "$builddir/scripts/gen-options.awk" <options.def >options.h])
34
35 AT_DATA([context.h],
36 [[struct option { const char *name; int has_arg; int *flag; int val; };
37 int x, a[5];
38 ]])
39
40 # test 0: sanity test
41 AT_DATA([test0.c],
42 [[#include "context.h"
43 #include "options.h"
44
45 static const char sopts[] = SOPT_STRING;
46 static const struct option opts[] = { LOPTS_INITIALIZER, {0} };
47
48 int main(void)
49 {
50   return 0;
51 }
52 ]])
53 AT_CHECK([$CC -o test0$EXEEXT test0.c && ./test0$EXEEXT], [0], [], [ignore])
54
55 # test 1: long option names and help text
56 AT_DATA([test1.c],
57 [[#include <stdio.h>
58 #include <stdlib.h>
59
60 #include "context.h"
61 #include "options.h"
62
63 static const struct option opts[] = { LOPTS_INITIALIZER };
64
65 int main(void)
66 {
67   unsigned i;
68
69   for (i = 0; i < sizeof opts / sizeof opts[0]; i++) {
70     struct lopt_help help = { "INVALID", "INVALID" };
71
72     if (!lopt_get_help(&opts[i], &help))
73       return EXIT_FAILURE;
74
75     printf("--%s", opts[i].name);
76     if (opts[i].has_arg)
77       printf("=%s", help.arg);
78     printf("\n%s", help.desc);
79     if (help.desc[0])
80       putchar('\n');
81   }
82
83   return 0;
84 }
85 ]])
86
87 # pick out interesting bits from the definitions file
88 sed -n '/^-/s/^.*--\([[^\= @<:@]]*\).*$/\1/p' options.def >options
89 sed -n '/^-/{
90   s/[[^=]]*\(=[[^@:>@ ]]*\).*$/\1/
91   s/^[[^=]].*//
92   s/^=//
93   p
94 }' options.def >argnames
95
96 AS_ECHO(["-"]) | sed -n '1s/^-.*//p
97 1,/^-/d
98 t clear
99 :clear
100 s/^-.*//p
101 t
102 s/^#.*//
103 s/^ *//
104 t next
105 :next
106 N
107 s/\n-.*//
108 t done
109 s/\n#.*//
110 s/\n */\n/g
111 t next
112 :done
113 s/"/\\\\"/g
114 s/[[^\n]][[^\n]]*/\\"&\\"/g
115 s/^\n*//
116 s/\n*$//
117 s/\n\n*/ /g
118 p
119 ' options.def - >helptext
120
121 exec 3<options 4<argnames 5<helptext 6>expout
122 while read opt <&3 && read arg <&4 && read help <&5; do
123   if test ${arg:+y}; then
124     AS_ECHO(["--$opt=$arg"]) >&6
125   else
126     AS_ECHO(["--$opt"]) >&6
127   fi
128   eval "set x $help"; shift
129   for arg
130   do
131     AS_ECHO(["$arg"]) >&6
132   done
133 done
134 exec 3<&- 4<&- 5<&- 6>&-
135
136 AT_CHECK([$CC -o test1$EXEEXT test1.c && ./test1$EXEEXT],
137   [0], [expout], [ignore])
138
139 # test 2: short option string
140 AT_DATA([test2.c],
141 [[#include <stdio.h>
142 #include <stdlib.h>
143
144 #include "context.h"
145 #include "options.h"
146
147 int main(void)
148 {
149   struct option lopts[] = {LOPTS_INITIALIZER};
150   unsigned i, j;
151
152   for (i = 0; i < sizeof SOPT_STRING - 1; i++) {
153     if (SOPT_STRING[i] != ':') {
154       for (j = 0; j < sizeof lopts / sizeof lopts[0]; j++) {
155         if (lopts[j].val == SOPT_STRING[i]) {
156           printf("--%s ", lopts[j].name);
157           break;
158         }
159       }
160     }
161     putchar(SOPT_STRING[i]);
162     if (SOPT_STRING[i+1] != ':')
163       putchar('\n');
164   }
165 }
166 ]])
167
168 sed -n '/^-/{
169   s/=.*/:/
170   s/[[@<:@]]/:/
171   s/^-\([[^-]]\)[[^:]]*/\1/p
172   s/^-.*//p
173 }' options.def >sopts
174
175 exec 3<options 4<sopts 5>expout
176 while read lopt <&3 && read sopt <&4; do
177   if test ${sopt:+y}; then
178     AS_ECHO(["--$lopt $sopt"]) >&5
179   fi
180 done
181 exec 3<&- 4<&- 5>&-
182
183 AT_CHECK([$CC -o test2$EXEEXT test2.c && ./test2$EXEEXT],
184   [0], [expout], [ignore])
185
186 # Check that all help strings are translatable
187 sed 's/\([[^\\]]\\\)" /\1\\n\\" /g' helptext >help-po
188 exec 3<options 4<argnames 5<help-po 6>expected.po
189 while read opt <&3 && read arg <&4 && read help <&5; do
190   if test ${arg:+y}; then
191     AS_ECHO(["msgctxt \"$opt\" msgid \"$arg\""]) >&6
192   fi
193   AS_ECHO(["msgctxt \"$opt\" msgid${help:+ }$help"]) >&6
194 done
195 exec 3<&- 4<&- 5<&- 6>&-
196
197 AT_CHECK([xgettext --keyword=PN_:1c,2 options.h
198   test -f messages.po || exit 77])
199
200 LC_ALL=C sort expected.po >expout
201 AT_CHECK([sed -n '/^msgctxt/{
202 t next
203 :next
204 N
205 s/\nmsgstr.*//
206 t done
207 s/\s*""//
208 s/\n/ /
209 t next
210 :done
211 p
212 }' messages.po | LC_ALL=C sort], [0], [expout])
213
214 AT_CLEANUP
215
216 AT_SETUP([gen-strtab.awk])
217
218 AT_DATA([test.def],
219 [[
220 &a world
221 &b
222 hello world
223 &c
224 hello
225 world
226 &d world\n
227 &e
228 \\not a newline
229 &f
230 \not a newline
231 &g inline
232 continued
233 &h    no\
234 newline\
235 &i
236 \   leading whitespace
237 &j oneline
238 # with a comment
239 ]])
240
241 AT_CHECK([$AWK -f "$builddir/scripts/gen-strtab.awk" <test.def >test.h])
242
243 sed -n 's/^[[&]]\([[^ ]]*\).*/\1/p' test.def >identifiers
244
245 # test 0: sanity test
246 AT_DATA([test0.c],
247 [[#include "test.h"
248 #include <stdio.h>
249
250 int main(void)
251 {
252   printf("---\n");
253 ]])
254 exec 3<identifiers 4>>test0.c
255 while read ident <&3; do
256   AS_ECHO(['  printf("%s\n---\n", '"strtab+$ident);"]) >&4
257 done
258 AS_ECHO(['  return 0;']) >&4
259 AS_ECHO(['}']) >&4
260 exec 3<&- 4>&-
261
262 AT_CHECK([$CC -o test0$EXEEXT test0.c && ./test0$EXEEXT], [0], [---
263 world
264 ---
265 hello world
266
267 ---
268 hello
269 world
270
271 ---
272 world
273
274 ---
275 \not a newline
276
277 ---
278
279 ot a newline
280
281 ---
282 inline
283 continued
284
285 ---
286 nonewline
287 ---
288    leading whitespace
289
290 ---
291 oneline
292 ---
293 ], [ignore])
294
295 AT_CLEANUP