2 * This file is part of DisOrder.
3 * Copyright (C) 2008 Richard Kettlewell
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.
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.
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
23 static 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;
30 /* Plain text ------------------------------------------------------------- */
32 /* As simple as it gets */
33 m = mx_parse("plaintext1", 1, "", NULL);
36 /* Almost as simple as that */
37 m = mx_parse("plaintext1", 1, plain, NULL);
38 check_integer(m->type, MX_TEXT);
39 check_string(m->filename, "plaintext1");
40 check_integer(m->line, 1);
41 check_string(m->text, L1 L2);
43 check_string(mx_dump(m), plain);
45 /* Check that partial parses stop in the right place */
46 m = mx_parse("plaintext2", 5, plain, plain + strlen(L1));
47 check_integer(m->type, MX_TEXT);
48 check_string(m->filename, "plaintext2");
49 check_integer(m->line, 5);
50 check_string(m->text, L1);
52 check_string(mx_dump(m), L1);
54 /* Simple macro parsing --------------------------------------------------- */
56 /* The simplest possible expansion */
57 m = mx_parse("macro1", 1, "@macro@", NULL);
58 check_integer(m->type, MX_EXPANSION);
59 check_string(m->filename, "macro1");
60 check_integer(m->line, 1);
61 check_string(m->name, "macro");
62 check_integer(m->nargs, 0);
65 check_string(mx_dump(m), "@macro@");
67 /* Spacing variants of the above */
68 m = mx_parse("macro2", 1, "@ macro@", NULL);
69 check_integer(m->type, MX_EXPANSION);
70 check_string(m->filename, "macro2");
71 check_integer(m->line, 1);
72 check_string(m->name, "macro");
73 check_integer(m->nargs, 0);
76 check_string(mx_dump(m), "@macro@");
77 m = mx_parse("macro3", 1, "@macro @", NULL);
78 check_integer(m->type, MX_EXPANSION);
79 check_string(m->filename, "macro3");
80 check_integer(m->line, 1);
81 check_string(m->name, "macro");
82 check_integer(m->nargs, 0);
85 check_string(mx_dump(m), "@macro@");
87 /* Unterminated variants */
88 m = mx_parse("macro4", 1, "@macro", NULL);
89 check_integer(m->type, MX_EXPANSION);
90 check_string(m->filename, "macro4");
91 check_integer(m->line, 1);
92 check_string(m->name, "macro");
93 check_integer(m->nargs, 0);
96 check_string(mx_dump(m), "@macro@");
97 m = mx_parse("macro5", 1, "@macro ", NULL);
98 check_integer(m->type, MX_EXPANSION);
99 check_string(m->filename, "macro5");
100 check_integer(m->line, 1);
101 check_string(m->name, "macro");
102 check_integer(m->nargs, 0);
103 insist(m->args == 0);
104 insist(m->next == 0);
105 check_string(mx_dump(m), "@macro@");
107 /* Macros with a :-separated argument */
108 m = mx_parse("macro5", 1, "@macro:arg@", NULL);
109 check_integer(m->type, MX_EXPANSION);
110 check_string(m->filename, "macro5");
111 check_integer(m->line, 1);
112 check_string(m->name, "macro");
113 check_integer(m->nargs, 1);
114 insist(m->next == 0);
116 check_integer(m->args[0]->type, MX_TEXT);
117 check_string(m->args[0]->filename, "macro5");
118 check_integer(m->args[0]->line, 1);
119 check_string(m->args[0]->text, "arg");
120 insist(m->args[0]->next == 0);
122 check_string(mx_dump(m), "@macro{arg}@");
124 /* Multiple :-separated arguments, and spacing, and newlines */
125 m = mx_parse("macro6", 1, "@macro : \n arg1 : \n arg2@", NULL);
126 check_integer(m->type, MX_EXPANSION);
127 check_string(m->filename, "macro6");
128 check_integer(m->line, 1);
129 check_string(m->name, "macro");
130 check_integer(m->nargs, 2);
131 insist(m->next == 0);
133 check_integer(m->args[0]->type, MX_TEXT);
134 check_string(m->args[0]->filename, "macro6");
135 check_integer(m->args[0]->line, 2);
136 check_string(m->args[0]->text, "arg1");
137 insist(m->args[0]->next == 0);
139 check_integer(m->args[1]->type, MX_TEXT);
140 check_string(m->args[1]->filename, "macro6");
141 check_integer(m->args[1]->line, 3);
142 check_string(m->args[1]->text, "arg2");
143 insist(m->args[1]->next == 0);
145 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
147 /* Multiple bracketed arguments */
148 m = mx_parse("macro7", 1, "@macro{arg1}{arg2}@", NULL);
149 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
151 m = mx_parse("macro8", 1, "@macro{\narg1}{\narg2}@", NULL);
152 check_string(mx_dump(m), "@macro{\narg1}{\narg2}@");
153 check_integer(m->args[0]->line, 1);
154 check_integer(m->args[1]->line, 2);
155 /* ...yes, lines 1 and 2: the first character of the first arg is
156 * the \n at the end of line 1. Compare with macro9: */
158 m = mx_parse("macro9", 1, "@macro\n{arg1}\n{arg2}@", NULL);
159 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
160 check_integer(m->args[0]->line, 2);
161 check_integer(m->args[1]->line, 3);
163 /* Arguments that themselves contain expansions */
164 m = mx_parse("macro10", 1, "@macro{@macro2{arg1}{arg2}@}@", NULL);
165 check_string(mx_dump(m), "@macro{@macro2{arg1}{arg2}@}@");
167 /* ...and with omitted trailing @ */
168 m = mx_parse("macro11", 1, "@macro{@macro2{arg1}{arg2}}", NULL);
169 check_string(mx_dump(m), "@macro{@macro2{arg1}{arg2}@}@");
171 /* Similarly but with more whitespace; NB that the whitespace is
173 m = mx_parse("macro12", 1, "@macro {@macro2 {arg1} {arg2} }\n", NULL);
174 check_string(mx_dump(m), "@macro{@macro2{arg1}{arg2}@ }@\n");
176 /* Simple expansions ------------------------------------------------------ */
178 mx_register_builtin();
180 #define check_macro(NAME, INPUT, OUTPUT) do { \
181 m = mx_parse(NAME, 1, INPUT, NULL); \
182 check_integer(mx_expandstr(m, &s, 0/*u*/, NAME), 0); \
183 if(s && strcmp(s, OUTPUT)) { \
184 fprintf(stderr, "%s:%d: test %s\n" \
186 " EXPECTED: '%s'\n" \
188 __FILE__, __LINE__, NAME, INPUT, OUTPUT, s); \
193 check_macro("empty", "", "");
194 check_macro("plain", plain, plain);
196 check_macro("if1", "@if{true}{yes}{no}", "yes");
197 check_macro("if2", "@if{true}{yes}", "yes");
198 check_macro("if3", "@if{false}{yes}{no}", "no");
199 check_macro("if4", "@if{false}{yes}", "");
200 check_macro("if5", "@if{ true}{yes}", "");
202 check_macro("and1", "@and", "true");
203 check_macro("and2", "@and{true}", "true");
204 check_macro("and3", "@and{false}", "false");
205 check_macro("and4", "@and{true}{true}", "true");
206 check_macro("and5", "@and{false}{true}", "false");
207 check_macro("and6", "@and{true}{false}", "false");
208 check_macro("and7", "@and{false}{false}", "false");
210 check_macro("or1", "@or", "false");
211 check_macro("or2", "@or{true}", "true");
212 check_macro("or2", "@or{false}", "false");
213 check_macro("or3", "@or{true}{true}", "true");
214 check_macro("or4", "@or{false}{true}", "true");
215 check_macro("or5", "@or{true}{false}", "true");
216 check_macro("or7", "@or{false}{false}", "false");
218 check_macro("not1", "@not{true}", "false");
219 check_macro("not2", "@not{false}", "true");
220 check_macro("not3", "@not{wibble}", "true");
222 check_macro("comment1", "@#{wibble}", "");
223 check_macro("comment2", "@#{comment with a\nnewline in}", "");
225 check_macro("discard1", "@discard{wibble}", "");
226 check_macro("discard2", "@discard{comment with a\nnewline in}", "");
228 check_macro("eq1", "@eq", "true");
229 check_macro("eq2", "@eq{}", "true");
230 check_macro("eq3", "@eq{a}", "true");
231 check_macro("eq4", "@eq{a}{a}", "true");
232 check_macro("eq5", "@eq{a}{a}{a}", "true");
233 check_macro("eq7", "@eq{a}{b}", "false");
234 check_macro("eq8", "@eq{a}{b}{a}", "false");
235 check_macro("eq9", "@eq{a}{a}{b}", "false");
236 check_macro("eq10", "@eq{b}{a}{a}", "false");
238 check_macro("ne1", "@ne", "true");
239 check_macro("ne2", "@ne{}", "true");
240 check_macro("ne3", "@ne{a}", "true");
241 check_macro("ne4", "@ne{a}{a}", "false");
242 check_macro("ne5", "@ne{a}{a}{a}", "false");
243 check_macro("ne7", "@ne{a}{b}", "true");
244 check_macro("ne8", "@ne{a}{b}{a}", "false");
245 check_macro("ne9", "@ne{a}{a}{b}", "false");
246 check_macro("ne10", "@ne{b}{a}{a}", "false");
247 check_macro("ne11", "@ne{a}{b}{c}", "true");
249 check_macro("sh1", "@shell{true}", "");
250 check_macro("sh2", "@shell{echo spong}", "spong\n");
251 fprintf(stderr, "expxect error message from macro expander:\n");
252 check_macro("sh3", "@shell{echo spong;exit 3}", "spong\n");
254 check_macro("url1", "@urlquote{unreserved}", "unreserved");
255 check_macro("url2", "@urlquote{has space}", "has%20space");
256 check_macro("url3", "@urlquote{\xc0\xc1}", "%c0%c1");