expect

package
v0.0.0-...-2102962 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Package cloudeng.io/cmdutil/expect

import cloudeng.io/cmdutil/expect

Package expect provides support for making expectations on the contents of input streams.

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.

Functions
func NewLineStream(rd io.Reader, opts ...Option) *Lines

NewLineStream creates a new instance of Lines.

Methods
func (s *Lines) Err() error

Err returns all errors encountered. Note that closing the underlying io.Reader is not considered an error unless ExpectEOF failed.

func (s *Lines) ExpectEOF(ctx context.Context) error

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 (s *Lines) ExpectEventually(ctx context.Context, lines ...string) error

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 (s *Lines) ExpectEventuallyRE(ctx context.Context, expressions ...*regexp.Regexp) error

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 (s *Lines) ExpectNext(ctx context.Context, lines ...string) error

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 (s *Lines) ExpectNextRE(ctx context.Context, expressions ...*regexp.Regexp) error

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.

func (s *Lines) LastMatch() (int, string)

LastMatch returns the line number and contents of the last successfully matched input line.

Type Option
type Option func(*options)

Option represents an option.

Functions
func TraceInput(out io.Writer) Option

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.

Methods
func (e *UnexpectedInputError) Error() string

Error implements error.

func (e *UnexpectedInputError) Expectation() string

Examples

ExampleLines

Documentation

Overview

Package expect provides support for making expectations on the contents of input streams.

Index

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

func NewLineStream(rd io.Reader, opts ...Option) *Lines

NewLineStream creates a new instance of Lines.

func (*Lines) Err

func (s *Lines) Err() error

Err returns all errors encountered. Note that closing the underlying io.Reader is not considered an error unless ExpectEOF failed.

func (*Lines) ExpectEOF

func (s *Lines) ExpectEOF(ctx context.Context) error

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

func (s *Lines) ExpectEventually(ctx context.Context, lines ...string) error

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

func (s *Lines) ExpectEventuallyRE(ctx context.Context, expressions ...*regexp.Regexp) error

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

func (s *Lines) ExpectNext(ctx context.Context, lines ...string) error

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

func (s *Lines) ExpectNextRE(ctx context.Context, expressions ...*regexp.Regexp) error

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.

func (*Lines) LastMatch

func (s *Lines) LastMatch() (int, string)

LastMatch returns the line number and contents of the last successfully matched input line.

type Option

type Option func(*options)

Option represents an option.

func TraceInput

func TraceInput(out io.Writer) Option

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

Jump to

Keyboard shortcuts

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