Skip to content

Commit ae655ff

Browse files
committed
Update documentation and add example
1 parent 7d2de7b commit ae655ff

File tree

2 files changed

+85
-30
lines changed

2 files changed

+85
-30
lines changed

README.md

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,19 @@
11
# 🤔 Huh Test
22

3-
`huhtest` is a work-in-progress test library for your [huh](https://github.com/charmbracelet/huh) forms
3+
`huhtest` is a work-in-progress test library for your [huh](https://github.com/charmbracelet/huh) forms.
44
If you're - for some reason - eager to test your huh-based interactive CLI applications then you've come to
55
the right place.
66
It works by matching messages in a form's output (stdout) and then sending pre-programmed text to the form's input (stdin).
77

8+
It's not 100% bug-free, as some combinations of groups and selects seem to have off-by-one errors.
9+
810
## ⬇️ Installation
911

1012
`go get github.com/survivorbat/huhtest`
1113

1214
## 📋 Usage
1315

14-
```go
15-
package main
16-
17-
import (
18-
"time"
19-
20-
"github.com/survivorbat/huhtest"
21-
)
22-
23-
func TestMyForm() {
24-
// Arrange
25-
myForm := huh.NewForm(/* ... */)
26-
27-
stdin, stdout, cancel := huhtest.NewResponder().
28-
AddResponse("How Are You Feeling?", "Amazing Thanks!").
29-
AddConfirm("Would you like a drink?", ConfirmAffirm).
30-
AddSelect("Make a second choice", 2).
31-
AddMultiSelect("Choose all options that apply", []int{2, 5, 6}),
32-
Start(t, 1 * time.Second)
33-
34-
defer cancel()
35-
36-
// Act
37-
err := myForm.WithInput(formInput).WithOutput(formOutput).Run()
38-
39-
// Assert
40-
// ...
41-
}
42-
```
16+
Check out [this example](./examples_test.go)
4317

4418
## 🧪 Testing
4519

@@ -55,3 +29,4 @@ encounter a bug or are suspicious about something not working, turn it on to see
5529
## 🔭 Plans
5630

5731
- Custom keymap support for select fields
32+
- Select fields should preferably be selected by output text and not index

examples_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package huhtest
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
"time"
8+
9+
"github.com/charmbracelet/huh"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
var t = new(testing.T)
14+
15+
func ExampleNewResponder() {
16+
// Arrange
17+
var (
18+
howAreYouFeelingAnswer string
19+
areYouReadyAnswer bool
20+
sleptWellAnswer string
21+
activitiesAnswer []string
22+
)
23+
24+
myForm := huh.NewForm(
25+
huh.NewGroup(
26+
huh.NewInput().
27+
Title("How Are You Feeling?").
28+
Value(&howAreYouFeelingAnswer),
29+
huh.NewConfirm().
30+
Title("Are you ready?").
31+
Value(&areYouReadyAnswer),
32+
huh.NewSelect[string]().
33+
Title("Have you slept well?").
34+
Options(
35+
huh.NewOption("Well!", "well"),
36+
huh.NewOption("Terribly!", "terrible"),
37+
huh.NewOption("It was OK!", "ok"),
38+
).
39+
Value(&sleptWellAnswer),
40+
),
41+
huh.NewGroup(
42+
huh.NewMultiSelect[string]().
43+
Title("What are your favourite activities?").
44+
Options(
45+
huh.NewOption("Cycling", "bike"),
46+
huh.NewOption("Sleeping", "sleep"),
47+
huh.NewOption("Boating", "boat"),
48+
huh.NewOption("Gaming", "game"),
49+
huh.NewOption("Flying", "fly"),
50+
).
51+
Value(&activitiesAnswer),
52+
),
53+
)
54+
55+
stdin, stdout, cancel := NewResponder().
56+
AddResponse("How Are You Feeling?", "Great").
57+
AddConfirm("Are you ready?", ConfirmAffirm).
58+
AddMultiSelect("activities", []int{1, 2, 4}).
59+
AddSelect("Have you slept well", 2).
60+
Start(t, 1*time.Second)
61+
62+
defer cancel()
63+
64+
// Act
65+
err := myForm.WithInput(stdin).WithOutput(stdout).Run()
66+
67+
// Assert
68+
require.NoError(t, err)
69+
70+
fmt.Println("How are you feeling?", howAreYouFeelingAnswer)
71+
fmt.Println("Are you ready?", areYouReadyAnswer)
72+
fmt.Println("Have you slept well?", sleptWellAnswer)
73+
fmt.Println("What are your favourite activities?", strings.Join(activitiesAnswer, ", "))
74+
75+
// Output:
76+
// How are you feeling? Great
77+
// Are you ready? true
78+
// Have you slept well? ok
79+
// What are your favourite activities? sleep, boat, fly
80+
}

0 commit comments

Comments
 (0)