1
1
package ui_test
2
2
3
3
import (
4
+ "fmt"
4
5
"testing"
5
6
6
7
. "github.com/cppforlife/go-cli-ui/ui"
@@ -87,11 +88,41 @@ func TestNonInteractiveUI(t *testing.T) {
87
88
})
88
89
89
90
t .Run ("AskForText" , func (t * testing.T ) {
90
- t .Run ("panics " , func (t * testing.T ) {
91
+ t .Run ("default non empty " , func (t * testing.T ) {
91
92
parentUI := & fakeui.FakeUI {}
92
93
ui := NewNonInteractiveUI (parentUI )
93
94
94
- assert .Panics (t , func () { ui .AskForText ("" ) })
95
+ text , err := ui .AskForText (TextOpts {
96
+ Label : "" ,
97
+ Default : "foo" ,
98
+ ValidateFunc : func (s string ) (bool , string , error ) {
99
+ if s == "" {
100
+ return false , "" , fmt .Errorf ("should not be empty" )
101
+ }
102
+ return true , "" , nil
103
+ },
104
+ })
105
+
106
+ assert .Equal (t , text , "foo" )
107
+ assert .Nil (t , err )
108
+ })
109
+ t .Run ("default empty" , func (t * testing.T ) {
110
+ parentUI := & fakeui.FakeUI {}
111
+ ui := NewNonInteractiveUI (parentUI )
112
+
113
+ text , err := ui .AskForText (TextOpts {
114
+ Label : "" ,
115
+ Default : "" ,
116
+ ValidateFunc : func (s string ) (bool , string , error ) {
117
+ if s == "" {
118
+ return false , "" , fmt .Errorf ("should not be empty" )
119
+ }
120
+ return true , "" , nil
121
+ },
122
+ })
123
+
124
+ assert .Equal (t , text , "" )
125
+ assert .NotNil (t , err )
95
126
})
96
127
})
97
128
@@ -105,11 +136,18 @@ func TestNonInteractiveUI(t *testing.T) {
105
136
})
106
137
107
138
t .Run ("AskForChoice" , func (t * testing.T ) {
108
- t .Run ("panics " , func (t * testing.T ) {
139
+ t .Run ("non negative default value " , func (t * testing.T ) {
109
140
parentUI := & fakeui.FakeUI {}
110
141
ui := NewNonInteractiveUI (parentUI )
111
142
112
- assert .Panics (t , func () { ui .AskForChoice ("" , nil ) })
143
+ choice , err := ui .AskForChoice (ChoiceOpts {
144
+ Label : "" ,
145
+ Default : 1 ,
146
+ Choices : []string {"a" , "b" , "c" },
147
+ })
148
+
149
+ assert .Equal (t , choice , 1 )
150
+ assert .Nil (t , err )
113
151
})
114
152
})
115
153
0 commit comments