Path: blob/main/vendor/github.com/onsi/gomega/matchers.go
3510 views
package gomega12import (3"fmt"4"time"56"github.com/google/go-cmp/cmp"7"github.com/onsi/gomega/matchers"8"github.com/onsi/gomega/types"9)1011// Equal uses reflect.DeepEqual to compare actual with expected. Equal is strict about12// types when performing comparisons.13// It is an error for both actual and expected to be nil. Use BeNil() instead.14func Equal(expected any) types.GomegaMatcher {15return &matchers.EqualMatcher{16Expected: expected,17}18}1920// BeEquivalentTo is more lax than Equal, allowing equality between different types.21// This is done by converting actual to have the type of expected before22// attempting equality with reflect.DeepEqual.23// It is an error for actual and expected to be nil. Use BeNil() instead.24func BeEquivalentTo(expected any) types.GomegaMatcher {25return &matchers.BeEquivalentToMatcher{26Expected: expected,27}28}2930// BeComparableTo uses gocmp.Equal from github.com/google/go-cmp (instead of reflect.DeepEqual) to perform a deep comparison.31// You can pass cmp.Option as options.32// It is an error for actual and expected to be nil. Use BeNil() instead.33func BeComparableTo(expected any, opts ...cmp.Option) types.GomegaMatcher {34return &matchers.BeComparableToMatcher{35Expected: expected,36Options: opts,37}38}3940// BeIdenticalTo uses the == operator to compare actual with expected.41// BeIdenticalTo is strict about types when performing comparisons.42// It is an error for both actual and expected to be nil. Use BeNil() instead.43func BeIdenticalTo(expected any) types.GomegaMatcher {44return &matchers.BeIdenticalToMatcher{45Expected: expected,46}47}4849// BeNil succeeds if actual is nil50func BeNil() types.GomegaMatcher {51return &matchers.BeNilMatcher{}52}5354// BeTrue succeeds if actual is true55//56// In general, it's better to use `BeTrueBecause(reason)` to provide a more useful error message if a true check fails.57func BeTrue() types.GomegaMatcher {58return &matchers.BeTrueMatcher{}59}6061// BeFalse succeeds if actual is false62//63// In general, it's better to use `BeFalseBecause(reason)` to provide a more useful error message if a false check fails.64func BeFalse() types.GomegaMatcher {65return &matchers.BeFalseMatcher{}66}6768// BeTrueBecause succeeds if actual is true and displays the provided reason if it is false69// fmt.Sprintf is used to render the reason70func BeTrueBecause(format string, args ...any) types.GomegaMatcher {71return &matchers.BeTrueMatcher{Reason: fmt.Sprintf(format, args...)}72}7374// BeFalseBecause succeeds if actual is false and displays the provided reason if it is true.75// fmt.Sprintf is used to render the reason76func BeFalseBecause(format string, args ...any) types.GomegaMatcher {77return &matchers.BeFalseMatcher{Reason: fmt.Sprintf(format, args...)}78}7980// HaveOccurred succeeds if actual is a non-nil error81// The typical Go error checking pattern looks like:82//83// err := SomethingThatMightFail()84// Expect(err).ShouldNot(HaveOccurred())85func HaveOccurred() types.GomegaMatcher {86return &matchers.HaveOccurredMatcher{}87}8889// Succeed passes if actual is a nil error90// Succeed is intended to be used with functions that return a single error value. Instead of91//92// err := SomethingThatMightFail()93// Expect(err).ShouldNot(HaveOccurred())94//95// You can write:96//97// Expect(SomethingThatMightFail()).Should(Succeed())98//99// It is a mistake to use Succeed with a function that has multiple return values. Gomega's Ω and Expect100// functions automatically trigger failure if any return values after the first return value are non-zero/non-nil.101// This means that Ω(MultiReturnFunc()).ShouldNot(Succeed()) can never pass.102func Succeed() types.GomegaMatcher {103return &matchers.SucceedMatcher{}104}105106// MatchError succeeds if actual is a non-nil error that matches the passed in107// string, error, function, or matcher.108//109// These are valid use-cases:110//111// When passed a string:112//113// Expect(err).To(MatchError("an error"))114//115// asserts that err.Error() == "an error"116//117// When passed an error:118//119// Expect(err).To(MatchError(SomeError))120//121// First checks if errors.Is(err, SomeError).122// If that fails then it checks if reflect.DeepEqual(err, SomeError) repeatedly for err and any errors wrapped by err123//124// When passed a matcher:125//126// Expect(err).To(MatchError(ContainSubstring("sprocket not found")))127//128// the matcher is passed err.Error(). In this case it asserts that err.Error() contains substring "sprocket not found"129//130// When passed a func(err) bool and a description:131//132// Expect(err).To(MatchError(os.IsNotExist, "IsNotExist"))133//134// the function is passed err and matches if the return value is true. The description is required to allow Gomega135// to print a useful error message.136//137// It is an error for err to be nil or an object that does not implement the138// Error interface139//140// 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.141func MatchError(expected any, functionErrorDescription ...any) types.GomegaMatcher {142return &matchers.MatchErrorMatcher{143Expected: expected,144FuncErrDescription: functionErrorDescription,145}146}147148// MatchErrorStrictly succeeds iff actual is a non-nil error that matches the passed in149// expected error according to errors.Is(actual, expected).150//151// This behavior differs from MatchError where152//153// Expect(errors.New("some error")).To(MatchError(errors.New("some error")))154//155// succeeds, but errors.Is would return false so:156//157// Expect(errors.New("some error")).To(MatchErrorStrictly(errors.New("some error")))158//159// fails.160func MatchErrorStrictly(expected error) types.GomegaMatcher {161return &matchers.MatchErrorStrictlyMatcher{162Expected: expected,163}164}165166// BeClosed succeeds if actual is a closed channel.167// It is an error to pass a non-channel to BeClosed, it is also an error to pass nil168//169// In order to check whether or not the channel is closed, Gomega must try to read from the channel170// (even in the `ShouldNot(BeClosed())` case). You should keep this in mind if you wish to make subsequent assertions about171// values coming down the channel.172//173// Also, if you are testing that a *buffered* channel is closed you must first read all values out of the channel before174// 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).175//176// Finally, as a corollary: it is an error to check whether or not a send-only channel is closed.177func BeClosed() types.GomegaMatcher {178return &matchers.BeClosedMatcher{}179}180181// Receive succeeds if there is a value to be received on actual.182// Actual must be a channel (and cannot be a send-only channel) -- anything else is an error.183//184// Receive returns immediately and never blocks:185//186// - If there is nothing on the channel `c` then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.187//188// - If the channel `c` is closed then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.189//190// - 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.191//192// If you have a go-routine running in the background that will write to channel `c` you can:193//194// Eventually(c).Should(Receive())195//196// This will timeout if nothing gets sent to `c` (you can modify the timeout interval as you normally do with `Eventually`)197//198// 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`:199//200// Consistently(c).ShouldNot(Receive())201//202// You can pass `Receive` a matcher. If you do so, it will match the received object against the matcher. For example:203//204// Expect(c).Should(Receive(Equal("foo")))205//206// When given a matcher, `Receive` will always fail if there is nothing to be received on the channel.207//208// Passing Receive a matcher is especially useful when paired with Eventually:209//210// Eventually(c).Should(Receive(ContainSubstring("bar")))211//212// will repeatedly attempt to pull values out of `c` until a value matching "bar" is received.213//214// 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:215//216// var myThing thing217// Eventually(thingChan).Should(Receive(&myThing))218// Expect(myThing.Sprocket).Should(Equal("foo"))219// Expect(myThing.IsValid()).Should(BeTrue())220//221// 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,222// you can pass a pointer to a variable of the appropriate type first, and second a matcher:223//224// var myThing thing225// Eventually(thingChan).Should(Receive(&myThing, ContainSubstring("bar")))226func Receive(args ...any) types.GomegaMatcher {227return &matchers.ReceiveMatcher{228Args: args,229}230}231232// BeSent succeeds if a value can be sent to actual.233// 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.234// In addition, actual must not be closed.235//236// BeSent never blocks:237//238// - If the channel `c` is not ready to receive then Expect(c).Should(BeSent("foo")) will fail immediately239// - 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 timeout240// - If the channel `c` is closed then Expect(c).Should(BeSent("foo")) and Ω(c).ShouldNot(BeSent("foo")) will both fail immediately241//242// 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).243// Rather, the point of `BeSent` is to make it possible to easily and expressively write tests that can timeout on blocked channel sends.244func BeSent(arg any) types.GomegaMatcher {245return &matchers.BeSentMatcher{246Arg: arg,247}248}249250// MatchRegexp succeeds if actual is a string or stringer that matches the251// passed-in regexp. Optional arguments can be provided to construct a regexp252// via fmt.Sprintf().253func MatchRegexp(regexp string, args ...any) types.GomegaMatcher {254return &matchers.MatchRegexpMatcher{255Regexp: regexp,256Args: args,257}258}259260// ContainSubstring succeeds if actual is a string or stringer that contains the261// passed-in substring. Optional arguments can be provided to construct the substring262// via fmt.Sprintf().263func ContainSubstring(substr string, args ...any) types.GomegaMatcher {264return &matchers.ContainSubstringMatcher{265Substr: substr,266Args: args,267}268}269270// HavePrefix succeeds if actual is a string or stringer that contains the271// passed-in string as a prefix. Optional arguments can be provided to construct272// via fmt.Sprintf().273func HavePrefix(prefix string, args ...any) types.GomegaMatcher {274return &matchers.HavePrefixMatcher{275Prefix: prefix,276Args: args,277}278}279280// HaveSuffix succeeds if actual is a string or stringer that contains the281// passed-in string as a suffix. Optional arguments can be provided to construct282// via fmt.Sprintf().283func HaveSuffix(suffix string, args ...any) types.GomegaMatcher {284return &matchers.HaveSuffixMatcher{285Suffix: suffix,286Args: args,287}288}289290// MatchJSON succeeds if actual is a string or stringer of JSON that matches291// the expected JSON. The JSONs are decoded and the resulting objects are compared via292// reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.293func MatchJSON(json any) types.GomegaMatcher {294return &matchers.MatchJSONMatcher{295JSONToMatch: json,296}297}298299// MatchXML succeeds if actual is a string or stringer of XML that matches300// the expected XML. The XMLs are decoded and the resulting objects are compared via301// reflect.DeepEqual so things like whitespaces shouldn't matter.302func MatchXML(xml any) types.GomegaMatcher {303return &matchers.MatchXMLMatcher{304XMLToMatch: xml,305}306}307308// MatchYAML succeeds if actual is a string or stringer of YAML that matches309// the expected YAML. The YAML's are decoded and the resulting objects are compared via310// reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.311func MatchYAML(yaml any) types.GomegaMatcher {312return &matchers.MatchYAMLMatcher{313YAMLToMatch: yaml,314}315}316317// BeEmpty succeeds if actual is empty. Actual must be of type string, array, map, chan, or slice.318func BeEmpty() types.GomegaMatcher {319return &matchers.BeEmptyMatcher{}320}321322// HaveLen succeeds if actual has the passed-in length. Actual must be of type string, array, map, chan, or slice.323func HaveLen(count int) types.GomegaMatcher {324return &matchers.HaveLenMatcher{325Count: count,326}327}328329// HaveCap succeeds if actual has the passed-in capacity. Actual must be of type array, chan, or slice.330func HaveCap(count int) types.GomegaMatcher {331return &matchers.HaveCapMatcher{332Count: count,333}334}335336// BeZero succeeds if actual is the zero value for its type or if actual is nil.337func BeZero() types.GomegaMatcher {338return &matchers.BeZeroMatcher{}339}340341// ContainElement succeeds if actual contains the passed in element. By default342// ContainElement() uses Equal() to perform the match, however a matcher can be343// passed in instead:344//345// Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubstring("Bar")))346//347// Actual must be an array, slice or map. For maps, ContainElement searches348// through the map's values.349//350// If you want to have a copy of the matching element(s) found you can pass a351// pointer to a variable of the appropriate type. If the variable isn't a slice352// or map, then exactly one match will be expected and returned. If the variable353// is a slice or map, then at least one match is expected and all matches will be354// stored in the variable.355//356// var findings []string357// Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubString("Bar", &findings)))358func ContainElement(element any, result ...any) types.GomegaMatcher {359return &matchers.ContainElementMatcher{360Element: element,361Result: result,362}363}364365// BeElementOf succeeds if actual is contained in the passed in elements.366// BeElementOf() always uses Equal() to perform the match.367// When the passed in elements are comprised of a single element that is either an Array or Slice, BeElementOf() behaves368// as the reverse of ContainElement() that operates with Equal() to perform the match.369//370// Expect(2).Should(BeElementOf([]int{1, 2}))371// Expect(2).Should(BeElementOf([2]int{1, 2}))372//373// Otherwise, BeElementOf() provides a syntactic sugar for Or(Equal(_), Equal(_), ...):374//375// Expect(2).Should(BeElementOf(1, 2))376//377// Actual must be typed.378func BeElementOf(elements ...any) types.GomegaMatcher {379return &matchers.BeElementOfMatcher{380Elements: elements,381}382}383384// BeKeyOf succeeds if actual is contained in the keys of the passed in map.385// BeKeyOf() always uses Equal() to perform the match between actual and the map keys.386//387// Expect("foo").Should(BeKeyOf(map[string]bool{"foo": true, "bar": false}))388func BeKeyOf(element any) types.GomegaMatcher {389return &matchers.BeKeyOfMatcher{390Map: element,391}392}393394// ConsistOf succeeds if actual contains precisely the elements passed into the matcher. The ordering of the elements does not matter.395// By default ConsistOf() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:396//397// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf("FooBar", "Foo"))398// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Bar"), "Foo"))399// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Foo"), ContainSubstring("Foo")))400//401// Actual must be an array, slice or map. For maps, ConsistOf matches against the map's values.402//403// 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 it404// is the only element passed in to ConsistOf:405//406// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf([]string{"FooBar", "Foo"}))407//408// Note that Go's type system does not allow you to write this as ConsistOf([]string{"FooBar", "Foo"}...) as []string and []any are different types - hence the need for this special rule.409func ConsistOf(elements ...any) types.GomegaMatcher {410return &matchers.ConsistOfMatcher{411Elements: elements,412}413}414415// HaveExactElements succeeds if actual contains elements that precisely match the elements passed into the matcher. The ordering of the elements does matter.416// By default HaveExactElements() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:417//418// Expect([]string{"Foo", "FooBar"}).Should(HaveExactElements("Foo", "FooBar"))419// Expect([]string{"Foo", "FooBar"}).Should(HaveExactElements("Foo", ContainSubstring("Bar")))420// Expect([]string{"Foo", "FooBar"}).Should(HaveExactElements(ContainSubstring("Foo"), ContainSubstring("Foo")))421//422// Actual must be an array or slice.423func HaveExactElements(elements ...any) types.GomegaMatcher {424return &matchers.HaveExactElementsMatcher{425Elements: elements,426}427}428429// ContainElements succeeds if actual contains the passed in elements. The ordering of the elements does not matter.430// By default ContainElements() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:431//432// Expect([]string{"Foo", "FooBar"}).Should(ContainElements("FooBar"))433// Expect([]string{"Foo", "FooBar"}).Should(ContainElements(ContainSubstring("Bar"), "Foo"))434//435// Actual must be an array, slice or map.436// For maps, ContainElements searches through the map's values.437func ContainElements(elements ...any) types.GomegaMatcher {438return &matchers.ContainElementsMatcher{439Elements: elements,440}441}442443// HaveEach succeeds if actual solely contains elements that match the passed in element.444// Please note that if actual is empty, HaveEach always will fail.445// By default HaveEach() uses Equal() to perform the match, however a446// matcher can be passed in instead:447//448// Expect([]string{"Foo", "FooBar"}).Should(HaveEach(ContainSubstring("Foo")))449//450// Actual must be an array, slice or map.451// For maps, HaveEach searches through the map's values.452func HaveEach(element any) types.GomegaMatcher {453return &matchers.HaveEachMatcher{454Element: element,455}456}457458// HaveKey succeeds if actual is a map with the passed in key.459// By default HaveKey uses Equal() to perform the match, however a460// matcher can be passed in instead:461//462// Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKey(MatchRegexp(`.+Foo$`)))463func HaveKey(key any) types.GomegaMatcher {464return &matchers.HaveKeyMatcher{465Key: key,466}467}468469// HaveKeyWithValue succeeds if actual is a map with the passed in key and value.470// By default HaveKeyWithValue uses Equal() to perform the match, however a471// matcher can be passed in instead:472//473// Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue("Foo", "Bar"))474// Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue(MatchRegexp(`.+Foo$`), "Bar"))475func HaveKeyWithValue(key any, value any) types.GomegaMatcher {476return &matchers.HaveKeyWithValueMatcher{477Key: key,478Value: value,479}480}481482// HaveField succeeds if actual is a struct and the value at the passed in field483// matches the passed in matcher. By default HaveField used Equal() to perform the match,484// however a matcher can be passed in in stead.485//486// The field must be a string that resolves to the name of a field in the struct. Structs can be traversed487// using the '.' delimiter. If the field ends with '()' a method named field is assumed to exist on the struct and is invoked.488// Such methods must take no arguments and return a single value:489//490// type Book struct {491// Title string492// Author Person493// }494// type Person struct {495// FirstName string496// LastName string497// DOB time.Time498// }499// Expect(book).To(HaveField("Title", "Les Miserables"))500// Expect(book).To(HaveField("Title", ContainSubstring("Les"))501// Expect(book).To(HaveField("Author.FirstName", Equal("Victor"))502// Expect(book).To(HaveField("Author.DOB.Year()", BeNumerically("<", 1900))503func HaveField(field string, expected any) types.GomegaMatcher {504return &matchers.HaveFieldMatcher{505Field: field,506Expected: expected,507}508}509510// HaveExistingField succeeds if actual is a struct and the specified field511// exists.512//513// HaveExistingField can be combined with HaveField in order to cover use cases514// with optional fields. HaveField alone would trigger an error in such situations.515//516// Expect(MrHarmless).NotTo(And(HaveExistingField("Title"), HaveField("Title", "Supervillain")))517func HaveExistingField(field string) types.GomegaMatcher {518return &matchers.HaveExistingFieldMatcher{519Field: field,520}521}522523// HaveValue applies the given matcher to the value of actual, optionally and524// repeatedly dereferencing pointers or taking the concrete value of interfaces.525// Thus, the matcher will always be applied to non-pointer and non-interface526// values only. HaveValue will fail with an error if a pointer or interface is527// nil. It will also fail for more than 31 pointer or interface dereferences to528// guard against mistakenly applying it to arbitrarily deep linked pointers.529//530// HaveValue differs from gstruct.PointTo in that it does not expect actual to531// be a pointer (as gstruct.PointTo does) but instead also accepts non-pointer532// and even interface values.533//534// actual := 42535// Expect(actual).To(HaveValue(Equal(42)))536// Expect(&actual).To(HaveValue(Equal(42)))537func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher {538return &matchers.HaveValueMatcher{539Matcher: matcher,540}541}542543// BeNumerically performs numerical assertions in a type-agnostic way.544// Actual and expected should be numbers, though the specific type of545// number is irrelevant (float32, float64, uint8, etc...).546//547// There are six, self-explanatory, supported comparators:548//549// Expect(1.0).Should(BeNumerically("==", 1))550// Expect(1.0).Should(BeNumerically("~", 0.999, 0.01))551// Expect(1.0).Should(BeNumerically(">", 0.9))552// Expect(1.0).Should(BeNumerically(">=", 1.0))553// Expect(1.0).Should(BeNumerically("<", 3))554// Expect(1.0).Should(BeNumerically("<=", 1.0))555func BeNumerically(comparator string, compareTo ...any) types.GomegaMatcher {556return &matchers.BeNumericallyMatcher{557Comparator: comparator,558CompareTo: compareTo,559}560}561562// BeTemporally compares time.Time's like BeNumerically563// Actual and expected must be time.Time. The comparators are the same as for BeNumerically564//565// Expect(time.Now()).Should(BeTemporally(">", time.Time{}))566// Expect(time.Now()).Should(BeTemporally("~", time.Now(), time.Second))567func BeTemporally(comparator string, compareTo time.Time, threshold ...time.Duration) types.GomegaMatcher {568return &matchers.BeTemporallyMatcher{569Comparator: comparator,570CompareTo: compareTo,571Threshold: threshold,572}573}574575// BeAssignableToTypeOf succeeds if actual is assignable to the type of expected.576// It will return an error when one of the values is nil.577//578// Expect(0).Should(BeAssignableToTypeOf(0)) // Same values579// Expect(5).Should(BeAssignableToTypeOf(-1)) // different values same type580// Expect("foo").Should(BeAssignableToTypeOf("bar")) // different values same type581// Expect(struct{ Foo string }{}).Should(BeAssignableToTypeOf(struct{ Foo string }{}))582func BeAssignableToTypeOf(expected any) types.GomegaMatcher {583return &matchers.AssignableToTypeOfMatcher{584Expected: expected,585}586}587588// Panic succeeds if actual is a function that, when invoked, panics.589// Actual must be a function that takes no arguments and returns no results.590func Panic() types.GomegaMatcher {591return &matchers.PanicMatcher{}592}593594// PanicWith succeeds if actual is a function that, when invoked, panics with a specific value.595// Actual must be a function that takes no arguments and returns no results.596//597// By default PanicWith uses Equal() to perform the match, however a598// matcher can be passed in instead:599//600// Expect(fn).Should(PanicWith(MatchRegexp(`.+Foo$`)))601func PanicWith(expected any) types.GomegaMatcher {602return &matchers.PanicMatcher{Expected: expected}603}604605// BeAnExistingFile succeeds if a file exists.606// Actual must be a string representing the abs path to the file being checked.607func BeAnExistingFile() types.GomegaMatcher {608return &matchers.BeAnExistingFileMatcher{}609}610611// BeARegularFile succeeds if a file exists and is a regular file.612// Actual must be a string representing the abs path to the file being checked.613func BeARegularFile() types.GomegaMatcher {614return &matchers.BeARegularFileMatcher{}615}616617// BeADirectory succeeds if a file exists and is a directory.618// Actual must be a string representing the abs path to the file being checked.619func BeADirectory() types.GomegaMatcher {620return &matchers.BeADirectoryMatcher{}621}622623// HaveHTTPStatus succeeds if the Status or StatusCode field of an HTTP response matches.624// Actual must be either a *http.Response or *httptest.ResponseRecorder.625// Expected must be either an int or a string.626//627// Expect(resp).Should(HaveHTTPStatus(http.StatusOK)) // asserts that resp.StatusCode == 200628// Expect(resp).Should(HaveHTTPStatus("404 Not Found")) // asserts that resp.Status == "404 Not Found"629// Expect(resp).Should(HaveHTTPStatus(http.StatusOK, http.StatusNoContent)) // asserts that resp.StatusCode == 200 || resp.StatusCode == 204630func HaveHTTPStatus(expected ...any) types.GomegaMatcher {631return &matchers.HaveHTTPStatusMatcher{Expected: expected}632}633634// HaveHTTPHeaderWithValue succeeds if the header is found and the value matches.635// Actual must be either a *http.Response or *httptest.ResponseRecorder.636// Expected must be a string header name, followed by a header value which637// can be a string, or another matcher.638func HaveHTTPHeaderWithValue(header string, value any) types.GomegaMatcher {639return &matchers.HaveHTTPHeaderWithValueMatcher{640Header: header,641Value: value,642}643}644645// HaveHTTPBody matches if the body matches.646// Actual must be either a *http.Response or *httptest.ResponseRecorder.647// Expected must be either a string, []byte, or other matcher648func HaveHTTPBody(expected any) types.GomegaMatcher {649return &matchers.HaveHTTPBodyMatcher{Expected: expected}650}651652// And succeeds only if all of the given matchers succeed.653// The matchers are tried in order, and will fail-fast if one doesn't succeed.654//655// Expect("hi").To(And(HaveLen(2), Equal("hi"))656//657// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.658func And(ms ...types.GomegaMatcher) types.GomegaMatcher {659return &matchers.AndMatcher{Matchers: ms}660}661662// SatisfyAll is an alias for And().663//664// Expect("hi").Should(SatisfyAll(HaveLen(2), Equal("hi")))665func SatisfyAll(matchers ...types.GomegaMatcher) types.GomegaMatcher {666return And(matchers...)667}668669// Or succeeds if any of the given matchers succeed.670// The matchers are tried in order and will return immediately upon the first successful match.671//672// Expect("hi").To(Or(HaveLen(3), HaveLen(2))673//674// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.675func Or(ms ...types.GomegaMatcher) types.GomegaMatcher {676return &matchers.OrMatcher{Matchers: ms}677}678679// SatisfyAny is an alias for Or().680//681// Expect("hi").SatisfyAny(Or(HaveLen(3), HaveLen(2))682func SatisfyAny(matchers ...types.GomegaMatcher) types.GomegaMatcher {683return Or(matchers...)684}685686// Not negates the given matcher; it succeeds if the given matcher fails.687//688// Expect(1).To(Not(Equal(2))689//690// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.691func Not(matcher types.GomegaMatcher) types.GomegaMatcher {692return &matchers.NotMatcher{Matcher: matcher}693}694695// WithTransform applies the `transform` to the actual value and matches it against `matcher`.696// The given transform must be either a function of one parameter that returns one value or a697// function of one parameter that returns two values, where the second value must be of the698// error type.699//700// var plus1 = func(i int) int { return i + 1 }701// Expect(1).To(WithTransform(plus1, Equal(2))702//703// var failingplus1 = func(i int) (int, error) { return 42, "this does not compute" }704// Expect(1).To(WithTransform(failingplus1, Equal(2)))705//706// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.707func WithTransform(transform any, matcher types.GomegaMatcher) types.GomegaMatcher {708return matchers.NewWithTransformMatcher(transform, matcher)709}710711// Satisfy matches the actual value against the `predicate` function.712// The given predicate must be a function of one parameter that returns bool.713//714// var isEven = func(i int) bool { return i%2 == 0 }715// Expect(2).To(Satisfy(isEven))716func Satisfy(predicate any) types.GomegaMatcher {717return matchers.NewSatisfyMatcher(predicate)718}719720721