-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_functional_test.go
More file actions
132 lines (118 loc) · 3.39 KB
/
prompt_functional_test.go
File metadata and controls
132 lines (118 loc) · 3.39 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
package clix
import (
"bytes"
"context"
"errors"
"strings"
"testing"
)
// TestFunctionalOptions tests that functional options work correctly
// alongside the struct-based API.
func TestFunctionalOptions(t *testing.T) {
t.Run("functional options work with TextPrompter", func(t *testing.T) {
in := bytes.NewBufferString("test-value\n")
out := &bytes.Buffer{}
prompter := TextPrompter{In: in, Out: out}
value, err := prompter.Prompt(context.Background(),
WithLabel("Enter value"),
WithTheme(DefaultPromptTheme),
)
if err != nil {
t.Fatalf("Prompt returned error: %v", err)
}
if value != "test-value" {
t.Fatalf("expected value 'test-value', got %q", value)
}
})
t.Run("functional options with default", func(t *testing.T) {
in := bytes.NewBufferString("\n")
out := &bytes.Buffer{}
prompter := TextPrompter{In: in, Out: out}
value, err := prompter.Prompt(context.Background(),
WithLabel("Color"),
WithDefault("blue"),
WithTheme(DefaultPromptTheme),
)
if err != nil {
t.Fatalf("Prompt returned error: %v", err)
}
if value != "blue" {
t.Fatalf("expected default value 'blue', got %q", value)
}
})
t.Run("functional options with validation", func(t *testing.T) {
in := bytes.NewBufferString("invalid\nvalid\n")
out := &bytes.Buffer{}
prompter := TextPrompter{In: in, Out: out}
value, err := prompter.Prompt(context.Background(),
WithLabel("Code"),
WithTheme(DefaultPromptTheme),
WithValidate(func(v string) error {
if v != "valid" {
return errors.New("must be 'valid'")
}
return nil
}),
)
if err != nil {
t.Fatalf("Prompt returned error: %v", err)
}
if value != "valid" {
t.Fatalf("expected value 'valid', got %q", value)
}
})
t.Run("functional options with confirm", func(t *testing.T) {
in := bytes.NewBufferString("y\n")
out := &bytes.Buffer{}
prompter := TextPrompter{In: in, Out: out}
value, err := prompter.Prompt(context.Background(),
WithLabel("Continue?"),
WithConfirm(),
WithTheme(DefaultPromptTheme),
)
if err != nil {
t.Fatalf("Prompt returned error: %v", err)
}
if value != "y" {
t.Fatalf("expected value 'y', got %q", value)
}
output := out.String()
if !strings.Contains(output, "(Y/n)") {
t.Errorf("output should show default, got: %s", output)
}
})
t.Run("functional options can be mixed with struct", func(t *testing.T) {
in := bytes.NewBufferString("test\n")
out := &bytes.Buffer{}
prompter := TextPrompter{In: in, Out: out}
// Mix: struct for Label, functional for Default
value, err := prompter.Prompt(context.Background(),
PromptRequest{Label: "Name"},
WithDefault("unknown"),
WithTheme(DefaultPromptTheme),
)
if err != nil {
t.Fatalf("Prompt returned error: %v", err)
}
if value != "test" {
t.Fatalf("expected value 'test', got %q", value)
}
})
t.Run("functional options override struct values", func(t *testing.T) {
in := bytes.NewBufferString("\n")
out := &bytes.Buffer{}
prompter := TextPrompter{In: in, Out: out}
// Struct sets default "old", functional option overrides to "new"
value, err := prompter.Prompt(context.Background(),
PromptRequest{Label: "Value", Default: "old"},
WithDefault("new"),
WithTheme(DefaultPromptTheme),
)
if err != nil {
t.Fatalf("Prompt returned error: %v", err)
}
if value != "new" {
t.Fatalf("expected value 'new' (from functional option), got %q", value)
}
})
}