11package ui_test
22
33import (
4+ "fmt"
45 "testing"
56
67 . "github.com/cppforlife/go-cli-ui/ui"
@@ -87,11 +88,41 @@ func TestNonInteractiveUI(t *testing.T) {
8788 })
8889
8990 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 ) {
9192 parentUI := & fakeui.FakeUI {}
9293 ui := NewNonInteractiveUI (parentUI )
9394
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 )
95126 })
96127 })
97128
@@ -105,11 +136,18 @@ func TestNonInteractiveUI(t *testing.T) {
105136 })
106137
107138 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 ) {
109140 parentUI := & fakeui.FakeUI {}
110141 ui := NewNonInteractiveUI (parentUI )
111142
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 )
113151 })
114152 })
115153
0 commit comments