Documentation ¶
Overview ¶
Package demotools provides a set of tools that help you write code examples.
**Questioner**
The questioner is used in interactive examples to ask for input from the user at a command prompt, validate the answer, and ask the question again, if needed. It is exposed through an interface so that it can be mocked for unit testing. A pre-written mock is provided in the testtools package.
Index ¶
- type IAnswerValidator
- type IQuestioner
- type InFloatRange
- type InIntRange
- type MockQuestioner
- func (mock *MockQuestioner) Ask(question string, validators ...IAnswerValidator) string
- func (mock *MockQuestioner) AskBool(question string, expected string) bool
- func (mock *MockQuestioner) AskChoice(question string, choices []string) int
- func (mock *MockQuestioner) AskFloat64(question string, validators ...IAnswerValidator) float64
- func (mock *MockQuestioner) AskInt(question string, validators ...IAnswerValidator) int
- func (mock *MockQuestioner) AskPassword(question string, minLength int) string
- func (mock *MockQuestioner) Next(question string) string
- type NotEmpty
- type Questioner
- func (questioner Questioner) Ask(question string, validators ...IAnswerValidator) string
- func (questioner Questioner) AskBool(question string, expected string) bool
- func (questioner Questioner) AskChoice(question string, choices []string) int
- func (questioner Questioner) AskFloat64(question string, validators ...IAnswerValidator) float64
- func (questioner Questioner) AskInt(question string, validators ...IAnswerValidator) int
- func (questioner Questioner) AskPassword(question string, minLength int) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IAnswerValidator ¶
IAnswerValidator defines an interface that validates string input.
type IQuestioner ¶
type IQuestioner interface { Ask(question string, validators ...IAnswerValidator) string AskBool(question string, expected string) bool AskInt(question string, validators ...IAnswerValidator) int AskFloat64(question string, validators ...IAnswerValidator) float64 AskChoice(question string, choices []string) int AskPassword(question string, minLength int) string }
IQuestioner is an interface that asks questions at a command prompt and validates the answers.
type InFloatRange ¶
InFloatRange is a validator that verifies that the input is a float in the specified range (inclusive).
func (InFloatRange) IsValid ¶
func (inRange InFloatRange) IsValid(answer string) bool
type InIntRange ¶
InIntRange is a validator that verifies that the input is an int in the specified range (inclusive).
func (InIntRange) IsValid ¶
func (inRange InIntRange) IsValid(answer string) bool
type MockQuestioner ¶
type MockQuestioner struct { Answers []string // contains filtered or unexported fields }
MockQuestioner is a mock questioner that can be used to simulate user input during a unit test. Specify a slice of Answers that are returned in sequence during a test run.
func (*MockQuestioner) Ask ¶
func (mock *MockQuestioner) Ask(question string, validators ...IAnswerValidator) string
func (*MockQuestioner) AskBool ¶
func (mock *MockQuestioner) AskBool(question string, expected string) bool
func (*MockQuestioner) AskChoice ¶
func (mock *MockQuestioner) AskChoice(question string, choices []string) int
func (*MockQuestioner) AskFloat64 ¶
func (mock *MockQuestioner) AskFloat64(question string, validators ...IAnswerValidator) float64
func (*MockQuestioner) AskInt ¶
func (mock *MockQuestioner) AskInt(question string, validators ...IAnswerValidator) int
func (*MockQuestioner) AskPassword ¶
func (mock *MockQuestioner) AskPassword(question string, minLength int) string
func (*MockQuestioner) Next ¶
func (mock *MockQuestioner) Next(question string) string
Next returns the next answer in the slice of answers.
type NotEmpty ¶
type NotEmpty struct{}
NotEmpty is a validator that requires that the input be non-empty.
type Questioner ¶
type Questioner struct {
// contains filtered or unexported fields
}
Questioner implements IQuestioner and stores input in a reader.
func NewQuestioner ¶
func NewQuestioner() *Questioner
NewQuestioner returns a Questioner that is initialized with a reader.
func (Questioner) Ask ¶
func (questioner Questioner) Ask(question string, validators ...IAnswerValidator) string
Ask asks the specified question at a command prompt and validates the answer with the specified set of validators. If the answer fails validation, the question is asked again. The answer is trimmed of whitespace, but is otherwise not altered. When no validators are specified, the questioner waits for any answer and then continues.
func (Questioner) AskBool ¶
func (questioner Questioner) AskBool(question string, expected string) bool
AskBool asks a question with an expected answer. If the expected answer is given, it returns true; otherwise, it returns false.
func (Questioner) AskChoice ¶
func (questioner Questioner) AskChoice(question string, choices []string) int
func (Questioner) AskFloat64 ¶
func (questioner Questioner) AskFloat64(question string, validators ...IAnswerValidator) float64
AskFloat64 asks a question and converts the answer to a float64. If the answer cannot be converted, the function panics.
func (Questioner) AskInt ¶
func (questioner Questioner) AskInt(question string, validators ...IAnswerValidator) int
AskInt asks a question and converts the answer to an int. If the answer cannot be converted, the function panics.
func (Questioner) AskPassword ¶
func (questioner Questioner) AskPassword(question string, minLength int) string