Path: blob/main/vendor/github.com/onsi/gomega/matchers.go
2875 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// BeClosed succeeds if actual is a closed channel.149// It is an error to pass a non-channel to BeClosed, it is also an error to pass nil150//151// In order to check whether or not the channel is closed, Gomega must try to read from the channel152// (even in the `ShouldNot(BeClosed())` case). You should keep this in mind if you wish to make subsequent assertions about153// values coming down the channel.154//155// Also, if you are testing that a *buffered* channel is closed you must first read all values out of the channel before156// 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).157//158// Finally, as a corollary: it is an error to check whether or not a send-only channel is closed.159func BeClosed() types.GomegaMatcher {160return &matchers.BeClosedMatcher{}161}162163// Receive succeeds if there is a value to be received on actual.164// Actual must be a channel (and cannot be a send-only channel) -- anything else is an error.165//166// Receive returns immediately and never blocks:167//168// - If there is nothing on the channel `c` then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.169//170// - If the channel `c` is closed then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.171//172// - 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.173//174// If you have a go-routine running in the background that will write to channel `c` you can:175//176// Eventually(c).Should(Receive())177//178// This will timeout if nothing gets sent to `c` (you can modify the timeout interval as you normally do with `Eventually`)179//180// 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`:181//182// Consistently(c).ShouldNot(Receive())183//184// You can pass `Receive` a matcher. If you do so, it will match the received object against the matcher. For example:185//186// Expect(c).Should(Receive(Equal("foo")))187//188// When given a matcher, `Receive` will always fail if there is nothing to be received on the channel.189//190// Passing Receive a matcher is especially useful when paired with Eventually:191//192// Eventually(c).Should(Receive(ContainSubstring("bar")))193//194// will repeatedly attempt to pull values out of `c` until a value matching "bar" is received.195//196// 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:197//198// var myThing thing199// Eventually(thingChan).Should(Receive(&myThing))200// Expect(myThing.Sprocket).Should(Equal("foo"))201// Expect(myThing.IsValid()).Should(BeTrue())202//203// 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,204// you can pass a pointer to a variable of the appropriate type first, and second a matcher:205//206// var myThing thing207// Eventually(thingChan).Should(Receive(&myThing, ContainSubstring("bar")))208func Receive(args ...any) types.GomegaMatcher {209return &matchers.ReceiveMatcher{210Args: args,211}212}213214// BeSent succeeds if a value can be sent to actual.215// 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.216// In addition, actual must not be closed.217//218// BeSent never blocks:219//220// - If the channel `c` is not ready to receive then Expect(c).Should(BeSent("foo")) will fail immediately221// - 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 timeout222// - If the channel `c` is closed then Expect(c).Should(BeSent("foo")) and Ω(c).ShouldNot(BeSent("foo")) will both fail immediately223//224// 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).225// Rather, the point of `BeSent` is to make it possible to easily and expressively write tests that can timeout on blocked channel sends.226func BeSent(arg any) types.GomegaMatcher {227return &matchers.BeSentMatcher{228Arg: arg,229}230}231232// MatchRegexp succeeds if actual is a string or stringer that matches the233// passed-in regexp. Optional arguments can be provided to construct a regexp234// via fmt.Sprintf().235func MatchRegexp(regexp string, args ...any) types.GomegaMatcher {236return &matchers.MatchRegexpMatcher{237Regexp: regexp,238Args: args,239}240}241242// ContainSubstring succeeds if actual is a string or stringer that contains the243// passed-in substring. Optional arguments can be provided to construct the substring244// via fmt.Sprintf().245func ContainSubstring(substr string, args ...any) types.GomegaMatcher {246return &matchers.ContainSubstringMatcher{247Substr: substr,248Args: args,249}250}251252// HavePrefix succeeds if actual is a string or stringer that contains the253// passed-in string as a prefix. Optional arguments can be provided to construct254// via fmt.Sprintf().255func HavePrefix(prefix string, args ...any) types.GomegaMatcher {256return &matchers.HavePrefixMatcher{257Prefix: prefix,258Args: args,259}260}261262// HaveSuffix succeeds if actual is a string or stringer that contains the263// passed-in string as a suffix. Optional arguments can be provided to construct264// via fmt.Sprintf().265func HaveSuffix(suffix string, args ...any) types.GomegaMatcher {266return &matchers.HaveSuffixMatcher{267Suffix: suffix,268Args: args,269}270}271272// MatchJSON succeeds if actual is a string or stringer of JSON that matches273// the expected JSON. The JSONs are decoded and the resulting objects are compared via274// reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.275func MatchJSON(json any) types.GomegaMatcher {276return &matchers.MatchJSONMatcher{277JSONToMatch: json,278}279}280281// MatchXML succeeds if actual is a string or stringer of XML that matches282// the expected XML. The XMLs are decoded and the resulting objects are compared via283// reflect.DeepEqual so things like whitespaces shouldn't matter.284func MatchXML(xml any) types.GomegaMatcher {285return &matchers.MatchXMLMatcher{286XMLToMatch: xml,287}288}289290// MatchYAML succeeds if actual is a string or stringer of YAML that matches291// the expected YAML. The YAML's are decoded and the resulting objects are compared via292// reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.293func MatchYAML(yaml any) types.GomegaMatcher {294return &matchers.MatchYAMLMatcher{295YAMLToMatch: yaml,296}297}298299// BeEmpty succeeds if actual is empty. Actual must be of type string, array, map, chan, or slice.300func BeEmpty() types.GomegaMatcher {301return &matchers.BeEmptyMatcher{}302}303304// HaveLen succeeds if actual has the passed-in length. Actual must be of type string, array, map, chan, or slice.305func HaveLen(count int) types.GomegaMatcher {306return &matchers.HaveLenMatcher{307Count: count,308}309}310311// HaveCap succeeds if actual has the passed-in capacity. Actual must be of type array, chan, or slice.312func HaveCap(count int) types.GomegaMatcher {313return &matchers.HaveCapMatcher{314Count: count,315}316}317318// BeZero succeeds if actual is the zero value for its type or if actual is nil.319func BeZero() types.GomegaMatcher {320return &matchers.BeZeroMatcher{}321}322323// ContainElement succeeds if actual contains the passed in element. By default324// ContainElement() uses Equal() to perform the match, however a matcher can be325// passed in instead:326//327// Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubstring("Bar")))328//329// Actual must be an array, slice or map. For maps, ContainElement searches330// through the map's values.331//332// If you want to have a copy of the matching element(s) found you can pass a333// pointer to a variable of the appropriate type. If the variable isn't a slice334// or map, then exactly one match will be expected and returned. If the variable335// is a slice or map, then at least one match is expected and all matches will be336// stored in the variable.337//338// var findings []string339// Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubString("Bar", &findings)))340func ContainElement(element any, result ...any) types.GomegaMatcher {341return &matchers.ContainElementMatcher{342Element: element,343Result: result,344}345}346347// BeElementOf succeeds if actual is contained in the passed in elements.348// BeElementOf() always uses Equal() to perform the match.349// When the passed in elements are comprised of a single element that is either an Array or Slice, BeElementOf() behaves350// as the reverse of ContainElement() that operates with Equal() to perform the match.351//352// Expect(2).Should(BeElementOf([]int{1, 2}))353// Expect(2).Should(BeElementOf([2]int{1, 2}))354//355// Otherwise, BeElementOf() provides a syntactic sugar for Or(Equal(_), Equal(_), ...):356//357// Expect(2).Should(BeElementOf(1, 2))358//359// Actual must be typed.360func BeElementOf(elements ...any) types.GomegaMatcher {361return &matchers.BeElementOfMatcher{362Elements: elements,363}364}365366// BeKeyOf succeeds if actual is contained in the keys of the passed in map.367// BeKeyOf() always uses Equal() to perform the match between actual and the map keys.368//369// Expect("foo").Should(BeKeyOf(map[string]bool{"foo": true, "bar": false}))370func BeKeyOf(element any) types.GomegaMatcher {371return &matchers.BeKeyOfMatcher{372Map: element,373}374}375376// ConsistOf succeeds if actual contains precisely the elements passed into the matcher. The ordering of the elements does not matter.377// By default ConsistOf() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:378//379// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf("FooBar", "Foo"))380// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Bar"), "Foo"))381// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Foo"), ContainSubstring("Foo")))382//383// Actual must be an array, slice or map. For maps, ConsistOf matches against the map's values.384//385// 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 it386// is the only element passed in to ConsistOf:387//388// Expect([]string{"Foo", "FooBar"}).Should(ConsistOf([]string{"FooBar", "Foo"}))389//390// 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.391func ConsistOf(elements ...any) types.GomegaMatcher {392return &matchers.ConsistOfMatcher{393Elements: elements,394}395}396397// HaveExactElements succeeds if actual contains elements that precisely match the elements passed into the matcher. The ordering of the elements does matter.398// By default HaveExactElements() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:399//400// Expect([]string{"Foo", "FooBar"}).Should(HaveExactElements("Foo", "FooBar"))401// Expect([]string{"Foo", "FooBar"}).Should(HaveExactElements("Foo", ContainSubstring("Bar")))402// Expect([]string{"Foo", "FooBar"}).Should(HaveExactElements(ContainSubstring("Foo"), ContainSubstring("Foo")))403//404// Actual must be an array or slice.405func HaveExactElements(elements ...any) types.GomegaMatcher {406return &matchers.HaveExactElementsMatcher{407Elements: elements,408}409}410411// ContainElements succeeds if actual contains the passed in elements. The ordering of the elements does not matter.412// By default ContainElements() uses Equal() to match the elements, however custom matchers can be passed in instead. Here are some examples:413//414// Expect([]string{"Foo", "FooBar"}).Should(ContainElements("FooBar"))415// Expect([]string{"Foo", "FooBar"}).Should(ContainElements(ContainSubstring("Bar"), "Foo"))416//417// Actual must be an array, slice or map.418// For maps, ContainElements searches through the map's values.419func ContainElements(elements ...any) types.GomegaMatcher {420return &matchers.ContainElementsMatcher{421Elements: elements,422}423}424425// HaveEach succeeds if actual solely contains elements that match the passed in element.426// Please note that if actual is empty, HaveEach always will fail.427// By default HaveEach() uses Equal() to perform the match, however a428// matcher can be passed in instead:429//430// Expect([]string{"Foo", "FooBar"}).Should(HaveEach(ContainSubstring("Foo")))431//432// Actual must be an array, slice or map.433// For maps, HaveEach searches through the map's values.434func HaveEach(element any) types.GomegaMatcher {435return &matchers.HaveEachMatcher{436Element: element,437}438}439440// HaveKey succeeds if actual is a map with the passed in key.441// By default HaveKey uses Equal() to perform the match, however a442// matcher can be passed in instead:443//444// Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKey(MatchRegexp(`.+Foo$`)))445func HaveKey(key any) types.GomegaMatcher {446return &matchers.HaveKeyMatcher{447Key: key,448}449}450451// HaveKeyWithValue succeeds if actual is a map with the passed in key and value.452// By default HaveKeyWithValue uses Equal() to perform the match, however a453// matcher can be passed in instead:454//455// Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue("Foo", "Bar"))456// Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue(MatchRegexp(`.+Foo$`), "Bar"))457func HaveKeyWithValue(key any, value any) types.GomegaMatcher {458return &matchers.HaveKeyWithValueMatcher{459Key: key,460Value: value,461}462}463464// HaveField succeeds if actual is a struct and the value at the passed in field465// matches the passed in matcher. By default HaveField used Equal() to perform the match,466// however a matcher can be passed in in stead.467//468// The field must be a string that resolves to the name of a field in the struct. Structs can be traversed469// using the '.' delimiter. If the field ends with '()' a method named field is assumed to exist on the struct and is invoked.470// Such methods must take no arguments and return a single value:471//472// type Book struct {473// Title string474// Author Person475// }476// type Person struct {477// FirstName string478// LastName string479// DOB time.Time480// }481// Expect(book).To(HaveField("Title", "Les Miserables"))482// Expect(book).To(HaveField("Title", ContainSubstring("Les"))483// Expect(book).To(HaveField("Author.FirstName", Equal("Victor"))484// Expect(book).To(HaveField("Author.DOB.Year()", BeNumerically("<", 1900))485func HaveField(field string, expected any) types.GomegaMatcher {486return &matchers.HaveFieldMatcher{487Field: field,488Expected: expected,489}490}491492// HaveExistingField succeeds if actual is a struct and the specified field493// exists.494//495// HaveExistingField can be combined with HaveField in order to cover use cases496// with optional fields. HaveField alone would trigger an error in such situations.497//498// Expect(MrHarmless).NotTo(And(HaveExistingField("Title"), HaveField("Title", "Supervillain")))499func HaveExistingField(field string) types.GomegaMatcher {500return &matchers.HaveExistingFieldMatcher{501Field: field,502}503}504505// HaveValue applies the given matcher to the value of actual, optionally and506// repeatedly dereferencing pointers or taking the concrete value of interfaces.507// Thus, the matcher will always be applied to non-pointer and non-interface508// values only. HaveValue will fail with an error if a pointer or interface is509// nil. It will also fail for more than 31 pointer or interface dereferences to510// guard against mistakenly applying it to arbitrarily deep linked pointers.511//512// HaveValue differs from gstruct.PointTo in that it does not expect actual to513// be a pointer (as gstruct.PointTo does) but instead also accepts non-pointer514// and even interface values.515//516// actual := 42517// Expect(actual).To(HaveValue(42))518// Expect(&actual).To(HaveValue(42))519func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher {520return &matchers.HaveValueMatcher{521Matcher: matcher,522}523}524525// BeNumerically performs numerical assertions in a type-agnostic way.526// Actual and expected should be numbers, though the specific type of527// number is irrelevant (float32, float64, uint8, etc...).528//529// There are six, self-explanatory, supported comparators:530//531// Expect(1.0).Should(BeNumerically("==", 1))532// Expect(1.0).Should(BeNumerically("~", 0.999, 0.01))533// Expect(1.0).Should(BeNumerically(">", 0.9))534// Expect(1.0).Should(BeNumerically(">=", 1.0))535// Expect(1.0).Should(BeNumerically("<", 3))536// Expect(1.0).Should(BeNumerically("<=", 1.0))537func BeNumerically(comparator string, compareTo ...any) types.GomegaMatcher {538return &matchers.BeNumericallyMatcher{539Comparator: comparator,540CompareTo: compareTo,541}542}543544// BeTemporally compares time.Time's like BeNumerically545// Actual and expected must be time.Time. The comparators are the same as for BeNumerically546//547// Expect(time.Now()).Should(BeTemporally(">", time.Time{}))548// Expect(time.Now()).Should(BeTemporally("~", time.Now(), time.Second))549func BeTemporally(comparator string, compareTo time.Time, threshold ...time.Duration) types.GomegaMatcher {550return &matchers.BeTemporallyMatcher{551Comparator: comparator,552CompareTo: compareTo,553Threshold: threshold,554}555}556557// BeAssignableToTypeOf succeeds if actual is assignable to the type of expected.558// It will return an error when one of the values is nil.559//560// Expect(0).Should(BeAssignableToTypeOf(0)) // Same values561// Expect(5).Should(BeAssignableToTypeOf(-1)) // different values same type562// Expect("foo").Should(BeAssignableToTypeOf("bar")) // different values same type563// Expect(struct{ Foo string }{}).Should(BeAssignableToTypeOf(struct{ Foo string }{}))564func BeAssignableToTypeOf(expected any) types.GomegaMatcher {565return &matchers.AssignableToTypeOfMatcher{566Expected: expected,567}568}569570// Panic succeeds if actual is a function that, when invoked, panics.571// Actual must be a function that takes no arguments and returns no results.572func Panic() types.GomegaMatcher {573return &matchers.PanicMatcher{}574}575576// PanicWith succeeds if actual is a function that, when invoked, panics with a specific value.577// Actual must be a function that takes no arguments and returns no results.578//579// By default PanicWith uses Equal() to perform the match, however a580// matcher can be passed in instead:581//582// Expect(fn).Should(PanicWith(MatchRegexp(`.+Foo$`)))583func PanicWith(expected any) types.GomegaMatcher {584return &matchers.PanicMatcher{Expected: expected}585}586587// BeAnExistingFile succeeds if a file exists.588// Actual must be a string representing the abs path to the file being checked.589func BeAnExistingFile() types.GomegaMatcher {590return &matchers.BeAnExistingFileMatcher{}591}592593// BeARegularFile succeeds if a file exists and is a regular file.594// Actual must be a string representing the abs path to the file being checked.595func BeARegularFile() types.GomegaMatcher {596return &matchers.BeARegularFileMatcher{}597}598599// BeADirectory succeeds if a file exists and is a directory.600// Actual must be a string representing the abs path to the file being checked.601func BeADirectory() types.GomegaMatcher {602return &matchers.BeADirectoryMatcher{}603}604605// HaveHTTPStatus succeeds if the Status or StatusCode field of an HTTP response matches.606// Actual must be either a *http.Response or *httptest.ResponseRecorder.607// Expected must be either an int or a string.608//609// Expect(resp).Should(HaveHTTPStatus(http.StatusOK)) // asserts that resp.StatusCode == 200610// Expect(resp).Should(HaveHTTPStatus("404 Not Found")) // asserts that resp.Status == "404 Not Found"611// Expect(resp).Should(HaveHTTPStatus(http.StatusOK, http.StatusNoContent)) // asserts that resp.StatusCode == 200 || resp.StatusCode == 204612func HaveHTTPStatus(expected ...any) types.GomegaMatcher {613return &matchers.HaveHTTPStatusMatcher{Expected: expected}614}615616// HaveHTTPHeaderWithValue succeeds if the header is found and the value matches.617// Actual must be either a *http.Response or *httptest.ResponseRecorder.618// Expected must be a string header name, followed by a header value which619// can be a string, or another matcher.620func HaveHTTPHeaderWithValue(header string, value any) types.GomegaMatcher {621return &matchers.HaveHTTPHeaderWithValueMatcher{622Header: header,623Value: value,624}625}626627// HaveHTTPBody matches if the body matches.628// Actual must be either a *http.Response or *httptest.ResponseRecorder.629// Expected must be either a string, []byte, or other matcher630func HaveHTTPBody(expected any) types.GomegaMatcher {631return &matchers.HaveHTTPBodyMatcher{Expected: expected}632}633634// And succeeds only if all of the given matchers succeed.635// The matchers are tried in order, and will fail-fast if one doesn't succeed.636//637// Expect("hi").To(And(HaveLen(2), Equal("hi"))638//639// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.640func And(ms ...types.GomegaMatcher) types.GomegaMatcher {641return &matchers.AndMatcher{Matchers: ms}642}643644// SatisfyAll is an alias for And().645//646// Expect("hi").Should(SatisfyAll(HaveLen(2), Equal("hi")))647func SatisfyAll(matchers ...types.GomegaMatcher) types.GomegaMatcher {648return And(matchers...)649}650651// Or succeeds if any of the given matchers succeed.652// The matchers are tried in order and will return immediately upon the first successful match.653//654// Expect("hi").To(Or(HaveLen(3), HaveLen(2))655//656// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.657func Or(ms ...types.GomegaMatcher) types.GomegaMatcher {658return &matchers.OrMatcher{Matchers: ms}659}660661// SatisfyAny is an alias for Or().662//663// Expect("hi").SatisfyAny(Or(HaveLen(3), HaveLen(2))664func SatisfyAny(matchers ...types.GomegaMatcher) types.GomegaMatcher {665return Or(matchers...)666}667668// Not negates the given matcher; it succeeds if the given matcher fails.669//670// Expect(1).To(Not(Equal(2))671//672// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.673func Not(matcher types.GomegaMatcher) types.GomegaMatcher {674return &matchers.NotMatcher{Matcher: matcher}675}676677// WithTransform applies the `transform` to the actual value and matches it against `matcher`.678// The given transform must be either a function of one parameter that returns one value or a679// function of one parameter that returns two values, where the second value must be of the680// error type.681//682// var plus1 = func(i int) int { return i + 1 }683// Expect(1).To(WithTransform(plus1, Equal(2))684//685// var failingplus1 = func(i int) (int, error) { return 42, "this does not compute" }686// Expect(1).To(WithTransform(failingplus1, Equal(2)))687//688// And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.689func WithTransform(transform any, matcher types.GomegaMatcher) types.GomegaMatcher {690return matchers.NewWithTransformMatcher(transform, matcher)691}692693// Satisfy matches the actual value against the `predicate` function.694// The given predicate must be a function of one parameter that returns bool.695//696// var isEven = func(i int) bool { return i%2 == 0 }697// Expect(2).To(Satisfy(isEven))698func Satisfy(predicate any) types.GomegaMatcher {699return matchers.NewSatisfyMatcher(predicate)700}701702703