X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fccanlint%2Ftests%2Fhas_examples.c;h=947d76a4a768751053e6830b4bb788cb2c56df77;hp=bfb298f2773c390dc8f7088f074a5e3d82051ad6;hb=2926cafb52b9d95646d9dafa877d53f2368d8b2c;hpb=ab4a6fd9a3b417456ccd8f1fb976783683ccaa26 diff --git a/tools/ccanlint/tests/has_examples.c b/tools/ccanlint/tests/has_examples.c index bfb298f2..947d76a4 100644 --- a/tools/ccanlint/tests/has_examples.c +++ b/tools/ccanlint/tests/has_examples.c @@ -56,28 +56,22 @@ static char *add_example(struct manifest *m, struct ccan_file *source, } /* FIXME: We should have one example per function in header. */ -struct score { - bool info_example, header_example; - char *error; -}; - -static void *extract_examples(struct manifest *m, - bool keep, - unsigned int *timeleft) +static void extract_examples(struct manifest *m, + bool keep, + unsigned int *timeleft, + struct score *score) { - struct ccan_file *f; + struct ccan_file *f, *mainh = NULL; /* gcc complains uninitialized */ struct doc_section *d; - struct score *score = talloc(m, struct score); - - score->info_example = score->header_example = false; - score->error = NULL; + bool have_info_example = false, have_header_example = false; + score->total = 2; list_for_each(get_ccan_file_docs(m->info_file), d, list) { if (streq(d->type, "example")) { score->error = add_example(m, m->info_file, keep, d); if (score->error) - return score; - score->info_example = true; + return; + have_info_example = true; } } @@ -87,61 +81,39 @@ static void *extract_examples(struct manifest *m, || strlen(f->name) != strlen(m->basename) + 2) continue; + mainh = f; list_for_each(get_ccan_file_docs(f), d, list) { if (streq(d->type, "example")) { score->error = add_example(m, f, keep, d); if (score->error) - return score; - score->header_example = true; + return; + have_header_example = true; } } } - return score; -} - -static unsigned int score_examples(struct manifest *m, void *check_result) -{ - struct score *score = check_result; - int total = 0; - if (score->error) - return 0; - total += score->info_example; - total += score->header_example; - return total; -} - -static const char *describe_examples(struct manifest *m, - void *check_result) -{ - struct score *score = check_result; - char *descrip = NULL; - - if (score->error) - return score->error; - - if (!score->info_example) - descrip = talloc_asprintf(score, - "Your _info file has no module example.\n\n" - "There should be an Example: section of the _info documentation\n" - "which provides a concise toy program which uses your module\n"); + if (have_info_example && have_header_example) { + score->score = score->total; + score->pass = true; + return; + } - if (!score->header_example) - descrip = talloc_asprintf(score, - "%sMain header file file has no examples\n\n" - "There should be an Example: section for each public function\n" - "demonstrating its use\n", descrip ? descrip : ""); + score->error = "Expect examples in header and _info"; + if (!have_info_example) + score_file_error(score, m->info_file, 0, "No Example: section"); + if (!have_header_example) + score_file_error(score, mainh, 0, "No Example: section"); - return descrip; + score->score = have_info_example + have_header_example; + /* We pass if we find any example. */ + score->pass = score->score != 0; } -struct ccanlint has_examples = { - .key = "has-examples", - .name = "_info and header files have examples", - .score = score_examples, +struct ccanlint examples_exist = { + .key = "examples_exist", + .name = "_info and main header file have Example: sections", .check = extract_examples, - .describe = describe_examples, - .total_score = 2, + .needs = "info_exists" }; -REGISTER_TEST(has_examples, &has_info, NULL); +REGISTER_TEST(examples_exist);