1 # Copyright 2022 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Test how GDB reformats the documentation string of commands
17 # implemented in Python for use as the help text of those same
20 load_lib gdb-python.exp
22 # Check that Python is supported.
24 if { [skip_python_tests] } { continue }
26 # A global counter used to number the tests.
29 # Run a test, create both a command and a parameter with INPUT_DOCS as
30 # the Python documentation string, load the command and parameter into
31 # GDB, and then ask for the help text of the command, and the show and
32 # set help for the parameter. The output from GDB should match
33 # EXPECTED_OUTPUT in each case.
35 # The INPUT_DOCS must start with 2 space characters, followed by the
36 # 3-quote characters to start the doc string, and must end with the
37 # final 3-quote characters. If this is not true then an error is
38 # thrown, and this function immediately returns.
39 proc test { input_docs expected_output } {
45 with_test_prefix "test ${local_idx}" {
47 # The tests in this script don't have descriptive names,
48 # rather they use the global IDX to generate a unique name for
49 # each test. To make it easier to track down a failing test,
50 # we print out the test name and the input string.
51 verbose -log "Start of: test_cmd ${local_idx}"
52 verbose -log "Input:\n${input_docs}"
54 if { [string range $input_docs 0 4] != " \"\"\"" } {
55 perror "Input string does not start with the required pattern"
59 if { [string range $input_docs end-2 end] != "\"\"\"" } {
60 perror "Input string does not end with the required pattern"
64 set python_filename [standard_output_file "${::gdb_test_file_name}-${local_idx}.py"]
66 set fd [open $python_filename w]
68 puts $fd "class test_cmd (gdb.Command):"
71 puts $fd " def __init__ (self):"
72 puts $fd " super ().__init__ (\"test-cmd\", gdb.COMMAND_OBSCURE)"
74 puts $fd " def invoke (self, arg, from_tty):"
75 puts $fd " print (\"In test-cmd\")"
78 puts $fd "class test_param (gdb.Parameter):"
81 puts $fd " show_doc = \"This is the show doc line\""
82 puts $fd " set_doc = \"This is the set doc line\""
84 puts $fd " def __init__ (self):"
85 puts $fd " super ().__init__ (\"test-param\", gdb.COMMAND_OBSCURE, gdb.PARAM_BOOLEAN)"
87 puts $fd " def get_show_string (self, value):"
88 puts $fd " return \"The state is '\" + value + \"'\""
91 puts $fd "test_param ()"
92 puts $fd "test_cmd ()"
95 puts $fd "print(\"Loaded OK.\")"
99 set remote_python_file \
100 [gdb_remote_download host $python_filename]
104 gdb_test "source ${remote_python_file}" \
106 "source python command file"
108 # Use gdb_test_multiple here as plain gdb_test will allow
109 # excess blank lines between the expected_output pattern and
110 # the gdb_prompt - a core part of this test is that we are
111 # checking that such blank lines are removed - so we can't use
113 gdb_test_multiple "help test-cmd" "" {
114 -re "^help test-cmd\r\n" {
117 -re "^$expected_output\r\n$::gdb_prompt $" {
122 gdb_test_multiple "help set test-param" "" {
123 -re "^help set test-param\r\n" {
126 -re "^This is the set doc line\r\n" {
129 -re "^$expected_output\r\n$::gdb_prompt $" {
134 gdb_test_multiple "help show test-param" "" {
135 -re "^help show test-param\r\n" {
138 -re "^This is the show doc line\r\n" {
141 -re "^$expected_output\r\n$::gdb_prompt $" {
148 # The first test is pretty vanilla. First line starts immediately
149 # after the opening quotes, and closing quotes are immediately after
153 " \"\"\"This is the first line." \
155 " This is the third line.\"\"\""] \
157 "This is the first line\\." \
159 "This is the third line\\."]
161 # In this test the first line starts on the line after the opening
162 # quotes. The blank line in the middle is still completely empty.
166 " This is the first line." \
168 " This is the third line.\"\"\""] \
170 "This is the first line\\." \
172 "This is the third line\\."]
174 # This test adds an indented line in the middle, we expect the
175 # relative indentation to be retained.
179 " This is the first line." \
180 " Indented second line." \
181 " This is the third line.\"\"\""] \
183 "This is the first line\\." \
184 " Indented second line\\." \
185 "This is the third line\\."]
187 # The indentation moves to the first line.
191 " Indented first line." \
192 " This is the second line." \
193 " This is the third line.\"\"\""] \
195 " Indented first line\\." \
196 "This is the second line\\." \
197 "This is the third line\\."]
199 # The indentation moves to the last line.
203 " This is the first line." \
204 " This is the second line." \
205 " Indented third line.\"\"\""] \
207 "This is the first line\\." \
208 "This is the second line\\." \
209 " Indented third line\\."]
211 # Tests using different amounts of white-space on a line of its own.
212 # We test with the white-space at the beginning, end, and in the
213 # middle of the doc string.
214 foreach line [list "" " " " " " " " "] {
215 # Blank lines at the beginning should be removed.
220 " This is the first line." \
221 " Indented second line." \
222 " This is the third line.\"\"\""] \
224 "This is the first line\\." \
225 " Indented second line\\." \
226 "This is the third line\\."]
228 # As should blank lines at the end.
232 " This is the first line." \
233 " Indented second line." \
234 " This is the third line." \
238 "This is the first line\\." \
239 " Indented second line\\." \
240 "This is the third line\\."]
242 # While blank lines in the middle should have all white-space
247 " This is the first line." \
249 " This is the third line." \
253 "This is the first line\\." \
255 "This is the third line\\."]
258 # Check for correct handling of closing quotes being on a line of
263 " This is the first line." \
264 " Indented second line." \
265 " This is the third line." \
268 "This is the first line\\." \
269 " Indented second line\\." \
270 "This is the third line\\." ]
273 # Check with a single 'x' character before the closing quotes. Make
274 # sure we don't drop this character.
278 " This is the first line." \
279 " Indented second line." \
280 " This is the third line." \
283 "This is the first line\\." \
284 " Indented second line\\." \
285 "This is the third line\\." \