Documentation ¶
Overview ¶
Package expect provides support for making expectations on the contents of input streams.
Index ¶
- type Lines
- func (s *Lines) Err() error
- func (s *Lines) ExpectEOF(ctx context.Context) error
- func (s *Lines) ExpectEventually(ctx context.Context, lines ...string) error
- func (s *Lines) ExpectEventuallyRE(ctx context.Context, expressions ...*regexp.Regexp) error
- func (s *Lines) ExpectNext(ctx context.Context, lines ...string) error
- func (s *Lines) ExpectNextRE(ctx context.Context, expressions ...*regexp.Regexp) error
- func (s *Lines) LastMatch() (int, string)
- type Option
- type UnexpectedInputError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Lines ¶
type Lines struct {
// contains filtered or unexported fields
}
Lines provides line oriented expecations and will block waiting for the expected input. A context with a timeout or deadline can be used to abort the expectation. Literal and regular expression matches are supported as is matching on EOF. Each operation accepts multiple literals or regular expressions that are treated as an 'or' to allow for convenient handling of different input orderings.
Example ¶
package main import ( "context" "fmt" "os" "cloudeng.io/cmdutil/expect" ) func main() { ctx := context.Background() rd, wr, _ := os.Pipe() st := expect.NewLineStream(rd) go func() { fmt.Fprintf(wr, "A\nready\nC\n") wr.Close() }() _ = st.ExpectEventually(ctx, "ready") fmt.Println(st.LastMatch()) _ = st.ExpectNext(ctx, "C") fmt.Println(st.LastMatch()) _ = st.ExpectEOF(ctx) if err := st.Err(); err != nil { panic(err) } }
Output: 2 ready 3 C
func NewLineStream ¶
NewLineStream creates a new instance of Lines.
func (*Lines) Err ¶
Err returns all errors encountered. Note that closing the underlying io.Reader is not considered an error unless ExpectEOF failed.
func (*Lines) ExpectEOF ¶
ExpectEOF will return nil if the underlying input stream is closed. It will block waiting for EOF; the supplied context can be used to provide a timeout.
func (*Lines) ExpectEventually ¶
ExpectEventually will return nil if (and as soon as) one of the supplied lines equals one of the lines read from the input stream. It will block waiting for matching lines; the supplied context can be used to provide a timeout.
func (*Lines) ExpectEventuallyRE ¶
ExpectEventuallyRE will return nil if (and as soon as) one of the supplied regular expressions matches one of the lines read from the input stream. It will block waiting for matching lines; the supplied context can be used to provide a timeout.
func (*Lines) ExpectNext ¶
ExpectNext will return nil if one of the supplied lines is equal to the next line read from the input stream. It will block waiting for the next line; the supplied context can be used to provide a timeout.
func (*Lines) ExpectNextRE ¶
ExpectNextRE will return nil if one of the supplied reqular expressions matches the next line read from the input stream. It will block waiting for the next line; the supplied context can be used to provide a timeout.
type Option ¶
type Option func(*options)
Option represents an option.
func TraceInput ¶
TraceInput enables tracing of input as it is read.
type UnexpectedInputError ¶
type UnexpectedInputError struct { Err error // An underlying error, if any, eg. context cancelation. Line int // The number of the last line that was read. Input string // The last input line that was read. EOF bool // Set if EOF was encountered. Eventually bool // Set if the input was 'eventually' expected. EOFExpected bool // Set if EOF was expected. Literals []string // The literal strings that were expected. Expressions []*regexp.Regexp // The regular sxpressiones that were expected. }
UnexpectedInputError represents a failed expectation, i.e. when the contents of the input do not match the expected contents.
func (*UnexpectedInputError) Error ¶
func (e *UnexpectedInputError) Error() string
Error implements error.
func (*UnexpectedInputError) Expectation ¶
func (e *UnexpectedInputError) Expectation() string