matchers

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ArrayMinLike = EachLike
View Source
var IPv4Address = IPAddress

IPv4Address matches valid IPv4 addresses.

View Source
var Regex = Term

Regex is a more appropriately named alias for the "Term" matcher

Functions

This section is empty.

Types

type HeadersMatcher

type HeadersMatcher = map[string][]Matcher

type Map

type Map MapMatcher

type MapMatcher

type MapMatcher map[string]Matcher

MapMatcher allows a map[string]string-like object to also contain complex matchers

func (*MapMatcher) UnmarshalJSON

func (m *MapMatcher) UnmarshalJSON(bytes []byte) (err error)

UnmarshalJSON is a custom JSON parser for MapMatcher It treats the matchers as strings

type Matcher

type Matcher interface {

	// GetValue returns the raw generated value for the matcher
	// without any of the matching detail context
	GetValue() interface{}
	// contains filtered or unexported methods
}

Matcher allows various implementations such String or StructMatcher to be provided in when matching with the DSL We use the strategy outlined at http://www.jerf.org/iri/post/2917 to create a "sum" or "union" type.

func ArrayContaining

func ArrayContaining(variants []interface{}) Matcher

ArrayContaining allows heterogenous items to be matched within a list. Unlike EachLike which must be an array with elements of the same shape, ArrayContaining allows objects of different types and shapes.

func ArrayMaxLike

func ArrayMaxLike(content interface{}, max int) Matcher

ArrayMaxLike is like EachLike except has a bounds on the max https://github.com/pact-foundation/pact-specification/tree/version-3#add-a-minmax-type-matcher

func ArrayMinMaxLike

func ArrayMinMaxLike(content interface{}, min int, max int) Matcher

ArrayMinMaxLike is like EachLike except has a bounds on the max and the min https://github.com/pact-foundation/pact-specification/tree/version-3#add-a-minmax-type-matcher

func Date

func Date() Matcher

Date matches a pattern corresponding to the ISO_DATE_FORMAT, which is "yyyy-MM-dd". The current date is used as the eaxmple.

func DateGenerated

func DateGenerated(example string, format string) Matcher

DateGenerated matches a cross platform formatted date, and generates a current date during verification String example value must match the provided date format string. See Java SimpleDateFormat https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html for formatting options

func DateTimeGenerated

func DateTimeGenerated(example string, format string) Matcher

DateTimeGenerated matches a cross platform formatted datetime, and generates a current datetime during verification String example value must match the provided datetime format string. See Java SimpleDateFormat https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html for formatting options

func Decimal

func Decimal(example float64) Matcher

Decimal defines a matcher that accepts any decimal value.

func EachKeyLike

func EachKeyLike(key string, template interface{}) Matcher

Object where the key itself is ignored, but the value template must match.

key - Example key to use (which will be ignored) template - Example value template to base the comparison on

func EachLike

func EachLike(content interface{}, minRequired int) Matcher

EachLike specifies that a given element in a JSON body can be repeated "minRequired" times. Number needs to be 1 or greater

Example
match := EachLike(1, 3)
fmt.Println(formatJSON(match))
Output:

{
	"pact:matcher:type": "type",
	"value": [
		1,
		1,
		1
	],
	"min": 3
}

func Equality

func Equality(content interface{}) Matcher

Equality resets matching cascades back to equality see https://github.com/pact-foundation/pact-specification/tree/version-3#add-an-equality-matcher

func FromProviderState

func FromProviderState(expression, example string) Matcher

Marks a item as to be injected from the provider state

"expression" is used to lookup the dynamic value from the provider state context during verification "example" is the example value to used in the consumer test

func HexValue

func HexValue() Matcher

HexValue defines a matcher that accepts hexidecimal values.

func IPAddress

func IPAddress() Matcher

IPAddress defines a matcher that accepts valid IPv4 addresses.

func IPv6Address

func IPv6Address() Matcher

IPv6Address defines a matcher that accepts IP addresses.

func Identifier

func Identifier() Matcher

Identifier defines a matcher that accepts integer values.

func Includes

