6 . "github.com/onsi/ginkgo"
7 . "github.com/onsi/gomega"
8 . "github.com/onsi/gomega/internal/assertion"
9 "github.com/onsi/gomega/internal/fakematcher"
12 var _ = Describe("Assertion", func() {
17 matcher *fakematcher.FakeMatcher
20 input := "The thing I'm testing"
22 var fakeFailHandler = func(message string, callerSkip ...int) {
23 failureMessage = message
24 if len(callerSkip) == 1 {
25 failureCallerSkip = callerSkip[0]
30 matcher = &fakematcher.FakeMatcher{}
33 a = New(input, fakeFailHandler, 1)
36 Context("when called", func() {
37 It("should pass the provided input value to the matcher", func() {
40 Ω(matcher.ReceivedActual).Should(Equal(input))
41 matcher.ReceivedActual = ""
45 Ω(matcher.ReceivedActual).Should(Equal(input))
46 matcher.ReceivedActual = ""
50 Ω(matcher.ReceivedActual).Should(Equal(input))
51 matcher.ReceivedActual = ""
55 Ω(matcher.ReceivedActual).Should(Equal(input))
56 matcher.ReceivedActual = ""
60 Ω(matcher.ReceivedActual).Should(Equal(input))
64 Context("when the matcher succeeds", func() {
66 matcher.MatchesToReturn = true
67 matcher.ErrToReturn = nil
70 Context("and a positive assertion is being made", func() {
71 It("should not call the failure callback", func() {
73 Ω(failureMessage).Should(Equal(""))
76 It("should be true", func() {
77 Ω(a.Should(matcher)).Should(BeTrue())
81 Context("and a negative assertion is being made", func() {
82 It("should call the failure callback", func() {
84 Ω(failureMessage).Should(Equal("negative: The thing I'm testing"))
85 Ω(failureCallerSkip).Should(Equal(3))
88 It("should be false", func() {
89 Ω(a.ShouldNot(matcher)).Should(BeFalse())
94 Context("when the matcher fails", func() {
96 matcher.MatchesToReturn = false
97 matcher.ErrToReturn = nil
100 Context("and a positive assertion is being made", func() {
101 It("should call the failure callback", func() {
103 Ω(failureMessage).Should(Equal("positive: The thing I'm testing"))
104 Ω(failureCallerSkip).Should(Equal(3))
107 It("should be false", func() {
108 Ω(a.Should(matcher)).Should(BeFalse())
112 Context("and a negative assertion is being made", func() {
113 It("should not call the failure callback", func() {
115 Ω(failureMessage).Should(Equal(""))
118 It("should be true", func() {
119 Ω(a.ShouldNot(matcher)).Should(BeTrue())
124 Context("When reporting a failure", func() {
126 matcher.MatchesToReturn = false
127 matcher.ErrToReturn = nil
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))
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))
147 Context("When the matcher returns an error", func() {
149 matcher.ErrToReturn = errors.New("Kaboom!")
152 Context("and a positive assertion is being made", func() {
153 It("should call the failure callback", func() {
154 matcher.MatchesToReturn = true
156 Ω(failureMessage).Should(Equal("Kaboom!"))
157 Ω(failureCallerSkip).Should(Equal(3))
161 Context("and a negative assertion is being made", func() {
162 It("should call the failure callback", func() {
163 matcher.MatchesToReturn = false
165 Ω(failureMessage).Should(Equal("Kaboom!"))
166 Ω(failureCallerSkip).Should(Equal(3))
170 It("should always be false", func() {
171 Ω(a.Should(matcher)).Should(BeFalse())
172 Ω(a.ShouldNot(matcher)).Should(BeFalse())
176 Context("when there are extra parameters", func() {
177 It("(a simple example)", func() {
178 Ω(func() (string, int, error) {
180 }()).Should(Equal("foo"))
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
188 var typedNil []string
189 a = New(input, fakeFailHandler, 1, 0, nil, typedNil)
191 result := a.Should(matcher)
192 Ω(result).Should(BeTrue())
193 Ω(matcher.ReceivedActual).Should(Equal(input))
195 Ω(failureMessage).Should(BeZero())
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
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"))
211 a = New(input, fakeFailHandler, 1, nil, 1)
212 result = a.ShouldNot(matcher)
213 Ω(result).Should(BeFalse())
214 Ω(failureMessage).Should(ContainSubstring("1"))
217 a = New(input, fakeFailHandler, 1, nil, 0, []string{"foo"})
218 result = a.To(matcher)
219 Ω(result).Should(BeFalse())
220 Ω(failureMessage).Should(ContainSubstring("foo"))
223 a = New(input, fakeFailHandler, 1, nil, 0, []string{"foo"})
224 result = a.ToNot(matcher)
225 Ω(result).Should(BeFalse())
226 Ω(failureMessage).Should(ContainSubstring("foo"))
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))
238 Context("Making an assertion without a registered fail handler", func() {
239 It("should panic", func() {
242 RegisterFailHandler(Fail)
244 Fail("expected a panic to have occurred")
248 RegisterFailHandler(nil)
249 Ω(true).Should(BeTrue())