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>
15 static const char *can_run(struct manifest *m)
17 struct list_head *list;
20 return "Safe mode enabled";
21 foreach_ptr(list, &m->examples, &m->mangled_examples)
22 if (!list_empty(list))
27 /* Very dumb scanner, allocates %s-strings. */
28 static bool scan_forv(const void *ctx,
29 const char *input, const char *fmt, va_list *args)
34 if (input[0] == '\0' || fmt[0] == '\0')
35 return input[0] == fmt[0];
39 if (isspace(fmt[0])) {
40 /* One format space can swallow many input spaces */
42 while (isspace(input[0])) {
43 if (scan_forv(ctx, ++input, fmt+1, &ap)) {
48 } else if (fmt[0] != '%') {
49 if (toupper(input[0]) != toupper(fmt[0]))
52 ret = scan_forv(ctx, input+1, fmt+1, &ap);
54 char **p = va_arg(ap, char **);
58 assert(fmt[1] == 's');
59 for (len = 1; input[len-1]; len++) {
60 ret = scan_forv(ctx, input + len, fmt+2, &ap);
62 *p = talloc_strndup(ctx, input, len);
72 static bool scan_for(const void *ctx, const char *input, const char *fmt, ...)
78 ret = scan_forv(ctx, input, fmt, &ap);
83 static char *find_expect(struct ccan_file *file,
84 char **lines, char **input, bool *exact,
90 for (; lines[*line]; (*line)++) {
91 char *p = lines[*line] + strspn(lines[*line], " \t");
92 if (!strstarts(p, "//"))
96 "given '%s', outputs '%s'",
97 "given '%s' outputs '%s'",
98 "given \"%s\", outputs \"%s\"",
99 "given \"%s\" outputs \"%s\"") {
100 if (scan_for(file, p, fmt, input, &expect)) {
107 "given '%s', output contains '%s'",
108 "given '%s' output contains '%s'",
109 "given \"%s\", output contains \"%s\"",
110 "given \"%s\" output contains \"%s\"") {
111 if (scan_for(file, p, fmt, input, &expect)) {
117 foreach_ptr(fmt, "outputs '%s'", "outputs \"%s\"") {
118 if (scan_for(file, p, fmt, &expect)) {
126 "given '%s', output contains '%s'",
127 "given '%s' output contains '%s'",
128 "given \"%s\", output contains \"%s\"",
129 "given \"%s\" output contains \"%s\"") {
130 if (scan_for(file, p, fmt, input, &expect)) {
136 /* Unquoted versions... we can get this wrong! */
138 "given %s, outputs '%s'",
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 if (scan_for(file, p, fmt, input, &expect)) {
155 "given %s, output contains '%s'",
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 if (scan_for(file, p, fmt, input, &expect)) {
175 if (scan_for(file, p, fmt, &expect)) {
183 "output contains '%s'",
184 "output contains \"%s\"",
185 "output contains %s") {
186 if (scan_for(file, p, fmt, &expect)) {
196 static char *trim(char *string)
198 while (strends(string, "\n"))
199 string[strlen(string)-1] = '\0';
203 static char *unexpected(struct ccan_file *i, const char *input,
204 const char *expect, bool exact)
208 unsigned int default_time = default_timeout_ms;
210 cmd = talloc_asprintf(i, "echo '%s' | %s %s",
211 input, i->compiled, input);
213 output = run_with_timeout(i, cmd, &ok, &default_time);
215 return talloc_asprintf(i, "Exited with non-zero status\n");
218 if (streq(output, expect) || streq(trim(output), expect))
221 if (strstr(output, expect))
227 static void run_examples(struct manifest *m, bool keep,
228 unsigned int *timeleft, struct score *score)
231 struct list_head *list;
236 foreach_ptr(list, &m->examples, &m->mangled_examples) {
237 list_for_each(list, i, list) {
238 char **lines, *expect, *input, *output;
239 unsigned int linenum = 0;
242 lines = get_ccan_file_lines(i);
244 for (expect = find_expect(i, lines, &input, &exact,
248 expect = find_expect(i, lines, &input,
250 if (i->compiled == NULL)
254 output = unexpected(i, input, expect, exact);
259 score_file_error(score, i, linenum+1,
260 "output '%s' didn't %s '%s'\n",
262 exact ? "match" : "contain",
270 struct ccanlint examples_run = {
271 .key = "examples_run",
272 .name = "Module examples with expected output give that output",
273 .check = run_examples,
275 .needs = "examples_compile"
278 REGISTER_TEST(examples_run);