1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/foreach/foreach.h>
5 #include <ccan/str/str.h>
6 #include <ccan/cast/cast.h>
16 static const char *can_run(struct manifest *m)
18 struct list_head *list;
21 return "Safe mode enabled";
22 foreach_ptr(list, &m->examples, &m->mangled_examples)
23 if (!list_empty(list))
28 /* Very dumb scanner, allocates %s-strings. */
29 static bool scan_forv(const void *ctx,
30 const char *input, const char *fmt, va_list *args)
35 if (input[0] == '\0' || fmt[0] == '\0')
36 return input[0] == fmt[0];
40 if (cisspace(fmt[0])) {
41 /* One format space can swallow many input spaces */
43 while (cisspace(input[0])) {
44 if (scan_forv(ctx, ++input, fmt+1, &ap)) {
49 } else if (fmt[0] != '%') {
50 if (toupper(input[0]) != toupper(fmt[0]))
53 ret = scan_forv(ctx, input+1, fmt+1, &ap);
55 char **p = va_arg(ap, char **);
59 assert(fmt[1] == 's');
60 for (len = 1; input[len-1]; len++) {
61 ret = scan_forv(ctx, input + len, fmt+2, &ap);
63 *p = talloc_strndup(ctx, input, len);
73 static bool scan_for(const void *ctx, const char *input, const char *fmt, ...)
79 ret = scan_forv(ctx, input, fmt, &ap);
84 static char *find_expect(struct ccan_file *file,
85 char **lines, char **input, bool *exact,
91 for (; lines[*line]; (*line)++) {
92 char *p = lines[*line] + strspn(lines[*line], " \t");
93 if (!strstarts(p, "//"))
97 "given '%s', outputs '%s'",
98 "given '%s' outputs '%s'",
99 "given \"%s\", outputs \"%s\"",
100 "given \"%s\" outputs \"%s\"") {
101 if (scan_for(file, p, fmt, input, &expect)) {
108 "given '%s', output contains '%s'",
109 "given '%s' output contains '%s'",
110 "given \"%s\", output contains \"%s\"",
111 "given \"%s\" output contains \"%s\"") {
112 if (scan_for(file, p, fmt, input, &expect)) {
118 foreach_ptr(fmt, "outputs '%s'", "outputs \"%s\"") {
119 if (scan_for(file, p, fmt, &expect)) {
120 *input = cast_const(char *, "");
127 "given '%s', output contains '%s'",
128 "given '%s' output contains '%s'",
129 "given \"%s\", output contains \"%s\"",
130 "given \"%s\" output contains \"%s\"") {
131 if (scan_for(file, p, fmt, input, &expect)) {
137 /* Unquoted versions... we can get this wrong! */
139 "given %s, outputs '%s'",
140 "given '%s', outputs %s",
141 "given %s, outputs \"%s\"",
142 "given \"%s\", outputs %s",
143 "given %s, outputs %s",
144 "given %s outputs '%s'",
145 "given '%s' outputs %s",
146 "given %s outputs \"%s\"",
147 "given \"%s\" outputs %s",
148 "given %s outputs %s") {
149 if (scan_for(file, p, fmt, input, &expect)) {
156 "given %s, output contains '%s'",
157 "given '%s', output contains %s",
158 "given %s, output contains \"%s\"",
159 "given \"%s\", output contains %s",
160 "given %s, output contains %s",
161 "given %s output contains '%s'",
162 "given '%s' output contains %s",
163 "given %s output contains \"%s\"",
164 "given \"%s\" output contains %s",
165 "given %s output contains %s") {
166 if (scan_for(file, p, fmt, input, &expect)) {
176 if (scan_for(file, p, fmt, &expect)) {
177 *input = cast_const(char *, "");
184 "output contains '%s'",
185 "output contains \"%s\"",
186 "output contains %s") {
187 if (scan_for(file, p, fmt, &expect)) {
188 *input = cast_const(char *, "");
197 static char *trim(char *string)
199 while (strends(string, "\n"))
200 string[strlen(string)-1] = '\0';
204 static char *unexpected(struct ccan_file *i, const char *input,
205 const char *expect, bool exact)
209 unsigned int default_time = default_timeout_ms;
211 cmd = talloc_asprintf(i, "echo '%s' | %s %s",
212 input, i->compiled[COMPILE_NORMAL], input);
214 output = run_with_timeout(i, cmd, &ok, &default_time);
216 return talloc_asprintf(i, "Exited with non-zero status\n");
219 if (streq(output, expect) || streq(trim(output), expect))
222 if (strstr(output, expect))
228 static void run_examples(struct manifest *m,
229 unsigned int *timeleft, struct score *score)
232 struct list_head *list;
237 foreach_ptr(list, &m->examples, &m->mangled_examples) {
238 list_for_each(list, i, list) {
239 char **lines, *expect, *input, *output;
240 unsigned int linenum = 0;
243 lines = get_ccan_file_lines(i);
245 for (expect = find_expect(i, lines, &input, &exact,
249 expect = find_expect(i, lines, &input,
251 if (i->compiled[COMPILE_NORMAL] == NULL)
255 output = unexpected(i, input, expect, exact);
260 score_file_error(score, i, linenum+1,
261 "output '%s' didn't %s '%s'\n",
263 exact ? "match" : "contain",
271 /* FIXME: Test with reduced features, valgrind! */
272 struct ccanlint examples_run = {
273 .key = "examples_run",
274 .name = "Module examples with expected output give that output",
275 .check = run_examples,
277 .needs = "examples_compile"
280 REGISTER_TEST(examples_run);