chiark / gitweb /
Remove some obsolete code
[disorder] / lib / t-macros.c
CommitLineData
1dcdf455
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2008 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20#include "test.h"
21#include "macros.h"
22
23static void test_macros(void) {
24 const struct mx_node *m;
25#define L1 "this is just some\n"
26#define L2 "plain text\n"
27 static const char plain[] = L1 L2;
b36be3a1 28 char *s;
dd0f422a 29 const char *cs;
1dcdf455 30
b36be3a1
RK
31 /* Plain text ------------------------------------------------------------- */
32
1dcdf455
RK
33 /* As simple as it gets */
34 m = mx_parse("plaintext1", 1, "", NULL);
35 insist(m == 0);
36
37 /* Almost as simple as that */
38 m = mx_parse("plaintext1", 1, plain, NULL);
915ea29a 39 check_integer(m->type, MX_TEXT);
1dcdf455 40 check_string(m->filename, "plaintext1");
915ea29a 41 check_integer(m->line, 1);
1dcdf455
RK
42 check_string(m->text, L1 L2);
43 insist(m->next == 0);
9dbb630e 44 check_string(mx_dump(m), plain);
1dcdf455
RK
45
46 /* Check that partial parses stop in the right place */
47 m = mx_parse("plaintext2", 5, plain, plain + strlen(L1));
915ea29a 48 check_integer(m->type, MX_TEXT);
1dcdf455 49 check_string(m->filename, "plaintext2");
915ea29a 50 check_integer(m->line, 5);
1dcdf455
RK
51 check_string(m->text, L1);
52 insist(m->next == 0);
9dbb630e 53 check_string(mx_dump(m), L1);
1dcdf455 54
b36be3a1
RK
55 /* Simple macro parsing --------------------------------------------------- */
56
1dcdf455
RK
57 /* The simplest possible expansion */
58 m = mx_parse("macro1", 1, "@macro@", NULL);
915ea29a 59 check_integer(m->type, MX_EXPANSION);
1dcdf455 60 check_string(m->filename, "macro1");
915ea29a 61 check_integer(m->line, 1);
1dcdf455 62 check_string(m->name, "macro");
915ea29a 63 check_integer(m->nargs, 0);
1dcdf455
RK
64 insist(m->args == 0);
65 insist(m->next == 0);
9dbb630e 66 check_string(mx_dump(m), "@macro@");
1dcdf455
RK
67
68 /* Spacing variants of the above */
69 m = mx_parse("macro2", 1, "@ macro@", NULL);
915ea29a 70 check_integer(m->type, MX_EXPANSION);
1dcdf455 71 check_string(m->filename, "macro2");
915ea29a 72 check_integer(m->line, 1);
1dcdf455 73 check_string(m->name, "macro");
915ea29a 74 check_integer(m->nargs, 0);
1dcdf455
RK
75 insist(m->args == 0);
76 insist(m->next == 0);
9dbb630e 77 check_string(mx_dump(m), "@macro@");
1dcdf455 78 m = mx_parse("macro3", 1, "@macro @", NULL);
915ea29a 79 check_integer(m->type, MX_EXPANSION);
1dcdf455 80 check_string(m->filename, "macro3");
915ea29a 81 check_integer(m->line, 1);
1dcdf455 82 check_string(m->name, "macro");
915ea29a 83 check_integer(m->nargs, 0);
1dcdf455
RK
84 insist(m->args == 0);
85 insist(m->next == 0);
9dbb630e 86 check_string(mx_dump(m), "@macro@");
1dcdf455
RK
87
88 /* Unterminated variants */
89 m = mx_parse("macro4", 1, "@macro", NULL);
915ea29a 90 check_integer(m->type, MX_EXPANSION);
1dcdf455 91 check_string(m->filename, "macro4");
915ea29a 92 check_integer(m->line, 1);
1dcdf455 93 check_string(m->name, "macro");
915ea29a 94 check_integer(m->nargs, 0);
1dcdf455
RK
95 insist(m->args == 0);
96 insist(m->next == 0);
9dbb630e 97 check_string(mx_dump(m), "@macro@");
1dcdf455 98 m = mx_parse("macro5", 1, "@macro ", NULL);
915ea29a 99 check_integer(m->type, MX_EXPANSION);
1dcdf455 100 check_string(m->filename, "macro5");
915ea29a 101 check_integer(m->line, 1);
1dcdf455 102 check_string(m->name, "macro");
915ea29a 103 check_integer(m->nargs, 0);
1dcdf455
RK
104 insist(m->args == 0);
105 insist(m->next == 0);
9dbb630e 106 check_string(mx_dump(m), "@macro@");
1dcdf455
RK
107
108 /* Macros with a :-separated argument */
109 m = mx_parse("macro5", 1, "@macro:arg@", NULL);
915ea29a 110 check_integer(m->type, MX_EXPANSION);
1dcdf455 111 check_string(m->filename, "macro5");
915ea29a 112 check_integer(m->line, 1);
1dcdf455 113 check_string(m->name, "macro");
915ea29a 114 check_integer(m->nargs, 1);
1dcdf455
RK
115 insist(m->next == 0);
116
915ea29a 117 check_integer(m->args[0]->type, MX_TEXT);
1dcdf455 118 check_string(m->args[0]->filename, "macro5");
915ea29a 119 check_integer(m->args[0]->line, 1);
1dcdf455
RK
120 check_string(m->args[0]->text, "arg");
121 insist(m->args[0]->next == 0);
122
9dbb630e
RK
123 check_string(mx_dump(m), "@macro{arg}@");
124
1dcdf455
RK
125 /* Multiple :-separated arguments, and spacing, and newlines */
126 m = mx_parse("macro6", 1, "@macro : \n arg1 : \n arg2@", NULL);
915ea29a 127 check_integer(m->type, MX_EXPANSION);
1dcdf455 128 check_string(m->filename, "macro6");
915ea29a 129 check_integer(m->line, 1);
1dcdf455 130 check_string(m->name, "macro");
915ea29a 131 check_integer(m->nargs, 2);
1dcdf455
RK
132 insist(m->next == 0);
133
915ea29a 134 check_integer(m->args[0]->type, MX_TEXT);
1dcdf455 135 check_string(m->args[0]->filename, "macro6");
915ea29a 136 check_integer(m->args[0]->line, 2);
1dcdf455
RK
137 check_string(m->args[0]->text, "arg1");
138 insist(m->args[0]->next == 0);
139
915ea29a 140 check_integer(m->args[1]->type, MX_TEXT);
1dcdf455 141 check_string(m->args[1]->filename, "macro6");
915ea29a 142 check_integer(m->args[1]->line, 3);
1dcdf455
RK
143 check_string(m->args[1]->text, "arg2");
144 insist(m->args[1]->next == 0);
145
9dbb630e
RK
146 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
147
148 /* Multiple bracketed arguments */
915ea29a
RK
149 m = mx_parse("macro7", 1, "@macro{arg1}{arg2}@", NULL);
150 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
151
774eb34c 152 m = mx_parse("macro8", 1, "@macro{\narg1}{\narg2}@", NULL);
915ea29a
RK
153 check_string(mx_dump(m), "@macro{\narg1}{\narg2}@");
154 check_integer(m->args[0]->line, 1);
155 check_integer(m->args[1]->line, 2);
156 /* ...yes, lines 1 and 2: the first character of the first arg is
774eb34c 157 * the \n at the end of line 1. Compare with macro9: */
1dcdf455 158
774eb34c 159 m = mx_parse("macro9", 1, "@macro\n{arg1}\n{arg2}@", NULL);
915ea29a
RK
160 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
161 check_integer(m->args[0]->line, 2);
162 check_integer(m->args[1]->line, 3);
774eb34c
RK
163
164 /* Arguments that themselves contain expansions */
165 m = mx_parse("macro10", 1, "@macro{@macro2{arg1}{arg2}@}@", NULL);
166 check_string(mx_dump(m), "@macro{@macro2{arg1}{arg2}@}@");
167
168 /* ...and with omitted trailing @ */
169 m = mx_parse("macro11", 1, "@macro{@macro2{arg1}{arg2}}", NULL);
170 check_string(mx_dump(m), "@macro{@macro2{arg1}{arg2}@}@");
49472b7d
RK
171
172 /* Similarly but with more whitespace; NB that the whitespace is
173 * preserved. */
174 m = mx_parse("macro12", 1, "@macro {@macro2 {arg1} {arg2} }\n", NULL);
175 check_string(mx_dump(m), "@macro{@macro2{arg1}{arg2}@ }@\n");
b36be3a1
RK
176
177 /* Simple expansions ------------------------------------------------------ */
178
179 mx_register_builtin();
dd0f422a
RK
180 mx_search_path(".");
181 mx_search_path("lib");
182 if((cs = getenv("srcdir")))
183 mx_search_path(cs);
b36be3a1 184
dd0f422a 185#define check_macro(NAME, INPUT, OUTPUT, RET) do { \
b36be3a1 186 m = mx_parse(NAME, 1, INPUT, NULL); \
dd0f422a 187 check_integer(mx_expandstr(m, &s, 0/*u*/, NAME), (RET)); \
b36be3a1
RK
188 if(s && strcmp(s, OUTPUT)) { \
189 fprintf(stderr, "%s:%d: test %s\n" \
c617bac5 190 " INPUT:\n%s\n" \
b36be3a1
RK
191 " EXPECTED: '%s'\n" \
192 " GOT: '%s'\n", \
193 __FILE__, __LINE__, NAME, INPUT, OUTPUT, s); \
194 count_error(); \
195 } \
196} while(0)
197
dd0f422a
RK
198 check_macro("empty", "", "", 0);
199 check_macro("plain", plain, plain, 0);
200
201 check_macro("if1", "@if{true}{yes}{no}", "yes", 0);
202 check_macro("if2", "@if{true}{yes}", "yes", 0);
203 check_macro("if3", "@if{false}{yes}{no}", "no", 0);
204 check_macro("if4", "@if{false}{yes}", "", 0);
205 check_macro("if5", "@if{ true}{yes}", "", 0);
206
207 check_macro("and1", "@and", "true", 0);
208 check_macro("and2", "@and{true}", "true", 0);
209 check_macro("and3", "@and{false}", "false", 0);
210 check_macro("and4", "@and{true}{true}", "true", 0);
211 check_macro("and5", "@and{false}{true}", "false", 0);
212 check_macro("and6", "@and{true}{false}", "false", 0);
213 check_macro("and7", "@and{false}{false}", "false", 0);
214
215 check_macro("or1", "@or", "false", 0);
216 check_macro("or2", "@or{true}", "true", 0);
217 check_macro("or2", "@or{false}", "false", 0);
218 check_macro("or3", "@or{true}{true}", "true", 0);
219 check_macro("or4", "@or{false}{true}", "true", 0);
220 check_macro("or5", "@or{true}{false}", "true", 0);
221 check_macro("or7", "@or{false}{false}", "false", 0);
222
223 check_macro("not1", "@not{true}", "false", 0);
224 check_macro("not2", "@not{false}", "true", 0);
225 check_macro("not3", "@not{wibble}", "true", 0);
226
227 check_macro("comment1", "@#{wibble}", "", 0);
228 check_macro("comment2", "@#{comment with a\nnewline in}", "", 0);
229
230 check_macro("discard1", "@discard{wibble}", "", 0);
231 check_macro("discard2", "@discard{comment with a\nnewline in}", "", 0);
232
233 check_macro("eq1", "@eq", "true", 0);
234 check_macro("eq2", "@eq{}", "true", 0);
235 check_macro("eq3", "@eq{a}", "true", 0);
236 check_macro("eq4", "@eq{a}{a}", "true", 0);
237 check_macro("eq5", "@eq{a}{a}{a}", "true", 0);
238 check_macro("eq7", "@eq{a}{b}", "false", 0);
239 check_macro("eq8", "@eq{a}{b}{a}", "false", 0);
240 check_macro("eq9", "@eq{a}{a}{b}", "false", 0);
241 check_macro("eq10", "@eq{b}{a}{a}", "false", 0);
242
243 check_macro("ne1", "@ne", "true", 0);
244 check_macro("ne2", "@ne{}", "true", 0);
245 check_macro("ne3", "@ne{a}", "true", 0);
246 check_macro("ne4", "@ne{a}{a}", "false", 0);
247 check_macro("ne5", "@ne{a}{a}{a}", "false", 0);
248 check_macro("ne7", "@ne{a}{b}", "true", 0);
249 check_macro("ne8", "@ne{a}{b}{a}", "false", 0);
250 check_macro("ne9", "@ne{a}{a}{b}", "false", 0);
251 check_macro("ne10", "@ne{b}{a}{a}", "false", 0);
252 check_macro("ne11", "@ne{a}{b}{c}", "true", 0);
253
254 check_macro("sh1", "@shell{true}", "", 0);
255 check_macro("sh2", "@shell{echo spong}", "spong\n", 0);
256 fprintf(stderr, ">>> expect error message about shell command:\n");
257 check_macro("sh3", "@shell{echo spong;exit 3}", "spong\n", 0);
258
259 check_macro("url1", "@urlquote{unreserved}", "unreserved", 0);
260 check_macro("url2", "@urlquote{has space}", "has%20space", 0);
261 check_macro("url3", "@urlquote{\xc0\xc1}", "%c0%c1", 0);
262
263 check_macro("include1", "@include{t-macros-1.tmpl}",
264 "yes\n", 0);
265 check_macro("include2", "@include{t-macros-2}",
266 "wibble\n", 0);
267 fprintf(stderr, ">>> expect error message about t-macros-nonesuch:\n");
268 check_macro("include3", "<@include{t-macros-nonesuch}@>",
269 "<[[cannot find template 't-macros-nonesuch']]>", 0);
270 fprintf(stderr, ">>> expect error message about 'wibble':\n");
271 check_macro("badex1", "<@wibble@>",
272 "<[['wibble' unknown]]>", 0);
273 fprintf(stderr, ">>> expect error message about 'if':\n");
274 check_macro("badex2", "<@if@>",
275 "<[['if' too few args]]>", 0);
276 fprintf(stderr, ">>> expect error message about 'if':\n");
277 check_macro("badex3", "<@if:1:2:3:4:5@>",
278 "<[['if' too many args]]>", 0);
279
8fc93a37
RK
280 /* Macro definitions ------------------------------------------------------ */
281
282 check_macro("macro1", "@define{m}{a b c}{@c@ @b@ @a@}@"
283 "@m{1}{2}{3}",
dd0f422a 284 "3 2 1", 0);
c617bac5 285 check_macro("macro2", "@m{b}{c}{a}",
dd0f422a 286 "a c b", 0);
c617bac5 287 check_macro("macro3", "@m{@eq{z}{z}}{p}{q}",
dd0f422a 288 "q p true", 0);
c617bac5
RK
289 check_macro("macro4",
290 "@discard{\n"
291 " @define{n}{a b c}\n"
292 " {@if{@eq{@a@}{@b@}} {@c@} {no}}\n"
293 "}@"
294 "@n{x}{y}{z}",
dd0f422a 295 "no", 0);
c617bac5
RK
296 check_macro("macro5",
297 "@n{x}{x}{z}",
dd0f422a 298 "z", 0);
c617bac5 299
1dcdf455
RK
300}
301
302TEST(macros);
303
304/*
305Local Variables:
306c-basic-offset:2
307comment-column:40
308fill-column:79
309indent-tabs-mode:nil
310End:
311*/