-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinding_test.go
More file actions
340 lines (318 loc) · 9.74 KB
/
binding_test.go
File metadata and controls
340 lines (318 loc) · 9.74 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package glyph
import "testing"
func TestListBindNavCollected(t *testing.T) {
items := []string{"a", "b", "c"}
tmpl := Build(VBox(
List(&items).Render(func(s *string) any { return Text(s) }).BindNav("j", "k"),
))
if len(tmpl.pendingBindings) != 2 {
t.Fatalf("expected 2 bindings, got %d", len(tmpl.pendingBindings))
}
if tmpl.pendingBindings[0].pattern != "j" {
t.Errorf("expected pattern 'j', got %q", tmpl.pendingBindings[0].pattern)
}
if tmpl.pendingBindings[1].pattern != "k" {
t.Errorf("expected pattern 'k', got %q", tmpl.pendingBindings[1].pattern)
}
}
func TestListBindDeleteCollected(t *testing.T) {
items := []string{"a", "b"}
tmpl := Build(VBox(
List(&items).Render(func(s *string) any { return Text(s) }).BindNav("j", "k").BindDelete("d"),
))
if len(tmpl.pendingBindings) != 3 {
t.Fatalf("expected 3 bindings, got %d", len(tmpl.pendingBindings))
}
if tmpl.pendingBindings[2].pattern != "d" {
t.Errorf("expected pattern 'd', got %q", tmpl.pendingBindings[2].pattern)
}
}
func TestCheckboxBindToggleCollected(t *testing.T) {
checked := false
tmpl := Build(VBox(
Checkbox(&checked, "agree").BindToggle("x"),
))
if len(tmpl.pendingBindings) != 1 {
t.Fatalf("expected 1 binding, got %d", len(tmpl.pendingBindings))
}
if tmpl.pendingBindings[0].pattern != "x" {
t.Errorf("expected pattern 'x', got %q", tmpl.pendingBindings[0].pattern)
}
}
func TestRadioBindNavCollected(t *testing.T) {
sel := 0
tmpl := Build(VBox(
Radio(&sel, "a", "b", "c").BindNav("n", "p"),
))
if len(tmpl.pendingBindings) != 2 {
t.Fatalf("expected 2 bindings, got %d", len(tmpl.pendingBindings))
}
if tmpl.pendingBindings[0].pattern != "n" {
t.Errorf("expected pattern 'n', got %q", tmpl.pendingBindings[0].pattern)
}
if tmpl.pendingBindings[1].pattern != "p" {
t.Errorf("expected pattern 'p', got %q", tmpl.pendingBindings[1].pattern)
}
}
func TestCheckListBindingsCollected(t *testing.T) {
type Item struct {
Name string
Done bool
}
items := []Item{{Name: "a"}, {Name: "b"}}
tmpl := Build(VBox(
CheckList(&items).
Checked(func(i *Item) *bool { return &i.Done }).
Render(func(i *Item) any { return Text(&i.Name) }).
BindNav("j", "k").
BindToggle("x").
BindDelete("d"),
))
// BindNav(2) + BindToggle(1) + BindDelete(1) = 4
if len(tmpl.pendingBindings) != 4 {
t.Fatalf("expected 4 bindings, got %d", len(tmpl.pendingBindings))
}
expected := []string{"j", "k", "x", "d"}
for i, exp := range expected {
if tmpl.pendingBindings[i].pattern != exp {
t.Errorf("binding %d: expected %q, got %q", i, exp, tmpl.pendingBindings[i].pattern)
}
}
}
func TestInputBindCollected(t *testing.T) {
tmpl := Build(VBox(
Input().Placeholder("name").Bind(),
))
if tmpl.pendingTIB == nil {
t.Fatal("expected textInputBinding to be set")
}
if tmpl.pendingTIB.value == nil {
t.Error("expected value pointer to be set")
}
if tmpl.pendingTIB.cursor == nil {
t.Error("expected cursor pointer to be set")
}
}
func TestMultipleComponentBindingsCollected(t *testing.T) {
checked := false
sel := 0
items := []string{"a", "b"}
tmpl := Build(VBox(
Checkbox(&checked, "agree").BindToggle("a"),
Radio(&sel, "x", "y").BindNav("n", "p"),
List(&items).Render(func(s *string) any { return Text(s) }).BindNav("j", "k"),
))
// 1 (checkbox) + 2 (radio) + 2 (list) = 5
if len(tmpl.pendingBindings) != 5 {
t.Fatalf("expected 5 bindings, got %d", len(tmpl.pendingBindings))
}
}
func TestListBindPageNavCollected(t *testing.T) {
items := []string{"a", "b", "c"}
tmpl := Build(VBox(
List(&items).Render(func(s *string) any { return Text(s) }).BindPageNav("<C-d>", "<C-u>"),
))
if len(tmpl.pendingBindings) != 2 {
t.Fatalf("expected 2 bindings, got %d", len(tmpl.pendingBindings))
}
if tmpl.pendingBindings[0].pattern != "<C-d>" {
t.Errorf("expected pattern '<C-d>', got %q", tmpl.pendingBindings[0].pattern)
}
if tmpl.pendingBindings[1].pattern != "<C-u>" {
t.Errorf("expected pattern '<C-u>', got %q", tmpl.pendingBindings[1].pattern)
}
}
func TestListBindFirstLastCollected(t *testing.T) {
items := []string{"a", "b", "c"}
tmpl := Build(VBox(
List(&items).Render(func(s *string) any { return Text(s) }).BindFirstLast("g", "G"),
))
if len(tmpl.pendingBindings) != 2 {
t.Fatalf("expected 2 bindings, got %d", len(tmpl.pendingBindings))
}
if tmpl.pendingBindings[0].pattern != "g" {
t.Errorf("expected pattern 'g', got %q", tmpl.pendingBindings[0].pattern)
}
if tmpl.pendingBindings[1].pattern != "G" {
t.Errorf("expected pattern 'G', got %q", tmpl.pendingBindings[1].pattern)
}
}
func TestListBindVimNavCollected(t *testing.T) {
items := []string{"a", "b", "c"}
tmpl := Build(VBox(
List(&items).Render(func(s *string) any { return Text(s) }).BindVimNav(),
))
// j, k, <C-d>, <C-u>, g, G = 6
if len(tmpl.pendingBindings) != 6 {
t.Fatalf("expected 6 bindings, got %d", len(tmpl.pendingBindings))
}
expected := []string{"j", "k", "<C-d>", "<C-u>", "g", "G"}
for i, exp := range expected {
if tmpl.pendingBindings[i].pattern != exp {
t.Errorf("binding %d: expected %q, got %q", i, exp, tmpl.pendingBindings[i].pattern)
}
}
}
func TestCheckListBindPageNavCollected(t *testing.T) {
type Item struct {
Name string
Done bool
}
items := []Item{{Name: "a"}, {Name: "b"}}
tmpl := Build(VBox(
CheckList(&items).
Checked(func(i *Item) *bool { return &i.Done }).
Render(func(i *Item) any { return Text(&i.Name) }).
BindPageNav("<C-d>", "<C-u>").
BindFirstLast("g", "G"),
))
// 2 (page) + 2 (first/last) = 4
if len(tmpl.pendingBindings) != 4 {
t.Fatalf("expected 4 bindings, got %d", len(tmpl.pendingBindings))
}
expected := []string{"<C-d>", "<C-u>", "g", "G"}
for i, exp := range expected {
if tmpl.pendingBindings[i].pattern != exp {
t.Errorf("binding %d: expected %q, got %q", i, exp, tmpl.pendingBindings[i].pattern)
}
}
}
func TestCheckListBindVimNavCollected(t *testing.T) {
type Item struct {
Name string
Done bool
}
items := []Item{{Name: "a"}, {Name: "b"}}
tmpl := Build(VBox(
CheckList(&items).
Checked(func(i *Item) *bool { return &i.Done }).
Render(func(i *Item) any { return Text(&i.Name) }).
BindVimNav(),
))
// j, k, <C-d>, <C-u>, g, G = 6
if len(tmpl.pendingBindings) != 6 {
t.Fatalf("expected 6 bindings, got %d", len(tmpl.pendingBindings))
}
expected := []string{"j", "k", "<C-d>", "<C-u>", "g", "G"}
for i, exp := range expected {
if tmpl.pendingBindings[i].pattern != exp {
t.Errorf("binding %d: expected %q, got %q", i, exp, tmpl.pendingBindings[i].pattern)
}
}
}
func TestListHandleCollected(t *testing.T) {
items := []string{"a", "b", "c"}
tmpl := Build(VBox(
List(&items).Render(func(s *string) any { return Text(s) }).
Handle("<Enter>", func(s *string) {}).
Handle("w", func(s *string) {}),
))
if len(tmpl.pendingBindings) != 2 {
t.Fatalf("expected 2 bindings, got %d", len(tmpl.pendingBindings))
}
if tmpl.pendingBindings[0].pattern != "<Enter>" {
t.Errorf("expected pattern '<Enter>', got %q", tmpl.pendingBindings[0].pattern)
}
if tmpl.pendingBindings[1].pattern != "w" {
t.Errorf("expected pattern 'w', got %q", tmpl.pendingBindings[1].pattern)
}
}
func TestListHandleCallsWithSelected(t *testing.T) {
items := []string{"a", "b", "c"}
var got string
var list *ListC[string]
tmpl := Build(VBox(
List(&items).Render(func(s *string) any { return Text(s) }).
Ref(func(l *ListC[string]) { list = l }).
Handle("<Enter>", func(s *string) { got = *s }),
))
buf := NewBuffer(40, 10)
tmpl.Execute(buf, 40, 10)
list.Down(nil) // select "b"
// extract and call the handler
for _, b := range list.bindings() {
if b.pattern == "<Enter>" {
b.handler.(func())()
}
}
if got != "b" {
t.Errorf("expected 'b', got %q", got)
}
}
func TestListHandleSkipsNilSelection(t *testing.T) {
items := []string{}
called := false
var list *ListC[string]
tmpl := Build(VBox(
List(&items).Render(func(s *string) any { return Text(s) }).
Ref(func(l *ListC[string]) { list = l }).
Handle("<Enter>", func(s *string) { called = true }),
))
buf := NewBuffer(40, 10)
tmpl.Execute(buf, 40, 10)
for _, b := range list.bindings() {
if b.pattern == "<Enter>" {
b.handler.(func())()
}
}
if called {
t.Error("expected handler not to be called with empty list")
}
}
func TestCheckListHandleCollected(t *testing.T) {
type Item struct {
Name string
Done bool
}
items := []Item{{Name: "a"}, {Name: "b"}}
tmpl := Build(VBox(
CheckList(&items).
Checked(func(i *Item) *bool { return &i.Done }).
Render(func(i *Item) any { return Text(&i.Name) }).
Handle("<Enter>", func(i *Item) {}),
))
if len(tmpl.pendingBindings) != 1 {
t.Fatalf("expected 1 binding, got %d", len(tmpl.pendingBindings))
}
if tmpl.pendingBindings[0].pattern != "<Enter>" {
t.Errorf("expected pattern '<Enter>', got %q", tmpl.pendingBindings[0].pattern)
}
}
func TestBindingsBubbleUpFromIfElse(t *testing.T) {
items := []string{"a", "b"}
show := true
tmpl := Build(VBox(
If(&show).Then(
List(&items).Render(func(s *string) any { return Text(s) }).BindNav("j", "k"),
).Else(
Text("empty"),
),
))
if len(tmpl.pendingBindings) != 2 {
t.Fatalf("expected 2 bindings from Then branch, got %d", len(tmpl.pendingBindings))
}
}
func TestBindingsBubbleUpFromElseBranch(t *testing.T) {
items := []string{"a", "b"}
show := false
tmpl := Build(VBox(
If(&show).Then(
Text("loading"),
).Else(
List(&items).Render(func(s *string) any { return Text(s) }).BindVimNav(),
),
))
// j, k, <C-d>, <C-u>, g, G = 6
if len(tmpl.pendingBindings) != 6 {
t.Fatalf("expected 6 bindings from Else branch, got %d", len(tmpl.pendingBindings))
}
}
func TestNoBindingsWhenNotUsed(t *testing.T) {
items := []string{"a", "b"}
tmpl := Build(VBox(
List(&items).Render(func(s *string) any { return Text(s) }),
))
if len(tmpl.pendingBindings) != 0 {
t.Errorf("expected 0 bindings, got %d", len(tmpl.pendingBindings))
}
}