-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_test.go
More file actions
83 lines (75 loc) · 1.93 KB
/
Copy pathselect_test.go
File metadata and controls
83 lines (75 loc) · 1.93 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
package huhx
import (
"errors"
"strings"
"testing"
"charm.land/huh/v2"
)
// TestRunner_SelectValidatorOnInjected confirms that when an injected
// answer matches an option, the configured validator still runs on the
// resolved Value and a returned error propagates through the runner's
// field-prefixed wrap.
func TestRunner_SelectValidatorOnInjected(t *testing.T) {
var env string
want := errors.New("env-rejected")
form := NewForm(NewGroup(
NewSelect[string]().Key("env").
Options(
huh.NewOption("staging", "staging"),
huh.NewOption("prod", "prod"),
).
Value(&env).
Validate(func(s string) error {
if s == "prod" {
return want
}
return nil
}),
))
r := New(form,
WithNonInteractive(Always),
WithAnswers(map[string]any{"env": "prod"}),
)
err := r.Run()
if err == nil {
t.Fatal("expected validator error")
}
if !strings.Contains(err.Error(), `field "env"`) {
t.Errorf("expected field-prefixed error, got %q", err.Error())
}
if !errors.Is(err, want) {
t.Errorf("expected validator sentinel wrapped, got %v", err)
}
}
func TestSelect_Forwarders(t *testing.T) {
s := NewSelect[string]().
Key("k").
Title("t").
TitleFunc(func() string { return "tf" }, nil).
Description("d").
DescriptionFunc(func() string { return "df" }, nil).
Options(huh.NewOption("a", "a")).
Filtering(false).
Inline(true).
Height(5)
if s == nil {
t.Fatal("expected non-nil select after forwarder chain")
}
}
func TestSelect_AccessorWritesValue(t *testing.T) {
var dst string
acc := huh.NewPointerAccessor(&dst)
form := NewForm(NewGroup(
NewSelect[string]().
Key("env").
Options(huh.NewOption("staging", "staging"), huh.NewOption("prod", "prod")).
Accessor(acc),
))
r := New(form, WithNonInteractive(Always), WithAnswers(map[string]any{"env": "prod"}))
if err := r.Run(); err != nil {
t.Fatal(err)
}
if dst != "prod" {
t.Errorf("expected accessor to receive %q, got %q", "prod", dst)
}
}