b
const (
// Debug for debug logging.
Debug = "\x1b[35m" // dark cyan
)
Runner runs UseCases. It provide focal point for an assembly of use case tests. Test output is feature based. A flag is available for each category of output.
type Runner struct {
// Server is the host and port of a URL. Example: http://localhost:8080
Server string
// Base of the URL path. By convention this is usually "/graphql".
Base string
// ShowComments if true will cause comments to be printed during a run.
ShowComments bool
// ShowRequests if true will cause request URL and content to be printed
// during a run.
ShowRequests bool
// ShowResponses if true will cause response JSON to be printed during a run.
ShowResponses bool
// NoColor if true turns off colorized output.
NoColor bool
// Indent is the number of spaces to indent JSON resonses. If 0 no
// modification to responses are made otherwise the JSON is unmarshalled
// and re-marshalled with an indentation.
Indent int
// UseCases to run.
UseCases []*UseCase
// Writer is an alternate io.Writer that will be used in place of writing
// to Stdout when logging if not nil.
Writer io.Writer
}
func (r *Runner) JSON(indents ...int) []byte
func (r *Runner) Log(color string, format string, args ...interface{})
Log output for one of the categories.
func (r *Runner) Native() interface{}
Native version of the Runner. Used for JSON() which is mostly for debugging.
func (r *Runner) Run() (err error)
Run the usecases.
func (r *Runner) String() string
Step is a step in a use case. Each describes how an HTTP request will be sent to a GraphQL server and what to do with the response.
type Step struct {
// Label of the step as a way to refer to the step.
Label string
// Comment describes the step.
Comment string
// Path is the path part of the URL that will be used in the request to
// the GraphQL server. The path is assumed to be relative to the base URL
// unless it starts with a '/' character.
Path string
// Content is the content when using a POST request. If the Content is not
// empty then a POST is used. If it is empty then a GET request is made to
// the server. The string should be either GraphQL or the HTTP GET format.
Content string
// UseJSON if true and a POST request then the JSON format will be used
// with a COntent-Type of "applicaiton/json" otherwise the Content will be
// sent as GraphQL with a Content-Type of "application/graphql".
UseJSON bool
// Remember in a series of steps it is often helpful to remember values
// from earlier steps. The Remember map describes what to remember and
// what key to store that value in. In the map, the keys are the keys for
// the memory cache while the Remember map values are the path to the
// value to remember. The path is a simple dot delimited path. It is not a
// full JSON path (maybe in the future).
Remember map[string]string
// Op is the operation to include in either the URL query or as a value
// for the 'operationName' if using JSON in the Content.
Op string
// Vars are the variables to be passed along with a GraphQL request. They
// are appended to the URL or added to the JSON 'variables' element if the
// POST contents is JSON. The values in the Vars map can be either a value
// or a string that begines with a '$' which indicates a rmembered value
// should be used instead.
Vars map[string]interface{}
// SortBy are the sort keys for the result. Depending on the
// implementation of the GraphQL server, the order of returned objects may
// not be consistent. To make it easier to use in testing the SortBy keys
// are the paths to arrays while the value for the keys are the attributes
// to sort on.
SortBy map[string]string
// Expect is the expected contents. Like the Content it can be a nil, string,
// array, or map. A nil value indicates no checking of the response is
// needed. Strings are used mostly for fetched HTML while the normal JSON
// responses are converted to a genereric map[string]interface{} and
// compared to the Expect value.
//
// The rules for comparison are:
// 1) Any element in the Expect value must be present in the response.
// 2) Elements in the response not in the Expect value are ignored.
// 3) If the Expect value is a string that starts and ends with a '/'
// character the string is assumed to be a regular expression is
// matched against the string in the reponse or the response is
// converted to a strings using fmt.Sprintf "%v" and then compared.
// 4) Maps and arrays are followed recursively.
Expect interface{}
}
func (s *Step) Execute(uc *UseCase) error
Execute the step using the information in the provided use case such as remembered values and base URL.
func (s *Step) Native() interface{}
func (s *Step) Set(data interface{}) (err error)
UseCase encapsulates a use case composed of multiple steps. The use case can be read from a JSON file in which case the Filepath member will be set.
type UseCase struct {
// Comment is the description of the use case.
Comment string
// Filepath is the path to the file that the use case was read from or
// will be written to.
Filepath string
// Steps are the steps to be taken in the use case.
Steps []*Step
// contains filtered or unexported fields
}
func NewUseCase(filepath string) (uc *UseCase, err error)
NewUseCase creates a new UseCase from a file.
func (uc *UseCase) JSON(indents ...int) []byte
func (uc *UseCase) Native() interface{}
func (uc *UseCase) Run(r *Runner) (err error)
func (uc *UseCase) String() string