chiark / gitweb /
Further macros.c testing
[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;
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);
915ea29a 35 check_integer(m->type, MX_TEXT);
1dcdf455 36 check_string(m->filename, "plaintext1");
915ea29a 37 check_integer(m->line, 1);
1dcdf455
RK
38 check_string(m->text, L1 L2);
39 insist(m->next == 0);
9dbb630e 40 check_string(mx_dump(m), plain);
1dcdf455
RK
41
42 /* Check that partial parses stop in the right place */
43 m = mx_parse("plaintext2", 5, plain, plain + strlen(L1));
915ea29a 44 check_integer(m->type, MX_TEXT);
1dcdf455 45 check_string(m->filename, "plaintext2");
915ea29a 46 check_integer(m->line, 5);
1dcdf455
RK
47 check_string(m->text, L1);
48 insist(m->next == 0);
9dbb630e 49 check_string(mx_dump(m), L1);
1dcdf455
RK
50
51 /* The simplest possible expansion */
52 m = mx_parse("macro1", 1, "@macro@", NULL);
915ea29a 53 check_integer(m->type, MX_EXPANSION);
1dcdf455 54 check_string(m->filename, "macro1");
915ea29a 55 check_integer(m->line, 1);
1dcdf455 56 check_string(m->name, "macro");
915ea29a 57 check_integer(m->nargs, 0);
1dcdf455
RK
58 insist(m->args == 0);
59 insist(m->next == 0);
9dbb630e 60 check_string(mx_dump(m), "@macro@");
1dcdf455
RK
61
62 /* Spacing variants of the above */
63 m = mx_parse("macro2", 1, "@ macro@", NULL);
915ea29a 64 check_integer(m->type, MX_EXPANSION);
1dcdf455 65 check_string(m->filename, "macro2");
915ea29a 66 check_integer(m->line, 1);
1dcdf455 67 check_string(m->name, "macro");
915ea29a 68 check_integer(m->nargs, 0);
1dcdf455
RK
69 insist(m->args == 0);
70 insist(m->next == 0);
9dbb630e 71 check_string(mx_dump(m), "@macro@");
1dcdf455 72 m = mx_parse("macro3", 1, "@macro @", NULL);
915ea29a 73 check_integer(m->type, MX_EXPANSION);
1dcdf455 74 check_string(m->filename, "macro3");
915ea29a 75 check_integer(m->line, 1);
1dcdf455 76 check_string(m->name, "macro");
915ea29a 77 check_integer(m->nargs, 0);
1dcdf455
RK
78 insist(m->args == 0);
79 insist(m->next == 0);
9dbb630e 80 check_string(mx_dump(m), "@macro@");
1dcdf455
RK
81
82 /* Unterminated variants */
83 m = mx_parse("macro4", 1, "@macro", NULL);
915ea29a 84 check_integer(m->type, MX_EXPANSION);
1dcdf455 85 check_string(m->filename, "macro4");
915ea29a 86 check_integer(m->line, 1);
1dcdf455 87 check_string(m->name, "macro");
915ea29a 88 check_integer(m->nargs, 0);
1dcdf455
RK
89 insist(m->args == 0);
90 insist(m->next == 0);
9dbb630e 91 check_string(mx_dump(m), "@macro@");
1dcdf455 92 m = mx_parse("macro5", 1, "@macro ", NULL);
915ea29a 93 check_integer(m->type, MX_EXPANSION);
1dcdf455 94 check_string(m->filename, "macro5");
915ea29a 95 check_integer(m->line, 1);
1dcdf455 96 check_string(m->name, "macro");
915ea29a 97 check_integer(m->nargs, 0);
1dcdf455
RK
98 insist(m->args == 0);
99 insist(m->next == 0);
9dbb630e 100 check_string(mx_dump(m), "@macro@");
1dcdf455
RK
101
102 /* Macros with a :-separated argument */
103 m = mx_parse("macro5", 1, "@macro:arg@", NULL);
915ea29a 104 check_integer(m->type, MX_EXPANSION);
1dcdf455 105 check_string(m->filename, "macro5");
915ea29a 106 check_integer(m->line, 1);
1dcdf455 107 check_string(m->name, "macro");
915ea29a 108 check_integer(m->nargs, 1);
1dcdf455
RK
109 insist(m->next == 0);
110
915ea29a 111 check_integer(m->args[0]->type, MX_TEXT);
1dcdf455 112 check_string(m->args[0]->filename, "macro5");
915ea29a 113 check_integer(m->args[0]->line, 1);
1dcdf455
RK
114 check_string(m->args[0]->text, "arg");
115 insist(m->args[0]->next == 0);
116
9dbb630e
RK
117 check_string(mx_dump(m), "@macro{arg}@");
118
1dcdf455
RK
119 /* Multiple :-separated arguments, and spacing, and newlines */
120 m = mx_parse("macro6", 1, "@macro : \n arg1 : \n arg2@", NULL);
915ea29a 121 check_integer(m->type, MX_EXPANSION);
1dcdf455 122 check_string(m->filename, "macro6");
915ea29a 123 check_integer(m->line, 1);
1dcdf455 124 check_string(m->name, "macro");
915ea29a 125 check_integer(m->nargs, 2);
1dcdf455
RK
126 insist(m->next == 0);
127
915ea29a 128 check_integer(m->args[0]->type, MX_TEXT);
1dcdf455 129 check_string(m->args[0]->filename, "macro6");
915ea29a 130 check_integer(m->args[0]->line, 2);
1dcdf455
RK
131 check_string(m->args[0]->text, "arg1");
132 insist(m->args[0]->next == 0);
133
915ea29a 134 check_integer(m->args[1]->type, MX_TEXT);
1dcdf455 135 check_string(m->args[1]->filename, "macro6");
915ea29a 136 check_integer(m->args[1]->line, 3);
1dcdf455
RK
137 check_string(m->args[1]->text, "arg2");
138 insist(m->args[1]->next == 0);
139
9dbb630e
RK
140 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
141
142 /* Multiple bracketed arguments */
915ea29a
RK
143 m = mx_parse("macro7", 1, "@macro{arg1}{arg2}@", NULL);
144 check_string(mx_dump(m), "@macro{arg1}{arg2}@");
145
774eb34c 146 m = mx_parse("macro8", 1, "@macro{\narg1}{\narg2}@", NULL);
915ea29a
RK
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
774eb34c 151 * the \n at the end of line 1. Compare with macro9: */
1dcdf455 152
774eb34c 153 m = mx_parse("macro9", 1, "@macro\n{arg1}\n{arg2}@", NULL);
915ea29a
RK
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);
774eb34c
RK
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}@}@");
915ea29a 165
1dcdf455
RK
166}
167
168TEST(macros);
169
170/*
171Local Variables:
172c-basic-offset:2
173comment-column:40
174fill-column:79
175indent-tabs-mode:nil
176End:
177*/