github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/prompt/mock/mock.go (about) 1 package mock 2 3 import ( 4 "reflect" 5 6 "github.com/ActiveState/cli/internal/prompt" 7 8 tmock "github.com/stretchr/testify/mock" 9 ) 10 11 var _ prompt.Prompter = &Mock{} 12 13 // Mock the struct to mock the Prompt struct 14 type Mock struct { 15 tmock.Mock 16 } 17 18 // Init an object 19 func Init() *Mock { 20 return &Mock{} 21 } 22 23 // Close it 24 func (m *Mock) Close() { 25 } 26 27 // Input prompts the user for input 28 func (m *Mock) Input(title, message string, defaultResponse *string, flags ...prompt.ValidatorFlag) (string, error) { 29 args := m.Called(title, message, defaultResponse, flags) 30 return args.String(0), failure(args.Get(1)) 31 } 32 33 // InputAndValidate prompts the user for input witha customer validator and validation flags 34 func (m *Mock) InputAndValidate(title, message string, defaultResponse *string, validator prompt.ValidatorFunc, flags ...prompt.ValidatorFlag) (response string, err error) { 35 args := m.Called(message, message, defaultResponse, validator) 36 return args.String(0), failure(args.Get(1)) 37 } 38 39 // Select prompts the user to select one entry from multiple choices 40 func (m *Mock) Select(title, message string, choices []string, defaultChoice *string) (string, error) { 41 args := m.Called(title, message, choices, defaultChoice) 42 return args.String(0), failure(args.Get(1)) 43 } 44 45 // Confirm prompts user for yes or no response. 46 func (m *Mock) Confirm(title, message string, defaultChoice *bool) (bool, error) { 47 args := m.Called(title, message, defaultChoice) 48 return args.Bool(0), failure(args.Get(1)) 49 } 50 51 // InputSecret prompts the user for input and obfuscates the text in stdout. 52 // Will fail if empty. 53 func (m *Mock) InputSecret(title, message string, flags ...prompt.ValidatorFlag) (response string, err error) { 54 args := m.Called(title, message) 55 return args.String(0), failure(args.Get(1)) 56 } 57 58 // OnMethod behaves like mock.On but disregards whether arguments match or not 59 func (m *Mock) OnMethod(methodName string) *tmock.Call { 60 methodType := reflect.ValueOf(m).MethodByName(methodName).Type() 61 anyArgs := []interface{}{} 62 for i := 0; i < methodType.NumIn(); i++ { 63 anyArgs = append(anyArgs, tmock.Anything) 64 } 65 return m.On(methodName, anyArgs...) 66 } 67 68 func (m *Mock) IsInteractive() bool { 69 return true 70 } 71 72 func failure(arg interface{}) error { 73 if arg == nil { 74 return nil 75 } 76 return arg.(error) 77 }