Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var FlagRunMain = flag.Bool("run-main", false, "Run the application's main function for recursive testing")
FlagRunMain is used to indicate that the -run-main flag was used.
Functions ¶
func NullContext ¶
NullContext returns a no-op command context.
Types ¶
type Prompter ¶
type Prompter struct {
// contains filtered or unexported fields
}
Prompter is designed to be used in a cmd.Context to check interactive request-response sequences using stdin and stdout.
func NewPrompter ¶
NewPrompter returns an io.ReadWriter implementation that calls the given function every time Read is called after some text has been written or if all the previously returned text has been read. The function's argument contains all the text printed since the last input. The function should return the text that the user is expected to type, or an error to return from Read. If it returns an empty string, and no error, it will return io.EOF instead.
func (*Prompter) HasUnread ¶
HasUnread reports whether any input from the last prompt remains unread.
func (*Prompter) String ¶
String returns all the text that has been written to the prompter since it was created.
type SeqPrompter ¶
type SeqPrompter struct { *Prompter // contains filtered or unexported fields }
func NewSeqPrompter ¶
func NewSeqPrompter(c *gc.C, userInputMarker, text string) *SeqPrompter
NewSeqPrompter returns a prompter that can be used to check a sequence of IO interactions. Expected input from the user is marked with the given user input marker (for example a distinctive unicode character that will not occur in the rest of the text) and runs to the end of a line.
All output text in between user input is treated as regular expressions.
As a special case, if an input marker is followed only by a single input marker on that line, the checker will cause io.EOF to be returned for that prompt.
The returned SeqPrompter wraps a Prompter and checks that each read and write corresponds to the expected action in the sequence.
After all interaction is done, CheckDone or AssertDone should be called to check that no more interactions are expected.
Any failures will result in the test failing.
For example given the prompter created with:
checker := NewSeqPrompter(c, "»", `What is your name: »Bob And your age: »148 You're .* old, Bob! `)
The following code will pass the checker:
fmt.Fprintf(checker, "What is your name: ") buf := make([]byte, 100) n, _ := checker.Read(buf) name := strings.TrimSpace(string(buf[0:n])) fmt.Fprintf(checker, "And your age: ") n, _ = checker.Read(buf) age, err := strconv.Atoi(strings.TrimSpace(string(buf[0:n]))) c.Assert(err, gc.IsNil) if age > 90 { fmt.Fprintf(checker, "You're very old, %s!\n", name) } checker.CheckDone()
func (*SeqPrompter) AssertDone ¶
func (p *SeqPrompter) AssertDone()
AssertDone is like CheckDone but aborts the test if the check fails.
func (*SeqPrompter) CheckDone ¶
func (p *SeqPrompter) CheckDone() bool
CheckDone asserts that all the expected prompts have been printed and all the replies read, and reports whether the check succeeded.