initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers.go
1 package gomega
2
3 import (
4         "time"
5
6         "github.com/onsi/gomega/matchers"
7         "github.com/onsi/gomega/types"
8 )
9
10 //Equal uses reflect.DeepEqual to compare actual with expected.  Equal is strict about
11 //types when performing comparisons.
12 //It is an error for both actual and expected to be nil.  Use BeNil() instead.
13 func Equal(expected interface{}) types.GomegaMatcher {
14         return &matchers.EqualMatcher{
15                 Expected: expected,
16         }
17 }
18
19 //BeEquivalentTo is more lax than Equal, allowing equality between different types.
20 //This is done by converting actual to have the type of expected before
21 //attempting equality with reflect.DeepEqual.
22 //It is an error for actual and expected to be nil.  Use BeNil() instead.
23 func BeEquivalentTo(expected interface{}) types.GomegaMatcher {
24         return &matchers.BeEquivalentToMatcher{
25                 Expected: expected,
26         }
27 }
28
29 //BeIdenticalTo uses the == operator to compare actual with expected.
30 //BeIdenticalTo is strict about types when performing comparisons.
31 //It is an error for both actual and expected to be nil.  Use BeNil() instead.
32 func BeIdenticalTo(expected interface{}) types.GomegaMatcher {
33         return &matchers.BeIdenticalToMatcher{
34                 Expected: expected,
35         }
36 }
37
38 //BeNil succeeds if actual is nil
39 func BeNil() types.GomegaMatcher {
40         return &matchers.BeNilMatcher{}
41 }
42
43 //BeTrue succeeds if actual is true
44 func BeTrue() types.GomegaMatcher {
45         return &matchers.BeTrueMatcher{}
46 }
47
48 //BeFalse succeeds if actual is false
49 func BeFalse() types.GomegaMatcher {
50         return &matchers.BeFalseMatcher{}
51 }
52
53 //HaveOccurred succeeds if actual is a non-nil error
54 //The typical Go error checking pattern looks like:
55 //    err := SomethingThatMightFail()
56 //    Ω(err).ShouldNot(HaveOccurred())
57 func HaveOccurred() types.GomegaMatcher {
58         return &matchers.HaveOccurredMatcher{}
59 }
60
61 //Succeed passes if actual is a nil error
62 //Succeed is intended to be used with functions that return a single error value. Instead of
63 //    err := SomethingThatMightFail()
64 //    Ω(err).ShouldNot(HaveOccurred())
65 //
66 //You can write:
67 //    Ω(SomethingThatMightFail()).Should(Succeed())
68 //
69 //It is a mistake to use Succeed with a function that has multiple return values.  Gomega's Ω and Expect
70 //functions automatically trigger failure if any return values after the first return value are non-zero/non-nil.
71 //This means that Ω(MultiReturnFunc()).ShouldNot(Succeed()) can never pass.
72 func Succeed() types.GomegaMatcher {
73         return &matchers.SucceedMatcher{}
74 }
75
76 //MatchError succeeds if actual is a non-nil error that matches the passed in string/error.
77 //
78 //These are valid use-cases:
79 //  Ω(err).Should(MatchError("an error")) //asserts that err.Error() == "an error"
80 //  Ω(err).Should(MatchError(SomeError)) //asserts that err == SomeError (via reflect.DeepEqual)
81 //
82 //It is an error for err to be nil or an object that does not implement the Error interface
83 func MatchError(expected interface{}) types.GomegaMatcher {
84         return &matchers.MatchErrorMatcher{
85                 Expected: expected,
86         }
87 }
88
89 //BeClosed succeeds if actual is a closed channel.
90 //It is an error to pass a non-channel to BeClosed, it is also an error to pass nil
91 //
92 //In order to check whether or not the channel is closed, Gomega must try to read from the channel
93 //(even in the `ShouldNot(BeClosed())` case).  You should keep this in mind if you wish to make subsequent assertions about
94 //values coming down the channel.
95 //
96 //Also, if you are testing that a *buffered* channel is closed you must first read all values out of the channel before
97 //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).
98 //
99 //Finally, as a corollary: it is an error to check whether or not a send-only channel is closed.
100 func BeClosed() types.GomegaMatcher {
101         return &matchers.BeClosedMatcher{}
102 }
103
104 //Receive succeeds if there is a value to be received on actual.
105 //Actual must be a channel (and cannot be a send-only channel) -- anything else is an error.
106 //
107 //Receive returns immediately and never blocks:
108 //
109 //- If there is nothing on the channel `c` then Ω(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.
110 //
111 //- If the channel `c` is closed then Ω(c).Should(Receive()) will fail and Ω(c).ShouldNot(Receive()) will pass.
112 //
113 //- If there is something on the channel `c` ready to be read, then Ω(c).Should(Receive()) will pass and Ω(c).ShouldNot(Receive()) will fail.
114 //
115 //If you have a go-routine running in the background that will write to channel `c` you can:
116 //    Eventually(c).Should(Receive())
117 //
118 //This will timeout if nothing gets sent to `c` (you can modify the timeout interval as you normally do with `Eventually`)
119 //
120 //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`:
121 //    Consistently(c).ShouldNot(Receive())
122 //
123 //You can pass `Receive` a matcher.  If you do so, it will match the received object against the matcher.  For example:
124 //    Ω(c).Should(Receive(Equal("foo")))
125 //
126 //When given a matcher, `Receive` will always fail if there is nothing to be received on the channel.
127 //
128 //Passing Receive a matcher is especially useful when paired with Eventually:
129 //
130 //    Eventually(c).Should(Receive(ContainSubstring("bar")))
131 //
132 //will repeatedly attempt to pull values out of `c` until a value matching "bar" is received.
133 //
134 //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:
135 //    var myThing thing
136 //    Eventually(thingChan).Should(Receive(&myThing))
137 //    Ω(myThing.Sprocket).Should(Equal("foo"))
138 //    Ω(myThing.IsValid()).Should(BeTrue())
139 func Receive(args ...interface{}) types.GomegaMatcher {
140         var arg interface{}
141         if len(args) > 0 {
142                 arg = args[0]
143         }
144
145         return &matchers.ReceiveMatcher{
146                 Arg: arg,
147         }
148 }
149
150 //BeSent succeeds if a value can be sent to actual.
151 //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.
152 //In addition, actual must not be closed.
153 //
154 //BeSent never blocks:
155 //
156 //- If the channel `c` is not ready to receive then Ω(c).Should(BeSent("foo")) will fail immediately
157 //- 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
158 //- If the channel `c` is closed then Ω(c).Should(BeSent("foo")) and Ω(c).ShouldNot(BeSent("foo")) will both fail immediately
159 //
160 //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).
161 //Rather, the point of `BeSent` is to make it possible to easily and expressively write tests that can timeout on blocked channel sends.
162 func BeSent(arg interface{}) types.GomegaMatcher {
163         return &matchers.BeSentMatcher{
164                 Arg: arg,
165         }
166 }
167
168 //MatchRegexp succeeds if actual is a string or stringer that matches the
169 //passed-in regexp.  Optional arguments can be provided to construct a regexp
170 //via fmt.Sprintf().
171 func MatchRegexp(regexp string, args ...interface{}) types.GomegaMatcher {
172         return &matchers.MatchRegexpMatcher{
173                 Regexp: regexp,
174                 Args:   args,
175         }
176 }
177
178 //ContainSubstring succeeds if actual is a string or stringer that contains the
179 //passed-in substring.  Optional arguments can be provided to construct the substring
180 //via fmt.Sprintf().
181 func ContainSubstring(substr string, args ...interface{}) types.GomegaMatcher {
182         return &matchers.ContainSubstringMatcher{
183                 Substr: substr,
184                 Args:   args,
185         }
186 }
187
188 //HavePrefix succeeds if actual is a string or stringer that contains the
189 //passed-in string as a prefix.  Optional arguments can be provided to construct
190 //via fmt.Sprintf().
191 func HavePrefix(prefix string, args ...interface{}) types.GomegaMatcher {
192         return &matchers.HavePrefixMatcher{
193                 Prefix: prefix,
194                 Args:   args,
195         }
196 }
197
198 //HaveSuffix succeeds if actual is a string or stringer that contains the
199 //passed-in string as a suffix.  Optional arguments can be provided to construct
200 //via fmt.Sprintf().
201 func HaveSuffix(suffix string, args ...interface{}) types.GomegaMatcher {
202         return &matchers.HaveSuffixMatcher{
203                 Suffix: suffix,
204                 Args:   args,
205         }
206 }
207
208 //MatchJSON succeeds if actual is a string or stringer of JSON that matches
209 //the expected JSON.  The JSONs are decoded and the resulting objects are compared via
210 //reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.
211 func MatchJSON(json interface{}) types.GomegaMatcher {
212         return &matchers.MatchJSONMatcher{
213                 JSONToMatch: json,
214         }
215 }
216
217 //MatchYAML succeeds if actual is a string or stringer of YAML that matches
218 //the expected YAML.  The YAML's are decoded and the resulting objects are compared via
219 //reflect.DeepEqual so things like key-ordering and whitespace shouldn't matter.
220 func MatchYAML(yaml interface{}) types.GomegaMatcher {
221         return &matchers.MatchYAMLMatcher{
222                 YAMLToMatch: yaml,
223         }
224 }
225
226 //BeEmpty succeeds if actual is empty.  Actual must be of type string, array, map, chan, or slice.
227 func BeEmpty() types.GomegaMatcher {
228         return &matchers.BeEmptyMatcher{}
229 }
230
231 //HaveLen succeeds if actual has the passed-in length.  Actual must be of type string, array, map, chan, or slice.
232 func HaveLen(count int) types.GomegaMatcher {
233         return &matchers.HaveLenMatcher{
234                 Count: count,
235         }
236 }
237
238 //HaveCap succeeds if actual has the passed-in capacity.  Actual must be of type array, chan, or slice.
239 func HaveCap(count int) types.GomegaMatcher {
240         return &matchers.HaveCapMatcher{
241                 Count: count,
242         }
243 }
244
245 //BeZero succeeds if actual is the zero value for its type or if actual is nil.
246 func BeZero() types.GomegaMatcher {
247         return &matchers.BeZeroMatcher{}
248 }
249
250 //ContainElement succeeds if actual contains the passed in element.
251 //By default ContainElement() uses Equal() to perform the match, however a
252 //matcher can be passed in instead:
253 //    Ω([]string{"Foo", "FooBar"}).Should(ContainElement(ContainSubstring("Bar")))
254 //
255 //Actual must be an array, slice or map.
256 //For maps, ContainElement searches through the map's values.
257 func ContainElement(element interface{}) types.GomegaMatcher {
258         return &matchers.ContainElementMatcher{
259                 Element: element,
260         }
261 }
262
263 //ConsistOf succeeds if actual contains preciely the elements passed into the matcher.  The ordering of the elements does not matter.
264 //By default ConsistOf() uses Equal() to match the elements, however custom matchers can be passed in instead.  Here are some examples:
265 //
266 //    Ω([]string{"Foo", "FooBar"}).Should(ConsistOf("FooBar", "Foo"))
267 //    Ω([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Bar"), "Foo"))
268 //    Ω([]string{"Foo", "FooBar"}).Should(ConsistOf(ContainSubstring("Foo"), ContainSubstring("Foo")))
269 //
270 //Actual must be an array, slice or map.  For maps, ConsistOf matches against the map's values.
271 //
272 //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
273 //is the only element passed in to ConsistOf:
274 //
275 //    Ω([]string{"Foo", "FooBar"}).Should(ConsistOf([]string{"FooBar", "Foo"}))
276 //
277 //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.
278 func ConsistOf(elements ...interface{}) types.GomegaMatcher {
279         return &matchers.ConsistOfMatcher{
280                 Elements: elements,
281         }
282 }
283
284 //HaveKey succeeds if actual is a map with the passed in key.
285 //By default HaveKey uses Equal() to perform the match, however a
286 //matcher can be passed in instead:
287 //    Ω(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKey(MatchRegexp(`.+Foo$`)))
288 func HaveKey(key interface{}) types.GomegaMatcher {
289         return &matchers.HaveKeyMatcher{
290                 Key: key,
291         }
292 }
293
294 //HaveKeyWithValue succeeds if actual is a map with the passed in key and value.
295 //By default HaveKeyWithValue uses Equal() to perform the match, however a
296 //matcher can be passed in instead:
297 //    Ω(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue("Foo", "Bar"))
298 //    Ω(map[string]string{"Foo": "Bar", "BazFoo": "Duck"}).Should(HaveKeyWithValue(MatchRegexp(`.+Foo$`), "Bar"))
299 func HaveKeyWithValue(key interface{}, value interface{}) types.GomegaMatcher {
300         return &matchers.HaveKeyWithValueMatcher{
301                 Key:   key,
302                 Value: value,
303         }
304 }
305
306 //BeNumerically performs numerical assertions in a type-agnostic way.
307 //Actual and expected should be numbers, though the specific type of
308 //number is irrelevant (floa32, float64, uint8, etc...).
309 //
310 //There are six, self-explanatory, supported comparators:
311 //    Ω(1.0).Should(BeNumerically("==", 1))
312 //    Ω(1.0).Should(BeNumerically("~", 0.999, 0.01))
313 //    Ω(1.0).Should(BeNumerically(">", 0.9))
314 //    Ω(1.0).Should(BeNumerically(">=", 1.0))
315 //    Ω(1.0).Should(BeNumerically("<", 3))
316 //    Ω(1.0).Should(BeNumerically("<=", 1.0))
317 func BeNumerically(comparator string, compareTo ...interface{}) types.GomegaMatcher {
318         return &matchers.BeNumericallyMatcher{
319                 Comparator: comparator,
320                 CompareTo:  compareTo,
321         }
322 }
323
324 //BeTemporally compares time.Time's like BeNumerically
325 //Actual and expected must be time.Time. The comparators are the same as for BeNumerically
326 //    Ω(time.Now()).Should(BeTemporally(">", time.Time{}))
327 //    Ω(time.Now()).Should(BeTemporally("~", time.Now(), time.Second))
328 func BeTemporally(comparator string, compareTo time.Time, threshold ...time.Duration) types.GomegaMatcher {
329         return &matchers.BeTemporallyMatcher{
330                 Comparator: comparator,
331                 CompareTo:  compareTo,
332                 Threshold:  threshold,
333         }
334 }
335
336 //BeAssignableToTypeOf succeeds if actual is assignable to the type of expected.
337 //It will return an error when one of the values is nil.
338 //        Ω(0).Should(BeAssignableToTypeOf(0))         // Same values
339 //        Ω(5).Should(BeAssignableToTypeOf(-1))        // different values same type
340 //        Ω("foo").Should(BeAssignableToTypeOf("bar")) // different values same type
341 //    Ω(struct{ Foo string }{}).Should(BeAssignableToTypeOf(struct{ Foo string }{}))
342 func BeAssignableToTypeOf(expected interface{}) types.GomegaMatcher {
343         return &matchers.AssignableToTypeOfMatcher{
344                 Expected: expected,
345         }
346 }
347
348 //Panic succeeds if actual is a function that, when invoked, panics.
349 //Actual must be a function that takes no arguments and returns no results.
350 func Panic() types.GomegaMatcher {
351         return &matchers.PanicMatcher{}
352 }
353
354 //BeAnExistingFile succeeds if a file exists.
355 //Actual must be a string representing the abs path to the file being checked.
356 func BeAnExistingFile() types.GomegaMatcher {
357         return &matchers.BeAnExistingFileMatcher{}
358 }
359
360 //BeARegularFile succeeds iff a file exists and is a regular file.
361 //Actual must be a string representing the abs path to the file being checked.
362 func BeARegularFile() types.GomegaMatcher {
363         return &matchers.BeARegularFileMatcher{}
364 }
365
366 //BeADirectory succeeds iff a file exists and is a directory.
367 //Actual must be a string representing the abs path to the file being checked.
368 func BeADirectory() types.GomegaMatcher {
369         return &matchers.BeADirectoryMatcher{}
370 }
371
372 //And succeeds only if all of the given matchers succeed.
373 //The matchers are tried in order, and will fail-fast if one doesn't succeed.
374 //  Expect("hi").To(And(HaveLen(2), Equal("hi"))
375 //
376 //And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
377 func And(ms ...types.GomegaMatcher) types.GomegaMatcher {
378         return &matchers.AndMatcher{Matchers: ms}
379 }
380
381 //SatisfyAll is an alias for And().
382 //  Ω("hi").Should(SatisfyAll(HaveLen(2), Equal("hi")))
383 func SatisfyAll(matchers ...types.GomegaMatcher) types.GomegaMatcher {
384         return And(matchers...)
385 }
386
387 //Or succeeds if any of the given matchers succeed.
388 //The matchers are tried in order and will return immediately upon the first successful match.
389 //  Expect("hi").To(Or(HaveLen(3), HaveLen(2))
390 //
391 //And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
392 func Or(ms ...types.GomegaMatcher) types.GomegaMatcher {
393         return &matchers.OrMatcher{Matchers: ms}
394 }
395
396 //SatisfyAny is an alias for Or().
397 //  Expect("hi").SatisfyAny(Or(HaveLen(3), HaveLen(2))
398 func SatisfyAny(matchers ...types.GomegaMatcher) types.GomegaMatcher {
399         return Or(matchers...)
400 }
401
402 //Not negates the given matcher; it succeeds if the given matcher fails.
403 //  Expect(1).To(Not(Equal(2))
404 //
405 //And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
406 func Not(matcher types.GomegaMatcher) types.GomegaMatcher {
407         return &matchers.NotMatcher{Matcher: matcher}
408 }
409
410 //WithTransform applies the `transform` to the actual value and matches it against `matcher`.
411 //The given transform must be a function of one parameter that returns one value.
412 //  var plus1 = func(i int) int { return i + 1 }
413 //  Expect(1).To(WithTransform(plus1, Equal(2))
414 //
415 //And(), Or(), Not() and WithTransform() allow matchers to be composed into complex expressions.
416 func WithTransform(transform interface{}, matcher types.GomegaMatcher) types.GomegaMatcher {
417         return matchers.NewWithTransformMatcher(transform, matcher)
418 }