func Includes(content string) Matcher

func Integer

func Integer(example int) Matcher

Integer defines a matcher that accepts any integer value.

func Like

func Like(content interface{}) Matcher

Like specifies that the given content type should be matched based on type (int, string etc.) instead of a verbatim match.

Example (Number)
match := Like(42)
fmt.Println(formatJSON(match))
Output:

{
	"specification": "2.0.0",
	"pact:matcher:type": "type",
	"value": 42
}
Example (Object)
match := Like(map[string]string{"baz": "bat"})
fmt.Println(formatJSON(match))
Output:

{
	"specification": "2.0.0",
	"pact:matcher:type": "type",
	"value": {
		"baz": "bat"
	}
}
Example (String)
match := Like("myspecialvalue")
fmt.Println(formatJSON(match))
Output:

{
	"specification": "2.0.0",
	"pact:matcher:type": "type",
	"value": "myspecialvalue"
}

func MatchV2

func MatchV2(src interface{}) Matcher

Match recursively traverses the provided type and outputs a matcher string for it that is compatible with the Pact dsl. By default, it requires slices to have a minimum of 1 element. For concrete types, it uses `dsl.Like` to assert that types match. Optionally, you may override these defaults by supplying custom pact tags on your structs.

Supported Tag Formats Minimum Slice Size: `pact:"min=2"` String RegEx: `pact:"example=2000-01-01,regex=^\\d{4}-\\d{2}-\\d{2}$"`

func Term

func Term(generate string, matcher string) Matcher

Term specifies that the matching should generate a value and also match using a regular expression.

Example
match := Term("myawesomeword", `\w+`)
fmt.Println(formatJSON(match))
Output:

{
	"pact:matcher:type": "regex",
	"value": "myawesomeword",
	"regex": "\\w+"
}

func Time

func Time() Matcher

Time matches a pattern corresponding to the ISO_DATE_FORMAT, which is "'T'HH:mm:ss". The current tem is used as the eaxmple.

func TimeGenerated

func TimeGenerated(example string, format string) Matcher

TimeGenerated matches a cross platform formatted date, and generates a current time during verification String example value must match the provided time format string. See Java SimpleDateFormat https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html for formatting options

func Timestamp

func Timestamp() Matcher

Timestamp matches a pattern corresponding to the ISO_DATETIME_FORMAT, which is "yyyy-MM-dd'T'HH:mm:ss". The current date and time is used as the eaxmple.

func UUID

func UUID() Matcher

UUID defines a matcher that accepts UUIDs. Produces a v4 UUID as the example.

type MetadataMatcher

type MetadataMatcher = MapMatcher

type Null

type Null struct{}

Null is a matcher that only accepts nulls

func (Null) GetValue

func (n Null) GetValue() interface{}

func (Null) MarshalJSON

func (n Null) MarshalJSON() ([]byte, error)

type QueryMatcher

type QueryMatcher map[string][]Matcher

QueryMatcher matches a query string interface type QueryMatcher map[string][]interface{} // Why was it an interface and not the same as the others? Can only be string values anyway, or

type S

type S string

S is the string primitive wrapper (alias) for the Matcher type, it allows plain strings to be matched To keep backwards compatible with previous versions we aren't using an alias here

func (S) GetValue

func (s S) GetValue() interface{}

GetValue returns the raw generated value for the matcher without any of the matching detail context

func (S) MarshalJSON

func (s S) MarshalJSON() ([]byte, error)

type String

type String string

String is the longer named form of the string primitive wrapper, it allows plain strings to be matched

func (String) GetValue

func (s String) GetValue() interface{}

GetValue returns the raw generated value for the matcher without any of the matching detail context

func (String) MarshalJSON

func (s String) MarshalJSON() ([]byte, error)

type StructMatcher

type StructMatcher map[string]interface{}

StructMatcher matches a complex object structure, which may itself contain nested Matchers

func (StructMatcher) GetValue

func (m StructMatcher) GetValue() interface{}

GetValue returns the raw generated value for the matcher without any of the matching detail context

Jump to

Keyboard shortcuts

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