aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar Sharipov <oskargit@riseup.net>2023-07-26 16:52:55 +0700
committerRobin Jarry <robin@jarry.cc>2023-07-28 21:18:09 +0200
commitcb57cd20e99c29391031a0bb27d1d3e099b21157 (patch)
tree8d286b9ec16120c958d2ef5b8a405bc0a0e473fd
parent8971f6edaeb5a6f3c08a3b00354ea15eb0707359 (diff)
downloadaerc-cb57cd20e99c29391031a0bb27d1d3e099b21157.tar.gz
aerc-cb57cd20e99c29391031a0bb27d1d3e099b21157.zip
textinput: fix deleteWord with an only whitespace
Fix a panic in the textinput.deleteWord when text is a whitespace symbol. Add tests for textinput. Fixes: https://todo.sr.ht/~rjarry/aerc/183 Signed-off-by: Oskar Sharipov <oskargit@riseup.net> Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r--lib/ui/textinput.go2
-rw-r--r--lib/ui/textinput_test.go72
2 files changed, 73 insertions, 1 deletions
diff --git a/lib/ui/textinput.go b/lib/ui/textinput.go
index e41cff75..dd946aec 100644
--- a/lib/ui/textinput.go
+++ b/lib/ui/textinput.go
@@ -220,7 +220,7 @@ func (ti *TextInput) deleteWord() {
for i >= 0 && ti.text[i] == ' ' {
i--
}
- if strings.ContainsRune(separators, ti.text[i]) {
+ if i >= 0 && strings.ContainsRune(separators, ti.text[i]) {
for i >= 0 && strings.ContainsRune(separators, ti.text[i]) {
i--
}
diff --git a/lib/ui/textinput_test.go b/lib/ui/textinput_test.go
new file mode 100644
index 00000000..47a4c294
--- /dev/null
+++ b/lib/ui/textinput_test.go
@@ -0,0 +1,72 @@
+package ui
+
+import "testing"
+
+func TestDeleteWord(t *testing.T) {
+ tests := []struct {
+ name string
+ text string
+ expected string
+ }{
+ {
+ name: "hello-world",
+ text: "hello world",
+ expected: "hello ",
+ },
+ {
+ name: "empty",
+ text: "",
+ expected: "",
+ },
+ {
+ name: "quoted",
+ text: `"hello"`,
+ expected: `"hello`,
+ },
+ {
+ name: "hello-and-space",
+ text: "hello ",
+ expected: "",
+ },
+ {
+ name: "space-and-hello",
+ text: " hello",
+ expected: " ",
+ },
+ {
+ name: "only-quote",
+ text: `"`,
+ expected: "",
+ },
+ {
+ name: "only-space",
+ text: " ",
+ expected: "",
+ },
+ {
+ name: "space-and-quoted",
+ text: " 'hello",
+ expected: " '",
+ },
+ {
+ name: "paths",
+ text: "foo/bar/baz",
+ expected: "foo/bar/",
+ },
+ {
+ name: "space-and-paths",
+ text: " /foo",
+ expected: " /",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ textinput := NewTextInput(test.text, nil)
+ textinput.deleteWord()
+ if string(textinput.text) != test.expected {
+ t.Errorf("word was deleted incorrectly: got %s but expected %s", string(textinput.text), test.expected)
+ }
+ })
+ }
+}