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