-
Notifications
You must be signed in to change notification settings - Fork 131
/
huh_test.go
535 lines (437 loc) · 11.9 KB
/
huh_test.go
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package huh
import (
"fmt"
"strings"
"testing"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var pretty = lipgloss.NewStyle().
Border(lipgloss.NormalBorder()).
MarginTop(1).
Padding(1, 3, 1, 2)
func TestForm(t *testing.T) {
type Taco struct {
Shell string
Base string
Toppings []string
}
type Order struct {
Taco Taco
Name string
Instructions string
Discount bool
}
var taco Taco
order := Order{Taco: taco}
f := NewForm(
NewGroup(
NewSelect[string]().
Options(NewOptions("Soft", "Hard")...).
Title("Shell?").
Description("Our tortillas are made fresh in-house every day.").
Validate(func(t string) error {
if t == "Hard" {
return fmt.Errorf("we're out of hard shells, sorry")
}
return nil
}).
Value(&order.Taco.Shell),
NewSelect[string]().
Options(NewOptions("Chicken", "Beef", "Fish", "Beans")...).
Value(&order.Taco.Base).
Title("Base"),
),
// Prompt for toppings and special instructions.
// The customer can ask for up to 4 toppings.
NewGroup(
NewMultiSelect[string]().
Title("Toppings").
Description("Choose up to 4.").
Options(
NewOption("Lettuce", "lettuce").Selected(true),
NewOption("Tomatoes", "tomatoes").Selected(true),
NewOption("Corn", "corn"),
NewOption("Salsa", "salsa"),
NewOption("Sour Cream", "sour cream"),
NewOption("Cheese", "cheese"),
).
Validate(func(t []string) error {
if len(t) <= 0 {
return fmt.Errorf("at least one topping is required")
}
return nil
}).
Value(&order.Taco.Toppings).
Filterable(true).
Limit(4),
),
// Gather final details for the order.
NewGroup(
NewInput().
Value(&order.Name).
Title("What's your name?").
Placeholder("Margaret Thatcher").
Description("For when your order is ready."),
NewText().
Value(&order.Instructions).
Placeholder("Just put it in the mailbox please").
Title("Special Instructions").
Description("Anything we should know?").
CharLimit(400),
NewConfirm().
Title("Would you like 15% off?").
Value(&order.Discount).
Affirmative("Yes!").
Negative("No."),
),
)
f.Update(f.Init())
view := f.View()
//
// ┃ Shell?
// ┃ Our tortillas are made fresh in-house every day.
// ┃ > Soft
// ┃ Hard
//
// Base
// > Chicken
// Beef
// Fish
// Beans
//
// ↑ up • ↓ down • / filter • enter select • shift+tab back
//
if !strings.Contains(view, "Shell?") {
t.Log(pretty.Render(view))
t.Error("Expected form to contain Shell? title")
}
if !strings.Contains(view, "Our tortillas are made fresh in-house every day.") {
t.Log(pretty.Render(view))
t.Error("Expected form to contain tortilla description")
}
if !strings.Contains(view, "Base") {
t.Log(pretty.Render(view))
t.Error("Expected form to contain Base title")
}
// Attempt to select hard shell and retrieve error.
m, _ := f.Update(keys('j'))
m, _ = m.Update(tea.KeyMsg{Type: tea.KeyTab})
view = m.View()
if !strings.Contains(view, "* we're out of hard shells, sorry") {
t.Log(pretty.Render(view))
t.Error("Expected form to show out of hard shells error")
}
m, _ = m.Update(keys('k'))
m, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = batchUpdate(m, cmd)
view = m.View()
if !strings.Contains(view, "┃ > Chicken") {
t.Log(pretty.Render(view))
t.Fatal("Expected form to continue to base group")
}
// batchMsg + nextGroup
m, cmd = m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m = batchUpdate(m, cmd)
view = m.View()
//
// ┃ Toppings
// ┃ Choose up to 4.
// ┃ > ✓ Lettuce
// ┃ ✓ Tomatoes
// ┃ • Corn
// ┃ • Salsa
// ┃ • Sour Cream
// ┃ • Cheese
//
// x toggle • ↑ up • ↓ down • enter confirm • shift+tab back
//
if !strings.Contains(view, "Toppings") {
t.Log(pretty.Render(view))
t.Fatal("Expected form to show toppings group")
}
if !strings.Contains(view, "Choose up to 4.") {
t.Log(pretty.Render(view))
t.Error("Expected form to show toppings description")
}
if !strings.Contains(view, "> ✓ Lettuce ") {
t.Log(pretty.Render(view))
t.Error("Expected form to preselect lettuce")
}
if !strings.Contains(view, " ✓ Tomatoes") {
t.Log(pretty.Render(view))
t.Error("Expected form to preselect tomatoes")
}
m, _ = m.Update(keys('j'))
m, _ = m.Update(keys('j'))
view = m.View()
if !strings.Contains(view, "> • Corn") {
t.Log(pretty.Render(view))
t.Error("Expected form to change selection to corn")
}
m, _ = m.Update(keys('x'))
view = m.View()
if !strings.Contains(view, "> ✓ Corn") {
t.Log(pretty.Render(view))
t.Error("Expected form to change selection to corn")
}
m = batchUpdate(m.Update(tea.KeyMsg{Type: tea.KeyEnter}))
view = m.View()
if !strings.Contains(view, "What's your name?") {
t.Log(pretty.Render(view))
t.Error("Expected form to prompt for name")
}
if !strings.Contains(view, "Special Instructions") {
t.Log(pretty.Render(view))
t.Error("Expected form to prompt for special instructions")
}
if !strings.Contains(view, "Would you like 15% off?") {
t.Log(pretty.Render(view))
t.Error("Expected form to prompt for discount")
}
//
// ┃ What's your name?
// ┃ For when your order is ready.
// ┃ > Margaret Thatcher
//
// Special Instructions
// Anything we should know?
// Just put it in the mailbox please
//
// Would you like 15% off?
//
// Yes! No.
//
// enter next • shift+tab back
//
m.Update(keys('G', 'l', 'e', 'n'))
view = m.View()
if !strings.Contains(view, "Glen") {
t.Log(pretty.Render(view))
t.Error("Expected form to accept user input")
}
if order.Taco.Shell != "Soft" {
t.Error("Expected order shell to be Soft")
}
if order.Taco.Base != "Chicken" {
t.Error("Expected order shell to be Chicken")
}
if len(order.Taco.Toppings) != 3 {
t.Error("Expected order to have 3 toppings")
}
if order.Name != "Glen" {
t.Error("Expected order name to be Glen")
}
// TODO: Finish and submit form.
}
func TestInput(t *testing.T) {
field := NewInput()
f := NewForm(NewGroup(field))
f.Update(f.Init())
view := f.View()
if !strings.Contains(view, ">") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain prompt.")
}
// Type Huh in the form.
m, _ := f.Update(keys('H', 'u', 'h'))
f = m.(*Form)
view = f.View()
if !strings.Contains(view, "Huh") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Huh.")
}
if !strings.Contains(view, "enter next • shift+tab back") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
}
func TestText(t *testing.T) {
field := NewText()
f := NewForm(NewGroup(field))
f.Update(f.Init())
// Type Huh in the form.
m, _ := f.Update(keys('H', 'u', 'h'))
f = m.(*Form)
view := f.View()
if !strings.Contains(view, "Huh") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Huh.")
}
if !strings.Contains(view, "enter next • alt+enter / ctrl+j new line • ctrl+e open editor • shift+tab back") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
}
func TestConfirm(t *testing.T) {
field := NewConfirm().Title("Are you sure?")
f := NewForm(NewGroup(field))
f.Update(f.Init())
// Type Huh in the form.
m, _ := f.Update(keys('H'))
f = m.(*Form)
view := f.View()
if !strings.Contains(view, "Yes") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Yes.")
}
if !strings.Contains(view, "No") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain No.")
}
if !strings.Contains(view, "Are you sure?") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Are you sure?.")
}
if !strings.Contains(view, "←/→ toggle • enter next • shift+tab back") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
}
func TestSelect(t *testing.T) {
field := NewSelect[string]().Options(NewOptions("Foo", "Bar", "Baz")...).Title("Which one?")
f := NewForm(NewGroup(field))
f.Update(f.Init())
view := f.View()
if !strings.Contains(view, "Foo") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Foo.")
}
if !strings.Contains(view, "Which one?") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Which one?.")
}
// Move selection cursor down
if !strings.Contains(view, "> Foo") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Foo.")
}
m, _ := f.Update(keys('j'))
f = m.(*Form)
view = f.View()
if strings.Contains(view, "> Foo") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
if !strings.Contains(view, "> Bar") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
if !strings.Contains(view, "↑ up • ↓ down • / filter • enter select • shift+tab back") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
}
func TestMultiSelect(t *testing.T) {
field := NewMultiSelect[string]().Options(NewOptions("Foo", "Bar", "Baz")...).Title("Which one?")
f := NewForm(NewGroup(field))
f.Update(f.Init())
view := f.View()
if !strings.Contains(view, "Foo") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Foo.")
}
if !strings.Contains(view, "Which one?") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain Which one?.")
}
if !strings.Contains(view, "> • Foo") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Foo.")
}
// Move selection cursor down
m, _ := f.Update(keys('j'))
view = m.View()
if strings.Contains(view, "> • Foo") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
if !strings.Contains(view, "> • Bar") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
// Toggle
m, _ = f.Update(keys('x'))
view = m.View()
if !strings.Contains(view, "> ✓ Bar") {
t.Log(pretty.Render(view))
t.Error("Expected cursor to be on Bar.")
}
if !strings.Contains(view, "x toggle • ↑ up • ↓ down • enter confirm • shift+tab back") {
t.Log(pretty.Render(view))
t.Error("Expected field to contain help.")
}
}
func TestHideGroup(t *testing.T) {
f := NewForm(
NewGroup(NewNote().Description("Foo")).WithHide(true),
NewGroup(NewNote().Description("Bar")),
NewGroup(NewNote().Description("Baz")),
NewGroup(NewNote().Description("Qux")).WithHideFunc(func() bool { return false }).WithHide(true),
)
f = batchUpdate(f, f.Init()).(*Form)
if v := f.View(); !strings.Contains(v, "Bar") {
t.Log(pretty.Render(v))
t.Error("expected Bar to not be hidden")
}
f.Update(nextGroup())
if v := f.View(); !strings.Contains(v, "Baz") {
t.Log(pretty.Render(v))
t.Error("expected Baz to not be hidden")
}
f = batchUpdate(f, nextGroup).(*Form)
if v := f.View(); strings.Contains(v, "Qux") {
t.Log(pretty.Render(v))
t.Error("expected Qux to be hidden")
}
}
func TestNote(t *testing.T) {
field := NewNote().Title("Taco").Description("How may we take your order?").Next(true)
f := NewForm(NewGroup(field))
f.theme.Focused.Base.Border(lipgloss.HiddenBorder())
f.Update(f.Init())
view := f.View()
if !strings.Contains(view, "Taco") {
t.Log(view)
t.Error("Expected field to contain Taco title.")
}
if !strings.Contains(view, "order?") {
t.Log(view)
t.Error("Expected field to contain Taco description.")
}
if !strings.Contains(view, "Next") {
t.Log(view)
t.Error("Expected field to contain next button")
}
if !strings.Contains(view, "enter next") {
t.Log(view)
t.Error("Expected field to contain help.")
}
}
func batchUpdate(m tea.Model, cmd tea.Cmd) tea.Model {
if cmd == nil {
return m
}
msg := cmd()
if msg == nil {
return m
}
switch msg := msg.(type) {
case tea.BatchMsg:
for _, c := range msg {
m, cmd = m.Update(c())
if cmd == nil {
continue
}
}
return batchUpdate(m, cmd)
}
m, cmd = m.Update(msg)
return batchUpdate(m, cmd)
}
func keys(runes ...rune) tea.KeyMsg {
return tea.KeyMsg{
Type: tea.KeyRunes,
Runes: runes,
}
}