matchers

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 7 Imported by: 3

README

OnPar Matchers

OnPar provides a set of minimalistic matchers to get you started. However, the intention is to be able to write your own custom matchers so that your code is more readable.

Matchers List

String Matchers

StartWith

StartWithMatcher accepts a string and succeeds if the actual string starts with the expected string.

Expect(t, "foobar").To(StartWith("foo"))
EndWith

EndWithMatcher accepts a string and succeeds if the actual string ends with the expected string.

Expect(t, "foobar").To(EndWith("bar"))
ContainSubstring

ContainSubstringMatcher accepts a string and succeeds if the expected string is a sub-string of the actual.

Expect(t, "foobar").To(ContainSubstring("ooba"))
MatchRegexp

Logical Matchers

Not

NotMatcher accepts a matcher and will succeed if the specified matcher fails.

Expect(t, false).To(Not(BeTrue()))
BeAbove

BeAboveMatcher accepts a float64. It succeeds if the actual is greater than the expected.

Expect(t, 100).To(BeAbove(99))
BeBelow

BeBelowMatcher accepts a float64. It succeeds if the actual is less than the expected.

Expect(t, 100).To(BeBelow(101))
BeFalse

BeFalseMatcher will succeed if actual is false.

Expect(t, 2 == 3).To(BeFalse())
BeTrue

BeTrueMatcher will succeed if actual is true.

Expect(t, 2 == 2).To(BeTrue())
Equal

EqualMatcher performs a DeepEqual between the actual and expected.

Expect(t, 42).To(Equal(42))

Error Matchers

HaveOccurred

HaveOccurredMatcher will succeed if the actual value is a non-nil error.

Expect(t, err).To(HaveOccurred())

Expect(t, nil).To(Not(HaveOccurred()))

Channel Matchers

Receive

ReceiveMatcher only accepts a readable channel. It will error for anything else. It will attempt to receive from the channel but will not block. It fails if the channel is closed.

c := make(chan bool, 1)
c <- true
Expect(t, c).To(Receive())
BeClosed

BeClosedMatcher only accepts a readable channel. It will error for anything else. It will succeed if the channel is closed.

c := make(chan bool)
close(c)
Expect(t, c).To(BeClosed())

Collection Matchers

HaveCap

This matcher works on Slices, Arrays, Maps and Channels and will succeed if the type has the specified capacity.

Expect(t, []string{"foo", "bar"}).To(HaveCap(2))
HaveKey

HaveKeyMatcher accepts map types and will succeed if the map contains the specified key.

Expect(t, fooMap).To(HaveKey("foo"))
HaveLen

HaveLenMatcher accepts Strings, Slices, Arrays, Maps and Channels. It will succeed if the type has the specified length.

Expect(t, "12345").To(HaveLen(5))

Other Matchers

Always

AlwaysMatcher matches by polling the child matcher until it returns an error. It will return an error the first time the child matcher returns an error. If the child matcher never returns an error, then it will return a nil.

By default, the duration is 100ms with an interval of 10ms.

isTrue := func() bool {
  return true
}
Expect(t, isTrue).To(Always(BeTrue()))
Chain
ViaPolling

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AlwaysMatcher

type AlwaysMatcher struct {
	Matcher            Matcher
	Duration, Interval time.Duration
}

AlwaysMatcher matches by polling the child matcher until it returns an error. It will return an error the first time the child matcher returns an error. If the child matcher never returns an error, then it will return a nil.

Duration is the longest scenario for the matcher if the child matcher continues to return nil

Interval is the period between polling.

func Always

func Always(m Matcher) AlwaysMatcher

Always returns a default AlwaysMatcher. Length of 100ms and rate 10ms

func (AlwaysMatcher) Match

func (m AlwaysMatcher) Match(actual any) (any, error)

Match takes a value that can change over time. Therefore, the only two valid options are a function with no arguments and a single return type, or a readable channel. Anything else will return an error.

If actual is a channel, then the child matcher will have to handle reading from the channel.

If the actual is a function, then the matcher will invoke the value and pass the returned value to the child matcher.

type AndMatcher

type AndMatcher struct {
	Children []Matcher
}

func And

func And(a, b Matcher, ms ...Matcher) AndMatcher

func (AndMatcher) Match

func (m AndMatcher) Match(actual any) (any, error)

type BeAboveMatcher

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

BeAboveMatcher accepts a float64. It succeeds if the actual is greater than the expected.

func BeAbove

func BeAbove(expected float64) BeAboveMatcher

BeAbove returns a BeAboveMatcher with the expected value.

func (BeAboveMatcher) Match

