github.com/onsi/gomega@v1.32.0/matchers.go (about)

     1  package gomega
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/onsi/gomega/matchers"
     9  	"github.com/onsi/gomega/types"
    10  )
    11  
    12  // Equal uses reflect.DeepEqual to compare actual with expected.  Equal is strict about
    13  // types when performing comparisons.
    14  // It is an error for both actual and expected to be nil.  Use BeNil() instead.
    15  func Equal(expected interface{}) types.GomegaMatcher {
    16  	return &matchers.EqualMatcher{
    17  		Expected: expected,
    18  	}
    19  }
    20  
    21  // BeEquivalentTo is more lax than Equal, allowing equality between different types.
    22  // This is done by converting actual to have the type of expected before
    23  // attempting equality with reflect.DeepEqual.
    24  // It is an error for actual and expected to be nil.  Use BeNil() instead.
    25  func BeEquivalentTo(expected interface{}) types.GomegaMatcher {
    26  	return &matchers.BeEquivalentToMatcher{
    27  		Expected: expected,
    28  	}
    29  }
    30  
    31  // BeComparableTo uses gocmp.Equal from github.com/google/go-cmp (instead of reflect.DeepEqual) to perform a deep comparison.
    32  // You can pass cmp.Option as options.
    33  // It is an error for actual and expected to be nil.  Use BeNil() instead.
    34  func BeComparableTo(expected interface{}, opts ...cmp.Option) types.GomegaMatcher {
    35  	return &matchers.BeComparableToMatcher{
    36  		Expected: expected,
    37  		Options:  opts,
    38  	}
    39  }
    40  
    41  // BeIdenticalTo uses the == operator to compare actual with expected.
    42  // BeIdenticalTo is strict about types when performing comparisons.
    43  // It is an error for both actual and expected to be nil.  Use BeNil() instead.
    44  func BeIdenticalTo(expected interface{}) types.GomegaMatcher {
    45  	return &matchers.BeIdenticalToMatcher{
    46  		Expected: expected,
    47  	}
    48  }
    49  
    50  // BeNil succeeds if actual is nil
    51  func BeNil() types.GomegaMatcher {
    52  	return &matchers.BeNilMatcher{}
    53  }
    54  
    55  // BeTrue succeeds if actual is true
    56  //
    57  // In general, it's better to use `BeTrueBecause(reason)` to provide a more useful error message if a true check fails.
    58  func BeTrue() types.GomegaMatcher {
    59  	return &matchers.BeTrueMatcher{}
    60  }
    61  
    62  // BeFalse succeeds if actual is false
    63  //
    64  // In general, it's better to use `BeFalseBecause(reason)` to provide a more useful error message if a false check fails.
    65  func BeFalse() types.GomegaMatcher {
    66  	return &matchers.BeFalseMatcher{}
    67  }
    68  
    69  // BeTrueBecause succeeds if actual is true and displays the provided reason if it is false
    70  // fmt.Sprintf is used to render the reason
    71  func BeTrueBecause(format string, args ...any) types.GomegaMatcher {
    72  	return &matchers.BeTrueMatcher{Reason: fmt.Sprintf(format, args...)}
    73  }
    74  
    75  // BeFalseBecause succeeds if actual is false and displays the provided reason if it is true.
    76  // fmt.Sprintf is used to render the reason
    77  func BeFalseBecause(format string, args ...any) types.GomegaMatcher {
    78  	return &matchers.BeFalseMatcher{Reason: fmt.Sprintf(format, args...)}
    79  }
    80  
    81  // HaveOccurred succeeds if actual is a non-nil error
    82  // The typical Go error checking pattern looks like:
    83  //
    84  //	err := SomethingThatMightFail()
    85  //	Expect(err).ShouldNot(HaveOccurred())
    86  func HaveOccurred() types.GomegaMatcher {
    87  	return &matchers.HaveOccurredMatcher{}
    88  }
    89  
    90  // Succeed passes if actual is a nil error
    91  // Succeed is intended to be used with functions that return a single error value. Instead of
    92  //
    93  //	err := SomethingThatMightFail()
    94  //	Expect(err).ShouldNot(HaveOccurred())
    95  //
    96  // You can write:
    97  //
    98  //	Expect(SomethingThatMightFail()).Should(Succeed())
    99  //
   100  // It is a mistake to use Succeed with a function that has multiple return values.  Gomega's Ω and Expect
   101  // functions automatically trigger failure if any return values after the first return value are non-zero/non-nil.
   102  // This means that Ω(MultiReturnFunc()).ShouldNot(Succeed()) can never pass.
   103  func Succeed() types.GomegaMatcher {
   104  	return &matchers.SucceedMatcher{}
   105  }
   106  
   107  // MatchError succeeds if actual is a non-nil error that matches the passed in
   108  // string, error, function, or matcher.
   109  //
   110  // These are valid use-cases:
   111  //
   112  // When passed a string:
   113  //
   114  //	Expect(err).To(MatchError("an error"))
   115  //
   116  // asserts that err.Error() == "an error"
   117  //
   118  // When passed an error:
   119  //
   120  //	Expect(err).To(MatchError(SomeError))
   121  //
   122  // First checks if errors.Is(err, SomeError).
   123  // If that fails then it checks if reflect.DeepEqual(err, SomeError) repeatedly for err and any errors wrapped by err
   124  //
   125  // When passed a matcher:
   126  //
   127  //	Expect(err).To(MatchError(ContainSubstring("sprocket not found")))
   128  //
   129  // the matcher is passed err.Error().  In this case it asserts that err.Error() contains substring "sprocket not found"
   130  //
   131  // When passed a func(err) bool and a description:
   132  //
   133  //	Expect(err).To(MatchError(os.IsNotExist, "IsNotExist"))
   134  //
   135  // the function is passed err and matches if the return value is true.  The description is required to allow Gomega
   136  // to print a useful error message.
   137  //
   138  // It is an error for err to be nil or an object that does not implement the
   139  // Error interface
   140  //
   141  // 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.
   142  func MatchError(expected interface{}, functionErrorDescription ...any) types.GomegaMatcher {
   143  	return &matchers.MatchErrorMatcher{
   144  		Expected:           expected,
   145  		FuncErrDescription: functionErrorDescription,
   146  	}
   147  }
   148  
   149  // BeClosed succeeds if actual is a closed channel.
   150  // It is an error to pass a non-channel to BeClosed, it is also an error to pass nil
   151  //
   152  // In order to check whether or not the channel is closed, Gomega must try to read from the channel
   153  // (even in the `ShouldNot(BeClosed())` case).  You should keep this in mind if you wish to make subsequent assertions about
   154  // values coming down the channel.
   155  //
   156  // Also, if you are testing that a *buffered* channel is closed you must first read all values out of the channel before
   157  // 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).
   158  //
   159  // Finally, as a corollary: it is an error to check whether or not a send-only channel is closed.
   160  func BeClosed() types.GomegaMatcher {
   161  	return &matchers.BeClosedMatcher{}
   162  }
   163  
   164  // Receive succeeds if there is a value to be received on actual.
   165  // Actual must be a channel (and cannot be a send-only channel) -- anything else is an error.
   166  //
   167  // Receive returns immediately and never blocks:
   168  //
   169  // - If there is nothing on the channel `c` then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.
   170  //
   171  // - If the channel `c` is closed then Expect(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.
   172  //
   173  // - 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.
   174  //
   175  // If you have a go-routine running in the background that will write to channel `c` you can:
   176  //
   177  //	Eventually(c).Should(Receive())
   178  //
   179  // This will timeout if nothing gets sent to `c` (you can modify the timeout interval as you normally do with `Eventually`)
   180  //
   181  // 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`:
   182  //
   183  //	Consistently(c).ShouldNot(Receive())
   184  //
   185  // You can pass `Receive` a matcher.  If you do so, it will match the received object against the matcher.  For example:
   186  //
   187  //	Expect(c).Should(Receive(Equal("foo")))
   188  //
   189  // When given a matcher, `Receive` will always fail if there is nothing to be received on the channel.
   190  //
   191  // Passing Receive a matcher is especially useful when paired with Eventually:
   192  //
   193  //	Eventually(c).Should(Receive(ContainSubstring("bar")))
   194  //
   195  // will repeatedly attempt to pull values out of `c` until a value matching "bar" is received.
   196  //
   197  // Finally, 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:
   198  //
   199  //	var myThing thing
   200  //	Eventually(thingChan).Should(Receive(&myThing))
   201  //	Expect(myThing.Sprocket).Should(Equal("foo"))
   202  //	Expect(myThing.IsValid()).Should(BeTrue())
   203  func Receive(args ...interface{}) types.GomegaMatcher {
   204  	var arg interface{}
   205  	if len(args) > 0 {
   206  		arg = args[0]
   207  	}
   208  
   209  	return &matchers.ReceiveMatcher{
   210  		Arg: arg,
   211  	}
   212  }
   213  
   214  // 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 immediately
   221  // - 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
   222  // - If the channel `c` is closed then Expect(c).Should(BeSent("foo")) and Ω(c).ShouldNot(BeSent("foo")) will both fail immediately
   223  //
   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.
   226  func BeSent(arg interface{}) types.GomegaMatcher {
   227  	return &matchers.BeSentMatcher{
   228  		Arg: arg,
   229  	}
   230  }
   231  
   232  // MatchRegexp succeeds if actual is a string or stringer that matches the
   233  // passed-in regexp.  Optional arguments can be provided to construct a regexp
   234  // via fmt.Sprintf().
   235  func MatchRegexp(regexp string, args ...interface{}) types.GomegaMatcher {
   236  	return &matchers.MatchRegexpMatcher{
   237  		Regexp: regexp,
   238  		Args:   args,
   239  	}
   240  }
   241  
   242  // ContainSubstring succeeds if actual is a string or stringer that contains the
   243  // passed-in substring.  Optional arguments can be provided to construct the substring
   244  // via fmt.Sprintf().
   245  func ContainSubstring(substr string, args ...interface{}) types.GomegaMatcher {
   246  	return &matchers.ContainSubstringMatcher{
   247  		Substr: substr,
   248  		Args:   args,
   249  	}
   250  }
   251  
   252  // HavePrefix succeeds if actual is a string or stringer that contains the
   253  // passed-in string as a prefix.  Optional arguments can be provided to construct
   254  // via fmt.Sprintf().
   255  func HavePrefix(prefix string, args ...interface{}) types.GomegaMatcher {
   256  	return &matchers.HavePrefixMatcher{
   257  		Prefix: prefix,
   258  		Args:   args,
   259  	}
   260  }
   261  
   262  // HaveSuffix succeeds if actual is a string or stringer that contains the
   263  // passed-in string as a suffix.  Optional arguments can be provided to construct
   264  // via fmt.Sprintf().
   265  func HaveSuffix(suffix string, args ...interface{}) types.GomegaMatcher {
   266  	return &matchers.HaveSuffixMatcher{
   267  		Suffix: suffix,
   268  		Args:   args,
   269  	}
   270  }
   271  
   272  // MatchJSON succeeds if actual is a string or stringer of JSON that matches
   273  // the expected JSON.  The JSONs are decoded and the resulting objects are compared via
   274  // reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.
   275  func MatchJSON(json interface{}) types.GomegaMatcher {
   276  	return &matchers.MatchJSONMatcher{
   277  		JSONToMatch: json,
   278  	}
   279  }
   280  
   281  // MatchXML succeeds if actual is a string or stringer of XML that matches
   282  // the expected XML.  The XMLs are decoded and the resulting objects are compared via
   283  // reflect.DeepEqual so things like whitespaces shouldn't matter.
   284  func MatchXML(xml interface{}) types.GomegaMatcher {
   285  	return &matchers.MatchXMLMatcher{
   286  		XMLToMatch: xml,
   287  	}
   288  }
   289  
   290  // MatchYAML succeeds if actual is a string or stringer of YAML that matches
   291  // the expected YAML.  The YAML's are decoded and the resulting objects are compared via
   292  // reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.
   293  func MatchYAML(yaml interface{}) types.GomegaMatcher {
   294  	return &matchers.MatchYAMLMatcher{
   295  		YAMLToMatch: yaml,
   296  	}
   297  }
   298  
   299  // BeEmpty succeeds if actual is empty.  Actual must be of type string, array, map, chan, or slice.
   300  func BeEmpty() types.GomegaMatcher {
   301  	return &matchers.BeEmptyMatcher{}
   302  }
   303  
   304  // HaveLen succeeds if actual has the passed-in length.  Actual must be of type string, array, map, chan, or slice.
   305  func HaveLen(count int) types.GomegaMatcher {
   306  	return &matchers.HaveLenMatcher{
   307  		Count: count,
   308  	}
   309  }
   310  
   311  // HaveCap succeeds if actual has the passed-in capacity.  Actual must be of type array, chan, or slice.
   312  func HaveCap(count int) types.GomegaMatcher {
   313  	return &matchers.HaveCapMatcher{
   314  		Count: count,
   315  	}
   316  }
   317  
   318  // BeZero succeeds if actual is the zero value for its type or if actual is nil.
   319  func BeZero() types.GomegaMatcher {
   320  	return &matchers.BeZeroMatcher{}
   321  }
   322  
   323  // ContainElement succeeds if actual contains the passed in element. By default
   324  // ContainElement() uses Equal() to perform the match, however a matcher can be
   325  // 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 searches
   330  // through the map's values.
   331  //
   332  // If you want to have a copy of the matching element(s) found you can pass a
   333  // pointer to a variable of the appropriate type. If the variable isn't a slice
   334  // or map, then exactly one match will be expected and returned. If the variable
   335  // is a slice or map, then at least one match is expected and all matches will be
   336  // stored in the variable.
   337  //
   338  //	var findings []string
   339  //	Expect([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubString("Bar", &findings)))
   340  func ContainElement(element interface{}, result ...interface{}) types.GomegaMatcher {
   341  	return &matchers.ContainElementMatcher{
   342  		Element: element,
   343  		Result:  result,
   344  	}
   345  }
   346  
   347  // 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() behaves
   350  // 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.
   360  func BeElementOf(elements ...interface{}) types.GomegaMatcher {
   361  	return &matchers.BeElementOfMatcher{
   362  		Elements: elements,
   363  	}
   364  }
   365  
   366  // 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}))
   370  func BeKeyOf(element interface{}) types.GomegaMatcher {
   371  	return &matchers.BeKeyOfMatcher{
   372  		Map: element,
   373  	}
   374  }
   375  
   376  // 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 it
   386  // 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 []interface{} are different types - hence the need for this special rule.
   391  func ConsistOf(elements ...interface{}) types.GomegaMatcher {
   392  	return &matchers.ConsistOfMatcher{
   393  		Elements: elements,
   394  	}
   395  }
   396  
   397  // HaveExactElements succeeds if actual contains elements that precisely match the elemets 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.
   405  func HaveExactElements(elements ...interface{}) types.GomegaMatcher {
   406  	return &matchers.HaveExactElementsMatcher{
   407  		Elements: elements,
   408  	}
   409  }
   410  
   411  // 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.
   419  func ContainElements(elements ...interface{}) types.GomegaMatcher {
   420  	return &matchers.ContainElementsMatcher{
   421  		Elements: elements,
   422  	}
   423  }
   424  
   425  // 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 a
   428  // 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.
   434  func HaveEach(element interface{}) types.GomegaMatcher {
   435  	return &matchers.HaveEachMatcher{
   436  		Element: element,
   437  	}
   438  }
   439  
   440  // HaveKey succeeds if actual is a map with the passed in key.
   441  // By default HaveKey uses Equal() to perform the match, however a
   442  // matcher can be passed in instead:
   443  //
   444  //	Expect(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKey(MatchRegexp(`.+Foo$`)))
   445  func HaveKey(key interface{}) types.GomegaMatcher {
   446  	return &matchers.HaveKeyMatcher{
   447  		Key: key,
   448  	}
   449  }
   450  
   451  // 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 a
   453  // 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"))
   457  func HaveKeyWithValue(key interface{}, value interface{}) types.GomegaMatcher {
   458  	return &matchers.HaveKeyWithValueMatcher{
   459  		Key:   key,
   460  		Value: value,
   461  	}
   462  }
   463  
   464  // HaveField succeeds if actual is a struct and the value at the passed in field
   465  // 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 traversed
   469  // 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 string
   474  //	    Author Person
   475  //	}
   476  //	type Person struct {
   477  //	    FirstName string
   478  //	    LastName string
   479  //	    DOB time.Time
   480  //	}
   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))
   485  func HaveField(field string, expected interface{}) types.GomegaMatcher {
   486  	return &matchers.HaveFieldMatcher{
   487  		Field:    field,
   488  		Expected: expected,
   489  	}
   490  }
   491  
   492  // HaveExistingField succeeds if actual is a struct and the specified field
   493  // exists.
   494  //
   495  // HaveExistingField can be combined with HaveField in order to cover use cases
   496  // with optional fields. HaveField alone would trigger an error in such situations.
   497  //
   498  //	Expect(MrHarmless).NotTo(And(HaveExistingField("Title"), HaveField("Title", "Supervillain")))
   499  func HaveExistingField(field string) types.GomegaMatcher {
   500  	return &matchers.HaveExistingFieldMatcher{
   501  		Field: field,
   502  	}
   503  }
   504  
   505  // HaveValue applies the given matcher to the value of actual, optionally and
   506  // repeatedly dereferencing pointers or taking the concrete value of interfaces.
   507  // Thus, the matcher will always be applied to non-pointer and non-interface
   508  // values only. HaveValue will fail with an error if a pointer or interface is
   509  // nil. It will also fail for more than 31 pointer or interface dereferences to
   510  // guard against mistakenly applying it to arbitrarily deep linked pointers.
   511  //
   512  // HaveValue differs from gstruct.PointTo in that it does not expect actual to
   513  // be a pointer (as gstruct.PointTo does) but instead also accepts non-pointer
   514  // and even interface values.
   515  //
   516  //	actual := 42
   517  //	Expect(actual).To(HaveValue(42))
   518  //	Expect(&actual).To(HaveValue(42))
   519  func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher {
   520  	return &matchers.HaveValueMatcher{
   521  		Matcher: matcher,
   522  	}
   523  }
   524  
   525  // BeNumerically performs numerical assertions in a type-agnostic way.
   526  // Actual and expected should be numbers, though the specific type of
   527  // 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))
   537  func BeNumerically(comparator string, compareTo ...interface{}) types.GomegaMatcher {
   538  	return &matchers.BeNumericallyMatcher{
   539  		Comparator: comparator,
   540  		CompareTo:  compareTo,
   541  	}
   542  }
   543  
   544  // BeTemporally compares time.Time's like BeNumerically
   545  // Actual and expected must be time.Time. The comparators are the same as for BeNumerically
   546  //
   547  //	Expect(time.Now()).Should(BeTemporally(">", time.Time{}))
   548  //	Expect(time.Now()).Should(BeTemporally("~", time.Now(), time.Second))
   549  func BeTemporally(comparator string, compareTo time.Time, threshold ...time.Duration) types.GomegaMatcher {
   550  	return &matchers.BeTemporallyMatcher{
   551  		Comparator: comparator,
   552  		CompareTo:  compareTo,
   553  		Threshold:  threshold,
   554  	}
   555  }
   556  
   557  // 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 values
   561  //	Expect(5).Should(BeAssignableToTypeOf(-1))        // different values same type
   562  //	Expect("foo").Should(BeAssignableToTypeOf("bar")) // different values same type
   563  //	Expect(struct{ Foo string }{}).Should(BeAssignableToTypeOf(struct{ Foo string }{}))
   564  func BeAssignableToTypeOf(expected interface{}) types.GomegaMatcher {
   565  	return &matchers.AssignableToTypeOfMatcher{
   566  		Expected: expected,
   567  	}
   568  }
   569  
   570  // 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.
   572  func Panic() types.GomegaMatcher {
   573  	return &matchers.PanicMatcher{}
   574  }
   575  
   576  // 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 a
   580  // matcher can be passed in instead:
   581  //
   582  //	Expect(fn).Should(PanicWith(MatchRegexp(`.+Foo$`)))
   583  func PanicWith(expected interface{}) types.GomegaMatcher {
   584  	return &matchers.PanicMatcher{Expected: expected}
   585  }
   586  
   587  // BeAnExistingFile succeeds if a file exists.
   588  // Actual must be a string representing the abs path to the file being checked.
   589  func BeAnExistingFile() types.GomegaMatcher {
   590  	return &matchers.BeAnExistingFileMatcher{}
   591  }
   592  
   593  // 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.
   595  func BeARegularFile() types.GomegaMatcher {
   596  	return &matchers.BeARegularFileMatcher{}
   597  }
   598  
   599  // 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.
   601  func BeADirectory() types.GomegaMatcher {
   602  	return &matchers.BeADirectoryMatcher{}
   603  }
   604  
   605  // 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 == 200
   610  //	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 == 204
   612  func HaveHTTPStatus(expected ...interface{}) types.GomegaMatcher {
   613  	return &matchers.HaveHTTPStatusMatcher{Expected: expected}
   614  }
   615  
   616  // 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 which
   619  // can be a string, or another matcher.
   620  func HaveHTTPHeaderWithValue(header string, value interface{}) types.GomegaMatcher {
   621  	return &matchers.HaveHTTPHeaderWithValueMatcher{
   622  		Header: header,
   623  		Value:  value,
   624  	}
   625  }
   626  
   627  // 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 matcher
   630  func HaveHTTPBody(expected interface{}) types.GomegaMatcher {
   631  	return &matchers.HaveHTTPBodyMatcher{Expected: expected}
   632  }
   633  
   634  // 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.
   640  func And(ms ...types.GomegaMatcher) types.GomegaMatcher {
   641  	return &matchers.AndMatcher{Matchers: ms}
   642  }
   643  
   644  // SatisfyAll is an alias for And().
   645  //
   646  //	Expect("hi").Should(SatisfyAll(HaveLen(2), Equal("hi")))
   647  func SatisfyAll(matchers ...types.GomegaMatcher) types.GomegaMatcher {
   648  	return And(matchers...)
   649  }
   650  
   651  // 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.
   657  func Or(ms ...types.GomegaMatcher) types.GomegaMatcher {
   658  	return &matchers.OrMatcher{Matchers: ms}
   659  }
   660  
   661  // SatisfyAny is an alias for Or().
   662  //
   663  //	Expect("hi").SatisfyAny(Or(HaveLen(3), HaveLen(2))
   664  func SatisfyAny(matchers ...types.GomegaMatcher) types.GomegaMatcher {
   665  	return Or(matchers...)
   666  }
   667  
   668  // 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.
   673  func Not(matcher types.GomegaMatcher) types.GomegaMatcher {
   674  	return &matchers.NotMatcher{Matcher: matcher}
   675  }
   676  
   677  // 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 a
   679  // function of one parameter that returns two values, where the second value must be of the
   680  // 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.
   689  func WithTransform(transform interface{}, matcher types.GomegaMatcher) types.GomegaMatcher {
   690  	return matchers.NewWithTransformMatcher(transform, matcher)
   691  }
   692  
   693  // Satisfy matches the actual value against the `predicate` function.
   694  // The given predicate must be a function of one paramter that returns bool.
   695  //
   696  //	var isEven = func(i int) bool { return i%2 == 0 }
   697  //	Expect(2).To(Satisfy(isEven))
   698  func Satisfy(predicate interface{}) types.GomegaMatcher {
   699  	return matchers.NewSatisfyMatcher(predicate)
   700  }