From 7d372429963db1860ca1242350c5f8a497c2f07f Mon Sep 17 00:00:00 2001 From: Kevin Miller Date: Fri, 22 Mar 2024 10:20:24 -0500 Subject: [PATCH] fix textinput.CurrentSuggestion() panic When suggestions are not yet set CurrentSuggestion() will panic. This change fixes that with a guard and returns an empty string when there is no current suggestion. --- textinput/textinput_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 textinput/textinput_test.go diff --git a/textinput/textinput_test.go b/textinput/textinput_test.go new file mode 100644 index 000000000..27a7640a8 --- /dev/null +++ b/textinput/textinput_test.go @@ -0,0 +1,32 @@ +package textinput + +import ( + "testing" +) + +func Test_CurrentSuggestion(t *testing.T) { + textinput := New() + textinput.ShowSuggestions = true + + suggestion := textinput.CurrentSuggestion() + expected := "" + if suggestion != expected { + t.Fatalf("Error: expected no current suggestion but was %s", suggestion) + } + + textinput.SetSuggestions([]string{"test1", "test2", "test3"}) + suggestion = textinput.CurrentSuggestion() + expected = "" + if suggestion != expected { + t.Fatalf("Error: expected no current suggestion but was %s", suggestion) + } + + textinput.SetValue("test") + textinput.updateSuggestions() + textinput.nextSuggestion() + suggestion = textinput.CurrentSuggestion() + expected = "test2" + if suggestion != expected { + t.Fatalf("Error: expected first suggestion but was %s", suggestion) + } +}