func (m BeAboveMatcher) Match(actual any) (any, error)

type BeBelowMatcher

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

BeBelowMatcher accepts a float64. It succeeds if the actual is less than the expected.

func BeBelow

func BeBelow(expected float64) BeBelowMatcher

BeBelow returns a BeBelowMatcher with the expected value.

func (BeBelowMatcher) Match

func (m BeBelowMatcher) Match(actual any) (any, error)

type BeClosedMatcher

type BeClosedMatcher struct{}

BeClosedMatcher only accepts a readable channel. It will error for anything else. It will succeed if the channel is closed.

func BeClosed

func BeClosed() BeClosedMatcher

BeClosed returns a BeClosedMatcher

func (BeClosedMatcher) Match

func (m BeClosedMatcher) Match(actual any) (any, error)

type BeFalseMatcher

type BeFalseMatcher struct{}

BeFalseMatcher will succeed if actual is false.

func BeFalse

func BeFalse() BeFalseMatcher

BeFalse will return a BeFalseMatcher

func (BeFalseMatcher) Match

func (m BeFalseMatcher) Match(actual any) (any, error)

type BeNilMatcher

type BeNilMatcher struct{}

BeNilMatcher will succeed if actual is nil.

func BeNil

func BeNil() BeNilMatcher

BeNil will return a BeNilMatcher.

func (BeNilMatcher) Match

func (m BeNilMatcher) Match(actual any) (any, error)

type BeTrueMatcher

type BeTrueMatcher struct{}

BeTrueMatcher will succeed if actual is true.

func BeTrue

func BeTrue() BeTrueMatcher

BeTrue will return a BeTrueMatcher

func (BeTrueMatcher) Match

func (m BeTrueMatcher) Match(actual any) (any, error)

type ChainMatcher

type ChainMatcher struct {
	Children []Matcher
}

func Chain

func Chain(a, b Matcher, ms ...Matcher) ChainMatcher

func (ChainMatcher) Match

func (m ChainMatcher) Match(actual any) (any, error)

type ContainMatcher

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

func Contain

func Contain(values ...any) ContainMatcher

func (ContainMatcher) Match

func (m ContainMatcher) Match(actual any) (any, error)

type ContainSubstringMatcher

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

ContainSubstringMatcher accepts a string and succeeds if the actual string contains the expected string.

func ContainSubstring

func ContainSubstring(substr string) ContainSubstringMatcher

ContainSubstring returns a ContainSubstringMatcher with the expected substring.

func (ContainSubstringMatcher) Match

func (m ContainSubstringMatcher) Match(actual any) (any, error)

type Differ

type Differ interface {
	Diff(actual, expected any) string
}

Differ is a type that can show a detailed difference between an actual and an expected value.

type EndWithMatcher

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

EndWithMatcher accepts a string and succeeds if the actual string ends with the expected string.

func EndWith

func EndWith(suffix string) EndWithMatcher

EndWith returns an EndWithMatcher with the expected suffix.

func (EndWithMatcher) Match

func (m EndWithMatcher) Match(actual any) (any, error)

type EqualMatcher

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

EqualMatcher performs a DeepEqual between the actual and expected.

func Equal

func Equal(expected any) *EqualMatcher

Equal returns an EqualMatcher with the expected value

func (EqualMatcher) Match

func (m EqualMatcher) Match(actual any) (any, error)

func (*EqualMatcher) UseDiffer

func (m *EqualMatcher) UseDiffer(d Differ)

type FetchMatcher

type FetchMatcher struct {
	OutputTo any
}

func Fetch

func Fetch(outputTo any) FetchMatcher

func (FetchMatcher) Match

func (m FetchMatcher) Match(actual any) (any, error)

type HaveCapMatcher

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

This matcher works on Slices, Arrays, Maps and Channels and will succeed if the type has the specified capacity.

func HaveCap

func HaveCap(expected int) HaveCapMatcher

HaveCap returns a HaveCapMatcher with the specified capacity

func (HaveCapMatcher) Match

func (m HaveCapMatcher) Match(actual any) (any, error)

type HaveKeyMatcher

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

HaveKeyMatcher accepts map types and will succeed if the map contains the specified key.

func HaveKey

func HaveKey(key any) HaveKeyMatcher

HaveKey returns a HaveKeyMatcher with the specified key.

func (HaveKeyMatcher) Match

func (m HaveKeyMatcher) Match(actual any) (any, error)

type HaveLenMatcher

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

HaveLenMatcher accepts Strings, Slices, Arrays, Maps and Channels. It will succeed if the type has the specified length.

func HaveLen

func HaveLen(expected int) HaveLenMatcher

