Documentation ¶
Overview ¶
Package surveymock provides mock for github.com/AlecAivazis/survey.
Index ¶
- Variables
- type Answer
- type Buffer
- type Confirm
- type ConfirmAnswer
- type Console
- type Expectation
- type HelpAnswer
- type InterruptAnswer
- type MockOption
- type Mocker
- type NoAnswer
- type Password
- func (p *Password) Answer(answer string) *PasswordAnswer
- func (p *Password) Expect(c Console) error
- func (p *Password) Interrupt()
- func (p *Password) Once() *Password
- func (b Password) Repeat() bool
- func (p *Password) ShowHelp(help string)
- func (p *Password) String() string
- func (p *Password) Times(i int) *Password
- func (p *Password) Twice() *Password
- type PasswordAnswer
- type StringWriter
- type Survey
- func (s *Survey) Expect(c Console) error
- func (s *Survey) ExpectConfirm(message string) *Confirm
- func (s *Survey) ExpectPassword(message string) *Password
- func (s *Survey) ExpectationsWereMet() error
- func (s *Survey) ResetExpectations()
- func (s *Survey) Start(fn func(stdio terminal.Stdio))
- func (s *Survey) WithTimeout(t time.Duration) *Survey
- type TestingT
Constants ¶
This section is empty.
Variables ¶
var ErrNoExpectation = errors.New("no expectation")
ErrNoExpectation indicates that there is no expectation.
var ReactionTime = 3 * time.Millisecond
ReactionTime is to create a small delay to simulate human reaction.
Functions ¶
This section is empty.
Types ¶
type Answer ¶ added in v0.2.0
type Answer interface { // Expect runs the expectation. Expect(c Console) error // String represents the answer as a string. String() string }
Answer is an expectation for answering a question.
type Buffer ¶ added in v0.3.6
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a goroutine safe bytes.Buffer.
func (*Buffer) Reset ¶ added in v0.3.7
func (b *Buffer) Reset()
Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset is the same as Truncate(0).
type Confirm ¶ added in v0.3.2
type Confirm struct {
// contains filtered or unexported fields
}
Confirm is an expectation of survey.Confirm.
func (*Confirm) Answer ¶ added in v0.3.2
func (c *Confirm) Answer(answer string) *ConfirmAnswer
Answer sets a custom answer to the prompt.
If the answer is not not empty, the mock expects to have a feedback from the survey:
`Sorry, your reply was invalid: "hello world!" is not a valid answer, please try again.` Survey.ExpectConfirm("Are you sure to delete this file?"). Answer("hello world!").
func (*Confirm) Interrupt ¶ added in v0.3.2
func (c *Confirm) Interrupt()
Interrupt marks the answer is interrupted.
Survey.ExpectConfirm("Are you sure to delete this file?"). Interrupt().
func (*Confirm) No ¶ added in v0.3.2
func (c *Confirm) No()
No sets "no" as the answer to the prompt.
Survey.ExpectConfirm("Are you sure to delete this file?"). No().
func (*Confirm) ShowHelp ¶ added in v0.3.2
ShowHelp sets help for the expectation.
Survey.ExpectConfirm("Are you sure to delete this file?"). ShowHelp("The file will be permanently deleted").
type ConfirmAnswer ¶ added in v0.3.2
type ConfirmAnswer struct {
// contains filtered or unexported fields
}
ConfirmAnswer is an answer for confirm question.
func (*ConfirmAnswer) Expect ¶ added in v0.3.2
func (a *ConfirmAnswer) Expect(c Console) error
Expect runs the expectation. nolint: errcheck,gosec
func (*ConfirmAnswer) Interrupted ¶ added in v0.3.2
func (a *ConfirmAnswer) Interrupted()
Interrupted expects the answer will be interrupted.
func (*ConfirmAnswer) String ¶ added in v0.3.2
func (a *ConfirmAnswer) String() string
String represents the answer as a string.
type Console ¶
type Console interface { // terminal device. Tty() *os.File // pty. Fd() uintptr // Close closes Console's tty. Calling Close will unblock Expect and ExpectEOF. Close() error // Send writes string s to Console's tty. Send(s string) (int, error) // SendLine writes string s to Console's tty with a trailing newline. SendLine(s string) (int, error) // Expectf reads from the Console's tty until the provided formatted string // is read or an error occurs, and returns the buffer read by Console. Expectf(format string, args ...interface{}) (string, error) // ExpectString reads from Console's tty until the provided string is read or // an error occurs, and returns the buffer read by Console. ExpectString(s string) (string, error) // ExpectEOF reads from Console's tty until EOF or an error occurs, and returns // the buffer read by Console. We also treat the PTSClosed error as an EOF. ExpectEOF() (string, error) // Expect reads from Console's tty until a condition specified from opts is // encountered or an error occurs, and returns the buffer read by console. // No extra bytes are read once a condition is met, so if a program isn't // expecting input yet, it will be blocked. Sends are queued up in tty's // internal buffer so that the next Expect will read the remaining bytes (i.e. // rest of prompt) as well as its conditions. Expect(opts ...expect.ExpectOpt) (string, error) }
Console is an interface wrapper around *expect.Console.
type Expectation ¶
type Expectation interface { // Expect runs the expectation. Expect(c Console) error // Repeat tells survey to repeat the same expectation. Repeat() bool // String represents the expectation as a string. String() string }
Expectation is an expectation for mocking survey.
type HelpAnswer ¶ added in v0.3.0
type HelpAnswer struct {
// contains filtered or unexported fields
}
HelpAnswer sends a ? to show the help.
func (*HelpAnswer) Expect ¶ added in v0.3.0
func (a *HelpAnswer) Expect(c Console) error
Expect runs the expectation. nolint: errcheck,gosec
func (*HelpAnswer) String ¶ added in v0.3.0
func (a *HelpAnswer) String() string
String represents the answer as a string.
type InterruptAnswer ¶ added in v0.2.0
type InterruptAnswer struct{}
InterruptAnswer sends an interrupt sequence to terminate the survey.
func (*InterruptAnswer) Expect ¶ added in v0.2.0
func (a *InterruptAnswer) Expect(c Console) error
Expect runs the expectation. nolint: errcheck,gosec
func (*InterruptAnswer) String ¶ added in v0.2.0
func (a *InterruptAnswer) String() string
String represents the answer as a string.
type Mocker ¶
Mocker mocks survey.
func Mock ¶
func Mock(mocks ...MockOption) Mocker
Mock creates a mocked server with expectations and assures that ExpectationsWereMet() is called.
type NoAnswer ¶ added in v0.2.0
type NoAnswer struct{}
NoAnswer sends an empty line to answer the question.
type Password ¶
type Password struct {
// contains filtered or unexported fields
}
Password is an expectation of survey.Password.
func (*Password) Answer ¶
func (p *Password) Answer(answer string) *PasswordAnswer
Answer sets the answer to the password prompt.
Survey.ExpectPassword("Enter password:"). Answer("hello world!").
func (*Password) Interrupt ¶
func (p *Password) Interrupt()
Interrupt marks the answer is interrupted.
Survey.ExpectPassword("Enter password:"). Interrupt().
func (*Password) Once ¶
Once indicates that the message should only be asked once.
Survey.ExpectPassword("Enter password:"). Answer("hello world!"). Once()
func (*Password) ShowHelp ¶ added in v0.3.0
ShowHelp sets help for the expectation.
Survey.ExpectPassword("Enter password:"). ShowHelp("Your shiny password").
type PasswordAnswer ¶ added in v0.2.0
type PasswordAnswer struct {
// contains filtered or unexported fields
}
PasswordAnswer is an answer for password question.
func (*PasswordAnswer) Expect ¶ added in v0.2.0
func (a *PasswordAnswer) Expect(c Console) error
Expect runs the expectation. nolint: errcheck,gosec
func (*PasswordAnswer) Interrupted ¶ added in v0.2.0
func (a *PasswordAnswer) Interrupted()
Interrupted expects the answer will be interrupted.
func (*PasswordAnswer) String ¶ added in v0.2.0
func (a *PasswordAnswer) String() string
String represents the answer as a string.
type StringWriter ¶
StringWriter is a wrapper for bytes.Buffer.
type Survey ¶
type Survey struct {
// contains filtered or unexported fields
}
Survey is a mocked survey.
func New ¶ added in v0.3.15
func New(t TestingT, mocks ...MockOption) *Survey
New creates a new mocked survey.
func (*Survey) ExpectConfirm ¶ added in v0.3.2
ExpectConfirm expects a Confirm.
Survey.ExpectConfirm("Confirm?"). Yes()
func (*Survey) ExpectPassword ¶
ExpectPassword expects a Password.
Survey.ExpectPassword("Enter password:"). Answer("hello world!")
func (*Survey) ExpectationsWereMet ¶
ExpectationsWereMet checks whether all queued expectations were met in order. If any of them was not met - an error is returned.
func (*Survey) ResetExpectations ¶ added in v0.3.8
func (s *Survey) ResetExpectations()
ResetExpectations resets all the expectations.