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