@@ -2,12 +2,52 @@ package ui
22
33import (
44 "bytes"
5+ "errors"
56 "fmt"
67 "testing"
78
89 "github.com/secrethub/secrethub-go/internals/assert"
910)
1011
12+ func TestAskWithDefault (t * testing.T ) {
13+ question := "foo?"
14+ defaultValue := "bar"
15+ defaultOutput := "foo? [" + defaultValue + "] "
16+ cases := map [string ]struct {
17+ in []string
18+ expected string
19+ expectedOut string
20+ }{
21+ "value entered" : {
22+ in : []string {"foobar\n " },
23+ expected : "foobar" ,
24+ expectedOut : defaultOutput ,
25+ },
26+ "no value entered" : {
27+ in : []string {"\n " },
28+ expected : defaultValue ,
29+ expectedOut : defaultOutput ,
30+ },
31+ }
32+
33+ for name , tc := range cases {
34+ t .Run (name , func (t * testing.T ) {
35+ // Setup
36+ io := NewFakeIO ()
37+ io .PromptIn .Reads = tc .in
38+
39+ // Run
40+ actual , err := AskWithDefault (io , question , defaultValue )
41+
42+ // Assert
43+ assert .OK (t , err )
44+ assert .Equal (t , actual , tc .expected )
45+
46+ assert .Equal (t , io .PromptOut .String (), tc .expectedOut )
47+ })
48+ }
49+ }
50+
1151func TestConfirmCaseInsensitive (t * testing.T ) {
1252 cases := map [string ]struct {
1353 expectedConfirmation []string
@@ -214,6 +254,88 @@ func TestAskYesNo(t *testing.T) {
214254}
215255
216256func TestChoose (t * testing.T ) {
257+ question := "foo?"
258+ defaultOptions := []string {
259+ "option 1" ,
260+ "second option" ,
261+ }
262+ defaultOutput := "foo?\n " +
263+ " 1) option 1\n " +
264+ " 2) second option\n " +
265+ "Give the number of an option: "
266+
267+ cases := map [string ]struct {
268+ in []string
269+ options []string
270+ n int
271+ expected int
272+ expectedErr error
273+ expectedOut string
274+ }{
275+ "first option" : {
276+ in : []string {"1\n " },
277+ options : defaultOptions ,
278+ n : 3 ,
279+ expected : 0 ,
280+ expectedOut : defaultOutput ,
281+ },
282+ "retry" : {
283+ in : []string {"a\n " , "1\n " },
284+ options : defaultOptions ,
285+ n : 3 ,
286+ expected : 0 ,
287+ expectedOut : defaultOutput + "\n Invalid input: not a valid number\n Please try again.\n Give the number of an option: " ,
288+ },
289+ "filter out )" : {
290+ in : []string {"1)\n " },
291+ options : defaultOptions ,
292+ n : 3 ,
293+ expected : 0 ,
294+ expectedOut : defaultOutput ,
295+ },
296+ "out of bounds lower" : {
297+ in : []string {"0\n " },
298+ options : defaultOptions ,
299+ n : 1 ,
300+ expectedErr : errors .New ("out of bounds" ),
301+ expectedOut : defaultOutput + "\n Invalid input: out of bounds\n " ,
302+ },
303+ "out of bounds upper" : {
304+ in : []string {"3\n " },
305+ options : defaultOptions ,
306+ n : 1 ,
307+ expectedErr : errors .New ("out of bounds" ),
308+ expectedOut : defaultOutput + "\n Invalid input: out of bounds\n " ,
309+ },
310+ "not a number" : {
311+ in : []string {"abc\n " },
312+ options : defaultOptions ,
313+ n : 1 ,
314+ expectedErr : errors .New ("not a valid number" ),
315+ expectedOut : defaultOutput + "\n Invalid input: not a valid number\n " ,
316+ },
317+ }
318+
319+ for name , tc := range cases {
320+ t .Run (name , func (t * testing.T ) {
321+ // Setup
322+ io := NewFakeIO ()
323+ io .PromptIn .Reads = tc .in
324+
325+ // Run
326+ actual , err := Choose (io , question , tc .options , tc .n )
327+
328+ // Assert
329+ assert .Equal (t , err , tc .expectedErr )
330+ if tc .expectedErr == nil {
331+ assert .Equal (t , actual , tc .expected )
332+ }
333+ assert .Equal (t , io .PromptOut .String (), tc .expectedOut )
334+ })
335+ }
336+ }
337+
338+ func TestChooseDynamicOptions (t * testing.T ) {
217339 cases := map [string ]struct {
218340 question string
219341 getOptions func () ([]Option , bool , error )
@@ -302,7 +424,7 @@ func TestChoose(t *testing.T) {
302424 }
303425
304426 // Run
305- actual , err := Choose (io , tc .question , tc .getOptions , tc .addOwn , "value" )
427+ actual , err := ChooseDynamicOptions (io , tc .question , tc .getOptions , tc .addOwn , "value" )
306428
307429 // Assert
308430 assert .Equal (t , err , nil )
0 commit comments