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
|
package tui
import "testing"
var (
flagUS = "\U0001F1FA\U0001F1F8"
zwjFamily = "\U0001F468\U0001F469\U0001F467"
skinWave = "\U0001F44B\U0001F3FD"
comboE = "é"
)
func TestTextArea_InsertText_KeepsClustersWhole(t *testing.T) {
type tc struct {
insert string
wantClusters int
wantCursorPos int
}
tests := map[string]tc{
"flag": {insert: flagUS, wantClusters: 1, wantCursorPos: 1},
"zwj family": {insert: zwjFamily, wantClusters: 1, wantCursorPos: 1},
"skin tone": {insert: skinWave, wantClusters: 1, wantCursorPos: 1},
"combining": {insert: comboE, wantClusters: 1, wantCursorPos: 1},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
ta := NewTextArea()
ta.BindApp(testApp)
ta.InsertText(tt.insert)
if got := ta.Text(); got != tt.insert {
t.Fatalf("text = %q, want %q", got, tt.insert)
}
if got := ClusterCount(ta.Text()); got != tt.wantClusters {
t.Fatalf("ClusterCount = %d, want %d", got, tt.wantClusters)
}
if got := ta.CursorPos(); got != tt.wantCursorPos {
t.Fatalf("CursorPos = %d, want %d", got, tt.wantCursorPos)
}
})
}
}
func TestTextArea_InsertText_RoutesThroughInsertPath(t *testing.T) {
ta := NewTextArea()
ta.BindApp(testApp)
ta.InsertText("e")
ta.InsertText("́")
if got := ta.Text(); got != comboE {
t.Fatalf("text = %q, want %q", got, comboE)
}
if got := ClusterCount(ta.Text()); got != 1 {
t.Fatalf("ClusterCount = %d, want 1", got)
}
if got := ta.CursorPos(); got != 1 {
t.Fatalf("CursorPos = %d, want 1", got)
}
if got := ta.cursorPos.Get(); got != 2 {
t.Fatalf("internal cursorPos = %d, want 2", got)
}
}
func TestTextArea_CursorPos_RoundTripsThroughSetCursorPos(t *testing.T) {
type tc struct {
text string
want int
}
tests := map[string]tc{
"ascii": {text: "hello", want: 5},
"flags": {text: flagUS + flagUS, want: 2},
"mixed clusters": {text: "a" + flagUS + comboE + "z", want: 4},
"zwj family": {text: zwjFamily, want: 1},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
ta := NewTextArea()
ta.BindApp(testApp)
ta.SetText(tt.text)
if got := ClusterCount(tt.text); got != tt.want {
t.Fatalf("ClusterCount(%q) = %d, want %d", tt.text, got, tt.want)
}
for pos := 0; pos <= tt.want; pos++ {
ta.SetCursorPos(pos)
if got := ta.CursorPos(); got != pos {
t.Fatalf("SetCursorPos(%d) -> CursorPos() = %d", pos, got)
}
}
})
}
}
func TestTextArea_SetCursorPos_ClampsAndSnaps(t *testing.T) {
ta := NewTextArea()
ta.BindApp(testApp)
ta.SetText("a" + flagUS)
ta.SetCursorPos(99)
if got := ta.CursorPos(); got != 2 {
t.Fatalf("CursorPos after over-clamp = %d, want 2", got)
}
if got := ta.cursorPos.Get(); got != 3 {
t.Fatalf("internal cursorPos after over-clamp = %d, want 3", got)
}
ta.SetCursorPos(-5)
if got := ta.CursorPos(); got != 0 {
t.Fatalf("CursorPos after negative = %d, want 0", got)
}
}
func TestTextArea_InsertText_AtCursor_ClusterIndexSemantics(t *testing.T) {
ta := NewTextArea()
ta.BindApp(testApp)
ta.SetText(flagUS + flagUS)
ta.SetCursorPos(1)
ta.InsertText("X")
if got := ta.Text(); got != flagUS+"X"+flagUS {
t.Fatalf("text = %q, want %q", got, flagUS+"X"+flagUS)
}
if got := ta.CursorPos(); got != 2 {
t.Fatalf("CursorPos = %d, want 2", got)
}
}
func TestInput_InsertText_KeepsClustersWhole(t *testing.T) {
type tc struct {
insert string
wantClusters int
}
tests := map[string]tc{
"flag": {insert: flagUS, wantClusters: 1},
"zwj family": {insert: zwjFamily, wantClusters: 1},
"skin tone": {insert: skinWave, wantClusters: 1},
"combining": {insert: comboE, wantClusters: 1},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
inp := NewInput(WithInputWidth(20))
inp.BindApp(testApp)
inp.InsertText(tt.insert)
if got := inp.Text(); got != tt.insert {
t.Fatalf("text = %q, want %q", got, tt.insert)
}
if got := ClusterCount(inp.Text()); got != tt.wantClusters {
t.Fatalf("ClusterCount = %d, want %d", got, tt.wantClusters)
}
if got := inp.CursorPos(); got != tt.wantClusters {
t.Fatalf("CursorPos = %d, want %d", got, tt.wantClusters)
}
})
}
}
func TestInput_InsertText_FiresOnChangeAndAdvancesCursor(t *testing.T) {
var changes []string
inp := NewInput(WithInputWidth(20), WithInputOnChange(func(s string) {
changes = append(changes, s)
}))
inp.BindApp(testApp)
inp.InsertText("ab")
inp.InsertText(flagUS)
if got := inp.Text(); got != "ab"+flagUS {
t.Fatalf("text = %q, want %q", got, "ab"+flagUS)
}
if got := inp.CursorPos(); got != 3 {
t.Fatalf("CursorPos = %d, want 3", got)
}
if len(changes) != 2 {
t.Fatalf("onChange called %d times, want 2", len(changes))
}
}
func TestInput_CursorPos_RoundTripsThroughSetCursorPos(t *testing.T) {
inp := NewInput(WithInputWidth(20))
inp.BindApp(testApp)
text := "a" + flagUS + comboE + "z"
inp.SetText(text)
want := ClusterCount(text)
if want != 4 {
t.Fatalf("ClusterCount = %d, want 4", want)
}
for pos := 0; pos <= want; pos++ {
inp.SetCursorPos(pos)
if got := inp.CursorPos(); got != pos {
t.Fatalf("SetCursorPos(%d) -> CursorPos() = %d", pos, got)
}
}
}
func TestInput_SetCursorPos_Clamps(t *testing.T) {
inp := NewInput(WithInputWidth(20))
inp.BindApp(testApp)
inp.SetText(flagUS)
inp.SetCursorPos(99)
if got := inp.CursorPos(); got != 1 {
t.Fatalf("CursorPos after over-clamp = %d, want 1", got)
}
if got := inp.cursorPos.Get(); got != 2 {
t.Fatalf("internal cursorPos = %d, want 2", got)
}
inp.SetCursorPos(-3)
if got := inp.CursorPos(); got != 0 {
t.Fatalf("CursorPos after negative = %d, want 0", got)
}
}
|