Keyword arguments work.
[pspp] / tests / language / control / define.at
1 dnl PSPP - a program for statistical analysis.
2 dnl Copyright (C) 2017 Free Software Foundation, Inc.
3 dnl
4 dnl This program is free software: you can redistribute it and/or modify
5 dnl it under the terms of the GNU General Public License as published by
6 dnl the Free Software Foundation, either version 3 of the License, or
7 dnl (at your option) any later version.
8 dnl
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 dnl GNU General Public License for more details.
13 dnl
14 dnl You should have received a copy of the GNU General Public License
15 dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 dnl
17 AT_BANNER([DEFINE])
18
19 m4_define([PSPP_CHECK_MACRO_EXPANSION],
20   [AT_SETUP([macro expansion - $1])
21    AT_DATA([define.sps], [$2
22 DEBUG EXPAND.
23 $3
24 ])
25    AT_DATA([expout], [$4
26 ])
27    AT_CHECK([pspp --testing-mode define.sps | sed '/^$/d'], [$6
28 ], [expout])
29    AT_CLEANUP])
30
31 AT_SETUP([simple macro expansion])
32 AT_DATA([define.sps], [dnl
33 DEFINE !macro()
34 a b c d
35 e f g h.
36 i j k l
37 1,2,3,4.
38 5+6+7.
39 m(n,o).
40 "a" "b" "c" 'a' 'b' 'c'.
41 "x "" y".
42 !ENDDEFINE.
43 DEBUG EXPAND.
44 !macro
45 ])
46 AT_CHECK([pspp --testing-mode define.sps], [0], [dnl
47 a b c d e f g h.
48 i j k l 1, 2, 3, 4.
49 5 + 6 + 7.
50 m(n, o).
51 "a" "b" "c" 'a' 'b' 'c'.
52 "x "" y".
53 ])
54 AT_CLEANUP
55 \f
56 PSPP_CHECK_MACRO_EXPANSION([one !TOKENS(1) positional argument],
57   [DEFINE !t1(!positional !tokens(1)) t1 (!1) !ENDDEFINE.],
58   [!t1 a.
59 !t1 b.
60 !t1 a b.],
61   [t1(a)
62 t1(b)
63 t1(a)
64 note: unexpanded token "b"])
65 \f
66 AT_SETUP([macro expansion with positional arguments])
67 AT_DATA([define.sps], [dnl
68 DEFINE !title(!positional !tokens(1)) !1 !ENDDEFINE.
69 DEFINE !t1(!positional !tokens(1)) t1 (!1) !ENDDEFINE.
70 DEFINE !t2(!positional !tokens(2)) t2 (!1) !ENDDEFINE.
71
72 DEFINE !ce(!positional !charend('/')) ce (!1) !ENDDEFINE.
73 DEFINE !ce2(!positional !charend('(')
74            /!positional !charend(')'))
75 ce2 (!1, !2)
76 !ENDDEFINE.
77
78 DEFINE !e(!positional !enclose('{','}')) e (!1) !ENDDEFINE.
79
80 DEFINE !cmd(!positional !cmdend) cmd(!1) !ENDDEFINE.
81 DEFINE !cmd2(!positional !cmdend
82             /!positional !tokens(1))
83 cmd2(!1, !2)
84 !ENDDEFINE.
85
86 DEFINE !p(!positional !tokens(1)
87          /!positional !tokens(1)
88          /!positional !tokens(1))
89 p(!1, !2, !3)
90 !ENDDEFINE.
91
92 DEBUG EXPAND.
93 !title "!TOKENS(1) argument."
94 !t1 a.
95 !t1 b.
96 !t1 a b.
97
98 !title "!TOKENS(2) argument."
99 !t2 a b.
100 !t2 b c d.
101
102 !title "!CHAREND argument."
103 !ce/.
104 !ce x/.
105 !ce x y/.
106 !ce x y z/.
107
108 !title "Two !CHAREND arguments."
109 !ce2 x(y).
110 !ce2 1 2 3 4().
111
112 !title "!ENCLOSE argument."
113 !e {}.
114 !e {a}.
115 !e {a b}.
116
117 !title "!CMDEND argument."
118 !cmd 1 2 3 4.
119 !cmd2 5 6.
120 7.
121
122 !title "Three !TOKENS(1) arguments."
123 !p a b c.
124 !p 1 -2 -3.
125 ])
126 AT_CHECK([pspp --testing-mode define.sps], [0], [dnl
127 "!TOKENS(1) argument."
128
129 t1(a)
130
131 t1(b)
132
133 t1(a)
134
135 note: unexpanded token "b"
136
137 "!TOKENS(2) argument."
138
139 t2(a b)
140
141 t2(b c)
142
143 note: unexpanded token "d"
144
145 "!CHAREND argument."
146
147 ce( )
148
149 ce(x)
150
151 ce(x y)
152
153 ce(x y z)
154
155 "Two !CHAREND arguments."
156
157 ce2(x, y)
158
159 ce2(1 2 3 4, )
160
161 "!ENCLOSE argument."
162
163 e( )
164
165 e(a)
166
167 e(a b)
168
169 "!CMDEND argument."
170
171 cmd(1 2 3 4)
172
173 cmd2(5 6, 7)
174
175 "Three !TOKENS(1) arguments."
176
177 p(a, b, c)
178
179 p(1, -2, -3)
180 ])
181 AT_CLEANUP
182 \f
183 AT_SETUP([macro expansion with positional arguments - negative])
184 AT_DATA([define.sps], [dnl
185 DEFINE !title(!positional !tokens(1)) !1 !ENDDEFINE.
186 DEFINE !p(!positional !tokens(1)
187          /!positional !tokens(1)
188          /!positional !tokens(1))
189 (!1, !2, !3)
190 !ENDDEFINE.
191
192 DEFINE !ce(!positional !charend('/')) ce(!1) !ENDDEFINE.
193
194 DEFINE !enc1(!positional !enclose('{', '}')) enc1(!1) !ENDDEFINE.
195 DEBUG EXPAND.
196 !title "Too few tokens for !TOKENS."
197 !p a b.
198 !p a.
199 !p.
200
201 !title "Missing charend delimiter."
202 !ce a b c.
203
204 !title "Missing start delimiter."
205 !enc1 a b c.
206
207 !title "Missing end delimiter."
208 !enc1{a b c.
209 ])
210 AT_CHECK([pspp --testing-mode define.sps], [1], [dnl
211 "Too few tokens for !TOKENS."
212
213 define.sps:13: error: DEBUG EXPAND: Unexpected end of command reading
214 argument !3 to macro !p.
215
216 note: unexpanded token "!p"
217
218 note: unexpanded token "a"
219
220 note: unexpanded token "b"
221
222 define.sps:14: error: DEBUG EXPAND: Unexpected end of command reading
223 argument !2 to macro !p.
224
225 note: unexpanded token "!p"
226
227 note: unexpanded token "a"
228
229 define.sps:15: error: DEBUG EXPAND: Unexpected end of command reading
230 argument !1 to macro !p.
231
232 note: unexpanded token "!p"
233
234 "Missing charend delimiter."
235
236 define.sps:18: error: DEBUG EXPAND: Unexpected end of command reading
237 argument !1 to macro !ce.
238
239 note: unexpanded token "!ce"
240
241 note: unexpanded token "a"
242
243 note: unexpanded token "b"
244
245 note: unexpanded token "c"
246
247 "Missing start delimiter."
248
249 define.sps:21: error: DEBUG EXPAND: Found `a' while expecting `{' reading
250 argument !1 to macro !enc1.
251
252 note: unexpanded token "!enc1"
253
254 note: unexpanded token "a"
255
256 note: unexpanded token "b"
257
258 note: unexpanded token "c"
259
260 "Missing end delimiter."
261
262 define.sps:24: error: DEBUG EXPAND: Unexpected end of command reading
263 argument !1 to macro !enc1.
264
265 note: unexpanded token "!enc1"
266
267 note: unexpanded token "{"
268
269 note: unexpanded token "a"
270
271 note: unexpanded token "b"
272
273 note: unexpanded token "c"
274 ])
275 AT_CLEANUP
276 \f
277 PSPP_CHECK_MACRO_EXPANSION([one !TOKENS(1) keyword argument],
278   [DEFINE !k(arg1 = !TOKENS(1)) k(!arg1) !ENDDEFINE.],
279   [!k arg1=x.
280 !k arg1=x y.
281 !k.],
282   [k(x)
283 k(x)
284 note: unexpanded token "y"
285 k( )])