initial commit
[govpp.git] / vendor / github.com / onsi / gomega / internal / assertion / assertion_test.go
1 package assertion_test
2
3 import (
4         "errors"
5
6         . "github.com/onsi/ginkgo"
7         . "github.com/onsi/gomega"
8         . "github.com/onsi/gomega/internal/assertion"
9         "github.com/onsi/gomega/internal/fakematcher"
10 )
11
12 var _ = Describe("Assertion", func() {
13         var (
14                 a                 *Assertion
15                 failureMessage    string
16                 failureCallerSkip int
17                 matcher           *fakematcher.FakeMatcher
18         )
19
20         input := "The thing I'm testing"
21
22         var fakeFailHandler = func(message string, callerSkip ...int) {
23                 failureMessage = message
24                 if len(callerSkip) == 1 {
25                         failureCallerSkip = callerSkip[0]
26                 }
27         }
28
29         BeforeEach(func() {
30                 matcher = &fakematcher.FakeMatcher{}
31                 failureMessage = ""
32                 failureCallerSkip = 0
33                 a = New(input, fakeFailHandler, 1)
34         })
35
36         Context("when called", func() {
37                 It("should pass the provided input value to the matcher", func() {
38                         a.Should(matcher)
39
40                         Ω(matcher.ReceivedActual).Should(Equal(input))
41                         matcher.ReceivedActual = ""
42
43                         a.ShouldNot(matcher)
44
45                         Ω(matcher.ReceivedActual).Should(Equal(input))
46                         matcher.ReceivedActual = ""
47
48                         a.To(matcher)
49
50                         Ω(matcher.ReceivedActual).Should(Equal(input))
51                         matcher.ReceivedActual = ""
52
53                         a.ToNot(matcher)
54
55                         Ω(matcher.ReceivedActual).Should(Equal(input))
56                         matcher.ReceivedActual = ""
57
58                         a.NotTo(matcher)
59
60                         Ω(matcher.ReceivedActual).Should(Equal(input))
61                 })
62         })
63
64         Context("when the matcher succeeds", func() {
65                 BeforeEach(func() {
66                         matcher.MatchesToReturn = true
67                         matcher.ErrToReturn = nil
68                 })
69
70                 Context("and a positive assertion is being made", func() {
71                         It("should not call the failure callback", func() {
72                                 a.Should(matcher)
73                                 Ω(failureMessage).Should(Equal(""))
74                         })
75
76                         It("should be true", func() {
77                                 Ω(a.Should(matcher)).Should(BeTrue())
78                         })
79                 })
80
81                 Context("and a negative assertion is being made", func() {
82                         It("should call the failure callback", func() {
83                                 a.ShouldNot(matcher)
84                                 Ω(failureMessage).Should(Equal("negative: The thing I'm testing"))
85                                 Ω(failureCallerSkip).Should(Equal(3))
86                         })
87
88                         It("should be false", func() {
89                                 Ω(a.ShouldNot(matcher)).Should(BeFalse())
90                         })
91                 })
92         })
93
94         Context("when the matcher fails", func() {
95                 BeforeEach(func() {
96                         matcher.MatchesToReturn = false
97                         matcher.ErrToReturn = nil
98                 })
99
100                 Context("and a positive assertion is being made", func() {
101                         It("should call the failure callback", func() {
102                                 a.Should(matcher)
103                                 Ω(failureMessage).Should(Equal("positive: The thing I'm testing"))
104                                 Ω(failureCallerSkip).Should(Equal(3))
105                         })
106
107                         It("should be false", func() {
108                                 Ω(a.Should(matcher)).Should(BeFalse())
109                         })
110                 })
111
112                 Context("and a negative assertion is being made", func() {
113                         It("should not call the failure callback", func() {
114                                 a.ShouldNot(matcher)
115                                 Ω(failureMessage).Should(Equal(""))
116                         })
117
118                         It("should be true", func() {
119                                 Ω(a.ShouldNot(matcher)).Should(BeTrue())
120                         })
121                 })
122         })
123
124         Context("When reporting a failure", func() {
125                 BeforeEach(func() {
126                         matcher.MatchesToReturn = false
127                         matcher.ErrToReturn = nil
128                 })
129
130                 Context("and there is an optional description", func() {
131                         It("should append the description to the failure message", func() {
132                                 a.Should(matcher, "A description")
133                                 Ω(failureMessage).Should(Equal("A description\npositive: The thing I'm testing"))
134                                 Ω(failureCallerSkip).Should(Equal(3))
135                         })
136                 })
137
138                 Context("and there are multiple arguments to the optional description", func() {
139                         It("should append the formatted description to the failure message", func() {
140                                 a.Should(matcher, "A description of [%d]", 3)
141                                 Ω(failureMessage).Should(Equal("A description of [3]\npositive: The thing I'm testing"))
142                                 Ω(failureCallerSkip).Should(Equal(3))
143                         })
144                 })
145         })
146
147         Context("When the matcher returns an error", func() {
148                 BeforeEach(func() {
149                         matcher.ErrToReturn = errors.New("Kaboom!")
150                 })
151
152                 Context("and a positive assertion is being made", func() {
153                         It("should call the failure callback", func() {
154                                 matcher.MatchesToReturn = true
155                                 a.Should(matcher)
156                                 Ω(failureMessage).Should(Equal("Kaboom!"))
157                                 Ω(failureCallerSkip).Should(Equal(3))
158                         })
159                 })
160
161                 Context("and a negative assertion is being made", func() {
162                         It("should call the failure callback", func() {
163                                 matcher.MatchesToReturn = false
164                                 a.ShouldNot(matcher)
165                                 Ω(failureMessage).Should(Equal("Kaboom!"))
166                                 Ω(failureCallerSkip).Should(Equal(3))
167                         })
168                 })
169
170                 It("should always be false", func() {
171                         Ω(a.Should(matcher)).Should(BeFalse())
172                         Ω(a.ShouldNot(matcher)).Should(BeFalse())
173                 })
174         })
175
176         Context("when there are extra parameters", func() {
177                 It("(a simple example)", func() {
178                         Ω(func() (string, int, error) {
179                                 return "foo", 0, nil
180                         }()).Should(Equal("foo"))
181                 })
182
183                 Context("when the parameters are all nil or zero", func() {
184                         It("should invoke the matcher", func() {
185                                 matcher.MatchesToReturn = true
186                                 matcher.ErrToReturn = nil
187
188                                 var typedNil []string
189                                 a = New(input, fakeFailHandler, 1, 0, nil, typedNil)
190
191                                 result := a.Should(matcher)
192                                 Ω(result).Should(BeTrue())
193                                 Ω(matcher.ReceivedActual).Should(Equal(input))
194
195                                 Ω(failureMessage).Should(BeZero())
196                         })
197                 })
198
199                 Context("when any of the parameters are not nil or zero", func() {
200                         It("should call the failure callback", func() {
201                                 matcher.MatchesToReturn = false
202                                 matcher.ErrToReturn = nil
203
204                                 a = New(input, fakeFailHandler, 1, errors.New("foo"))
205                                 result := a.Should(matcher)
206                                 Ω(result).Should(BeFalse())
207                                 Ω(matcher.ReceivedActual).Should(BeZero(), "The matcher doesn't even get called")
208                                 Ω(failureMessage).Should(ContainSubstring("foo"))
209                                 failureMessage = ""
210
211                                 a = New(input, fakeFailHandler, 1, nil, 1)
212                                 result = a.ShouldNot(matcher)
213                                 Ω(result).Should(BeFalse())
214                                 Ω(failureMessage).Should(ContainSubstring("1"))
215                                 failureMessage = ""
216
217                                 a = New(input, fakeFailHandler, 1, nil, 0, []string{"foo"})
218                                 result = a.To(matcher)
219                                 Ω(result).Should(BeFalse())
220                                 Ω(failureMessage).Should(ContainSubstring("foo"))
221                                 failureMessage = ""
222
223                                 a = New(input, fakeFailHandler, 1, nil, 0, []string{"foo"})
224                                 result = a.ToNot(matcher)
225                                 Ω(result).Should(BeFalse())
226                                 Ω(failureMessage).Should(ContainSubstring("foo"))
227                                 failureMessage = ""
228
229                                 a = New(input, fakeFailHandler, 1, nil, 0, []string{"foo"})
230                                 result = a.NotTo(matcher)
231                                 Ω(result).Should(BeFalse())
232                                 Ω(failureMessage).Should(ContainSubstring("foo"))
233                                 Ω(failureCallerSkip).Should(Equal(3))
234                         })
235                 })
236         })
237
238         Context("Making an assertion without a registered fail handler", func() {
239                 It("should panic", func() {
240                         defer func() {
241                                 e := recover()
242                                 RegisterFailHandler(Fail)
243                                 if e == nil {
244                                         Fail("expected a panic to have occurred")
245                                 }
246                         }()
247
248                         RegisterFailHandler(nil)
249                         Ω(true).Should(BeTrue())
250                 })
251         })
252 })