@@ -52,15 +52,8 @@ func TestRichSelect(t *testing.T) {
52
52
go func () {
53
53
resp , err := newRichSelect (ptty , cliui.RichSelectOptions {
54
54
Options : []codersdk.TemplateVersionParameterOption {
55
- {
56
- Name : "A-Name" ,
57
- Value : "A-Value" ,
58
- Description : "A-Description." ,
59
- }, {
60
- Name : "B-Name" ,
61
- Value : "B-Value" ,
62
- Description : "B-Description." ,
63
- },
55
+ {Name : "A-Name" , Value : "A-Value" , Description : "A-Description." },
56
+ {Name : "B-Name" , Value : "B-Value" , Description : "B-Description." },
64
57
},
65
58
})
66
59
assert .NoError (t , err )
@@ -86,63 +79,119 @@ func newRichSelect(ptty *ptytest.PTY, opts cliui.RichSelectOptions) (string, err
86
79
return value , inv .Run ()
87
80
}
88
81
89
- func TestMultiSelect (t * testing.T ) {
82
+ func TestRichMultiSelect (t * testing.T ) {
90
83
t .Parallel ()
91
- t .Run ("MultiSelect" , func (t * testing.T ) {
92
- items := []string {"aaa" , "bbb" , "ccc" }
93
84
94
- t .Parallel ()
95
- ptty := ptytest .New (t )
96
- msgChan := make (chan []string )
97
- go func () {
98
- resp , err := newMultiSelect (ptty , items )
99
- assert .NoError (t , err )
100
- msgChan <- resp
101
- }()
102
- require .Equal (t , items , <- msgChan )
103
- })
85
+ tests := []struct {
86
+ name string
87
+ options []codersdk.TemplateVersionParameterOption
88
+ defaults []string
89
+ allowCustom bool
90
+ want []string
91
+ }{
92
+ {
93
+ name : "Predefined" ,
94
+ options : []codersdk.TemplateVersionParameterOption {
95
+ {Name : "AAA" , Description : "This is AAA" , Value : "aaa" },
96
+ {Name : "BBB" , Description : "This is BBB" , Value : "bbb" },
97
+ {Name : "CCC" , Description : "This is CCC" , Value : "ccc" },
98
+ },
99
+ defaults : []string {"bbb" , "ccc" },
100
+ allowCustom : false ,
101
+ want : []string {"bbb" , "ccc" },
102
+ },
103
+ {
104
+ name : "Custom" ,
105
+ options : []codersdk.TemplateVersionParameterOption {
106
+ {Name : "AAA" , Description : "This is AAA" , Value : "aaa" },
107
+ {Name : "BBB" , Description : "This is BBB" , Value : "bbb" },
108
+ {Name : "CCC" , Description : "This is CCC" , Value : "ccc" },
109
+ },
110
+ defaults : []string {"aaa" , "bbb" },
111
+ allowCustom : true ,
112
+ want : []string {"aaa" , "bbb" },
113
+ },
114
+ }
104
115
105
- t .Run ("MultiSelectWithCustomInput" , func (t * testing.T ) {
106
- t .Parallel ()
107
- items := []string {"Code" , "Chairs" , "Whale" , "Diamond" , "Carrot" }
108
- ptty := ptytest .New (t )
109
- msgChan := make (chan []string )
110
- go func () {
111
- resp , err := newMultiSelectWithCustomInput (ptty , items )
112
- assert .NoError (t , err )
113
- msgChan <- resp
114
- }()
115
- require .Equal (t , items , <- msgChan )
116
- })
117
- }
116
+ for _ , tt := range tests {
117
+ t .Run (tt .name , func (t * testing.T ) {
118
+ t .Parallel ()
118
119
119
- func newMultiSelectWithCustomInput ( ptty * ptytest. PTY , items []string ) ([] string , error ) {
120
- var values [] string
121
- cmd := & serpent.Command {
122
- Handler : func (inv * serpent.Invocation ) error {
123
- selectedItems , err : = cliui .MultiSelect (inv , cliui.MultiSelectOptions {
124
- Options : items ,
125
- Defaults : items ,
126
- EnableCustomInput : true ,
127
- })
128
- if err == nil {
129
- values = selectedItems
120
+ var selectedItems []string
121
+ var err error
122
+ cmd := & serpent.Command {
123
+ Handler : func (inv * serpent.Invocation ) error {
124
+ selectedItems , err = cliui .RichMultiSelect (inv , cliui.RichMultiSelectOptions {
125
+ Options : tt . options ,
126
+ Defaults : tt . defaults ,
127
+ EnableCustomInput : tt . allowCustom ,
128
+ })
129
+ return err
130
+ },
130
131
}
131
- return err
132
+
133
+ doneChan := make (chan struct {})
134
+ go func () {
135
+ defer close (doneChan )
136
+ err := cmd .Invoke ().Run ()
137
+ assert .NoError (t , err )
138
+ }()
139
+ <- doneChan
140
+
141
+ require .Equal (t , tt .want , selectedItems )
142
+ })
143
+ }
144
+ }
145
+
146
+ func TestMultiSelect (t * testing.T ) {
147
+ t .Parallel ()
148
+
149
+ tests := []struct {
150
+ name string
151
+ items []string
152
+ allowCustom bool
153
+ want []string
154
+ }{
155
+ {
156
+ name : "MultiSelect" ,
157
+ items : []string {"aaa" , "bbb" , "ccc" },
158
+ allowCustom : false ,
159
+ want : []string {"aaa" , "bbb" , "ccc" },
160
+ },
161
+ {
162
+ name : "MultiSelectWithCustomInput" ,
163
+ items : []string {"Code" , "Chairs" , "Whale" , "Diamond" , "Carrot" },
164
+ allowCustom : true ,
165
+ want : []string {"Code" , "Chairs" , "Whale" , "Diamond" , "Carrot" },
132
166
},
133
167
}
134
- inv := cmd .Invoke ()
135
- ptty .Attach (inv )
136
- return values , inv .Run ()
168
+
169
+ for _ , tt := range tests {
170
+ t .Run (tt .name , func (t * testing.T ) {
171
+ t .Parallel ()
172
+
173
+ ptty := ptytest .New (t )
174
+ msgChan := make (chan []string )
175
+
176
+ go func () {
177
+ resp , err := newMultiSelect (ptty , tt .items , tt .allowCustom )
178
+ assert .NoError (t , err )
179
+ msgChan <- resp
180
+ }()
181
+
182
+ require .Equal (t , tt .want , <- msgChan )
183
+ })
184
+ }
137
185
}
138
186
139
- func newMultiSelect (ptty * ptytest.PTY , items []string ) ([]string , error ) {
187
+ func newMultiSelect (pty * ptytest.PTY , items []string , custom bool ) ([]string , error ) {
140
188
var values []string
141
189
cmd := & serpent.Command {
142
190
Handler : func (inv * serpent.Invocation ) error {
143
191
selectedItems , err := cliui .MultiSelect (inv , cliui.MultiSelectOptions {
144
- Options : items ,
145
- Defaults : items ,
192
+ Options : items ,
193
+ Defaults : items ,
194
+ EnableCustomInput : custom ,
146
195
})
147
196
if err == nil {
148
197
values = selectedItems
@@ -151,6 +200,6 @@ func newMultiSelect(ptty *ptytest.PTY, items []string) ([]string, error) {
151
200
},
152
201
}
153
202
inv := cmd .Invoke ()
154
- ptty .Attach (inv )
203
+ pty .Attach (inv )
155
204
return values , inv .Run ()
156
205
}
0 commit comments