Skip to content

Latest commit

 

History

History
160 lines (122 loc) · 3.34 KB

README.md

File metadata and controls

160 lines (122 loc) · 3.34 KB

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