forked from sohomdatta1/promptui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_test.go
168 lines (141 loc) · 4.05 KB
/
select_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
package promptui
import (
"bytes"
"testing"
"github.com/NozomiNetworks/promptui/screenbuf"
)
func TestSelectTemplateRender(t *testing.T) {
t.Run("when using default style", func(t *testing.T) {
values := []string{"Zero"}
s := Select{
Label: "Select Number",
Items: values,
}
err := s.prepareTemplates()
if err != nil {
t.Fatalf("Unexpected error preparing templates %v", err)
}
result := string(render(s.Templates.label, s.Label))
exp := "\x1b[34m?\x1b[0m Select Number: "
if result != exp {
t.Errorf("Expected label to eq %q, got %q", exp, result)
}
result = string(render(s.Templates.active, values[0]))
exp = "\x1b[1m▸\x1b[0m \x1b[4mZero\x1b[0m"
if result != exp {
t.Errorf("Expected active item to eq %q, got %q", exp, result)
}
result = string(render(s.Templates.inactive, values[0]))
exp = " Zero"
if result != exp {
t.Errorf("Expected inactive item to eq %q, got %q", exp, result)
}
result = string(render(s.Templates.selected, values[0]))
exp = "\x1b[32m\x1b[32m✔\x1b[0m \x1b[2mZero\x1b[0m"
if result != exp {
t.Errorf("Expected selected item to eq %q, got %q", exp, result)
}
})
t.Run("when using custom style", func(t *testing.T) {
type pepper struct {
Name string
HeatUnit int
Peppers int
Description string
}
peppers := []pepper{
{
Name: "Bell Pepper",
HeatUnit: 0,
Peppers: 1,
Description: "Not very spicy!",
},
}
templates := &SelectTemplates{
Label: "{{ . }}?",
Active: "\U0001F525 {{ .Name | bold }} ({{ .HeatUnit | red | italic }})",
Inactive: " {{ .Name | bold }} ({{ .HeatUnit | red | italic }})",
Selected: "\U0001F525 {{ .Name | red | bold }}",
Details: `Name: {{.Name}}
Peppers: {{.Peppers}}
Description: {{.Description}}`,
}
s := Select{
Label: "Spicy Level",
Items: peppers,
Templates: templates,
}
err := s.prepareTemplates()
if err != nil {
t.Fatalf("Unexpected error preparing templates %v", err)
}
result := string(render(s.Templates.label, s.Label))
exp := "Spicy Level?"
if result != exp {
t.Errorf("Expected label to eq %q, got %q", exp, result)
}
result = string(render(s.Templates.active, peppers[0]))
exp = "🔥 \x1b[1mBell Pepper\x1b[0m (\x1b[3m\x1b[31m0\x1b[0m)"
if result != exp {
t.Errorf("Expected active item to eq %q, got %q", exp, result)
}
result = string(render(s.Templates.inactive, peppers[0]))
exp = " \x1b[1mBell Pepper\x1b[0m (\x1b[3m\x1b[31m0\x1b[0m)"
if result != exp {
t.Errorf("Expected inactive item to eq %q, got %q", exp, result)
}
result = string(render(s.Templates.selected, peppers[0]))
exp = "🔥 \x1b[1m\x1b[31mBell Pepper\x1b[0m"
if result != exp {
t.Errorf("Expected selected item to eq %q, got %q", exp, result)
}
result = string(render(s.Templates.details, peppers[0]))
exp = "Name: Bell Pepper\nPeppers: 1\nDescription: Not very spicy!"
if result != exp {
t.Errorf("Expected selected item to eq %q, got %q", exp, result)
}
})
t.Run("when a template is invalid", func(t *testing.T) {
templates := &SelectTemplates{
Label: "{{ . ",
}
s := Select{
Label: "Spicy Level",
Templates: templates,
}
err := s.prepareTemplates()
if err == nil {
t.Fatalf("Expected error got none")
}
})
t.Run("when a template render fails", func(t *testing.T) {
templates := &SelectTemplates{
Label: "{{ .InvalidName }}",
}
s := Select{
Label: struct{ Name string }{Name: "Pepper"},
Items: []string{},
Templates: templates,
}
err := s.prepareTemplates()
if err != nil {
t.Fatalf("Unexpected error preparing templates %v", err)
}
result := string(render(s.Templates.label, s.Label))
exp := "{Pepper}"
if result != exp {
t.Errorf("Expected label to eq %q, got %q", exp, result)
}
})
}
func TestClearScreen(t *testing.T) {
var buf bytes.Buffer
sb := screenbuf.New(&buf)
sb.WriteString("test")
clearScreen(sb)
got := buf.String()
except := "\x1b[1A\x1b[2K\r"
if except != got {
t.Errorf("expected %q, got %q", except, got)
}
}