assistdog

package module
v0.0.0-...-b5b791d Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: MIT Imports: 7 Imported by: 16

README

Assist Dog

Utility functions for using with Gherkin specs

Please see the Godoc documentation and examples.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Assist

type Assist struct {
	// contains filtered or unexported fields
}

Assist provides utility methods to deal with Gherkin tables.

func NewDefault

func NewDefault() *Assist

NewDefault creates a new Assist instance with all the default parsers and comparers.

func (*Assist) CompareToInstance

func (a *Assist) CompareToInstance(actual interface{}, table *godog.Table) error

CompareToInstance compares an actual value to the expected fields from a Gherkin table.

Example
table := NewTable([][]string{
	{"Name", "John"},  //  | Name   | John |
	{"Height", "182"}, //  | Height | 182  |
})

actual := &Person{
	Name:   "John",
	Height: 182,
}

assist := assistdog.NewDefault()
err := assist.CompareToInstance(actual, table)
if err != nil {
	panic(err)
}
Output:

func (*Assist) CompareToSlice

func (a *Assist) CompareToSlice(actual interface{}, table *godog.Table) error

CompareToSlice compares an actual slice of values to the expected rows from a Gherkin table.

Example
table := NewTable([][]string{
	{"Name", "Height"}, // | Name | Height |
	{"John", "182"},    // | John | 182    |
	{"Mary", "170"},    // | Mary | 170    |
})

actual := []*Person{
	{Name: "John", Height: 182},
	{Name: "Mary", Height: 170},
}

assist := assistdog.NewDefault()
err := assist.CompareToSlice(actual, table)
if err != nil {
	panic(err)
}
Output:

func (*Assist) CreateInstance

func (a *Assist) CreateInstance(tp interface{}, table *godog.Table) (interface{}, error)

CreateInstance takes a type and a Gherkin table and returns an instance of that type filled with the table's parsed values. The table must have exactly two columns, where the first represents the field names and the second represents the values.

Example
table := NewTable([][]string{
	{"Name", "John"},  //  | Name   | John |
	{"Height", "182"}, //  | Height | 182  |
})

assist := assistdog.NewDefault()
result, err := assist.CreateInstance(new(Person), table)
if err != nil {
	panic(err)
}

reflect.DeepEqual(result, &Person{
	Name:   "John",
	Height: 182,
})
Output:

func (*Assist) CreateSlice

func (a *Assist) CreateSlice(tp interface{}, table *godog.Table) (interface{}, error)

CreateSlice takes a type and a Gherkin table and returns a slice of that type filled with each row as an instance. The first row acts as a header and provides the field names for each column.

Example
table := NewTable([][]string{
	{"Name", "Height"}, // | Name | Height |
	{"John", "182"},    // | John | 182    |
	{"Mary", "170"},    // | Mary | 170    |
})

assist := assistdog.NewDefault()
result, err := assist.CreateSlice(new(Person), table)
if err != nil {
	panic(err)
}

reflect.DeepEqual(result, []*Person{
	{Name: "John", Height: 182},
	{Name: "Mary", Height: 170},
})
Output:

func (*Assist) ParseMap

func (a *Assist) ParseMap(table *godog.Table) (map[string]string, error)

ParseMap takes a Gherkin table and returns a map that represents it. The table must have exactly two columns, where the first represents the key and the second represents the value.

func (*Assist) ParseSlice

func (a *Assist) ParseSlice(table *godog.Table) ([]map[string]string, error)

ParseSlice takes a Gherkin table and returns a slice of maps representing each row. The first row acts as a header and provides the keys.

func (*Assist) RegisterComparer

func (a *Assist) RegisterComparer(i interface{}, comparer CompareFunc)

RegisterComparer registers a new value comparer for a type. If a previous comparer already exists for the given type, it will be replaced.

func (*Assist) RegisterParser

func (a *Assist) RegisterParser(i interface{}, parser ParseFunc)

RegisterParser registers a new value parser for a type. If a previous parser already exists for the given type, it will be replaced.

func (*Assist) RemoveComparer

func (a *Assist) RemoveComparer(i interface{})

RemoveComparer removes the value comparer for a type.

func (*Assist) RemoveParser

func (a *Assist) RemoveParser(i interface{})

RemoveParser removes the value parser for a type.

type CompareFunc

type CompareFunc func(raw string, actual interface{}) error

CompareFunc compares a raw string value from a table to an actual typed value. If the values are considered a match, no error should be returned. Otherwise, an error that describes the differences should be returned.

type ParseFunc

type ParseFunc func(raw string) (interface{}, error)

ParseFunc parses a raw string value from a table into a given type. If it succeeds, it should return the parsed typed value. Otherwise, it should return an error describing why the value could not be parsed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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