-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
83 lines (69 loc) · 2.13 KB
/
prompt.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
package prompt
import (
"reflect"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/stretchr/testify/mock"
)
// Prompt can be used to request user input.
type Prompt interface {
AskOne(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error
}
type prompt struct{}
// New returns a prompt which just directly calls survey.AskOne.
func New() Prompt { return new(prompt) }
// AskOne implements Prompt.
func (prompt) AskOne(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error {
return survey.AskOne(p, response, opts...)
}
// FakePrompt is a mock implementation of a Prompt.
type FakePrompt struct {
mock.Mock
}
// AskOne implements Prompt.
func (f *FakePrompt) AskOne(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error {
args := f.Called(p, response, opts)
return args.Error(0)
}
// Stubber can stub prompts.
type Stubber struct {
p *FakePrompt
}
// NewStubber creates a new *Stubber which can stub out calls to returned
// *FakePrompt's AskOne method.
func NewStubber() (*Stubber, *FakePrompt) {
s := &Stubber{p: &FakePrompt{}}
return s, s.p
}
// StubOne makes the next prompt return value.
func (s *Stubber) StubOne(value interface{}) {
s.p.On("AskOne", mock.Anything, mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
_ = core.WriteAnswer(args.Get(1), "", value)
}).
Return(nil).
Once()
}
// StubOneDefault makes the next prompt return its default value.
func (s *Stubber) StubOneDefault() {
s.p.On("AskOne", mock.Anything, mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
fieldValue := reflect.ValueOf(args.Get(0)).Elem().FieldByName("Default")
defaultValue := fieldValue.Interface()
_ = core.WriteAnswer(args.Get(1), "", defaultValue)
}).
Return(nil).
Once()
}
// StubOneError makes the next prompt return err.
func (s *Stubber) StubOneError(err error) {
s.p.On("AskOne", mock.Anything, mock.Anything, mock.Anything).
Return(err).
Once()
}
// StubOne makes the next prompts return the provided values.
func (s *Stubber) StubMany(values ...interface{}) {
for _, value := range values {
s.StubOne(value)
}
}