Main template expander and some built-in expansions. All untested.
[disorder] / lib / t-macros.c
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
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;
28
29 /* As simple as it gets */
30 m = mx_parse("plaintext1", 1, "", NULL);
31 insist(m == 0);
32
33 /* Almost as simple as that */
34 m = mx_parse("plaintext1", 1, plain, NULL);
35 check_integer(m->type, MX_TEXT);
36 check_string(m->filename, "plaintext1");
37 check_integer(m->line, 1);
38 check_string(m->text, L1 L2);
39 insist(m->next == 0);
40 check_string(mx_dump(m), plain);
41
42 /* Check that partial parses stop in the right place */
43 m = mx_parse("plaintext2", 5, plain, plain + strlen(L1));
44 check_integer(m->type, MX_TEXT);
45 check_string(m->filename, "plaintext2");
46 check_integer(m->line, 5);
47 check_string(m->text, L1);
48 insist(m->next == 0);
49 check_string(mx_dump(m), L1);
50
51 /* The simplest possible expansion */
52 m = mx_parse("macro1", 1, "@macro@", NULL);
53 check_integer(m->type, MX_EXPANSION);
54 check_string(m->filename, "macro1");
55 check_integer(m->line, 1);
56 check_string(m->name, "macro");
57 check_integer(m->nargs, 0);
58 insist(m->args == 0);
59 insist(m->next == 0);
60 check_string(mx_dump(m), "@macro@");
61
62 /* Spacing variants of the above */
63 m = mx_parse("macro2", 1, "@ macro@", NULL);
64 check_integer(m->type, MX_EXPANSION);
65 check_string(m->filename, "macro2");
66 check_integer(m->line, 1);
67 check_string(m->name, "macro");
68 check_integer(m->nargs, 0);
69 insist(m->args == 0);
70 insist(m->next == 0);
71 check_string(mx_dump(m), "@macro@");
72 m = mx_parse("macro3", 1, "@macro @", NULL);
73 check_integer(m->type, MX_EXPANSION);
74 check_string(m->filename, "macro3");
75 check_integer(m->line, 1);
76 check_string(m->name, "macro");
77 check_integer(m->nargs, 0);
78 insist(m->args == 0);
79 insist(m->next == 0);
80 check_string(mx_dump(m), "@macro@");
81
82 /* Unterminated variants */
83 m = mx_parse("macro4", 1, "@macro", NULL);
84 check_integer(m->type, MX_EXPANSION);
85 check_string(m->filename, "macro4");
86 check_integer(m->line, 1);
87 check_string(m->name, "macro");
88 check_integer(m->nargs, 0);
89 insist(m->args == 0);
90 insist(m->next == 0);
91 check_string(mx_dump(m), "@macro@");
92 m = mx_parse("macro5", 1, "@macro ", NULL);
93 check_integer(m->type, MX_EXPANSION);
94 check_string(m->filename, "macro5");
95 check_integer(m->line, 1);
96 check_string(m->name, "macro");
97 check_integer(m->nargs, 0);
98 insist(m->args == 0);
99 insist(m->next == 0);
100 check_string(mx_dump(m), "@macro@");
101
102 /* Macros with a :-separated argument */
103 m = mx_parse("macro5", 1, "@macro:arg@", NULL);
104 check_integer(m->type, MX_EXPANSION);
105 check_string(m->filename, "macro5");
106 check_integer(m->line, 1);
107 check_string(m->name, "macro");
108 check_integer(m->nargs, 1);
109 insist(m->next == 0);
110
111 check_integer(m->args[0]->type, MX_TEXT);
112 check_string(m->args[0]->filename, "macro5");
113 check_integer(m->args[0]->line, 1);
114 check_string(m->args[0]->text, "arg");
115 insist(m->args[0]->next == 0);
116
117 check_string(mx_dump(m), "@macro{arg}@");
118
119 /* Multiple :-separated arguments, and spacing, and newlines */
120 m = mx_parse("macro6", 1, "@macro : \n arg1 : \n arg2@", NULL);
121 check_integer(m->type, MX_EXPANSION);
122 check_string(m->filename, "macro6");
123 check_integer(m->line, 1);
124 check_string(m->name, "macro");
125 check_integer(m->nargs, 2);
126 insist(m->next == 0);
127
128 check_integer(m->args[0]->type, MX_TEXT);
129 check_string(m->args[0]->filename, "macro6");
130 check_integer(m->args[0]->line, 2);
131 check_string(m->args[0]->text, "arg1");
132 insist(m->args[0]->next == 0);
133
134 check_integer(m->args[1]->type, MX_TEXT);
135 check_string(m->args[1]->filename, "macro6");
136 check_integer(m->args[1]->line, 3);
137 check_string(m->args[1]->text, "arg2");
138 insist(m->args[1]->next == 0);
139
140 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
141
142 /* Multiple bracketed arguments */
143 m = mx_parse("macro7", 1, "@macro{arg1}{arg2}@", NULL);
144 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
145
146 m = mx_parse("macro8", 1, "@macro{\narg1}{\narg2}@", NULL);
147 check_string(mx_dump(m), "@macro{\narg1}{\narg2}@");
148 check_integer(m->args[0]->line, 1);
149 check_integer(m->args[1]->line, 2);
150 /* ...yes, lines 1 and 2: the first character of the first arg is
151 * the \n at the end of line 1. Compare with macro9: */
152
153 m = mx_parse("macro9", 1, "@macro\n{arg1}\n{arg2}@", NULL);
154 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
155 check_integer(m->args[0]->line, 2);
156 check_integer(m->args[1]->line, 3);
157
158 /* Arguments that themselves contain expansions */
159 m = mx_parse("macro10", 1, "@macro{@macro2{arg1}{arg2}@}@", NULL);
160 check_string(mx_dump(m), "@macro{@macro2{arg1}{arg2}@}@");
161
162 /* ...and with omitted trailing @ */
163 m = mx_parse("macro11", 1, "@macro{@macro2{arg1}{arg2}}", NULL);
164 check_string(mx_dump(m), "@macro{@macro2{arg1}{arg2}@}@");
165
166 /* Similarly but with more whitespace; NB that the whitespace is
167 * preserved. */
168 m = mx_parse("macro12", 1, "@macro {@macro2 {arg1} {arg2} }\n", NULL);
169 check_string(mx_dump(m), "@macro{@macro2{arg1}{arg2}@ }@\n");
170 }
171
172 TEST(macros);
173
174 /*
175 Local Variables:
176 c-basic-offset:2
177 comment-column:40
178 fill-column:79
179 indent-tabs-mode:nil
180 End:
181 */