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.
StartWithMatcher accepts a string and succeeds if the actual string starts with the expected string.
Expect(t, "foobar").To(StartWith("foo"))
EndWithMatcher accepts a string and succeeds if the actual string ends with the expected string.
Expect(t, "foobar").To(EndWith("bar"))
ContainSubstringMatcher accepts a string and succeeds if the expected string is a sub-string of the actual.
Expect(t, "foobar").To(ContainSubstring("ooba"))
NotMatcher accepts a matcher and will succeed if the specified matcher fails.
Expect(t, false).To(Not(BeTrue()))
BeAboveMatcher accepts a float64. It succeeds if the actual is greater than the expected.
Expect(t, 100).To(BeAbove(99))
BeBelowMatcher accepts a float64. It succeeds if the actual is less than the expected.
Expect(t, 100).To(BeBelow(101))
BeFalseMatcher will succeed if actual is false.
Expect(t, 2 == 3).To(BeFalse())
BeTrueMatcher will succeed if actual is true.
Expect(t, 2 == 2).To(BeTrue())
EqualMatcher performs a DeepEqual between the actual and expected.
Expect(t, 42).To(Equal(42))
HaveOccurredMatcher will succeed if the actual value is a non-nil error.
Expect(t, err).To(HaveOccurred())
Expect(t, nil).To(Not(HaveOccurred()))
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())
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())
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))
HaveKeyMatcher accepts map types and will succeed if the map contains the specified key.
Expect(t, fooMap).To(HaveKey("foo"))
HaveLenMatcher accepts Strings, Slices, Arrays, Maps and Channels. It will succeed if the type has the specified length.
Expect(t, "12345").To(HaveLen(5))
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()))