Introduce generic command options framework
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / options.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2019 Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 # Test the gdb::option framework.
19
20 # The test uses the "maintenance test-options" subcommands to exercise
21 # TAB-completion and option processing.
22
23 load_lib completion-support.exp
24
25 clean_restart
26
27 if { ![readline_is_used] } {
28 untested "no tab completion support without readline"
29 return -1
30 }
31
32 # Check the completion result, as returned by the "maintenance show
33 # test-options-completion-result" command. TEST is used as test name.
34 proc check_completion_result {expected test} {
35 gdb_test "maintenance show test-options-completion-result" \
36 "$expected" \
37 "$test: res=$expected"
38 }
39
40 # Like test_gdb_complete_unique, but the expected output is expected
41 # to be the input line. I.e., the line is already complete. We're
42 # just checking whether GDB recognizes the option and auto-appends a
43 # space.
44 proc test_completer_recognizes {res input_line} {
45 set expected_re [string_to_regexp $input_line]
46 test_gdb_complete_unique $input_line $expected_re
47 check_completion_result $res $input_line
48 }
49
50 # Wrapper around test_gdb_complete_multiple that also checks the
51 # completion result is RES.
52 proc res_test_gdb_complete_multiple {res cmd_prefix completion_word args} {
53 test_gdb_complete_multiple $cmd_prefix $completion_word {*}$args
54 check_completion_result $res "$cmd_prefix$completion_word"
55 }
56
57 # Wrapper around test_gdb_complete_none that also checks the
58 # completion result is RES.
59 proc res_test_gdb_complete_none { res input_line } {
60 test_gdb_complete_none $input_line
61 check_completion_result $res "$input_line"
62 }
63
64 # Wrapper around test_gdb_complete_unique that also checks the
65 # completion result is RES.
66 proc res_test_gdb_complete_unique { res input_line args} {
67 test_gdb_complete_unique $input_line {*}$args
68 check_completion_result $res "$input_line"
69 }
70
71 # Make a full command name from VARIANT. VARIANT is either
72 # "require-delimiter", "unknown-is-error" or "unknown-is-operand".
73 proc make_cmd {variant} {
74 return "maint test-options $variant"
75 }
76
77 # Return a string for the expected result of running "maint
78 # test-options xxx", with no flag/option set. OPERAND is the expected
79 # operand.
80 proc expect_none {operand} {
81 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
82 }
83
84 # Return a string for the expected result of running "maint
85 # test-options xxx", with -flag set. OPERAND is the expected operand.
86 proc expect_flag {operand} {
87 return "-flag 1 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
88 }
89
90 # Return a string for the expected result of running "maint
91 # test-options xxx", with -bool set. OPERAND is the expected operand.
92 proc expect_bool {operand} {
93 return "-flag 0 -xx1 0 -xx2 0 -bool 1 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
94 }
95
96 # Return a string for the expected result of running "maint
97 # test-options xxx", with one of the integer options set to $VAL.
98 # OPTION determines which option to expect set. OPERAND is the
99 # expected operand.
100 proc expect_integer {option val operand} {
101 if {$option == "uinteger"} {
102 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint $val -zuint-unl 0 -- $operand"
103 } elseif {$option == "zuinteger-unlimited"} {
104 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl $val -- $operand"
105 } else {
106 error "unsupported option: $option"
107 }
108 }
109
110 set all_options {
111 "-bool"
112 "-enum"
113 "-flag"
114 "-uinteger"
115 "-xx1"
116 "-xx2"
117 "-zuinteger-unlimited"
118 }
119
120 # Miscellaneous tests.
121 proc_with_prefix test-misc {variant} {
122 global all_options
123
124 set cmd [make_cmd $variant]
125
126 # Call test command with no arguments at all.
127 gdb_test "$cmd" [expect_none ""]
128
129 # Now with a single dash.
130 if {$variant == "require-delimiter"} {
131 gdb_test "$cmd -" [expect_none "-"]
132 } else {
133 gdb_test "$cmd -" "Ambiguous option at: -"
134 }
135
136 # Completing at "-" should list all options.
137 res_test_gdb_complete_multiple "1" "$cmd " "-" "" $all_options
138
139 # Now with a double dash.
140 gdb_test "$cmd --" [expect_none ""]
141
142 # "--" is recognized by options completer, gdb auto-appends a
143 # space.
144 test_completer_recognizes 1 "$cmd --"
145
146 # Now with a double dash, plus a dash as operand.
147 gdb_test "$cmd -- -" [expect_none "-"]
148 res_test_gdb_complete_none "0 -" "$cmd -- -"
149
150 # Completing an unambiguous option just appends an empty space.
151 test_completer_recognizes 1 "$cmd -flag"
152
153 # Try running an ambiguous option.
154 if {$variant == "require-delimiter"} {
155 gdb_test "$cmd -xx" [expect_none "-xx"]
156 } else {
157 gdb_test "$cmd -xx" "Ambiguous option at: -xx"
158 }
159
160 # Check that options are not case insensitive.
161 gdb_test "$cmd -flag --" [expect_flag ""]
162
163 # Check how the different modes behave on unknown option, with a
164 # delimiter.
165 gdb_test "$cmd -FLAG --" \
166 "Unrecognized option at: -FLAG --"
167
168 # Check how the different modes behave on unknown option, without
169 # a delimiter.
170 if {$variant == "unknown-is-error"} {
171 gdb_test "$cmd -FLAG" \
172 "Unrecognized option at: -FLAG"
173 } else {
174 gdb_test "$cmd -FLAG" [expect_none "-FLAG"]
175 }
176
177 # Test parsing stops at a negative integer.
178 gdb_test "$cmd -1 --" \
179 "Unrecognized option at: -1 --"
180 gdb_test "$cmd -2 --" \
181 "Unrecognized option at: -2 --"
182 }
183
184 # Flag option tests.
185 proc_with_prefix test-flag {variant} {
186 global all_options
187
188 set cmd [make_cmd $variant]
189
190 # Completing a flag just appends a space.
191 test_completer_recognizes 1 "$cmd -flag"
192
193 # Add a dash, and all options should be shown.
194 test_gdb_complete_multiple "$cmd -flag " "-" "" $all_options
195
196 # Basic smoke tests of accepted / not accepted values.
197
198 # Check all the different variants a bool option may be specified.
199 if {$variant == "require-delimiter"} {
200 gdb_test "$cmd -flag 999" [expect_none "-flag 999"]
201 } else {
202 gdb_test "$cmd -flag 999" [expect_flag "999"]
203 }
204 gdb_test "$cmd -flag -- 999" [expect_flag "999"]
205
206 # If the "--" separator is present, then GDB errors out if the
207 # flag option is passed some value -- check that too.
208 gdb_test "$cmd -flag xxx 999 --" "Unrecognized option at: xxx 999 --"
209 gdb_test "$cmd -flag o 999 --" "Unrecognized option at: o 999 --"
210 gdb_test "$cmd -flag 1 999 --" "Unrecognized option at: 1 999 --"
211
212 # Extract twice the same flag, separated by one space.
213 gdb_test "$cmd -flag -flag -- non flags args" \
214 [expect_flag "non flags args"]
215
216 # Extract twice the same flag, separated by one space.
217 gdb_test "$cmd -xx1 -xx2 -xx1 -xx2 -xx1 -- non flags args" \
218 "-flag 0 -xx1 1 -xx2 1 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- non flags args"
219
220 # Extract 2 known flags in front of unknown flags.
221 gdb_test "$cmd -xx1 -xx2 -a -b -c -xx1 --" \
222 "Unrecognized option at: -a -b -c -xx1 --"
223
224 # Check that combined flags are not recognised.
225 gdb_test "$cmd -xx1 -xx1xx2 -xx1 --" \
226 "Unrecognized option at: -xx1xx2 -xx1 --"
227
228 # Make sure the completer don't confuse a flag option with a
229 # boolean option. Specifically, "o" should not complete to
230 # "on/off".
231
232 if {$variant == "require-delimiter"} {
233 res_test_gdb_complete_none "1" "$cmd -flag o"
234
235 gdb_test "$cmd -flag o" [expect_none "-flag o"]
236 } else {
237 res_test_gdb_complete_none "0 o" "$cmd -flag o"
238
239 gdb_test "$cmd -flag o" [expect_flag "o"]
240 }
241 }
242
243 # Boolean option tests.
244 proc_with_prefix test-boolean {variant} {
245 global all_options
246
247 set cmd [make_cmd $variant]
248
249 # Boolean option's values are optional -- "on" is implied. Check
250 # that:
251 #
252 # - For require-delimiter commands, completing after a boolean
253 # option lists all other options, plus "on/off". This is
254 # because operands won't be processed until we see a "--"
255 # delimiter.
256 #
257 # - For !require-delimiter commands, completing after a boolean
258 # option completes as an operand, since that will tend to be
259 # more common than typing "on/off".
260 # E.g., "frame apply all -past-main COMMAND".
261
262 if {$variant == "require-delimiter"} {
263 res_test_gdb_complete_multiple 1 "$cmd -bool " "" "" {
264 "-bool"
265 "-enum"
266 "-flag"
267 "-uinteger"
268 "-xx1"
269 "-xx2"
270 "-zuinteger-unlimited"
271 "off"
272 "on"
273 }
274 } else {
275 res_test_gdb_complete_none "0 " "$cmd -bool "
276 }
277
278 # Add another dash, and "on/off" are no longer offered:
279 res_test_gdb_complete_multiple 1 "$cmd -bool " "-" "" $all_options
280
281 # Basic smoke tests of accepted / not accepted values.
282
283 # The command accepts all of "1/0/enable/disable/yes/no" too, even
284 # though like the "set" command, we don't offer those as
285 # completion candidates if you complete right after the boolean
286 # command's name, like:
287 #
288 # (gdb) maint test-options require-delimiter -bool [TAB]
289 # off on
290 #
291 # However, the completer does recognize them if you start typing
292 # the boolean value.
293 foreach value {"0" "1"} {
294 test_completer_recognizes 1 "$cmd -bool $value"
295 }
296 foreach value {"of" "off"} {
297 res_test_gdb_complete_unique 1 \
298 "$cmd -bool $value" \
299 "$cmd -bool off"
300 }
301 foreach value {"y" "ye" "yes"} {
302 res_test_gdb_complete_unique 1 \
303 "$cmd -bool $value" \
304 "$cmd -bool yes"
305 }
306 foreach value {"n" "no"} {
307 res_test_gdb_complete_unique 1 \
308 "$cmd -bool $value" \
309 "$cmd -bool no"
310 }
311 foreach value {
312 "e"
313 "en"
314 "ena"
315 "enab"
316 "enabl"
317 "enable"
318 } {
319 res_test_gdb_complete_unique 1 \
320 "$cmd -bool $value" \
321 "$cmd -bool enable"
322 }
323 foreach value {
324 "d"
325 "di"
326 "dis"
327 "disa"
328 "disab"
329 "disabl"
330 "disable"
331 } {
332 res_test_gdb_complete_unique 1 \
333 "$cmd -bool $value" \
334 "$cmd -bool disable"
335 }
336
337 if {$variant == "require-delimiter"} {
338 res_test_gdb_complete_none "1" "$cmd -bool xxx"
339 } else {
340 res_test_gdb_complete_none "0 xxx" "$cmd -bool xxx"
341 }
342
343 # The command accepts abbreviations of "enable/disable/yes/no",
344 # even though we don't offer those for completion.
345 foreach value {
346 "1"
347 "y" "ye" "yes"
348 "e"
349 "en"
350 "ena"
351 "enab"
352 "enabl"
353 "enable"} {
354 gdb_test "$cmd -bool $value --" [expect_bool ""]
355 }
356 foreach value {
357 "0"
358 "of" "off"
359 "n" "no"
360 "d"
361 "di"
362 "dis"
363 "disa"
364 "disab"
365 "disabl"
366 "disable"} {
367 gdb_test "$cmd -bool $value --" [expect_none ""]
368 }
369
370 if {$variant == "require-delimiter"} {
371 gdb_test "$cmd -bool 999" [expect_none "-bool 999"]
372 } else {
373 gdb_test "$cmd -bool 999" [expect_bool "999"]
374 }
375 gdb_test "$cmd -bool -- 999" [expect_bool "999"]
376
377 # Since "on" is implied after a boolean option, for
378 # !require-delimiter commands, anything that is not
379 # yes/no/1/0/on/off/enable/disable should be considered as the raw
380 # input after the last option. Also check "o", which might look
381 # like "on" or "off", but it's treated the same.
382
383 foreach arg {"xxx" "o"} {
384 if {$variant == "require-delimiter"} {
385 gdb_test "$cmd -bool $arg" [expect_none "-bool $arg"]
386 } else {
387 gdb_test "$cmd -bool $arg" [expect_bool "$arg"]
388 }
389 }
390 # Also try -1. "unknown-is-error" commands error out saying that
391 # that's not a valid option.
392 if {$variant == "require-delimiter"} {
393 gdb_test "$cmd -bool -1" \
394 [expect_none "-bool -1"]
395 } elseif {$variant == "unknown-is-error"} {
396 gdb_test "$cmd -bool -1" \
397 "Unrecognized option at: -1"
398 } else {
399 gdb_test "$cmd -bool -1" [expect_bool "-1"]
400 }
401
402 # OTOH, if the "--" separator is present, then GDB errors out if
403 # the boolean option is passed an invalid value -- check that too.
404 gdb_test "$cmd -bool -1 999 --" \
405 "Unrecognized option at: -1 999 --"
406 gdb_test "$cmd -bool xxx 999 --" \
407 "Value given for `-bool' is not a boolean: xxx"
408 gdb_test "$cmd -bool o 999 --" \
409 "Value given for `-bool' is not a boolean: o"
410
411 # Completing after a boolean option + "o" does list "on/off",
412 # though.
413 if {$variant == "require-delimiter"} {
414 res_test_gdb_complete_multiple 1 "$cmd -bool " "o" "" {
415 "off"
416 "on"
417 }
418 } else {
419 res_test_gdb_complete_multiple "0 o" "$cmd -bool " "o" "" {
420 "off"
421 "on"
422 }
423 }
424 }
425
426 # Uinteger option tests. OPTION is which integer option we're
427 # testing. Can be "uinteger" or "zuinteger-unlimited".
428 proc_with_prefix test-uinteger {variant option} {
429 global all_options
430
431 set cmd "[make_cmd $variant] -$option"
432
433 # Test completing a uinteger option:
434 res_test_gdb_complete_multiple 1 "$cmd " "" "" {
435 "NUMBER"
436 "unlimited"
437 }
438
439 # NUMBER above is just a placeholder, make sure we don't complete
440 # it as a valid option.
441 res_test_gdb_complete_none 1 "$cmd NU"
442
443 # "unlimited" is valid though.
444 res_test_gdb_complete_unique 1 \
445 "$cmd u" \
446 "$cmd unlimited"
447
448 # Basic smoke test of accepted / not accepted values.
449 gdb_test "$cmd 1 -- 999" [expect_integer $option "1" "999"]
450 gdb_test "$cmd unlimited -- 999" \
451 [expect_integer $option "unlimited" "999"]
452 if {$option == "zuinteger-unlimited"} {
453 gdb_test "$cmd -1 --" [expect_integer $option "unlimited" ""]
454 gdb_test "$cmd 0 --" [expect_integer $option "0" ""]
455 } else {
456 gdb_test "$cmd -1 --" "integer -1 out of range"
457 gdb_test "$cmd 0 --" [expect_integer $option "unlimited" ""]
458 }
459 gdb_test "$cmd xxx --" \
460 "Expected integer at: xxx --"
461 gdb_test "$cmd unlimitedx --" \
462 "Expected integer at: unlimitedx --"
463
464 # Don't offer completions until we're past the
465 # -uinteger/-zuinteger-unlimited argument.
466 res_test_gdb_complete_none 1 "$cmd 1"
467
468 # A number of invalid values.
469 foreach value {"x" "x " "1a" "1a " "1-" "1- " "unlimitedx"} {
470 res_test_gdb_complete_none 1 "$cmd $value"
471 }
472
473 # Try "-1".
474 if {$option == "uinteger"} {
475 # -1 is invalid uinteger.
476 foreach value {"-1" "-1 "} {
477 res_test_gdb_complete_none 1 "$cmd $value"
478 }
479 } else {
480 # -1 is valid for zuinteger-unlimited.
481 res_test_gdb_complete_none 1 "$cmd -1"
482 if {$variant == "require-delimiter"} {
483 res_test_gdb_complete_multiple 1 "$cmd -1 " "" "-" $all_options
484 } else {
485 res_test_gdb_complete_none "0 " "$cmd -1 "
486 }
487 }
488
489 # Check that after a fully parsed option:
490 #
491 # - for require-delimiter commands, completion offers all
492 # options.
493 #
494 # - for !require-delimiter commands, completion offers nothing
495 # and returns false.
496 if {$variant == "require-delimiter"} {
497 res_test_gdb_complete_multiple 1 "$cmd 1 " "" "-" $all_options
498 } else {
499 res_test_gdb_complete_none "0 " "$cmd 1 "
500 }
501
502 # Test completing non-option arguments after "-uinteger 1 ".
503 foreach operand {"x" "x " "1a" "1a " "1-" "1- "} {
504 if {$variant == "require-delimiter"} {
505 res_test_gdb_complete_none 1 "$cmd 1 $operand"
506 } else {
507 res_test_gdb_complete_none "0 $operand" "$cmd 1 $operand"
508 }
509 }
510 # These look like options, but they aren't.
511 foreach operand {"-1" "-1 "} {
512 if {$variant == "unknown-is-operand"} {
513 res_test_gdb_complete_none "0 $operand" "$cmd 1 $operand"
514 } else {
515 res_test_gdb_complete_none 1 "$cmd 1 $operand"
516 }
517 }
518 }
519
520 # Enum option tests.
521 proc_with_prefix test-enum {variant} {
522 set cmd [make_cmd $variant]
523
524 res_test_gdb_complete_multiple 1 "$cmd -enum " "" "" {
525 "xxx"
526 "yyy"
527 "zzz"
528 }
529
530 # Check that "-" where a value is expected does not show the
531 # command's options. I.e., an enum's value is not optional.
532 # Check both completion and running the command.
533 res_test_gdb_complete_none 1 "$cmd -enum -"
534 gdb_test "$cmd -enum --"\
535 "Requires an argument. Valid arguments are xxx, yyy, zzz\\."
536
537 # Try passing an undefined item to an enum option.
538 gdb_test "$cmd -enum www --" "Undefined item: \"www\"."
539 }
540
541 # Run the options framework tests first.
542 foreach_with_prefix cmd {
543 "require-delimiter"
544 "unknown-is-error"
545 "unknown-is-operand"
546 } {
547 test-misc $cmd
548 test-flag $cmd
549 test-boolean $cmd
550 foreach subcmd {"uinteger" "zuinteger-unlimited" } {
551 test-uinteger $cmd $subcmd
552 }
553 test-enum $cmd
554 }
This page took 0.042965 seconds and 5 git commands to generate.