[1/2] gdb/testsuite: add test with regex for multiple completion patterns

Message ID 20230117130007.1686917-2-blarsen@redhat.com
State New
Headers
Series Fix testing gdb.linespec/cp-completion-aliases with |

Commit Message

Guinevere Larsen Jan. 17, 2023, 1 p.m. UTC
  Currently there is no way to test tab completion on GDB using regexes if
you expect multiple suggestions. This commit adds a proc for that,
test_gdb_complete_multiple_re, which does just that.

To achieve that, test_gdb_complete_cmd_multiple_re and
test_gdb_complete_tab_multiple_re are introduced, following a similar
logic to the unique tests, which already have an _re version.
---
 gdb/testsuite/lib/completion-support.exp | 73 +++++++++++++++++++-----
 1 file changed, 60 insertions(+), 13 deletions(-)
  

Patch

diff --git a/gdb/testsuite/lib/completion-support.exp b/gdb/testsuite/lib/completion-support.exp
index bf9c5ad352c..babe802e9a1 100644
--- a/gdb/testsuite/lib/completion-support.exp
+++ b/gdb/testsuite/lib/completion-support.exp
@@ -47,7 +47,7 @@  proc make_tab_completion_list_re { completion_list } {
 
     set completion_list_re ""
     foreach c $completion_list {
-	append completion_list_re [string_to_regexp $c]
+	append completion_list_re $c
 	append completion_list_re $ws
     }
     append completion_list_re $ws
@@ -58,13 +58,18 @@  proc make_tab_completion_list_re { completion_list } {
 # Make a regular expression that matches a "complete" command
 # completion list.  CMD_PREFIX is the command prefix added to each
 # completion match.
+# COMPLETION_LIST is expected to already be regexp.  This is done so
+# we can have regex options in the completion list, to account for
+# differences in compiler results.
 
 proc make_cmd_completion_list_re { cmd_prefix completion_list start_quote_char end_quote_char } {
 
     set completion_list_re ""
     foreach c $completion_list {
 	# The command prefix is included in all completion matches.
-	append completion_list_re [string_to_regexp $cmd_prefix$start_quote_char$c$end_quote_char]
+	append completion_list_re [string_to_regexp $cmd_prefix$start_quote_char]
+	append completion_list_re $c
+	append completion_list_re [string_to_regexp $end_quote_char]
 	append completion_list_re "\r\n"
     }
 
@@ -124,18 +129,18 @@  proc test_gdb_complete_tab_unique { input_line complete_line_re append_char_re }
 }
 
 # Test that completing INPUT_LINE with TAB completes to "INPUT_LINE +
-# ADD_COMPLETED_LINE" and that it displays the completion matches in
+# ADD_COMPLETED_LINE_RE" and that it displays the completion matches in
 # COMPLETION_LIST.  If MAX_COMPLETIONS then we expect the completion
 # to hit the max-completions limit.
+# ADD_COMPLETED_LINE_RE and EXPECTED_RE must be regular expressions,
+# while INPUT_LINE is does is not.
 
-proc test_gdb_complete_tab_multiple { input_line add_completed_line \
-					  completion_list {max_completions 0}} {
+proc test_gdb_complete_tab_multiple_re { input_line add_completed_line_re \
+					  completion_list_re {max_completions 0}} {
     global gdb_prompt
 
     set input_line_re [string_to_regexp $input_line]
-    set add_completed_line_re [string_to_regexp $add_completed_line]
-
-    set expected_re [make_tab_completion_list_re $completion_list]
+    set expected_re [make_tab_completion_list_re $completion_list_re]
 
     if {$max_completions} {
 	append expected_re "\r\n"
@@ -150,14 +155,14 @@  proc test_gdb_complete_tab_multiple { input_line add_completed_line \
 	    send_gdb "\t"
 	    # If we auto-completed to an ambiguous prefix, we need an
 	    # extra tab to show the matches list.
-	    if {$add_completed_line != ""} {
+	    if {$add_completed_line_re != ""} {
 		send_gdb "\t"
 		set maybe_bell ${completion::bell_re}
 	    } else {
 		set maybe_bell ""
 	    }
 	    gdb_test_multiple "" "$test (second tab)" {
-		-re "^${maybe_bell}\r\n$expected_re\r\n$gdb_prompt " {
+		-re "^$maybe_bell\r\n$expected_re\r\n$gdb_prompt " {
 		    gdb_test_multiple "" "$test (second tab)" {
 			-re "^$input_line_re$add_completed_line_re$" {
 			    pass "$test"
@@ -171,6 +176,21 @@  proc test_gdb_complete_tab_multiple { input_line add_completed_line \
     clear_input_line $test
 }
 
+# Simplified call test completeing an INPUT_LINE using TAB.  This just
+# turns the given COMPLETION_LIST into regexps and calls the proc
+# test_gdb_complete_tab_multiple_re
+
+proc test_gdb_complete_tab_multiple { input_line add_completed_line \
+					  completion_list {max_completions 0}} {
+    set add_completed_line_re [string_to_regexp $add_completed_line]
+    set completion_list_re ""
+    foreach c $completion_list {
+	lappend completion_list_re [string_to_regexp $c]
+    }
+
+    test_gdb_complete_tab_multiple_re $input_line $add_completed_line_re \
+	$completion_list_re $max_completions
+}
 # Test that completing LINE with the complete command completes to
 # nothing.
 
@@ -195,15 +215,16 @@  proc test_gdb_complete_cmd_unique { input_line complete_line_re } {
 }
 
 # Test that completing "CMD_PREFIX + COMPLETION_WORD" with the
-# complete command displays the COMPLETION_LIST completion list.  Each
+# complete command displays the COMPLETION_LIST_RE completion list.  Each
 # entry in the list should be prefixed by CMD_PREFIX.  If
 # MAX_COMPLETIONS then we expect the completion to hit the
 # max-completions limit.
+# COMPLETION_LIST_RE must already be valid regexes.
 
-proc test_gdb_complete_cmd_multiple { cmd_prefix completion_word completion_list {start_quote_char ""} {end_quote_char ""} {max_completions 0}} {
+proc test_gdb_complete_cmd_multiple_re { cmd_prefix completion_word completion_list_re {start_quote_char ""} {end_quote_char ""} {max_completions 0}} {
     global gdb_prompt
 
-    set expected_re [make_cmd_completion_list_re $cmd_prefix $completion_list $start_quote_char $end_quote_char]
+    set expected_re [make_cmd_completion_list_re $cmd_prefix $completion_list_re $start_quote_char $end_quote_char]
 
     if {$max_completions} {
 	set cmd_prefix_re [string_to_regexp $cmd_prefix]
@@ -220,6 +241,19 @@  proc test_gdb_complete_cmd_multiple { cmd_prefix completion_word completion_list
     }
 }
 
+# Simplified call to test_gdb_complete_cmd_multiple_re, allowing for
+# passing completions that are not regular expressions.
+
+proc test_gdb_complete_cmd_multiple { cmd_prefix completion_word completion_list {start_quote_char ""} {end_quote_char ""} {max_completions 0}} {
+    set completion_list_re ""
+    foreach c $completion_list {
+	lappend completion_list_re [string_to_regexp $c]
+    }
+
+    test_gdb_complete_cmd_multiple_re $cmd_prefix $completion_word \
+	$completion_list_re $start_quote_char $end_quote_char $max_completions
+}
+
 # Test that completing LINE completes to nothing.
 
 proc test_gdb_complete_none { input_line } {
@@ -300,6 +334,19 @@  proc test_gdb_complete_multiple {
     test_gdb_complete_cmd_multiple $cmd_prefix $completion_word $completion_list $start_quote_char $end_quote_char $max_completions
 }
 
+# Similar to test_gdb_complete_multiple, but requires regular expressions
+# in COMPLETION_LIST_RE.
+
+proc test_gdb_complete_multiple_re {
+  cmd_prefix completion_word add_completed_line completion_list_re
+  {start_quote_char ""} {end_quote_char ""} {max_completions 0}
+} {
+    if { [readline_is_used] } {
+      test_gdb_complete_tab_multiple_re "$cmd_prefix$completion_word" $add_completed_line $completion_list_re $max_completions
+    }
+    test_gdb_complete_cmd_multiple_re $cmd_prefix $completion_word $completion_list_re $start_quote_char $end_quote_char $max_completions
+}
+
 # Test that all the substring prefixes of INPUT from [0..START) to
 # [0..END) complete to COMPLETION_RE (a regular expression).  If END
 # is ommitted, default to the length of INPUT.