1 /* Self tests for help doc for GDB, the GNU debugger.
3 Copyright (C) 2019 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "cli/cli-cmds.h"
22 #include "cli/cli-decode.h"
23 #include "gdbsupport/selftest.h"
26 namespace help_doc_tests
{
28 static unsigned int nr_failed_invariants
;
30 /* Report a broken invariant and increments nr_failed_invariants. */
33 broken_doc_invariant (const char *prefix
, const char *name
, const char *msg
)
35 fprintf_filtered (gdb_stdout
,
36 "help doc broken invariant: command '%s%s' help doc %s\n",
38 nr_failed_invariants
++;
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
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. */
50 check_doc (struct cmd_list_element
*commandlist
, const char *prefix
)
52 struct cmd_list_element
*c
;
54 /* Walk through the commands. */
55 for (c
= commandlist
; c
; c
= c
->next
)
57 /* Checks the doc has a first line terminated with a '.'. */
58 const char *p
= c
->doc
;
60 /* Position p on the first LF, or on terminating null byte. */
61 while (*p
&& *p
!= '\n')
66 "is missing the first line terminated with a '.' character");
67 else if (*(p
-1) != '.')
70 "first line is not terminated with a '.' character");
72 /* Checks the doc is not terminated with a new line. */
73 if (c
->doc
[strlen (c
->doc
) - 1] == '\n')
76 "has a superfluous trailing end of line");
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
)
83 /* Recursively call ourselves on the subcommand list,
84 passing the right prefix in. */
85 check_doc (*c
->prefixlist
, c
->prefixname
);
91 help_doc_invariants_tests ()
93 nr_failed_invariants
= 0;
94 check_doc (cmdlist
, "");
95 SELF_CHECK (nr_failed_invariants
== 0);
98 } /* namespace help_doc_tests */
99 } /* namespace selftests */
102 _initialize_help_doc_selftests ()
104 selftests::register_test
105 ("help_doc_invariants",
106 selftests::help_doc_tests::help_doc_invariants_tests
);