Add a selftest that checks documentation invariants.
[external/binutils.git] / gdb / unittests / help-doc-selftests.c
1 /* Self tests for help doc for GDB, the GNU debugger.
2
3    Copyright (C) 2019 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "cli/cli-cmds.h"
22 #include "cli/cli-decode.h"
23 #include "gdbsupport/selftest.h"
24
25 namespace selftests {
26 namespace help_doc_tests {
27
28 static unsigned int nr_failed_invariants;
29
30 /* Report a broken invariant and increments nr_failed_invariants.  */
31
32 static void
33 broken_doc_invariant (const char *prefix, const char *name, const char *msg)
34 {
35   fprintf_filtered (gdb_stdout,
36                     "help doc broken invariant: command '%s%s' help doc %s\n",
37                     prefix, name, msg);
38   nr_failed_invariants++;
39 }
40
41 /* Recursively walk the commandlist structures, and check doc invariants:
42    - The first line of the doc must end with a '.'.
43    - the doc must not end with a new line.
44   If an invariant is not respected, produce a message and increment
45   nr_failed_invariants.
46   Note that we do not call SELF_CHECK in this function, as we want
47   all commands to be checked before making the test fail.  */
48
49 static void
50 check_doc (struct cmd_list_element *commandlist, const char *prefix)
51 {
52   struct cmd_list_element *c;
53
54   /* Walk through the commands.  */
55   for (c = commandlist; c; c = c->next)
56     {
57       /* Checks the doc has a first line terminated with a '.'.  */
58       const char *p = c->doc;
59
60       /* Position p on the first LF, or on terminating null byte.  */
61       while (*p && *p != '\n')
62         p++;
63       if (p == c->doc)
64         broken_doc_invariant
65           (prefix, c->name,
66            "is missing the first line terminated with a '.' character");
67       else if (*(p-1) != '.')
68         broken_doc_invariant
69           (prefix, c->name,
70            "first line is not terminated with a '.' character");
71
72       /* Checks the doc is not terminated with a new line.  */
73       if (c->doc[strlen (c->doc) - 1] == '\n')
74         broken_doc_invariant
75           (prefix, c->name,
76            "has a superfluous trailing end of line");
77
78       /* Check if this command has subcommands and is not an
79          abbreviation.  We skip checking subcommands of abbreviations
80          in order to avoid duplicates in the output.  */
81       if (c->prefixlist != NULL && !c->abbrev_flag)
82         {
83           /* Recursively call ourselves on the subcommand list,
84              passing the right prefix in.  */
85           check_doc (*c->prefixlist, c->prefixname);
86         }
87     }
88 }
89
90 static void
91 help_doc_invariants_tests ()
92 {
93   nr_failed_invariants = 0;
94   check_doc (cmdlist, "");
95   SELF_CHECK (nr_failed_invariants == 0);
96 }
97
98 } /* namespace help_doc_tests */
99 } /* namespace selftests */
100
101 void
102 _initialize_help_doc_selftests ()
103 {
104   selftests::register_test
105     ("help_doc_invariants",
106      selftests::help_doc_tests::help_doc_invariants_tests);
107 }