2019-05-31 09:45:11 +00:00
package gomega
import (
2023-11-14 14:36:00 +00:00
"fmt"
2019-05-31 09:45:11 +00:00
"time"
2022-08-19 16:42:45 +00:00
"github.com/google/go-cmp/cmp"
2019-05-31 09:45:11 +00:00
"github.com/onsi/gomega/matchers"
"github.com/onsi/gomega/types"
)
2022-10-17 17:38:08 +00:00
// Equal uses reflect.DeepEqual to compare actual with expected. Equal is strict about
// types when performing comparisons.
// It is an error for both actual and expected to be nil. Use BeNil() instead.
2019-05-31 09:45:11 +00:00
func Equal ( expected interface { } ) types . GomegaMatcher {
return & matchers . EqualMatcher {
Expected : expected ,
}
}
2022-10-17 17:38:08 +00:00
// BeEquivalentTo is more lax than Equal, allowing equality between different types.
// This is done by converting actual to have the type of expected before
// attempting equality with reflect.DeepEqual.
// It is an error for actual and expected to be nil. Use BeNil() instead.
2019-05-31 09:45:11 +00:00
func BeEquivalentTo ( expected interface { } ) types . GomegaMatcher {
return & matchers . BeEquivalentToMatcher {
Expected : expected ,
}
}
2023-01-30 20:05:01 +00:00
// BeComparableTo uses gocmp.Equal from github.com/google/go-cmp (instead of reflect.DeepEqual) to perform a deep comparison.
// You can pass cmp.Option as options.
2022-10-17 17:38:08 +00:00
// It is an error for actual and expected to be nil. Use BeNil() instead.
2022-08-19 16:42:45 +00:00
func BeComparableTo ( expected interface { } , opts ... cmp . Option ) types . GomegaMatcher {
return & matchers . BeComparableToMatcher {
Expected : expected ,
Options : opts ,
}
}
2022-10-17 17:38:08 +00:00
// BeIdenticalTo uses the == operator to compare actual with expected.
// BeIdenticalTo is strict about types when performing comparisons.
// It is an error for both actual and expected to be nil. Use BeNil() instead.
2019-05-31 09:45:11 +00:00
func BeIdenticalTo ( expected interface { } ) types . GomegaMatcher {
return & matchers . BeIdenticalToMatcher {
Expected : expected ,
}
}
2022-10-17 17:38:08 +00:00
// BeNil succeeds if actual is nil
2019-05-31 09:45:11 +00:00
func BeNil ( ) types . GomegaMatcher {
return & matchers . BeNilMatcher { }
}
2022-10-17 17:38:08 +00:00
// BeTrue succeeds if actual is true
2023-11-14 14:36:00 +00:00
//
// In general, it's better to use `BeTrueBecause(reason)` to provide a more useful error message if a true check fails.
2019-05-31 09:45:11 +00:00
func BeTrue ( ) types . GomegaMatcher {
return & matchers . BeTrueMatcher { }
}
2022-10-17 17:38:08 +00:00
// BeFalse succeeds if actual is false
2023-11-14 14:36:00 +00:00
//
// In general, it's better to use `BeFalseBecause(reason)` to provide a more useful error message if a false check fails.
2019-05-31 09:45:11 +00:00
func BeFalse ( ) types . GomegaMatcher {
return & matchers . BeFalseMatcher { }
}
2023-11-14 14:36:00 +00:00
// BeTrueBecause succeeds if actual is true and displays the provided reason if it is false
// fmt.Sprintf is used to render the reason
func BeTrueBecause ( format string , args ... any ) types . GomegaMatcher {
return & matchers . BeTrueMatcher { Reason : fmt . Sprintf ( format , args ... ) }
}
// BeFalseBecause succeeds if actual is false and displays the provided reason if it is true.
// fmt.Sprintf is used to render the reason
func BeFalseBecause ( format string , args ... any ) types . GomegaMatcher {
return & matchers . BeFalseMatcher { Reason : fmt . Sprintf ( format , args ... ) }
}
2022-10-17 17:38:08 +00:00
// HaveOccurred succeeds if actual is a non-nil error
// The typical Go error checking pattern looks like:
//
// err := SomethingThatMightFail()
// Expect(err).ShouldNot(HaveOccurred())
2019-05-31 09:45:11 +00:00
func HaveOccurred ( ) types . GomegaMatcher {
return & matchers . HaveOccurredMatcher { }
}
2022-10-17 17:38:08 +00:00
// Succeed passes if actual is a nil error
// Succeed is intended to be used with functions that return a single error value. Instead of
//
// err := SomethingThatMightFail()
// Expect(err).ShouldNot(HaveOccurred())
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// You can write:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Expect(SomethingThatMightFail()).Should(Succeed())
//
// It is a mistake to use Succeed with a function that has multiple return values. Gomega's Ω and Expect
// functions automatically trigger failure if any return values after the first return value are non-zero/non-nil.
// This means that Ω(MultiReturnFunc()).ShouldNot(Succeed()) can never pass.
2019-05-31 09:45:11 +00:00
func Succeed ( ) types . GomegaMatcher {
return & matchers . SucceedMatcher { }
}
2023-04-18 08:07:36 +00:00
// MatchError succeeds if actual is a non-nil error that matches the passed in
2023-10-26 09:28:31 +00:00
// string, error, function, or matcher.
2022-10-17 17:38:08 +00:00
//
// These are valid use-cases:
2019-05-31 09:45:11 +00:00
//
2023-10-26 09:28:31 +00:00
// When passed a string:
//
// Expect(err).To(MatchError("an error"))
//
// asserts that err.Error() == "an error"
//
// When passed an error:
//
// Expect(err).To(MatchError(SomeError))
//
// First checks if errors.Is(err, SomeError).
// If that fails then it checks if reflect.DeepEqual(err, SomeError) repeatedly for err and any errors wrapped by err
//
// When passed a matcher:
//
// Expect(err).To(MatchError(ContainSubstring("sprocket not found")))
//
// the matcher is passed err.Error(). In this case it asserts that err.Error() contains substring "sprocket not found"
//
// When passed a func(err) bool and a description:
//
// Expect(err).To(MatchError(os.IsNotExist, "IsNotExist"))
//
// the function is passed err and matches if the return value is true. The description is required to allow Gomega
// to print a useful error message.
2019-05-31 09:45:11 +00:00
//
2023-04-18 08:07:36 +00:00
// It is an error for err to be nil or an object that does not implement the
// Error interface
2023-10-26 09:28:31 +00:00
//
// The optional second argument is a description of the error function, if used. This is required when passing a function but is ignored in all other cases.
func MatchError ( expected interface { } , functionErrorDescription ... any ) types . GomegaMatcher {
2019-05-31 09:45:11 +00:00
return & matchers . MatchErrorMatcher {
2023-10-26 09:28:31 +00:00
Expected : expected ,
FuncErrDescription : functionErrorDescription ,
2019-05-31 09:45:11 +00:00
}
}
2022-10-17 17:38:08 +00:00
// BeClosed succeeds if actual is a closed channel.
// It is an error to pass a non-channel to BeClosed, it is also an error to pass nil
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// In order to check whether or not the channel is closed, Gomega must try to read from the channel
// (even in the `ShouldNot(BeClosed())` case). You should keep this in mind if you wish to make subsequent assertions about
// values coming down the channel.
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Also, if you are testing that a *buffered* channel is closed you must first read all values out of the channel before
// asserting that it is closed (it is not possible to detect that a buffered-channel has been closed until all its buffered values are read).
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Finally, as a corollary: it is an error to check whether or not a send-only channel is closed.
2019-05-31 09:45:11 +00:00
func BeClosed ( ) types . GomegaMatcher {
return & matchers . BeClosedMatcher { }
}
2022-10-17 17:38:08 +00:00
// Receive succeeds if there is a value to be received on actual.
// Actual must be a channel (and cannot be a send-only channel) -- anything else is an error.
//
// Receive returns immediately and never blocks:
//
// - If there is nothing on the channel `c` then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.
//
// - If the channel `c` is closed then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.
//
// - If there is something on the channel `c` ready to be read, then Expect(c).Should(Receive()) will pass and Ω(c).ShouldNot(Receive()) will fail.
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// If you have a go-routine running in the background that will write to channel `c` you can:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Eventually(c).Should(Receive())
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// This will timeout if nothing gets sent to `c` (you can modify the timeout interval as you normally do with `Eventually`)
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// A similar use-case is to assert that no go-routine writes to a channel (for a period of time). You can do this with `Consistently`:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Consistently(c).ShouldNot(Receive())
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// You can pass `Receive` a matcher. If you do so, it will match the received object against the matcher. For example:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Expect(c).Should(Receive(Equal("foo")))
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// When given a matcher, `Receive` will always fail if there is nothing to be received on the channel.
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Passing Receive a matcher is especially useful when paired with Eventually:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Eventually(c).Should(Receive(ContainSubstring("bar")))
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// will repeatedly attempt to pull values out of `c` until a value matching "bar" is received.
2019-05-31 09:45:11 +00:00
//
2024-06-24 20:58:34 +00:00
// Furthermore, if you want to have a reference to the value *sent* to the channel you can pass the `Receive` matcher a pointer to a variable of the appropriate type:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// var myThing thing
// Eventually(thingChan).Should(Receive(&myThing))
// Expect(myThing.Sprocket).Should(Equal("foo"))
// Expect(myThing.IsValid()).Should(BeTrue())
2024-06-24 20:58:34 +00:00
//
// Finally, if you want to match the received object as well as get the actual received value into a variable, so you can reason further about the value received,
// you can pass a pointer to a variable of the approriate type first, and second a matcher:
//
// var myThing thing
// Eventually(thingChan).Should(Receive(&myThing, ContainSubstring("bar")))
2019-05-31 09:45:11 +00:00
func Receive ( args ... interface { } ) types . GomegaMatcher {
return & matchers . ReceiveMatcher {
2024-06-24 20:58:34 +00:00
Args : args ,
2019-05-31 09:45:11 +00:00
}
}
2022-10-17 17:38:08 +00:00
// BeSent succeeds if a value can be sent to actual.
// Actual must be a channel (and cannot be a receive-only channel) that can sent the type of the value passed into BeSent -- anything else is an error.
// In addition, actual must not be closed.
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// BeSent never blocks:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// - If the channel `c` is not ready to receive then Expect(c).Should(BeSent("foo")) will fail immediately
// - If the channel `c` is eventually ready to receive then Eventually(c).Should(BeSent("foo")) will succeed.. presuming the channel becomes ready to receive before Eventually's timeout
// - If the channel `c` is closed then Expect(c).Should(BeSent("foo")) and Ω(c).ShouldNot(BeSent("foo")) will both fail immediately
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Of course, the value is actually sent to the channel. The point of `BeSent` is less to make an assertion about the availability of the channel (which is typically an implementation detail that your test should not be concerned with).
// Rather, the point of `BeSent` is to make it possible to easily and expressively write tests that can timeout on blocked channel sends.
2019-05-31 09:45:11 +00:00
func BeSent ( arg interface { } ) types . GomegaMatcher {
return & matchers . BeSentMatcher {
Arg : arg ,
}
}
2022-10-17 17:38:08 +00:00
// MatchRegexp succeeds if actual is a string or stringer that matches the
// passed-in regexp. Optional arguments can be provided to construct a regexp
// via fmt.Sprintf().
2019-05-31 09:45:11 +00:00
func MatchRegexp ( regexp string , args ... interface { } ) types . GomegaMatcher {
return & matchers . MatchRegexpMatcher {
Regexp : regexp ,
Args : args ,
}
}
2022-10-17 17:38:08 +00:00
// ContainSubstring succeeds if actual is a string or stringer that contains the
// passed-in substring. Optional arguments can be provided to construct the substring
// via fmt.Sprintf().
2019-05-31 09:45:11 +00:00
func ContainSubstring ( substr string , args ... interface { } ) types . GomegaMatcher {
return & matchers . ContainSubstringMatcher {
Substr : substr ,
Args : args ,
}
}
2022-10-17 17:38:08 +00:00
// HavePrefix succeeds if actual is a string or stringer that contains the
// passed-in string as a prefix. Optional arguments can be provided to construct
// via fmt.Sprintf().
2019-05-31 09:45:11 +00:00
func HavePrefix ( prefix string , args ... interface { } ) types . GomegaMatcher {
return & matchers . HavePrefixMatcher {
Prefix : prefix ,
Args : args ,
}
}
2022-10-17 17:38:08 +00:00
// HaveSuffix succeeds if actual is a string or stringer that contains the
// passed-in string as a suffix. Optional arguments can be provided to construct
// via fmt.Sprintf().
2019-05-31 09:45:11 +00:00
func HaveSuffix ( suffix string , args ... interface { } ) types . GomegaMatcher {
return & matchers . HaveSuffixMatcher {
Suffix : suffix ,
Args : args ,
}
}
2022-10-17 17:38:08 +00:00
// MatchJSON succeeds if actual is a string or stringer of JSON that matches
// the expected JSON. The JSONs are decoded and the resulting objects are compared via
// reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.
2019-05-31 09:45:11 +00:00
func MatchJSON ( json interface { } ) types . GomegaMatcher {
return & matchers . MatchJSONMatcher {
JSONToMatch : json ,
}
}
2022-10-17 17:38:08 +00:00
// MatchXML succeeds if actual is a string or stringer of XML that matches
// the expected XML. The XMLs are decoded and the resulting objects are compared via
// reflect.DeepEqual so things like whitespaces shouldn't matter.
2019-05-31 09:45:11 +00:00
func MatchXML ( xml interface { } ) types . GomegaMatcher {
return & matchers . MatchXMLMatcher {
XMLToMatch : xml ,
}
}
2022-10-17 17:38:08 +00:00
// MatchYAML succeeds if actual is a string or stringer of YAML that matches
// the expected YAML. The YAML's are decoded and the resulting objects are compared via
// reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.
2019-05-31 09:45:11 +00:00
func MatchYAML ( yaml interface { } ) types . GomegaMatcher {
return & matchers . MatchYAMLMatcher {
YAMLToMatch : yaml ,
}
}
2022-10-17 17:38:08 +00:00
// BeEmpty succeeds if actual is empty. Actual must be of type string, array, map, chan, or slice.
2019-05-31 09:45:11 +00:00
func BeEmpty ( ) types . GomegaMatcher {
return & matchers . BeEmptyMatcher { }
}
2022-10-17 17:38:08 +00:00
// HaveLen succeeds if actual has the passed-in length. Actual must be of type string, array, map, chan, or slice.
2019-05-31 09:45:11 +00:00
func HaveLen ( count int ) types . GomegaMatcher {
return & matchers . HaveLenMatcher {
Count : count ,
}
}
2022-10-17 17:38:08 +00:00
// HaveCap succeeds if actual has the passed-in capacity. Actual must be of type array, chan, or slice.
2019-05-31 09:45:11 +00:00
func HaveCap ( count int ) types . GomegaMatcher {
return & matchers . HaveCapMatcher {
Count : count ,
}
}
2022-10-17 17:38:08 +00:00
// BeZero succeeds if actual is the zero value for its type or if actual is nil.
2019-05-31 09:45:11 +00:00
func BeZero ( ) types . GomegaMatcher {
return & matchers . BeZeroMatcher { }
}
2022-10-17 17:38:08 +00:00
// ContainElement succeeds if actual contains the passed in element. By default
// ContainElement() uses Equal() to perform the match, however a matcher can be
// passed in instead:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubstring("Bar")))
2022-03-28 20:10:26 +00:00
//
2022-10-17 17:38:08 +00:00
// Actual must be an array, slice or map. For maps, ContainElement searches
// through the map's values.
2022-03-28 20:10:26 +00:00
//
2022-10-17 17:38:08 +00:00
// If you want to have a copy of the matching element(s) found you can pass a
// pointer to a variable of the appropriate type. If the variable isn't a slice
// or map, then exactly one match will be expected and returned. If the variable
// is a slice or map, then at least one match is expected and all matches will be
// stored in the variable.
//
// var findings []string
// Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubString("Bar", &findings)))
2022-03-28 20:10:26 +00:00
func ContainElement ( element interface { } , result ... interface { } ) types . GomegaMatcher {
2019-05-31 09:45:11 +00:00
return & matchers . ContainElementMatcher {
Element : element ,
2022-03-28 20:10:26 +00:00
Result : result ,
2019-05-31 09:45:11 +00:00
}
}
2022-10-17 17:38:08 +00:00
// BeElementOf succeeds if actual is contained in the passed in elements.
// BeElementOf() always uses Equal() to perform the match.
// When the passed in elements are comprised of a single element that is either an Array or Slice, BeElementOf() behaves
// as the reverse of ContainElement() that operates with Equal() to perform the match.
//
// Expect(2).Should(BeElementOf([]int{1, 2}))
// Expect(2).Should(BeElementOf([2]int{1, 2}))
//
// Otherwise, BeElementOf() provides a syntactic sugar for Or(Equal(_), Equal(_), ...):
//
// Expect(2).Should(BeElementOf(1, 2))
2019-09-20 10:45:13 +00:00
//
2022-10-17 17:38:08 +00:00
// Actual must be typed.
2019-09-20 10:45:13 +00:00
func BeElementOf ( elements ... interface { } ) types . GomegaMatcher {
return & matchers . BeElementOfMatcher {
Elements : elements ,
}
}
2022-10-17 17:38:08 +00:00
// BeKeyOf succeeds if actual is contained in the keys of the passed in map.
// BeKeyOf() always uses Equal() to perform the match between actual and the map keys.
//
// Expect("foo").Should(BeKeyOf(map[string]bool{"foo": true, "bar": false}))
func BeKeyOf ( element interface { } ) types . GomegaMatcher {
return & matchers . BeKeyOfMatcher {
Map : element ,
}
}
// ConsistOf succeeds if actual contains precisely the elements passed into the matcher. The ordering of the elements does not matter.
// By default ConsistOf() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf("FooBar", "Foo"))
// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Bar"), "Foo"))
// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Foo"), ContainSubstring("Foo")))
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Actual must be an array, slice or map. For maps, ConsistOf matches against the map's values.
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// You typically pass variadic arguments to ConsistOf (as in the examples above). However, if you need to pass in a slice you can provided that it
// is the only element passed in to ConsistOf:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf([]string{"FooBar", "Foo"}))
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Note that Go's type system does not allow you to write this as ConsistOf([]string{"FooBar", "Foo"}...) as []string and []interface{} are different types - hence the need for this special rule.
2019-05-31 09:45:11 +00:00
func ConsistOf ( elements ... interface { } ) types . GomegaMatcher {
return & matchers . ConsistOfMatcher {
Elements : elements ,
}
}
2024-01-29 20:22:47 +00:00
// HaveExactElements succeeds if actual contains elements that precisely match the elemets passed into the matcher. The ordering of the elements does matter.
2023-02-20 14:30:40 +00:00
// By default HaveExactElements() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:
//
// Expect([]string{"Foo", "FooBar"}).Should(HaveExactElements("Foo", "FooBar"))
// Expect([]string{"Foo", "FooBar"}).Should(HaveExactElements("Foo", ContainSubstring("Bar")))
// Expect([]string{"Foo", "FooBar"}).Should(HaveExactElements(ContainSubstring("Foo"), ContainSubstring("Foo")))
//
// Actual must be an array or slice.
func HaveExactElements ( elements ... interface { } ) types . GomegaMatcher {
return & matchers . HaveExactElementsMatcher {
Elements : elements ,
}
}
2022-10-17 17:38:08 +00:00
// ContainElements succeeds if actual contains the passed in elements. The ordering of the elements does not matter.
// By default ContainElements() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:
2020-04-14 07:04:33 +00:00
//
2022-10-17 17:38:08 +00:00
// Expect([]string{"Foo", "FooBar"}).Should(ContainElements("FooBar"))
// Expect([]string{"Foo", "FooBar"}).Should(ContainElements(ContainSubstring("Bar"), "Foo"))
2020-04-14 07:04:33 +00:00
//
2022-10-17 17:38:08 +00:00
// Actual must be an array, slice or map.
// For maps, ContainElements searches through the map's values.
2020-04-14 07:04:33 +00:00
func ContainElements ( elements ... interface { } ) types . GomegaMatcher {
return & matchers . ContainElementsMatcher {
Elements : elements ,
}
}
2022-10-17 17:38:08 +00:00
// HaveEach succeeds if actual solely contains elements that match the passed in element.
2023-10-25 10:05:28 +00:00
// Please note that if actual is empty, HaveEach always will fail.
2022-10-17 17:38:08 +00:00
// By default HaveEach() uses Equal() to perform the match, however a
// matcher can be passed in instead:
2022-03-28 20:10:26 +00:00
//
2022-10-17 17:38:08 +00:00
// Expect([]string{"Foo", "FooBar"}).Should(HaveEach(ContainSubstring("Foo")))
//
// Actual must be an array, slice or map.
// For maps, HaveEach searches through the map's values.
2022-03-28 20:10:26 +00:00
func HaveEach ( element interface { } ) types . GomegaMatcher {
return & matchers . HaveEachMatcher {
Element : element ,
}
}
2022-10-17 17:38:08 +00:00
// HaveKey succeeds if actual is a map with the passed in key.
// By default HaveKey uses Equal() to perform the match, however a
// matcher can be passed in instead:
//
// Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKey(MatchRegexp(`.+Foo$`)))
2019-05-31 09:45:11 +00:00
func HaveKey ( key interface { } ) types . GomegaMatcher {
return & matchers . HaveKeyMatcher {
Key : key ,
}
}
2022-10-17 17:38:08 +00:00
// HaveKeyWithValue succeeds if actual is a map with the passed in key and value.
// By default HaveKeyWithValue uses Equal() to perform the match, however a
// matcher can be passed in instead:
//
// Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue("Foo", "Bar"))
// Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue(MatchRegexp(`.+Foo$`), "Bar"))
2019-05-31 09:45:11 +00:00
func HaveKeyWithValue ( key interface { } , value interface { } ) types . GomegaMatcher {
return & matchers . HaveKeyWithValueMatcher {
Key : key ,
Value : value ,
}
}
2022-10-17 17:38:08 +00:00
// HaveField succeeds if actual is a struct and the value at the passed in field
// matches the passed in matcher. By default HaveField used Equal() to perform the match,
// however a matcher can be passed in in stead.
//
// The field must be a string that resolves to the name of a field in the struct. Structs can be traversed
// using the '.' delimiter. If the field ends with '()' a method named field is assumed to exist on the struct and is invoked.
// Such methods must take no arguments and return a single value:
//
// type Book struct {
// Title string
// Author Person
// }
// type Person struct {
// FirstName string
// LastName string
// DOB time.Time
// }
// Expect(book).To(HaveField("Title", "Les Miserables"))
// Expect(book).To(HaveField("Title", ContainSubstring("Les"))
// Expect(book).To(HaveField("Author.FirstName", Equal("Victor"))
// Expect(book).To(HaveField("Author.DOB.Year()", BeNumerically("<", 1900))
2021-11-08 20:26:59 +00:00
func HaveField ( field string , expected interface { } ) types . GomegaMatcher {
return & matchers . HaveFieldMatcher {
Field : field ,
Expected : expected ,
}
}
2022-08-19 16:42:45 +00:00
// HaveExistingField succeeds if actual is a struct and the specified field
// exists.
//
// HaveExistingField can be combined with HaveField in order to cover use cases
// with optional fields. HaveField alone would trigger an error in such situations.
//
2022-10-17 17:38:08 +00:00
// Expect(MrHarmless).NotTo(And(HaveExistingField("Title"), HaveField("Title", "Supervillain")))
2022-08-19 16:42:45 +00:00
func HaveExistingField ( field string ) types . GomegaMatcher {
return & matchers . HaveExistingFieldMatcher {
Field : field ,
}
}
2022-01-24 20:14:51 +00:00
// HaveValue applies the given matcher to the value of actual, optionally and
// repeatedly dereferencing pointers or taking the concrete value of interfaces.
// Thus, the matcher will always be applied to non-pointer and non-interface
// values only. HaveValue will fail with an error if a pointer or interface is
// nil. It will also fail for more than 31 pointer or interface dereferences to
// guard against mistakenly applying it to arbitrarily deep linked pointers.
//
// HaveValue differs from gstruct.PointTo in that it does not expect actual to
// be a pointer (as gstruct.PointTo does) but instead also accepts non-pointer
// and even interface values.
//
2022-10-17 17:38:08 +00:00
// actual := 42
// Expect(actual).To(HaveValue(42))
// Expect(&actual).To(HaveValue(42))
2022-01-24 20:14:51 +00:00
func HaveValue ( matcher types . GomegaMatcher ) types . GomegaMatcher {
return & matchers . HaveValueMatcher {
Matcher : matcher ,
}
}
2022-10-17 17:38:08 +00:00
// BeNumerically performs numerical assertions in a type-agnostic way.
// Actual and expected should be numbers, though the specific type of
// number is irrelevant (float32, float64, uint8, etc...).
//
// There are six, self-explanatory, supported comparators:
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// Expect(1.0).Should(BeNumerically("==", 1))
// Expect(1.0).Should(BeNumerically("~", 0.999, 0.01))
// Expect(1.0).Should(BeNumerically(">", 0.9))
// Expect(1.0).Should(BeNumerically(">=", 1.0))
// Expect(1.0).Should(BeNumerically("<", 3))
// Expect(1.0).Should(BeNumerically("<=", 1.0))
2019-05-31 09:45:11 +00:00
func BeNumerically ( comparator string , compareTo ... interface { } ) types . GomegaMatcher {
return & matchers . BeNumericallyMatcher {
Comparator : comparator ,
CompareTo : compareTo ,
}
}
2022-10-17 17:38:08 +00:00
// BeTemporally compares time.Time's like BeNumerically
// Actual and expected must be time.Time. The comparators are the same as for BeNumerically
//
// Expect(time.Now()).Should(BeTemporally(">", time.Time{}))
// Expect(time.Now()).Should(BeTemporally("~", time.Now(), time.Second))
2019-05-31 09:45:11 +00:00
func BeTemporally ( comparator string , compareTo time . Time , threshold ... time . Duration ) types . GomegaMatcher {
return & matchers . BeTemporallyMatcher {
Comparator : comparator ,
CompareTo : compareTo ,
Threshold : threshold ,
}
}
2022-10-17 17:38:08 +00:00
// BeAssignableToTypeOf succeeds if actual is assignable to the type of expected.
// It will return an error when one of the values is nil.
//
// Expect(0).Should(BeAssignableToTypeOf(0)) // Same values
// Expect(5).Should(BeAssignableToTypeOf(-1)) // different values same type
// Expect("foo").Should(BeAssignableToTypeOf("bar")) // different values same type
// Expect(struct{ Foo string }{}).Should(BeAssignableToTypeOf(struct{ Foo string }{}))
2019-05-31 09:45:11 +00:00
func BeAssignableToTypeOf ( expected interface { } ) types . GomegaMatcher {
return & matchers . AssignableToTypeOfMatcher {
Expected : expected ,
}
}
2022-10-17 17:38:08 +00:00
// Panic succeeds if actual is a function that, when invoked, panics.
// Actual must be a function that takes no arguments and returns no results.
2019-05-31 09:45:11 +00:00
func Panic ( ) types . GomegaMatcher {
return & matchers . PanicMatcher { }
}
2022-10-17 17:38:08 +00:00
// PanicWith succeeds if actual is a function that, when invoked, panics with a specific value.
// Actual must be a function that takes no arguments and returns no results.
2021-06-04 08:59:18 +00:00
//
2022-10-17 17:38:08 +00:00
// By default PanicWith uses Equal() to perform the match, however a
// matcher can be passed in instead:
//
// Expect(fn).Should(PanicWith(MatchRegexp(`.+Foo$`)))
2021-06-04 08:59:18 +00:00
func PanicWith ( expected interface { } ) types . GomegaMatcher {
return & matchers . PanicMatcher { Expected : expected }
}
2022-10-17 17:38:08 +00:00
// BeAnExistingFile succeeds if a file exists.
// Actual must be a string representing the abs path to the file being checked.
2019-05-31 09:45:11 +00:00
func BeAnExistingFile ( ) types . GomegaMatcher {
return & matchers . BeAnExistingFileMatcher { }
}
2022-10-17 17:38:08 +00:00
// BeARegularFile succeeds if a file exists and is a regular file.
// Actual must be a string representing the abs path to the file being checked.
2019-05-31 09:45:11 +00:00
func BeARegularFile ( ) types . GomegaMatcher {
return & matchers . BeARegularFileMatcher { }
}
2022-10-17 17:38:08 +00:00
// BeADirectory succeeds if a file exists and is a directory.
// Actual must be a string representing the abs path to the file being checked.
2019-05-31 09:45:11 +00:00
func BeADirectory ( ) types . GomegaMatcher {
return & matchers . BeADirectoryMatcher { }
}
2022-10-17 17:38:08 +00:00
// HaveHTTPStatus succeeds if the Status or StatusCode field of an HTTP response matches.
// Actual must be either a *http.Response or *httptest.ResponseRecorder.
// Expected must be either an int or a string.
//
// Expect(resp).Should(HaveHTTPStatus(http.StatusOK)) // asserts that resp.StatusCode == 200
// Expect(resp).Should(HaveHTTPStatus("404 Not Found")) // asserts that resp.Status == "404 Not Found"
// Expect(resp).Should(HaveHTTPStatus(http.StatusOK, http.StatusNoContent)) // asserts that resp.StatusCode == 200 || resp.StatusCode == 204
2021-09-01 13:50:18 +00:00
func HaveHTTPStatus ( expected ... interface { } ) types . GomegaMatcher {
2021-06-04 08:59:18 +00:00
return & matchers . HaveHTTPStatusMatcher { Expected : expected }
}
2021-09-01 13:50:18 +00:00
// HaveHTTPHeaderWithValue succeeds if the header is found and the value matches.
// Actual must be either a *http.Response or *httptest.ResponseRecorder.
// Expected must be a string header name, followed by a header value which
// can be a string, or another matcher.
func HaveHTTPHeaderWithValue ( header string , value interface { } ) types . GomegaMatcher {
return & matchers . HaveHTTPHeaderWithValueMatcher {
Header : header ,
Value : value ,
}
}
// HaveHTTPBody matches if the body matches.
// Actual must be either a *http.Response or *httptest.ResponseRecorder.
// Expected must be either a string, []byte, or other matcher
func HaveHTTPBody ( expected interface { } ) types . GomegaMatcher {
return & matchers . HaveHTTPBodyMatcher { Expected : expected }
}
2022-10-17 17:38:08 +00:00
// And succeeds only if all of the given matchers succeed.
// The matchers are tried in order, and will fail-fast if one doesn't succeed.
//
// Expect("hi").To(And(HaveLen(2), Equal("hi"))
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
2019-05-31 09:45:11 +00:00
func And ( ms ... types . GomegaMatcher ) types . GomegaMatcher {
return & matchers . AndMatcher { Matchers : ms }
}
2022-10-17 17:38:08 +00:00
// SatisfyAll is an alias for And().
//
// Expect("hi").Should(SatisfyAll(HaveLen(2), Equal("hi")))
2019-05-31 09:45:11 +00:00
func SatisfyAll ( matchers ... types . GomegaMatcher ) types . GomegaMatcher {
return And ( matchers ... )
}
2022-10-17 17:38:08 +00:00
// Or succeeds if any of the given matchers succeed.
// The matchers are tried in order and will return immediately upon the first successful match.
//
// Expect("hi").To(Or(HaveLen(3), HaveLen(2))
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
2019-05-31 09:45:11 +00:00
func Or ( ms ... types . GomegaMatcher ) types . GomegaMatcher {
return & matchers . OrMatcher { Matchers : ms }
}
2022-10-17 17:38:08 +00:00
// SatisfyAny is an alias for Or().
//
// Expect("hi").SatisfyAny(Or(HaveLen(3), HaveLen(2))
2019-05-31 09:45:11 +00:00
func SatisfyAny ( matchers ... types . GomegaMatcher ) types . GomegaMatcher {
return Or ( matchers ... )
}
2022-10-17 17:38:08 +00:00
// Not negates the given matcher; it succeeds if the given matcher fails.
//
// Expect(1).To(Not(Equal(2))
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
2019-05-31 09:45:11 +00:00
func Not ( matcher types . GomegaMatcher ) types . GomegaMatcher {
return & matchers . NotMatcher { Matcher : matcher }
}
2022-10-17 17:38:08 +00:00
// WithTransform applies the `transform` to the actual value and matches it against `matcher`.
// The given transform must be either a function of one parameter that returns one value or a
2021-11-08 20:26:59 +00:00
// function of one parameter that returns two values, where the second value must be of the
// error type.
2019-05-31 09:45:11 +00:00
//
2022-10-17 17:38:08 +00:00
// var plus1 = func(i int) int { return i + 1 }
// Expect(1).To(WithTransform(plus1, Equal(2))
2021-11-08 20:26:59 +00:00
//
2022-10-17 17:38:08 +00:00
// var failingplus1 = func(i int) (int, error) { return 42, "this does not compute" }
// Expect(1).To(WithTransform(failingplus1, Equal(2)))
//
// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
2019-05-31 09:45:11 +00:00
func WithTransform ( transform interface { } , matcher types . GomegaMatcher ) types . GomegaMatcher {
return matchers . NewWithTransformMatcher ( transform , matcher )
}
2021-06-04 08:59:18 +00:00
2022-10-17 17:38:08 +00:00
// Satisfy matches the actual value against the `predicate` function.
// The given predicate must be a function of one paramter that returns bool.
//
// var isEven = func(i int) bool { return i%2 == 0 }
// Expect(2).To(Satisfy(isEven))
2021-06-04 08:59:18 +00:00
func Satisfy ( predicate interface { } ) types . GomegaMatcher {
return matchers . NewSatisfyMatcher ( predicate )
}