Documentation
¶
Overview ¶
Package demotools provides a set of tools that help you write code examples.
**FileSystem**
The filesystem is used to abstract the os module and allow for variations to be swapped out, such as mocks for testing.
Package demotools provides a set of tools that help you write code examples.
**Pausable**
The pausable interface creates an easy to mock pausing object for testing.
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 FileSystem
- type IAnswerValidator
- type IFileSystem
- type IPausable
- type IQuestioner
- type InFloatRange
- type InIntRange
- type MockFileSystem
- 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 Pauser
- 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 FileSystem ¶
type FileSystem struct{}
FileSystem is a struct to hold the injected object.
func NewStandardFileSystem ¶
func NewStandardFileSystem() *FileSystem
NewStandardFileSystem uses the plain os module implementations.
func (FileSystem) CloseFile ¶
func (filesystem FileSystem) CloseFile(file io.ReadWriteCloser)
CloseFile closes the provided file.
func (FileSystem) Getwd ¶
func (filesystem FileSystem) Getwd() (string, error)
Getwd returns the current working directory using the os module.
func (FileSystem) OpenFile ¶
func (filesystem FileSystem) OpenFile(filename string) (io.ReadWriteCloser, error)
OpenFile returns an io.ReadWriterCloser object based on the provided filename.
type IAnswerValidator ¶
IAnswerValidator defines an interface that validates string input.
type IFileSystem ¶
type IFileSystem interface { Getwd() (string, error) OpenFile(filename string) (io.ReadWriteCloser, error) CloseFile(file io.ReadWriteCloser) }
IFileSystem defines an interface that provides basic file i/o actions.
type IPausable ¶
type IPausable interface {
Pause(secs int)
}
IPausable defines the interface for pausable objects.
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 MockFileSystem ¶
type MockFileSystem struct {
// contains filtered or unexported fields
}
MockFileSystem is a mock version of IFileSystem for testing.
func NewMockFileSystem ¶
func NewMockFileSystem(mockfile io.ReadWriteCloser) *MockFileSystem
NewMockFileSystem mocks the FileSystem and holds a singular io.ReadWriteCloser object to return.
func (MockFileSystem) CloseFile ¶
func (filesystem MockFileSystem) CloseFile(_ io.ReadWriteCloser)
CloseFile closes the io.ReadWriteCloser provided on object creation.
func (MockFileSystem) Getwd ¶
func (filesystem MockFileSystem) Getwd() (string, error)
Getwd returns a basic mock directory string.
func (MockFileSystem) OpenFile ¶
func (filesystem MockFileSystem) OpenFile(_ string) (io.ReadWriteCloser, error)
OpenFile returns the io.ReadWriteCloser provided on object creation.
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