testing

package
v0.0.0-...-a0d7459 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2017 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 BadRun

func BadRun(c *gc.C, exit int, args ...string) string

BadRun is used to run a command, check the exit code, and return the output.

func HelpText

func HelpText(command cmd.Command, name string) string

HelpText returns a command's formatted help text.

func NullContext

func NullContext(c *gc.C) *cmd.Context

NullContext returns a no-op command context.

func RunCommand

func RunCommand(ctx *cmd.Context, com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error)

RunCommand runs the command and returns channels holding the command's operations and errors.

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

func NewPrompter(prompt func(string) (string, error)) *Prompter

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

func (p *Prompter) HasUnread() bool

HasUnread reports whether any input from the last prompt remains unread.

func (*Prompter) Read

func (p *Prompter) Read(buf []byte) (int, error)

Read implements io.Reader.

func (*Prompter) String

func (p *Prompter) String() string

String returns all the text that has been written to the prompter since it was created.

func (*Prompter) Tail

func (p *Prompter) Tail() string

Tail returns all the text written since the last prompt.

func (*Prompter) Write

func (p *Prompter) Write(buf []byte) (int, error)

Write implements io.Writer.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL