1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2019 Free Software Foundation, Inc.
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.
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.
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/>.
18 # Test the gdb::option framework.
20 # The test uses the "maintenance test-options" subcommands to exercise
21 # TAB-completion and option processing.
23 load_lib completion-support.exp
27 if { ![readline_is_used] } {
28 untested "no tab completion support without readline"
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" \
37 "$test: res=$expected"
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
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
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"
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"
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"
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"
77 # Return a string for the expected result of running "maint
78 # test-options xxx", with no flag/option set. OPERAND is the expected
80 proc expect_none {operand} {
81 return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -- $operand"
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"
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"
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
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"
106 error "unsupported option: $option"
117 "-zuinteger-unlimited"
120 # Miscellaneous tests.
121 proc_with_prefix test-misc {variant} {
124 set cmd [make_cmd $variant]
126 # Call test command with no arguments at all.
127 gdb_test "$cmd" [expect_none ""]
129 # Now with a single dash.
130 if {$variant == "require-delimiter"} {
131 gdb_test "$cmd -" [expect_none "-"]
133 gdb_test "$cmd -" "Ambiguous option at: -"
136 # Completing at "-" should list all options.
137 res_test_gdb_complete_multiple "1" "$cmd " "-" "" $all_options
139 # Now with a double dash.
140 gdb_test "$cmd --" [expect_none ""]
142 # "--" is recognized by options completer, gdb auto-appends a
144 test_completer_recognizes 1 "$cmd --"
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 -- -"
150 # Completing an unambiguous option just appends an empty space.
151 test_completer_recognizes 1 "$cmd -flag"
153 # Try running an ambiguous option.
154 if {$variant == "require-delimiter"} {
155 gdb_test "$cmd -xx" [expect_none "-xx"]
157 gdb_test "$cmd -xx" "Ambiguous option at: -xx"
160 # Check that options are not case insensitive.
161 gdb_test "$cmd -flag --" [expect_flag ""]
163 # Check how the different modes behave on unknown option, with a
165 gdb_test "$cmd -FLAG --" \
166 "Unrecognized option at: -FLAG --"
168 # Check how the different modes behave on unknown option, without
170 if {$variant == "unknown-is-error"} {
171 gdb_test "$cmd -FLAG" \
172 "Unrecognized option at: -FLAG"
174 gdb_test "$cmd -FLAG" [expect_none "-FLAG"]
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 --"
185 proc_with_prefix test-flag {variant} {
188 set cmd [make_cmd $variant]
190 # Completing a flag just appends a space.
191 test_completer_recognizes 1 "$cmd -flag"
193 # Add a dash, and all options should be shown.
194 test_gdb_complete_multiple "$cmd -flag " "-" "" $all_options
196 # Basic smoke tests of accepted / not accepted values.
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"]
202 gdb_test "$cmd -flag 999" [expect_flag "999"]
204 gdb_test "$cmd -flag -- 999" [expect_flag "999"]
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 --"
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"]
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"
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 --"
224 # Check that combined flags are not recognised.
225 gdb_test "$cmd -xx1 -xx1xx2 -xx1 --" \
226 "Unrecognized option at: -xx1xx2 -xx1 --"
228 # Make sure the completer don't confuse a flag option with a
229 # boolean option. Specifically, "o" should not complete to
232 if {$variant == "require-delimiter"} {
233 res_test_gdb_complete_none "1" "$cmd -flag o"
235 gdb_test "$cmd -flag o" [expect_none "-flag o"]
237 res_test_gdb_complete_none "0 o" "$cmd -flag o"
239 gdb_test "$cmd -flag o" [expect_flag "o"]
243 # Boolean option tests.
244 proc_with_prefix test-boolean {variant} {
247 set cmd [make_cmd $variant]
249 # Boolean option's values are optional -- "on" is implied. Check
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 "--"
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".
262 if {$variant == "require-delimiter"} {
263 res_test_gdb_complete_multiple 1 "$cmd -bool " "" "" {
270 "-zuinteger-unlimited"
275 res_test_gdb_complete_none "0 " "$cmd -bool "
278 # Add another dash, and "on/off" are no longer offered:
279 res_test_gdb_complete_multiple 1 "$cmd -bool " "-" "" $all_options
281 # Basic smoke tests of accepted / not accepted values.
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:
288 # (gdb) maint test-options require-delimiter -bool [TAB]
291 # However, the completer does recognize them if you start typing
293 foreach value {"0" "1"} {
294 test_completer_recognizes 1 "$cmd -bool $value"
296 foreach value {"of" "off"} {
297 res_test_gdb_complete_unique 1 \
298 "$cmd -bool $value" \
301 foreach value {"y" "ye" "yes"} {
302 res_test_gdb_complete_unique 1 \
303 "$cmd -bool $value" \
306 foreach value {"n" "no"} {
307 res_test_gdb_complete_unique 1 \
308 "$cmd -bool $value" \
319 res_test_gdb_complete_unique 1 \
320 "$cmd -bool $value" \
332 res_test_gdb_complete_unique 1 \
333 "$cmd -bool $value" \
337 if {$variant == "require-delimiter"} {
338 res_test_gdb_complete_none "1" "$cmd -bool xxx"
340 res_test_gdb_complete_none "0 xxx" "$cmd -bool xxx"
343 # The command accepts abbreviations of "enable/disable/yes/no",
344 # even though we don't offer those for completion.
354 gdb_test "$cmd -bool $value --" [expect_bool ""]
367 gdb_test "$cmd -bool $value --" [expect_none ""]
370 if {$variant == "require-delimiter"} {
371 gdb_test "$cmd -bool 999" [expect_none "-bool 999"]
373 gdb_test "$cmd -bool 999" [expect_bool "999"]
375 gdb_test "$cmd -bool -- 999" [expect_bool "999"]
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.
383 foreach arg {"xxx" "o"} {
384 if {$variant == "require-delimiter"} {
385 gdb_test "$cmd -bool $arg" [expect_none "-bool $arg"]
387 gdb_test "$cmd -bool $arg" [expect_bool "$arg"]
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"
399 gdb_test "$cmd -bool -1" [expect_bool "-1"]
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"
411 # Completing after a boolean option + "o" does list "on/off",
413 if {$variant == "require-delimiter"} {
414 res_test_gdb_complete_multiple 1 "$cmd -bool " "o" "" {
419 res_test_gdb_complete_multiple "0 o" "$cmd -bool " "o" "" {
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} {
431 set cmd "[make_cmd $variant] -$option"
433 # Test completing a uinteger option:
434 res_test_gdb_complete_multiple 1 "$cmd " "" "" {
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"
443 # "unlimited" is valid though.
444 res_test_gdb_complete_unique 1 \
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" ""]
456 gdb_test "$cmd -1 --" "integer -1 out of range"
457 gdb_test "$cmd 0 --" [expect_integer $option "unlimited" ""]
459 gdb_test "$cmd xxx --" \
460 "Expected integer at: xxx --"
461 gdb_test "$cmd unlimitedx --" \
462 "Expected integer at: unlimitedx --"
464 # Don't offer completions until we're past the
465 # -uinteger/-zuinteger-unlimited argument.
466 res_test_gdb_complete_none 1 "$cmd 1"
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"
474 if {$option == "uinteger"} {
475 # -1 is invalid uinteger.
476 foreach value {"-1" "-1 "} {
477 res_test_gdb_complete_none 1 "$cmd $value"
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
485 res_test_gdb_complete_none "0 " "$cmd -1 "
489 # Check that after a fully parsed option:
491 # - for require-delimiter commands, completion offers all
494 # - for !require-delimiter commands, completion offers nothing
496 if {$variant == "require-delimiter"} {
497 res_test_gdb_complete_multiple 1 "$cmd 1 " "" "-" $all_options
499 res_test_gdb_complete_none "0 " "$cmd 1 "
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"
507 res_test_gdb_complete_none "0 $operand" "$cmd 1 $operand"
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"
515 res_test_gdb_complete_none 1 "$cmd 1 $operand"
521 proc_with_prefix test-enum {variant} {
522 set cmd [make_cmd $variant]
524 res_test_gdb_complete_multiple 1 "$cmd -enum " "" "" {
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\\."
537 # Try passing an undefined item to an enum option.
538 gdb_test "$cmd -enum www --" "Undefined item: \"www\"."
541 # Run the options framework tests first.
542 foreach_with_prefix cmd {
550 foreach subcmd {"uinteger" "zuinteger-unlimited" } {
551 test-uinteger $cmd $subcmd