-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
boolean.go
331 lines (283 loc) · 6.59 KB
/
boolean.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package httpexpect
import (
"errors"
)
// Boolean provides methods to inspect attached bool value
// (Go representation of JSON boolean).
type Boolean struct {
noCopy noCopy
chain *chain
value bool
}
// NewBoolean returns a new Boolean instance.
//
// If reporter is nil, the function panics.
//
// Example:
//
// boolean := NewBoolean(t, true)
// boolean.IsTrue()
func NewBoolean(reporter Reporter, value bool) *Boolean {
return newBoolean(newChainWithDefaults("Boolean()", reporter), value)
}
// NewBooleanC returns a new Boolean instance with config.
//
// Requirements for config are same as for WithConfig function.
//
// Example:
//
// boolean := NewBooleanC(config, true)
// boolean.IsTrue()
func NewBooleanC(config Config, value bool) *Boolean {
return newBoolean(newChainWithConfig("Boolean()", config.withDefaults()), value)
}
func newBoolean(parent *chain, val bool) *Boolean {
return &Boolean{chain: parent.clone(), value: val}
}
// Raw returns underlying value attached to Boolean.
// This is the value originally passed to NewBoolean.
//
// Example:
//
// boolean := NewBoolean(t, true)
// assert.Equal(t, true, boolean.Raw())
func (b *Boolean) Raw() bool {
return b.value
}
// Decode unmarshals the underlying value attached to the Boolean to a target variable.
// target should be one of these:
//
// - pointer to an empty interface
// - pointer to a boolean
//
// Example:
//
// value := NewBoolean(t, true)
//
// var target bool
// value.Decode(&target)
//
// assert.Equal(t, true, target)
func (b *Boolean) Decode(target interface{}) *Boolean {
opChain := b.chain.enter("Decode()")
defer opChain.leave()
if opChain.failed() {
return b
}
canonDecode(opChain, b.value, target)
return b
}
// Alias is similar to Value.Alias.
func (b *Boolean) Alias(name string) *Boolean {
opChain := b.chain.enter("Alias(%q)", name)
defer opChain.leave()
b.chain.setAlias(name)
return b
}
// Path is similar to Value.Path.
func (b *Boolean) Path(path string) *Value {
opChain := b.chain.enter("Path(%q)", path)
defer opChain.leave()
return jsonPath(opChain, b.value, path)
}
// Schema is similar to Value.Schema.
func (b *Boolean) Schema(schema interface{}) *Boolean {
opChain := b.chain.enter("Schema()")
defer opChain.leave()
jsonSchema(opChain, b.value, schema)
return b
}
// IsTrue succeeds if boolean is true.
//
// Example:
//
// boolean := NewBoolean(t, true)
// boolean.IsTrue()
func (b *Boolean) IsTrue() *Boolean {
opChain := b.chain.enter("IsTrue()")
defer opChain.leave()
if opChain.failed() {
return b
}
if !(b.value == true) {
opChain.fail(AssertionFailure{
Type: AssertEqual,
Actual: &AssertionValue{b.value},
Expected: &AssertionValue{true},
Errors: []error{
errors.New("expected: boolean is true"),
},
})
}
return b
}
// IsFalse succeeds if boolean is false.
//
// Example:
//
// boolean := NewBoolean(t, false)
// boolean.IsFalse()
func (b *Boolean) IsFalse() *Boolean {
opChain := b.chain.enter("IsFalse()")
defer opChain.leave()
if opChain.failed() {
return b
}
if !(b.value == false) {
opChain.fail(AssertionFailure{
Type: AssertEqual,
Actual: &AssertionValue{b.value},
Expected: &AssertionValue{false},
Errors: []error{
errors.New("expected: boolean is false"),
},
})
}
return b
}
// Deprecated: use IsTrue instead.
func (b *Boolean) True() *Boolean {
return b.IsTrue()
}
// Deprecated: use IsFalse instead.
func (b *Boolean) False() *Boolean {
return b.IsFalse()
}
// IsEqual succeeds if boolean is equal to given value.
//
// Example:
//
// boolean := NewBoolean(t, true)
// boolean.IsEqual(true)
func (b *Boolean) IsEqual(value bool) *Boolean {
opChain := b.chain.enter("IsEqual()")
defer opChain.leave()
if opChain.failed() {
return b
}
if !(b.value == value) {
opChain.fail(AssertionFailure{
Type: AssertEqual,
Actual: &AssertionValue{b.value},
Expected: &AssertionValue{value},
Errors: []error{
errors.New("expected: booleans are equal"),
},
})
}
return b
}
// NotEqual succeeds if boolean is not equal to given value.
//
// Example:
//
// boolean := NewBoolean(t, true)
// boolean.NotEqual(false)
func (b *Boolean) NotEqual(value bool) *Boolean {
opChain := b.chain.enter("NotEqual()")
defer opChain.leave()
if opChain.failed() {
return b
}
if b.value == value {
opChain.fail(AssertionFailure{
Type: AssertNotEqual,
Actual: &AssertionValue{b.value},
Expected: &AssertionValue{value},
Errors: []error{
errors.New("expected: booleans are non-equal"),
},
})
}
return b
}
// Deprecated: use IsEqual instead.
func (b *Boolean) Equal(value bool) *Boolean {
return b.IsEqual(value)
}
// InList succeeds if boolean is equal to one of the values from given
// list of booleans.
//
// Example:
//
// boolean := NewBoolean(t, true)
// boolean.InList(true, false)
func (b *Boolean) InList(values ...bool) *Boolean {
opChain := b.chain.enter("InList()")
defer opChain.leave()
if opChain.failed() {
return b
}
if len(values) == 0 {
opChain.fail(AssertionFailure{
Type: AssertUsage,
Errors: []error{
errors.New("unexpected empty list argument"),
},
})
return b
}
var isListed bool
for _, v := range values {
if b.value == v {
isListed = true
break
}
}
if !isListed {
valueList := make([]interface{}, 0, len(values))
for _, v := range values {
valueList = append(valueList, v)
}
opChain.fail(AssertionFailure{
Type: AssertBelongs,
Actual: &AssertionValue{b.value},
Expected: &AssertionValue{AssertionList(valueList)},
Errors: []error{
errors.New("expected: boolean is equal to one of the values"),
},
})
}
return b
}
// NotInList succeeds if boolean is not equal to any of the values from
// given list of booleans.
//
// Example:
//
// boolean := NewBoolean(t, true)
// boolean.NotInList(true, false) // failure
func (b *Boolean) NotInList(values ...bool) *Boolean {
opChain := b.chain.enter("NotInList()")
defer opChain.leave()
if opChain.failed() {
return b
}
if len(values) == 0 {
opChain.fail(AssertionFailure{
Type: AssertUsage,
Errors: []error{
errors.New("unexpected empty list argument"),
},
})
return b
}
for _, v := range values {
if b.value == v {
valueList := make([]interface{}, 0, len(values))
for _, v := range values {
valueList = append(valueList, v)
}
opChain.fail(AssertionFailure{
Type: AssertNotBelongs,
Actual: &AssertionValue{b.value},
Expected: &AssertionValue{AssertionList(valueList)},
Errors: []error{
errors.New("expected: boolean is not equal to any of the values"),
},
})
return b
}
}
return b
}