HaveLen returns a HaveLenMatcher with the specified length.

func (HaveLenMatcher) Match

func (m HaveLenMatcher) Match(actual any) (any, error)

type HaveOccurredMatcher

type HaveOccurredMatcher struct {
}

HaveOccurredMatcher will succeed if the actual value is a non-nil error.

func HaveOccurred

func HaveOccurred() HaveOccurredMatcher

HaveOccurred returns a HaveOccurredMatcher

func (HaveOccurredMatcher) Match

func (m HaveOccurredMatcher) Match(actual any) (any, error)

type IsNilMatcher

type IsNilMatcher struct{}

IsNilMatcher will succeed if actual is nil.

func IsNil

func IsNil() IsNilMatcher

IsNil will return a IsNilMatcher.

func (IsNilMatcher) Match

func (m IsNilMatcher) Match(actual any) (any, error)

type MatchJSONMatcher

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

MatchJSONMatcher converts both expected and actual to a map[string]any and does a reflect.DeepEqual between them

func MatchJSON

func MatchJSON(expected any) MatchJSONMatcher

MatchJSON returns an MatchJSONMatcher with the expected value

func (MatchJSONMatcher) Match

func (m MatchJSONMatcher) Match(actual any) (any, error)

type MatchRegexpMatcher

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

func MatchRegexp

func MatchRegexp(pattern string) MatchRegexpMatcher

func (MatchRegexpMatcher) Match

func (m MatchRegexpMatcher) Match(actual any) (any, error)

type Matcher

type Matcher interface {
	Match(actual any) (resultValue any, err error)
}

Matcher is a type that matches expected against actuals.

type NotMatcher

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

NotMatcher accepts a matcher and will succeed if the specified matcher fails.

func Not

func Not(child Matcher) NotMatcher

Not returns a NotMatcher with the specified child matcher.

func (NotMatcher) Match

func (m NotMatcher) Match(actual any) (any, error)

type OrMatcher

type OrMatcher struct {
	Children []Matcher
}

func Or

func Or(a, b Matcher, ms ...Matcher) OrMatcher

func (OrMatcher) Match

func (m OrMatcher) Match(actual any) (any, error)

type PanicMatcher

type PanicMatcher struct {
}

PanicMatcher accepts a function. It succeeds if the function panics.

func Panic

func Panic() PanicMatcher

Panic returns a Panic matcher.

func (PanicMatcher) Match

func (m PanicMatcher) Match(actual any) (result any, err error)

type ReceiveMatcher

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

ReceiveMatcher only accepts a readable channel. It will error for anything else. It will attempt to receive from the channel but will not block. It fails if the channel is closed.

func Receive

func Receive(opts ...ReceiveOpt) ReceiveMatcher

Receive will return a ReceiveMatcher

func (ReceiveMatcher) Match

func (m ReceiveMatcher) Match(actual any) (any, error)

type ReceiveOpt

type ReceiveOpt func(ReceiveMatcher) ReceiveMatcher

ReceiveOpt is an option that can be passed to the ReceiveMatcher constructor.

func ReceiveWait

func ReceiveWait(t time.Duration) ReceiveOpt

ReceiveWait is an option that makes the ReceiveMatcher wait for values for the provided duration before deciding that the channel failed to receive.

type StartWithMatcher

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

StartWithMatcher accepts a string and succeeds if the actual string starts with the expected string.

func StartWith

func StartWith(prefix string) StartWithMatcher

StartWith returns a StartWithMatcher with the expected prefix.

func (StartWithMatcher) Match

func (m StartWithMatcher) Match(actual any) (any, error)

type ViaPollingMatcher

type ViaPollingMatcher struct {
	Matcher            Matcher
	Duration, Interval time.Duration
}

ViaPollingMatcher matches by polling the child matcher until it returns a success. It will return success the first time the child matcher returns a success. If the child matcher never returns a nil, then it will return the last error.

Duration is the worst case scenario for the matcher if the child matcher continues to return an error

Interval is the period between polling.

func ViaPolling

func ViaPolling(m Matcher) ViaPollingMatcher

ViaPolling returns the default ViaPollingMatcher. Length of 1s and Rate of 10ms

func (ViaPollingMatcher) Match

func (m ViaPollingMatcher) Match(actual any) (any, error)

Match takes a value that can change over time. Therefore, the only two valid options are a function with no arguments and a single return type, or a readable channel. Anything else will return an error.

If actual is a channel, then the child matcher will have to handle reading from the channel.

If the actual is a function, then the matcher will invoke the value and pass the returned value to the child matcher.

Jump to

Keyboard shortcuts

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