-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
80 lines (69 loc) · 1.88 KB
/
examples_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
package huhtest
import (
"fmt"
"strings"
"testing"
"time"
"github.com/charmbracelet/huh"
"github.com/stretchr/testify/require"
)
var t = new(testing.T)
func ExampleNewResponder() {
// Arrange
var (
howAreYouFeelingAnswer string
areYouReadyAnswer bool
sleptWellAnswer string
activitiesAnswer []string
)
myForm := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("How Are You Feeling?").
Value(&howAreYouFeelingAnswer),
huh.NewConfirm().
Title("Are you ready?").
Value(&areYouReadyAnswer),
huh.NewSelect[string]().
Title("Have you slept well?").
Options(
huh.NewOption("Well!", "well"),
huh.NewOption("Terribly!", "terrible"),
huh.NewOption("It was OK!", "ok"),
).
Value(&sleptWellAnswer),
),
huh.NewGroup(
huh.NewMultiSelect[string]().
Title("What are your favourite activities?").
Options(
huh.NewOption("Cycling", "bike"),
huh.NewOption("Sleeping", "sleep"),
huh.NewOption("Boating", "boat"),
huh.NewOption("Gaming", "game"),
huh.NewOption("Flying", "fly"),
).
Value(&activitiesAnswer),
),
)
stdin, stdout, cancel := NewResponder().
AddResponse("How Are You Feeling?", "Great").
AddConfirm("Are you ready?", ConfirmAffirm).
AddMultiSelect("activities", []int{1, 2, 4}).
AddSelect("Have you slept well", 2).
Start(t, 1*time.Second)
defer cancel()
// Act
err := myForm.WithInput(stdin).WithOutput(stdout).Run()
// Assert
require.NoError(t, err)
fmt.Println("How are you feeling?", howAreYouFeelingAnswer)
fmt.Println("Are you ready?", areYouReadyAnswer)
fmt.Println("Have you slept well?", sleptWellAnswer)
fmt.Println("What are your favourite activities?", strings.Join(activitiesAnswer, ", "))
// Output:
// How are you feeling? Great
// Are you ready? true
// Have you slept well? ok
// What are your favourite activities? sleep, boat, fly
}