File: textinput_test.go

package info (click to toggle)
aerc 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,900 kB
  • sloc: ansic: 1,181; python: 1,000; sh: 553; awk: 360; makefile: 23
file content (254 lines) | stat: -rw-r--r-- 5,795 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package ui

import "testing"

func TestWordStarts(t *testing.T) {
	// Test both wordStart and nextWordStart
	type string_case struct {
		name           string
		cursor_pos     int
		this_start_pos int
		next_start_pos int
	}
	tests := []struct {
		text  string
		cases []string_case
	}{
		{
			text: "hello world",
			cases: []string_case{
				{
					name:           "hello-world",
					cursor_pos:     0,
					this_start_pos: 0,
					next_start_pos: 6,
				},
				{
					name:           "hello-world-middle",
					cursor_pos:     3,
					this_start_pos: 0,
					next_start_pos: 6,
				},
				{
					name:           "hello-world-second-word",
					cursor_pos:     7,
					this_start_pos: 6,
					next_start_pos: len("hello world"),
				},
				{
					name:           "hello-world-npos",
					cursor_pos:     len("hello world"),
					this_start_pos: 6,
					next_start_pos: len("hello world"),
				},
				{
					name:           "hello-world-npos-1",
					cursor_pos:     len("hello world") - 1,
					this_start_pos: 6,
					next_start_pos: len("hello world"),
				},
			},
		},
		{
			text: "    hello   ",
			cases: []string_case{
				{
					name:           "space-around-mid",
					cursor_pos:     6,
					this_start_pos: 4,
					next_start_pos: 9,
				},
				{
					name:           "space-around-start",
					cursor_pos:     4,
					this_start_pos: 0,
					next_start_pos: 9,
				},
				{
					name:           "space-around-end",
					cursor_pos:     9,
					this_start_pos: 4,
					next_start_pos: len("    hello   "),
				},
			},
		},
		{
			text: "",
			cases: []string_case{
				{
					name:           "empty",
					cursor_pos:     0,
					this_start_pos: 0,
					next_start_pos: 0,
				},
			},
		},
		{
			text: " 'hello '  world",
			cases: []string_case{
				{
					name:           "space-and-quote-midword",
					cursor_pos:     len(" 'he"),
					this_start_pos: len(" '"),
					next_start_pos: len(" 'hello "),
				},
				{
					name:           "space-and-quote-startword",
					cursor_pos:     len(" '"),
					this_start_pos: len(" "),
					next_start_pos: len(" 'hello "),
				},
				{
					name:           "space-and-quote-atquote",
					cursor_pos:     len(" "),
					this_start_pos: len(""),
					next_start_pos: len(" '"),
				},
				{
					name:           "space-and-quote-at-second-quote",
					cursor_pos:     len(" 'hello "),
					this_start_pos: len(" '"),
					next_start_pos: len(" 'hello '  "),
				},
				{
					name:           "space-and-quote-after-second-quote",
					cursor_pos:     len(" 'hello '"),
					this_start_pos: len(" 'hello "),
					next_start_pos: len(" 'hello '  "),
				},
				{
					name:           "space-and-quote-after-second-quote",
					cursor_pos:     len(" 'hello '  "),
					this_start_pos: len(" 'hello "),
					next_start_pos: len(" 'hello '  world"),
				},
				{
					name:           "space-and-quote-at-last-word",
					cursor_pos:     len(" 'hello '  w"),
					this_start_pos: len(" 'hello '  "),
					next_start_pos: len(" 'hello '  world"),
				},
				{
					name:           "space-and-quote-start",
					cursor_pos:     0,
					this_start_pos: 0,
					next_start_pos: len(" "),
				},
			},
		},
		{
			text: " /hello",
			cases: []string_case{
				{
					name:           "space-and-path-midword",
					cursor_pos:     4,
					this_start_pos: 2,
					next_start_pos: len(" 'hello"),
				},
				{
					name:           "space-and-path-startword",
					cursor_pos:     2,
					this_start_pos: 1,
					next_start_pos: len(" 'hello"),
				},
				{
					name:           "space-and-path-atpath",
					cursor_pos:     1,
					this_start_pos: 0,
					next_start_pos: 2,
				},
				{
					name:           "space-and-path-start",
					cursor_pos:     0,
					this_start_pos: 0,
					next_start_pos: 1,
				},
			},
		},
	}
	for _, strt := range tests {
		for _, test := range strt.cases {
			t.Run(test.name, func(t *testing.T) {
				textinput := NewTextInput(strt.text, nil)
				textinput.index = test.cursor_pos
				this_word_start := textinput.wordStart()
				if this_word_start != test.this_start_pos {
					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))
				}
				next_word_start := textinput.nextWordStart()
				if next_word_start != test.next_start_pos {
					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))
				}
			})
		}
	}
}

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 charactersToString(textinput.text) != test.expected {
				t.Errorf("word was deleted incorrectly: got %s but expected %s", charactersToString(textinput.text), test.expected)
			}
		})
	}
}