-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
assertion_test.go
226 lines (209 loc) · 8.21 KB
/
assertion_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package internal_test
import (
"errors"
"reflect"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Making Synchronous Assertions", func() {
var SHOULD_MATCH = true
var SHOULD_NOT_MATCH = false
var IT_PASSES = true
var IT_FAILS = false
Extras := func(extras ...interface{}) []interface{} {
return extras
}
OptionalDescription := func(optionalDescription ...interface{}) []interface{} {
return optionalDescription
}
DescribeTable(
"the various cases",
func(actual interface{}, extras []interface{}, optionalDescription []interface{}, isPositiveAssertion bool, expectedFailureMessage string, expectedReturnValue bool) {
if isPositiveAssertion {
ig := NewInstrumentedGomega()
returnValue := ig.G.Expect(actual, extras...).To(SpecMatch(), optionalDescription...)
Expect(returnValue).To(Equal(expectedReturnValue))
Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage))
if expectedFailureMessage != "" {
Expect(ig.FailureSkip).To(Equal([]int{2}))
}
Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).To"))
ig = NewInstrumentedGomega()
returnValue = ig.G.ExpectWithOffset(3, actual, extras...).To(SpecMatch(), optionalDescription...)
Expect(returnValue).To(Equal(expectedReturnValue))
Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage))
if expectedFailureMessage != "" {
Expect(ig.FailureSkip).To(Equal([]int{5}))
}
Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).To"))
ig = NewInstrumentedGomega()
returnValue = ig.G.Ω(actual, extras...).Should(SpecMatch(), optionalDescription...)
Expect(returnValue).To(Equal(expectedReturnValue))
Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage))
if expectedFailureMessage != "" {
Expect(ig.FailureSkip).To(Equal([]int{2}))
}
Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).Should"))
} else {
ig := NewInstrumentedGomega()
returnValue := ig.G.Expect(actual, extras...).ToNot(SpecMatch(), optionalDescription...)
Expect(returnValue).To(Equal(expectedReturnValue))
Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage))
if expectedFailureMessage != "" {
Expect(ig.FailureSkip).To(Equal([]int{2}))
}
Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).ToNot"))
ig = NewInstrumentedGomega()
returnValue = ig.G.Expect(actual, extras...).NotTo(SpecMatch(), optionalDescription...)
Expect(returnValue).To(Equal(expectedReturnValue))
Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage))
if expectedFailureMessage != "" {
Expect(ig.FailureSkip).To(Equal([]int{2}))
}
Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).NotTo"))
ig = NewInstrumentedGomega()
returnValue = ig.G.ExpectWithOffset(3, actual, extras...).NotTo(SpecMatch(), optionalDescription...)
Expect(returnValue).To(Equal(expectedReturnValue))
Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage))
if expectedFailureMessage != "" {
Expect(ig.FailureSkip).To(Equal([]int{5}))
}
Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).NotTo"))
ig = NewInstrumentedGomega()
returnValue = ig.G.Ω(actual, extras...).ShouldNot(SpecMatch(), optionalDescription...)
Expect(returnValue).To(Equal(expectedReturnValue))
Expect(ig.FailureMessage).To(ContainSubstring(expectedFailureMessage))
if expectedFailureMessage != "" {
Expect(ig.FailureSkip).To(Equal([]int{2}))
}
Expect(ig.RegisteredHelpers).To(ContainElement("(*Assertion).ShouldNot"))
}
},
Entry(
"when the matcher matches and a positive assertion is being made",
MATCH, Extras(), OptionalDescription(),
SHOULD_MATCH, "", IT_PASSES,
),
Entry(
"when the matcher matches and a negative assertion is being made",
MATCH, Extras(), OptionalDescription(),
SHOULD_NOT_MATCH, "negative: match", IT_FAILS,
),
Entry(
"when the matcher does not match and a positive assertion is being made",
NO_MATCH, Extras(), OptionalDescription(),
SHOULD_MATCH, "positive: no match", IT_FAILS,
),
Entry(
"when the matcher does not match and a negative assertion is being made",
NO_MATCH, Extras(), OptionalDescription(),
SHOULD_NOT_MATCH, "", IT_PASSES,
),
Entry(
"when the matcher returns an error and a positive assertion is being made",
ERR_MATCH, Extras(), OptionalDescription(),
SHOULD_MATCH, "spec matcher error", IT_FAILS,
),
Entry(
"when the matcher returns an error and a negative assertion is being made",
ERR_MATCH, Extras(), OptionalDescription(),
SHOULD_NOT_MATCH, "spec matcher error", IT_FAILS,
),
Entry(
"when a failure occurs and there is a single optional description",
NO_MATCH, Extras(), OptionalDescription("a description"),
SHOULD_MATCH, "a description\npositive: no match", IT_FAILS,
),
Entry(
"when a failure occurs and there are multiple optional descriptions",
NO_MATCH, Extras(), OptionalDescription("a description of [%d]", 3),
SHOULD_MATCH, "a description of [3]\npositive: no match", IT_FAILS,
),
Entry(
"when a failure occurs and the optional description is a function",
NO_MATCH, Extras(), OptionalDescription(func() string { return "a description" }),
SHOULD_MATCH, "a description\npositive: no match", IT_FAILS,
),
Entry(
"when the matcher matches and zero-valued extra parameters are included, it passes",
MATCH, Extras(0, "", struct{ Foo string }{}, nil), OptionalDescription(),
SHOULD_MATCH, "", IT_PASSES,
),
Entry(
"when the matcher matches but a non-zero-valued extra parameter is included, it fails",
MATCH, Extras(1, "bam", struct{ Foo string }{Foo: "foo"}, nil), OptionalDescription(),
SHOULD_MATCH, "Unexpected non-nil/non-zero argument at index 1:\n\t<int>: 1", IT_FAILS,
),
Entry(
"when the matcher matches but an error is included, it fails",
MATCH, Extras(0, "", errors.New("welp!")), OptionalDescription(),
SHOULD_MATCH, "Unexpected error: welp!", IT_FAILS,
),
)
var SHOULD_OCCUR = true
var SHOULD_NOT_OCCUR = false
DescribeTable("error expectations",
func(a, b int, e error, isPositiveAssertion bool, expectedFailureMessage string, expectedReturnValue bool) {
abe := func(a, b int, e error) (int, int, error) {
return a, b, e
}
ig := NewInstrumentedGomega()
var returnValue bool
if isPositiveAssertion {
returnValue = ig.G.Expect(abe(a, b, e)).Error().To(HaveOccurred())
} else {
returnValue = ig.G.Expect(abe(a, b, e)).Error().NotTo(HaveOccurred())
}
Expect(returnValue).To(Equal(expectedReturnValue))
Expect(ig.FailureMessage).To(Equal(expectedFailureMessage))
if expectedFailureMessage != "" {
Expect(ig.FailureSkip).To(Equal([]int{2}))
}
},
Entry(
"when non-zero results without error",
1, 2, nil,
SHOULD_NOT_OCCUR, "", IT_PASSES,
),
Entry(
"when non-zero results with error",
1, 2, errors.New("D'oh!"),
SHOULD_NOT_OCCUR, "Unexpected non-nil/non-zero argument at index 0:\n\t<int>: 1", IT_FAILS,
),
Entry(
"when non-zero results without error",
0, 0, errors.New("D'oh!"),
SHOULD_OCCUR, "", IT_PASSES,
),
Entry(
"when non-zero results with error",
1, 2, errors.New("D'oh!"),
SHOULD_OCCUR, "Unexpected non-nil/non-zero argument at index 0:\n\t<int>: 1", IT_FAILS,
),
)
When("vetting optional description parameters", func() {
It("panics when Gomega matcher is at the beginning of optional description parameters", func() {
ig := NewInstrumentedGomega()
for _, expectator := range []string{
"To", "NotTo", "ToNot",
"Should", "ShouldNot",
} {
Expect(func() {
expect := ig.G.Expect(42) // sic!
meth := reflect.ValueOf(expect).MethodByName(expectator)
Expect(meth.IsValid()).To(BeTrue())
meth.Call([]reflect.Value{
reflect.ValueOf(HaveLen(1)),
reflect.ValueOf(ContainElement(42)),
})
}).To(PanicWith(MatchRegexp("Assertion has a GomegaMatcher as the first element of optionalDescription")))
}
})
It("accepts Gomega matchers in optional description parameters after the first", func() {
Expect(func() {
ig := NewInstrumentedGomega()
ig.G.Expect(42).To(HaveLen(1), "foo", ContainElement(42))
}).NotTo(Panic())
})
})
})