| 1 |
package ui |
| 2 |
|
| 3 |
import "testing" |
| 4 |
|
| 5 |
func TestWordStarts(t *testing.T) { |
| 6 |
// Test both wordStart and nextWordStart |
| 7 |
type string_case struct { |
| 8 |
name string |
| 9 |
cursor_pos int |
| 10 |
this_start_pos int |
| 11 |
next_start_pos int |
| 12 |
} |
| 13 |
tests := []struct { |
| 14 |
text string |
| 15 |
cases []string_case |
| 16 |
}{ |
| 17 |
{ |
| 18 |
text: "hello world", |
| 19 |
cases: []string_case{ |
| 20 |
{ |
| 21 |
name: "hello-world", |
| 22 |
cursor_pos: 0, |
| 23 |
this_start_pos: 0, |
| 24 |
next_start_pos: 6, |
| 25 |
}, |
| 26 |
{ |
| 27 |
name: "hello-world-middle", |
| 28 |
cursor_pos: 3, |
| 29 |
this_start_pos: 0, |
| 30 |
next_start_pos: 6, |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "hello-world-second-word", |
| 34 |
cursor_pos: 7, |
| 35 |
this_start_pos: 6, |
| 36 |
next_start_pos: len("hello world"), |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "hello-world-npos", |
| 40 |
cursor_pos: len("hello world"), |
| 41 |
this_start_pos: 6, |
| 42 |
next_start_pos: len("hello world"), |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "hello-world-npos-1", |
| 46 |
cursor_pos: len("hello world") - 1, |
| 47 |
this_start_pos: 6, |
| 48 |
next_start_pos: len("hello world"), |
| 49 |
}, |
| 50 |
}, |
| 51 |
}, |
| 52 |
{ |
| 53 |
text: " hello ", |
| 54 |
cases: []string_case{ |
| 55 |
{ |
| 56 |
name: "space-around-mid", |
| 57 |
cursor_pos: 6, |
| 58 |
this_start_pos: 4, |
| 59 |
next_start_pos: 9, |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "space-around-start", |
| 63 |
cursor_pos: 4, |
| 64 |
this_start_pos: 0, |
| 65 |
next_start_pos: 9, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "space-around-end", |
| 69 |
cursor_pos: 9, |
| 70 |
this_start_pos: 4, |
| 71 |
next_start_pos: len(" hello "), |
| 72 |
}, |
| 73 |
}, |
| 74 |
}, |
| 75 |
{ |
| 76 |
text: "", |
| 77 |
cases: []string_case{ |
| 78 |
{ |
| 79 |
name: "empty", |
| 80 |
cursor_pos: 0, |
| 81 |
this_start_pos: 0, |
| 82 |
next_start_pos: 0, |
| 83 |
}, |
| 84 |
}, |
| 85 |
}, |
| 86 |
{ |
| 87 |
text: " 'hello ' world", |
| 88 |
cases: []string_case{ |
| 89 |
{ |
| 90 |
name: "space-and-quote-midword", |
| 91 |
cursor_pos: len(" 'he"), |
| 92 |
this_start_pos: len(" '"), |
| 93 |
next_start_pos: len(" 'hello "), |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "space-and-quote-startword", |
| 97 |
cursor_pos: len(" '"), |
| 98 |
this_start_pos: len(" "), |
| 99 |
next_start_pos: len(" 'hello "), |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "space-and-quote-atquote", |
| 103 |
cursor_pos: len(" "), |
| 104 |
this_start_pos: len(""), |
| 105 |
next_start_pos: len(" '"), |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "space-and-quote-at-second-quote", |
| 109 |
cursor_pos: len(" 'hello "), |
| 110 |
this_start_pos: len(" '"), |
| 111 |
next_start_pos: len(" 'hello ' "), |
| 112 |
}, |
| 113 |
{ |
| 114 |
name: "space-and-quote-after-second-quote", |
| 115 |
cursor_pos: len(" 'hello '"), |
| 116 |
this_start_pos: len(" 'hello "), |
| 117 |
next_start_pos: len(" 'hello ' "), |
| 118 |
}, |
| 119 |
{ |
| 120 |
name: "space-and-quote-after-second-quote", |
| 121 |
cursor_pos: len(" 'hello ' "), |
| 122 |
this_start_pos: len(" 'hello "), |
| 123 |
next_start_pos: len(" 'hello ' world"), |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "space-and-quote-at-last-word", |
| 127 |
cursor_pos: len(" 'hello ' w"), |
| 128 |
this_start_pos: len(" 'hello ' "), |
| 129 |
next_start_pos: len(" 'hello ' world"), |
| 130 |
}, |
| 131 |
{ |
| 132 |
name: "space-and-quote-start", |
| 133 |
cursor_pos: 0, |
| 134 |
this_start_pos: 0, |
| 135 |
next_start_pos: len(" "), |
| 136 |
}, |
| 137 |
}, |
| 138 |
}, |
| 139 |
{ |
| 140 |
text: " /hello", |
| 141 |
cases: []string_case{ |
| 142 |
{ |
| 143 |
name: "space-and-path-midword", |
| 144 |
cursor_pos: 4, |
| 145 |
this_start_pos: 2, |
| 146 |
next_start_pos: len(" 'hello"), |
| 147 |
}, |
| 148 |
{ |
| 149 |
name: "space-and-path-startword", |
| 150 |
cursor_pos: 2, |
| 151 |
this_start_pos: 1, |
| 152 |
next_start_pos: len(" 'hello"), |
| 153 |
}, |
| 154 |
{ |
| 155 |
name: "space-and-path-atpath", |
| 156 |
cursor_pos: 1, |
| 157 |
this_start_pos: 0, |
| 158 |
next_start_pos: 2, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "space-and-path-start", |
| 162 |
cursor_pos: 0, |
| 163 |
this_start_pos: 0, |
| 164 |
next_start_pos: 1, |
| 165 |
}, |
| 166 |
}, |
| 167 |
}, |
| 168 |
} |
| 169 |
for _, strt := range tests { |
| 170 |
for _, test := range strt.cases { |
| 171 |
t.Run(test.name, func(t *testing.T) { |
| 172 |
textinput := NewTextInput(strt.text, nil) |
| 173 |
textinput.index = test.cursor_pos |
| 174 |
this_word_start := textinput.wordStart() |
| 175 |
if this_word_start != test.this_start_pos { |
| 176 |
t.Errorf("cursor was moved (this word start) incorrectly: got %d but expected %d (test string: \"%s\")", this_word_start, test.this_start_pos, charactersToString(textinput.text)) |
| 177 |
} |
| 178 |
next_word_start := textinput.nextWordStart() |
| 179 |
if next_word_start != test.next_start_pos { |
| 180 |
t.Errorf("cursor was moved (next word start) incorrectly: got %d but expected %d (test string: \"%s\")", next_word_start, test.next_start_pos, charactersToString(textinput.text)) |
| 181 |
} |
| 182 |
}) |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func TestDeleteWord(t *testing.T) { |
| 188 |
tests := []struct { |
| 189 |
name string |
| 190 |
text string |
| 191 |
expected string |
| 192 |
}{ |
| 193 |
{ |
| 194 |
name: "hello-world", |
| 195 |
text: "hello world", |
| 196 |
expected: "hello ", |
| 197 |
}, |
| 198 |
{ |
| 199 |
name: "empty", |
| 200 |
text: "", |
| 201 |
expected: "", |
| 202 |
}, |
| 203 |
{ |
| 204 |
name: "quoted", |
| 205 |
text: `"hello"`, |
| 206 |
expected: `"hello`, |
| 207 |
}, |
| 208 |
{ |
| 209 |
name: "hello-and-space", |
| 210 |
text: "hello ", |
| 211 |
expected: "", |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "space-and-hello", |
| 215 |
text: " hello", |
| 216 |
expected: " ", |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "only-quote", |
| 220 |
text: `"`, |
| 221 |
expected: "", |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "only-space", |
| 225 |
text: " ", |
| 226 |
expected: "", |
| 227 |
}, |
| 228 |
{ |
| 229 |
name: "space-and-quoted", |
| 230 |
text: " 'hello", |
| 231 |
expected: " '", |
| 232 |
}, |
| 233 |
{ |
| 234 |
name: "paths", |
| 235 |
text: "foo/bar/baz", |
| 236 |
expected: "foo/bar/", |
| 237 |
}, |
| 238 |
{ |
| 239 |
name: "space-and-paths", |
| 240 |
text: " /foo", |
| 241 |
expected: " /", |
| 242 |
}, |
| 243 |
} |
| 244 |
|
| 245 |
for _, test := range tests { |
| 246 |
t.Run(test.name, func(t *testing.T) { |
| 247 |
textinput := NewTextInput(test.text, nil) |
| 248 |
textinput.deleteWord() |
| 249 |
if charactersToString(textinput.text) != test.expected { |
| 250 |
t.Errorf("word was deleted incorrectly: got %s but expected %s", charactersToString(textinput.text), test.expected) |
| 251 |
} |
| 252 |
}) |
| 253 |
} |
| 254 |
} |