1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2019-2021 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# It also tests option integration in various commands, including:
24#
25#  - print
26#  - compile print
27#  - backtrace
28#  - frame apply
29#  - faas
30#  - tfaas
31#  - thread apply
32#  - taas
33
34load_lib completion-support.exp
35
36standard_testfile .c
37
38if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
39    return -1
40}
41
42clean_restart
43
44if { ![readline_is_used] } {
45    untested "no tab completion support without readline"
46    return -1
47}
48
49# Check the completion result, as returned by the "maintenance show
50# test-options-completion-result" command.  TEST is used as test name.
51proc check_completion_result {expected test} {
52    gdb_test "maintenance show test-options-completion-result" \
53	"$expected" \
54	"$test: res=$expected"
55}
56
57# Like test_gdb_complete_unique, but the expected output is expected
58# to be the input line.  I.e., the line is already complete.  We're
59# just checking whether GDB recognizes the option and auto-appends a
60# space.
61proc test_completer_recognizes {res input_line} {
62    set expected_re [string_to_regexp $input_line]
63    test_gdb_complete_unique $input_line $expected_re
64    check_completion_result $res $input_line
65}
66
67# Wrapper around test_gdb_complete_multiple that also checks the
68# completion result is RES.
69proc res_test_gdb_complete_multiple {res cmd_prefix completion_word args} {
70    test_gdb_complete_multiple $cmd_prefix $completion_word {*}$args
71    check_completion_result $res "$cmd_prefix$completion_word"
72}
73
74# Wrapper around test_gdb_complete_none that also checks the
75# completion result is RES.
76proc res_test_gdb_complete_none { res input_line } {
77    test_gdb_complete_none $input_line
78    check_completion_result $res "$input_line"
79}
80
81# Wrapper around test_gdb_complete_unique that also checks the
82# completion result is RES.
83proc res_test_gdb_complete_unique { res input_line args} {
84    test_gdb_complete_unique $input_line {*}$args
85    check_completion_result $res "$input_line"
86}
87
88# Make a full command name from VARIANT.  VARIANT is either
89# "require-delimiter", "unknown-is-error" or "unknown-is-operand".
90proc make_cmd {variant} {
91    return "maint test-options $variant"
92}
93
94# Return a string for the expected result of running "maint
95# test-options xxx", with no flag/option set.  OPERAND is the expected
96# operand.
97proc expect_none {operand} {
98    return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -string '' -- $operand"
99}
100
101# Return a string for the expected result of running "maint
102# test-options xxx", with -flag set.  OPERAND is the expected operand.
103proc expect_flag {operand} {
104    return "-flag 1 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -string '' -- $operand"
105}
106
107# Return a string for the expected result of running "maint
108# test-options xxx", with -bool set.  OPERAND is the expected operand.
109proc expect_bool {operand} {
110    return "-flag 0 -xx1 0 -xx2 0 -bool 1 -enum xxx -uint 0 -zuint-unl 0 -string '' -- $operand"
111}
112
113# Return a string for the expected result of running "maint
114# test-options xxx", with one of the integer options set to $VAL.
115# OPTION determines which option to expect set.  OPERAND is the
116# expected operand.
117proc expect_integer {option val operand} {
118    if {$option == "uinteger"} {
119	return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint $val -zuint-unl 0 -string '' -- $operand"
120    } elseif {$option == "zuinteger-unlimited"} {
121	return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl $val -string '' -- $operand"
122    } else {
123	error "unsupported option: $option"
124    }
125}
126
127# Return a string for the expected result of running "maint
128# test-options xxx", with -string set to $STR.  OPERAND is the
129# expected operand.
130proc expect_string {str operand} {
131    # Dequote the string in the expected output.
132    if { ( [string range $str 0 0] == "\""
133	   && [string range $str end end] == "\"")
134	 || ([string range $str 0 0] == "'"
135	     && [string range $str end end] == "'")} {
136	set str [string range $str 1 end-1]
137    }
138    return "-flag 0 -xx1 0 -xx2 0 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -string '$str' -- $operand"
139}
140
141set all_options {
142    "-bool"
143    "-enum"
144    "-flag"
145    "-string"
146    "-uinteger"
147    "-xx1"
148    "-xx2"
149    "-zuinteger-unlimited"
150}
151
152# Basic option-machinery + "print" command integration tests.
153proc_with_prefix test-print {{prefix ""}} {
154    clean_restart
155
156    # Completing "print" with no argument completes on symbols only,
157    # no options are offered.  Since we haven't loaded any symbols,
158    # the match list should be empty.
159    test_gdb_complete_none "${prefix}print "
160
161    # OTOH, completing at "-" should list all options.
162    test_gdb_complete_multiple "${prefix}print " "-" "" {
163	"-address"
164	"-array"
165	"-array-indexes"
166	"-elements"
167	"-max-depth"
168	"-memory-tag-violations"
169	"-null-stop"
170	"-object"
171	"-pretty"
172	"-raw-values"
173	"-repeats"
174	"-static-members"
175	"-symbol"
176	"-union"
177	"-vtbl"
178    }
179
180    global binfile
181    clean_restart $binfile
182
183    if ![runto_main] {
184	fail "cannot run to main"
185	return
186    }
187
188    # Mix options and format.
189    gdb_test "${prefix}print -pretty -- /x 1" " = 0x1"
190
191    # Smoke test that options actually work.
192    gdb_test "${prefix}print -pretty -- g_s" \
193	[multi_line  \
194	     " = {" \
195	     "  a = 1," \
196	     "  b = 2," \
197	     "  c = 3" \
198	     "}"]
199
200    test_gdb_complete_unique \
201	"${prefix}print xxx" \
202	"${prefix}print xxx1"
203    test_gdb_complete_unique \
204	"${prefix}print -- xxx" \
205	"${prefix}print -- xxx1"
206
207    # Error messages when testing with "compile" are different from
208    # the error messages gdb's internal parser throws.  This procedure
209    # hides the difference.  EXPECTED_RE is only considered when not
210    # testing with "compile".
211    proc test_invalid_expression {cmd expected_re} {
212	upvar prefix prefix
213
214	if {$prefix != "compile "} {
215	    gdb_test $cmd $expected_re
216	} else {
217	    # Error messages depend on compiler version, so we just
218	    # look for the last line indicating a failure.
219	    gdb_test $cmd "Compilation failed\\."
220	}
221    }
222
223    # Check that '-XXX' without a "--" is handled as an
224    # expression.
225    gdb_test "${prefix}print -1" " = -1"
226    test_invalid_expression \
227	"${prefix}print --1" \
228	"Left operand of assignment is not an lvalue\\."
229    test_invalid_expression \
230	"${prefix}print -object" \
231	"No symbol \"object\".*"
232
233    # Test printing with options and no expression.
234    set test "${prefix}print -object --"
235    if {$prefix != "compile "} {
236	# Regular "print" repeats the last history value.
237	gdb_test $test " = -1"
238    } else {
239	# "compile print" starts a multiline expression.
240	gdb_test_multiple $test $test {
241	    -re ">$" {
242		gdb_test "-1\nend" " = -1" \
243		    $test
244	    }
245	}
246    }
247
248    # Check that everything after "-- " is treated as an
249    # expression, not confused with an option.
250    test_invalid_expression \
251	"${prefix}print -- -address" \
252	"No symbol.*"
253    gdb_test "${prefix}print -- -1" " = -1"
254    test_invalid_expression \
255	"${prefix}print -- --1" \
256	"Left operand of assignment is not an lvalue\\."
257}
258
259# Basic option-machinery + "backtrace" command integration tests.
260proc_with_prefix test-backtrace {} {
261    clean_restart
262
263    test_gdb_complete_unique "backtrace" "backtrace"
264    test_gdb_complete_none "backtrace "
265
266    gdb_test "backtrace -" "Ambiguous option at: -"
267    gdb_test "backtrace --" "No stack\\."
268    gdb_test "backtrace -- -" "No stack\\."
269
270    test_gdb_complete_multiple "backtrace " "-" "" {
271	"-entry-values"
272	"-frame-arguments"
273	"-frame-info"
274	"-full"
275	"-hide"
276	"-no-filters"
277	"-past-entry"
278	"-past-main"
279	"-raw-frame-arguments"
280    }
281
282    # Test that we complete the qualifiers, if there's any.
283    test_gdb_complete_unique \
284	"backtrace ful" \
285	"backtrace full"
286    test_gdb_complete_unique \
287	"backtrace hid" \
288	"backtrace hide"
289    test_gdb_complete_unique \
290	"backtrace no-fil" \
291	"backtrace no-filters"
292
293    global binfile
294    clean_restart $binfile
295
296    if ![runto_main] {
297	fail "cannot run to main"
298	return
299    }
300
301    # COUNT in "backtrace COUNT" is parsed as an expression.  Check
302    # that we complete expressions.
303
304    test_gdb_complete_unique \
305	"backtrace xxx" \
306	"backtrace xxx1"
307
308    test_gdb_complete_unique \
309	"backtrace -xxx" \
310	"backtrace -xxx1"
311
312    test_gdb_complete_unique \
313	"backtrace 1 + xxx" \
314	"backtrace 1 + xxx1"
315
316    test_gdb_complete_unique \
317	"backtrace (1 + xxx" \
318	"backtrace (1 + xxx1"
319}
320
321# Basic option-machinery + "frame apply" command integration tests.
322proc_with_prefix test-frame-apply {} {
323    test_gdb_complete_unique "frame apply all" "frame apply all"
324
325    gdb_test "frame apply level 0-" \
326	"Please specify a command to apply on the selected frames"
327    test_gdb_complete_none "frame apply level 0-"
328
329    foreach cmd {
330	"frame apply all"
331	"frame apply 1"
332	"frame apply level 0"
333	"faas"
334	"tfaas"
335    } {
336	test_gdb_completion_offers_commands "$cmd "
337
338	# tfaas is silent on command error by design.  This procedure
339	# hides that aspect.  EXPECTED_RE is only considered when not
340	# testing with "faas"/"tfaas".
341	proc test_error_cmd {cmd arg expected_re} {
342	    if {$cmd == "tfaas"} {
343		gdb_test_no_output "$cmd$arg"
344	    } else {
345		gdb_test "$cmd$arg" $expected_re
346	    }
347	}
348	# Same, but for tests where both "faas" and "tfaas" are
349	# expected to be silent.
350	proc test_error_cmd2 {cmd arg expected_re} {
351	    if {$cmd == "tfaas" || $cmd == "faas"} {
352		gdb_test_no_output "$cmd$arg"
353	    } else {
354		gdb_test "$cmd$arg" $expected_re
355	    }
356	}
357
358	test_error_cmd $cmd " -" "Ambiguous option at: -"
359	test_gdb_complete_multiple "$cmd " "-" "" {
360	    "-c"
361	    "-past-entry"
362	    "-past-main"
363	    "-q"
364	    "-s"
365	}
366
367	with_test_prefix "no-trailing-space" {
368	    test_error_cmd $cmd " --" \
369		"Please specify a command to apply on the selected frames"
370	    test_gdb_complete_unique "$cmd --" "$cmd --"
371	}
372
373	with_test_prefix "trailing-space" {
374	    test_error_cmd $cmd " -- " \
375		"Please specify a command to apply on the selected frames"
376	    test_gdb_completion_offers_commands "$cmd -- "
377	}
378
379	# '-' is a valid TUI command.
380	test_error_cmd2 $cmd " -- -" \
381	    "Cannot enable the TUI when output is not a terminal"
382	test_gdb_complete_unique \
383	    "$cmd -- -" \
384	    "$cmd -- -"
385
386	test_error_cmd2 $cmd " -foo" \
387	    "Undefined command: \"-foo\".  Try \"help\"\\."
388	test_gdb_complete_none "$cmd -foo"
389
390	test_gdb_completion_offers_commands "$cmd -s "
391    }
392}
393
394# Basic option-machinery + "thread apply" command integration tests.
395proc_with_prefix test-thread-apply {} {
396
397    test_gdb_complete_unique "thread apply all" "thread apply all"
398    test_gdb_complete_unique "taas" "taas"
399
400    gdb_test "thread apply 1-" \
401	"inverted range"
402    test_gdb_complete_none "frame apply level 1-"
403
404    foreach cmd {
405	"thread apply all"
406	"thread apply 1"
407	"taas"
408    } {
409	test_gdb_completion_offers_commands "$cmd "
410
411	# taas is silent on command error by design.  This procedure
412	# hides the difference.  EXPECTED_RE is only considered when
413	# not testing with "taas".
414	proc test_invalid_cmd {cmd arg expected_re} {
415	    if {$cmd != "taas"} {
416		gdb_test "$cmd$arg" $expected_re
417	    } else {
418		gdb_test_no_output "$cmd$arg"
419	    }
420	}
421
422	gdb_test "$cmd -" "Ambiguous option at: -"
423
424	if {$cmd != "thread apply 1"} {
425	    test_gdb_complete_multiple "$cmd " "-" "" {
426		"-ascending"
427		"-c"
428		"-q"
429		"-s"
430	    }
431	} else {
432	    # "-ascending" only works with "all".
433	    test_gdb_complete_multiple "$cmd " "-" "" {
434		"-c"
435		"-q"
436		"-s"
437	    }
438	}
439
440	if {$cmd == "thread apply all" || $cmd == "taas"} {
441	    set errmsg \
442		"Please specify a command at the end of 'thread apply all'"
443	} elseif {$cmd == "thread apply 1"} {
444	    set errmsg \
445		"Please specify a command following the thread ID list"
446	} else {
447	    error "unexpected cmd: $cmd"
448	}
449
450	with_test_prefix "no-trailing-space" {
451	    gdb_test "$cmd --" $errmsg
452	    test_gdb_complete_unique "$cmd --" "$cmd --"
453	}
454
455	with_test_prefix "trailing-space" {
456	    gdb_test "$cmd -- " $errmsg
457	    test_gdb_completion_offers_commands "$cmd -- "
458	}
459
460	# '-' is a valid TUI command.
461	test_invalid_cmd "$cmd" " -- -" \
462	    "Cannot enable the TUI when output is not a terminal"
463	test_gdb_complete_unique \
464	    "$cmd -- -" \
465	    "$cmd -- -"
466
467	test_invalid_cmd $cmd " -foo" \
468	    "Undefined command: \"-foo\".  Try \"help\"\\."
469	test_gdb_complete_none "$cmd -foo"
470
471	test_gdb_completion_offers_commands "$cmd -c "
472    }
473}
474
475# Basic option-machinery + "info threads" command integration tests.
476proc_with_prefix test-info-threads {} {
477    test_gdb_complete_multiple "info threads " "" "" {
478	"-gid"
479	"ID"
480    }
481
482    test_gdb_complete_unique \
483	"info threads -" \
484	"info threads -gid"
485
486    # "ID" isn't really something the user can type.
487    test_gdb_complete_none "info threads I"
488}
489
490# Miscellaneous tests.
491proc_with_prefix test-misc {variant} {
492    global all_options
493
494    set cmd [make_cmd $variant]
495
496    # Call test command with no arguments at all.
497    gdb_test "$cmd" [expect_none ""]
498
499    # Now with a single dash.
500    if {$variant == "require-delimiter"} {
501	gdb_test "$cmd -" [expect_none "-"]
502    } else {
503	gdb_test "$cmd -" "Ambiguous option at: -"
504    }
505
506    # Completing at "-" should list all options.
507    res_test_gdb_complete_multiple \
508	"1 [expect_none "-"]" \
509	"$cmd " "-" "" $all_options
510
511    # Now with a double dash.
512    gdb_test "$cmd --" [expect_none ""]
513
514    # "--" is recognized by options completer, gdb auto-appends a
515    # space.
516    test_completer_recognizes \
517	"1 [expect_none "--"]" \
518	"$cmd --"
519
520    # Now with a double dash, plus a dash as operand.
521    gdb_test "$cmd -- -" [expect_none "-"]
522    res_test_gdb_complete_none "0 -" "$cmd -- -"
523
524    # Completing an unambiguous option just appends an empty space.
525    test_completer_recognizes \
526	"1 [expect_none "-flag"]" \
527	"$cmd -flag"
528
529    # Try running an ambiguous option.
530    if {$variant == "require-delimiter"} {
531	gdb_test "$cmd -xx" [expect_none "-xx"]
532    } else {
533	gdb_test "$cmd -xx" "Ambiguous option at: -xx"
534    }
535
536    # Check that options are not case insensitive.
537    gdb_test "$cmd -flag --" [expect_flag ""]
538
539    # Check how the different modes behave on unknown option, with a
540    # delimiter.
541    gdb_test "$cmd -FLAG --" \
542	"Unrecognized option at: -FLAG --"
543
544    # Check how the different modes behave on unknown option, without
545    # a delimiter.
546    if {$variant == "unknown-is-error"} {
547	gdb_test "$cmd -FLAG" \
548	    "Unrecognized option at: -FLAG"
549    } else {
550	gdb_test "$cmd -FLAG" [expect_none "-FLAG"]
551    }
552
553    # Test parsing stops at a negative integer.
554    gdb_test "$cmd -1 --" \
555	"Unrecognized option at: -1 --"
556    gdb_test "$cmd -2 --" \
557	"Unrecognized option at: -2 --"
558}
559
560# Flag option tests.
561proc_with_prefix test-flag {variant} {
562    global all_options
563
564    set cmd [make_cmd $variant]
565
566    # Completing a flag just appends a space.
567    test_completer_recognizes \
568	"1 [expect_none "-flag"]" \
569	"$cmd -flag"
570
571    # Add a dash, and all options should be shown.
572    res_test_gdb_complete_multiple \
573	"1 [expect_flag "-"]" \
574	"$cmd  -flag " "-" "" $all_options
575
576    # Basic smoke tests of accepted / not accepted values.
577
578    # Check all the different variants a bool option may be specified.
579    if {$variant == "require-delimiter"} {
580	gdb_test "$cmd -flag 999" [expect_none "-flag 999"]
581    } else {
582	gdb_test "$cmd -flag 999" [expect_flag "999"]
583    }
584    gdb_test "$cmd -flag -- 999" [expect_flag "999"]
585
586    # If the "--" separator is present, then GDB errors out if the
587    # flag option is passed some value -- check that too.
588    gdb_test "$cmd -flag xxx 999 --" "Unrecognized option at: xxx 999 --"
589    gdb_test "$cmd -flag o 999 --" "Unrecognized option at: o 999 --"
590    gdb_test "$cmd -flag 1 999 --" "Unrecognized option at: 1 999 --"
591
592    # Extract twice the same flag, separated by one space.
593    gdb_test "$cmd -flag -flag -- non flags args" \
594	[expect_flag "non flags args"]
595
596    # Extract twice the same flag, separated by one space.
597    gdb_test "$cmd -xx1     -xx2 -xx1  -xx2 -xx1    -- non flags args" \
598	"-flag 0 -xx1 1 -xx2 1 -bool 0 -enum xxx -uint 0 -zuint-unl 0 -string '' -- non flags args"
599
600    # Extract 2 known flags in front of unknown flags.
601    gdb_test "$cmd -xx1 -xx2 -a -b -c -xx1 --" \
602	"Unrecognized option at: -a -b -c -xx1 --"
603
604    # Check that combined flags are not recognised.
605    gdb_test "$cmd -xx1 -xx1xx2 -xx1 --" \
606	"Unrecognized option at: -xx1xx2 -xx1 --"
607
608    # Make sure the completer don't confuse a flag option with a
609    # boolean option.  Specifically, "o" should not complete to
610    # "on/off".
611
612    if {$variant == "require-delimiter"} {
613	res_test_gdb_complete_none \
614	    "1 [expect_flag "o"]" \
615	    "$cmd -flag o"
616
617	gdb_test "$cmd -flag o" [expect_none "-flag o"]
618    } else {
619	res_test_gdb_complete_none "0 o" "$cmd -flag o"
620
621	gdb_test "$cmd -flag o" [expect_flag "o"]
622    }
623}
624
625# Boolean option tests.
626proc_with_prefix test-boolean {variant} {
627    global all_options
628
629    set cmd [make_cmd $variant]
630
631    # Boolean option's values are optional -- "on" is implied.  Check
632    # that:
633    #
634    # - For require-delimiter commands, completing after a boolean
635    #   option lists all other options, plus "on/off".  This is
636    #   because operands won't be processed until we see a "--"
637    #   delimiter.
638    #
639    # - For !require-delimiter commands, completing after a boolean
640    #   option completes as an operand, since that will tend to be
641    #   more common than typing "on/off".
642    #   E.g., "frame apply all -past-main COMMAND".
643
644    if {$variant == "require-delimiter"} {
645	set match_list $all_options
646	lappend match_list "off" "on"
647	res_test_gdb_complete_multiple \
648	    "1 [expect_none ""]" \
649	    "$cmd -bool " "" "" $match_list
650    } else {
651	res_test_gdb_complete_none "0 " "$cmd -bool "
652    }
653
654    # Add another dash, and "on/off" are no longer offered:
655    res_test_gdb_complete_multiple \
656	"1 [expect_bool "-"]" \
657	"$cmd -bool " "-" ""  $all_options
658
659    # Basic smoke tests of accepted / not accepted values.
660
661    # The command accepts all of "1/0/enable/disable/yes/no" too, even
662    # though like the "set" command, we don't offer those as
663    # completion candidates if you complete right after the boolean
664    # command's name, like:
665    #
666    #  (gdb) maint test-options require-delimiter -bool [TAB]
667    #  off        on
668    #
669    # However, the completer does recognize them if you start typing
670    # the boolean value.
671    foreach value {"0" "1"} {
672	test_completer_recognizes \
673	    "1 [expect_none ""]" \
674	    "$cmd -bool $value"
675    }
676    foreach value {"of" "off"} {
677	res_test_gdb_complete_unique \
678	    "1 [expect_none ""]" \
679	    "$cmd -bool $value" \
680	    "$cmd -bool off"
681    }
682    foreach value {"y" "ye" "yes"} {
683	res_test_gdb_complete_unique \
684	    "1 [expect_none ""]" \
685	    "$cmd -bool $value" \
686	    "$cmd -bool yes"
687    }
688    foreach value {"n" "no"} {
689	res_test_gdb_complete_unique \
690	    "1 [expect_none ""]" \
691	    "$cmd -bool $value" \
692	    "$cmd -bool no"
693    }
694    foreach value {
695	"e"
696	"en"
697	"ena"
698	"enab"
699	"enabl"
700	"enable"
701    } {
702	res_test_gdb_complete_unique \
703	    "1 [expect_none ""]" \
704	    "$cmd -bool $value" \
705	    "$cmd -bool enable"
706    }
707    foreach value {
708	"d"
709	"di"
710	"dis"
711	"disa"
712	"disab"
713	"disabl"
714	"disable"
715    } {
716	res_test_gdb_complete_unique \
717	    "1 [expect_none ""]" \
718	    "$cmd -bool $value" \
719	    "$cmd -bool disable"
720    }
721
722    if {$variant == "require-delimiter"} {
723	res_test_gdb_complete_none \
724	    "1 [expect_none "xxx"]" \
725	    "$cmd -bool xxx"
726    } else {
727	res_test_gdb_complete_none "0 xxx" "$cmd -bool xxx"
728    }
729
730    # The command accepts abbreviations of "enable/disable/yes/no",
731    # even though we don't offer those for completion.
732    foreach value {
733	"1"
734	"y" "ye" "yes"
735	"e"
736	"en"
737	"ena"
738	"enab"
739	"enabl"
740	"enable"} {
741	gdb_test "$cmd -bool $value --" [expect_bool ""]
742    }
743    foreach value {
744	"0"
745	"of" "off"
746	"n" "no"
747	"d"
748	"di"
749	"dis"
750	"disa"
751	"disab"
752	"disabl"
753	"disable"} {
754	gdb_test "$cmd -bool $value --" [expect_none ""]
755    }
756
757    if {$variant == "require-delimiter"} {
758	gdb_test "$cmd -bool 999" [expect_none "-bool 999"]
759    } else {
760	gdb_test "$cmd -bool 999" [expect_bool "999"]
761    }
762    gdb_test "$cmd -bool -- 999" [expect_bool "999"]
763
764    # Since "on" is implied after a boolean option, for
765    # !require-delimiter commands, anything that is not
766    # yes/no/1/0/on/off/enable/disable should be considered as the raw
767    # input after the last option.  Also check "o", which might look
768    # like "on" or "off", but it's treated the same.
769
770    foreach arg {"xxx" "o"} {
771	if {$variant == "require-delimiter"} {
772	    gdb_test "$cmd -bool $arg" [expect_none "-bool $arg"]
773	} else {
774	    gdb_test "$cmd -bool $arg" [expect_bool "$arg"]
775	}
776    }
777    # Also try -1.  "unknown-is-error" commands error out saying that
778    # that's not a valid option.
779    if {$variant == "require-delimiter"} {
780	gdb_test "$cmd -bool -1" \
781	     [expect_none "-bool -1"]
782    } elseif {$variant == "unknown-is-error"} {
783	gdb_test "$cmd -bool -1" \
784	    "Unrecognized option at: -1"
785    } else {
786	gdb_test "$cmd -bool -1" [expect_bool "-1"]
787    }
788
789    # OTOH, if the "--" separator is present, then GDB errors out if
790    # the boolean option is passed an invalid value -- check that too.
791    gdb_test "$cmd -bool -1 999 --" \
792	"Unrecognized option at: -1 999 --"
793    gdb_test "$cmd -bool xxx 999 --" \
794	"Value given for `-bool' is not a boolean: xxx"
795    gdb_test "$cmd -bool o 999 --" \
796	"Value given for `-bool' is not a boolean: o"
797
798    # Completing after a boolean option + "o" does list "on/off",
799    # though.
800    if {$variant == "require-delimiter"} {
801	res_test_gdb_complete_multiple \
802	    "1 [expect_none "o"]" \
803	    "$cmd -bool " "o" "" {
804	    "off"
805	    "on"
806	}
807    } else {
808	res_test_gdb_complete_multiple "0 o" "$cmd -bool " "o" "" {
809	    "off"
810	    "on"
811	}
812    }
813}
814
815# Uinteger option tests.  OPTION is which integer option we're
816# testing.  Can be "uinteger" or "zuinteger-unlimited".
817proc_with_prefix test-uinteger {variant option} {
818    global all_options
819
820    set cmd "[make_cmd $variant] -$option"
821
822    # Test completing a uinteger option:
823    res_test_gdb_complete_multiple \
824	"1 [expect_none ""]" \
825	"$cmd " "" "" {
826	"NUMBER"
827	"unlimited"
828    }
829
830    # NUMBER above is just a placeholder, make sure we don't complete
831    # it as a valid option.
832    res_test_gdb_complete_none \
833	"1 [expect_none "NU"]" \
834	"$cmd NU"
835
836    # "unlimited" is valid though.
837    res_test_gdb_complete_unique \
838	"1 [expect_none "u"]" \
839	"$cmd u" \
840	"$cmd unlimited"
841
842    # Basic smoke test of accepted / not accepted values.
843    gdb_test "$cmd 1 -- 999" [expect_integer $option "1" "999"]
844    gdb_test "$cmd unlimited -- 999" \
845	[expect_integer $option "unlimited" "999"]
846    if {$option == "zuinteger-unlimited"} {
847	gdb_test "$cmd -1 --" [expect_integer $option "unlimited" ""]
848	gdb_test "$cmd 0 --" [expect_integer $option "0" ""]
849    } else {
850	gdb_test "$cmd -1 --" "integer -1 out of range"
851	gdb_test "$cmd 0 --" [expect_integer $option "unlimited" ""]
852    }
853    gdb_test "$cmd xxx --" \
854	"Expected integer at: xxx --"
855    gdb_test "$cmd unlimitedx --" \
856	"Expected integer at: unlimitedx --"
857
858    # Don't offer completions until we're past the
859    # -uinteger/-zuinteger-unlimited argument.
860    res_test_gdb_complete_none \
861	"1 [expect_none ""]" \
862	"$cmd 1"
863
864    # A number of invalid values.
865    foreach value {"x" "x " "1a" "1a " "1-" "1- " "unlimitedx"} {
866	res_test_gdb_complete_none \
867	    "1 [expect_none $value]" \
868	    "$cmd $value"
869    }
870
871    # Try "-1".
872    if {$option == "uinteger"} {
873	# -1 is invalid uinteger.
874	foreach value {"-1" "-1 "} {
875	    res_test_gdb_complete_none \
876		"1 [expect_none ""]" \
877		"$cmd $value"
878	}
879    } else {
880	# -1 is valid for zuinteger-unlimited.
881	res_test_gdb_complete_none \
882	    "1 [expect_none ""]" \
883	    "$cmd -1"
884	if {$variant == "require-delimiter"} {
885	    res_test_gdb_complete_multiple \
886		"1 [expect_integer $option "unlimited" ""]" \
887		"$cmd -1 " "" "-" $all_options
888	} else {
889	    res_test_gdb_complete_none "0 " "$cmd -1 "
890	}
891    }
892
893    # Check that after a fully parsed option:
894    #
895    #  - for require-delimiter commands, completion offers all
896    #    options.
897    #
898    #  - for !require-delimiter commands, completion offers nothing
899    #    and returns false.
900    if {$variant == "require-delimiter"} {
901	res_test_gdb_complete_multiple \
902	    "1 [expect_integer $option 1 ""]" \
903	    "$cmd 1 " "" "-" $all_options
904    } else {
905	res_test_gdb_complete_none "0 " "$cmd 1 "
906    }
907
908    # Test completing non-option arguments after "-uinteger 1 ".
909    foreach operand {"x" "x " "1a" "1a " "1-" "1- "} {
910	if {$variant == "require-delimiter"} {
911	    res_test_gdb_complete_none \
912		"1 [expect_integer $option 1 $operand]" \
913		"$cmd 1 $operand"
914	} else {
915	    res_test_gdb_complete_none "0 $operand" "$cmd 1 $operand"
916	}
917    }
918    # These look like options, but they aren't.
919    foreach operand {"-1" "-1 "} {
920	if {$variant == "unknown-is-operand"} {
921	    res_test_gdb_complete_none "0 $operand" "$cmd 1 $operand"
922	} else {
923	    res_test_gdb_complete_none \
924		"1 [expect_integer $option 1 $operand]" \
925		"$cmd 1 $operand"
926	}
927    }
928}
929
930# Enum option tests.
931proc_with_prefix test-enum {variant} {
932    set cmd [make_cmd $variant]
933
934    res_test_gdb_complete_multiple \
935	"1 [expect_none ""]" \
936	"$cmd -enum " "" "" {
937	"xxx"
938	"yyy"
939	"zzz"
940    }
941
942    # Check that "-" where a value is expected does not show the
943    # command's options.  I.e., an enum's value is not optional.
944    # Check both completion and running the command.
945    res_test_gdb_complete_none \
946	"1 [expect_none "-"]" \
947	"$cmd -enum -"
948    gdb_test "$cmd -enum --"\
949	"Requires an argument. Valid arguments are xxx, yyy, zzz\\."
950
951    # Try passing an undefined item to an enum option.
952    gdb_test "$cmd -enum www --" "Undefined item: \"www\"."
953}
954
955# String option tests.
956proc_with_prefix test-string {variant} {
957    global all_options
958
959    set cmd [make_cmd $variant]
960
961    res_test_gdb_complete_none \
962	"1 [expect_none ""]" \
963	"$cmd -string "
964
965    # Check that "-" where a value is expected does not show the
966    # command's options.  I.e., a string's value is not optional.
967    # Check both completion and running the command.
968    res_test_gdb_complete_none \
969	"1 [expect_none ""]" \
970	"$cmd -string -"
971    gdb_test "$cmd -string --"\
972	"-string requires an argument"
973    if {$variant == "require-delimiter"} {
974	gdb_test "$cmd -string" [expect_none "-string"]
975    } else {
976	gdb_test "$cmd -string"\
977	    "-string requires an argument"
978    }
979
980    foreach_with_prefix str {
981	"STR"
982	"\"STR\""
983	"\\\"STR"
984	"'STR'"
985	"\\'STR"
986	"\"STR AAA\""
987	"'STR BBB'"
988	"\"STR 'CCC' DDD\""
989	"'STR \"EEE\" FFF'"
990	"\"STR \\\"GGG\\\" HHH\""
991	"'STR \\\'III\\\' JJJ'"
992    } {
993	res_test_gdb_complete_none \
994	    "1 [expect_none ""]" \
995	    "$cmd -string ${str}"
996	gdb_test "$cmd -string ${str} --" [expect_string "${str}" ""]
997
998	# Completing at "-" after parsing STR should list all options.
999	res_test_gdb_complete_multiple \
1000	    "1 [expect_string "${str}" "-"]" \
1001	    "$cmd -string ${str} " "-" "" $all_options
1002
1003	# Check that only $STR is considered part of the string's value.
1004	# I.e., that we stop parsing the string at the first
1005	# whitespace or after the closing quote of $STR.
1006	if {$variant == "require-delimiter"} {
1007	    res_test_gdb_complete_none \
1008		"1 [expect_string "${str}" "BAR"]" \
1009		"$cmd -string ${str} BAR"
1010	} else {
1011	    res_test_gdb_complete_none "0 BAR" "$cmd -string ${str} BAR"
1012	}
1013	gdb_test "$cmd -string ${str} BAR --" "Unrecognized option at: BAR --"
1014    }
1015}
1016
1017# Run the options framework tests first.
1018foreach_with_prefix cmd {
1019    "require-delimiter"
1020    "unknown-is-error"
1021    "unknown-is-operand"
1022} {
1023    test-misc $cmd
1024    test-flag $cmd
1025    test-boolean $cmd
1026    foreach subcmd {"uinteger" "zuinteger-unlimited" } {
1027	test-uinteger $cmd $subcmd
1028    }
1029    test-enum $cmd
1030    test-string $cmd
1031}
1032
1033# Run the print integration tests, both as "standalone", and under
1034# "frame/thread apply".  The latter checks that the "frame/thread
1035# apply ... COMMAND" commands recurse the completion machinery for
1036# COMMAND completion correctly.
1037foreach prefix {
1038    ""
1039    "frame apply all "
1040    "frame apply 1 "
1041    "frame apply level 0 "
1042    "thread apply all "
1043    "thread apply 1 "
1044    "thread apply 1 frame apply 1 "
1045} {
1046    test-print $prefix
1047}
1048
1049# Same for "compile print".  Not really a wrapper prefix command like
1050# "frame apply", but similar enough that we test pretty much the same
1051# things.
1052if ![skip_compile_feature_tests] {
1053    test-print "compile "
1054}
1055
1056# Basic "backtrace" integration tests.
1057test-backtrace
1058
1059# Basic "frame apply" integration tests.
1060test-frame-apply
1061
1062# Basic "thread apply" integration tests.
1063test-thread-apply
1064
1065# Basic "info threads" integration tests.
1066test-info-threads
1067