forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
surveyorMock.go
41 lines (33 loc) · 887 Bytes
/
surveyorMock.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
package survey
import (
"github.com/AlecAivazis/survey/v2/core"
)
type SurveyorMock struct {
singleResponse interface{}
multipleResponses map[string]interface{}
}
func (mock *SurveyorMock) AskOne(p Prompt, response interface{}, opts ...AskOpt) error {
err := core.WriteAnswer(response, "", mock.singleResponse)
if err != nil {
// panicing is fine inside a mock
panic(err)
}
return nil
}
func (mock *SurveyorMock) Ask(qs []*Question, response interface{}, opts ...AskOpt) error {
for _, q := range qs {
err := core.WriteAnswer(response, q.Name, mock.multipleResponses[q.Name])
if err != nil {
// panicing is fine inside a mock
panic(err)
}
}
return nil
}
func (mock *SurveyorMock) SetResponse(response interface{}) {
if val, ok := response.(map[string]interface{}); ok {
mock.multipleResponses = val
} else {
mock.singleResponse = response
}
}