@@ -191,15 +191,115 @@ func (a *Array) Element(index int) *Value {
191
191
return a .Value (index )
192
192
}
193
193
194
- // First returns a new Value instance for the first element of array .
194
+ // HasValue succeeds if array's value at the given index is equal to given value .
195
195
//
196
- // If given array is empty, First reports failure and returns empty
197
- // (but non-nil) instance .
196
+ // Before comparison, both values are converted to canonical form. value should be
197
+ // map[string]interface{} or struct .
198
198
//
199
199
// Example:
200
200
//
201
- // array := NewArray(t, []interface{}{"foo", 123})
202
- // array.First().String().IsEqual("foo")
201
+ // array := NewArray(t, []interface{}{"foo", "123"})
202
+ // array.HasValue(1, 123)
203
+ func (a * Array ) HasValue (index int , value interface {}) * Array {
204
+ opChain := a .chain .enter ("HasValue(%d)" , index )
205
+ defer opChain .leave ()
206
+
207
+ if opChain .failed () {
208
+ return a
209
+ }
210
+
211
+ if index < 0 || index >= len (a .value ) {
212
+ opChain .fail (AssertionFailure {
213
+ Type : AssertInRange ,
214
+ Actual : & AssertionValue {index },
215
+ Expected : & AssertionValue {AssertionRange {
216
+ Min : 0 ,
217
+ Max : len (a .value ) - 1 ,
218
+ }},
219
+ Errors : []error {
220
+ errors .New ("expected: valid element index" ),
221
+ },
222
+ })
223
+ return a
224
+ }
225
+
226
+ expected , ok := canonValue (opChain , value )
227
+ if ! ok {
228
+ return a
229
+ }
230
+
231
+ if ! reflect .DeepEqual (expected , a .value [index ]) {
232
+ opChain .fail (AssertionFailure {
233
+ Type : AssertEqual ,
234
+ Actual : & AssertionValue {a .value [index ]},
235
+ Expected : & AssertionValue {value },
236
+ Errors : []error {
237
+ fmt .Errorf (
238
+ "expected: array value at index %d is equal to given value" ,
239
+ index ),
240
+ },
241
+ })
242
+ return a
243
+ }
244
+
245
+ return a
246
+ }
247
+
248
+ // NotHasValue succeeds if array's value at the given index is not equal to given value.
249
+ //
250
+ // Before comparison, both values are converted to canonical form. value should be
251
+ // map[string]interface{} or struct.
252
+ //
253
+ // Example:
254
+ //
255
+ // array := NewArray(t, []interface{}{"foo", "123"})
256
+ // array.NotHasValue(1, 234)
257
+ func (a * Array ) NotHasValue (index int , value interface {}) * Array {
258
+ opChain := a .chain .enter ("NotHasValue(%d)" , index )
259
+ defer opChain .leave ()
260
+
261
+ if opChain .failed () {
262
+ return a
263
+ }
264
+
265
+ if index < 0 || index >= len (a .value ) {
266
+ opChain .fail (AssertionFailure {
267
+ Type : AssertInRange ,
268
+ Actual : & AssertionValue {index },
269
+ Expected : & AssertionValue {AssertionRange {
270
+ Min : 0 ,
271
+ Max : len (a .value ) - 1 ,
272
+ }},
273
+ Errors : []error {
274
+ errors .New ("expected: valid element index" ),
275
+ },
276
+ })
277
+ return a
278
+ }
279
+
280
+ expected , ok := canonValue (opChain , value )
281
+ if ! ok {
282
+ return a
283
+ }
284
+
285
+ if reflect .DeepEqual (expected , a .value [index ]) {
286
+ opChain .fail (AssertionFailure {
287
+ Type : AssertNotEqual ,
288
+ Actual : & AssertionValue {a .value [index ]},
289
+ Expected : & AssertionValue {value },
290
+ Errors : []error {
291
+ fmt .Errorf (
292
+ "expected: array value at index %d is not equal to given value" ,
293
+ index ),
294
+ },
295
+ })
296
+ return a
297
+ }
298
+
299
+ return a
300
+ }
301
+
302
+ // Deprecated: use Value or HasValue instead.
203
303
func (a * Array ) First () * Value {
204
304
opChain := a .chain .enter ("First()" )
205
305
defer opChain .leave ()
@@ -222,15 +322,7 @@ func (a *Array) First() *Value {
222
322
return newValue (opChain , a .value [0 ])
223
323
}
224
324
225
- // Last returns a new Value instance for the last element of array.
226
- //
227
- // If given array is empty, Last reports failure and returns empty
228
- // (but non-nil) instance.
229
- //
230
- // Example:
231
- //
232
- // array := NewArray(t, []interface{}{"foo", 123})
233
- // array.Last().Number().IsEqual(123)
325
+ // Deprecated: use Value or HasValue instead.
234
326
func (a * Array ) Last () * Value {
235
327
opChain := a .chain .enter ("Last()" )
236
328
defer opChain .leave ()
@@ -261,7 +353,7 @@ func (a *Array) Last() *Value {
261
353
// array := NewArray(t, strings)
262
354
//
263
355
// for index, value := range array.Iter() {
264
- // value.String().IsEqual(strings[index])
356
+ // value.String().IsEqual(strings[index])
265
357
// }
266
358
func (a * Array ) Iter () []Value {
267
359
opChain := a .chain .enter ("Iter()" )
@@ -1452,114 +1544,6 @@ func (a *Array) NotContainsOnly(values ...interface{}) *Array {
1452
1544
return a
1453
1545
}
1454
1546
1455
- // IsValueEqual succeeds if array's value at the given index is equal to given value.
1456
- //
1457
- // Before comparison, both values are converted to canonical form. value should be
1458
- // map[string]interface{} or struct.
1459
- //
1460
- // Example:
1461
- //
1462
- // array := NewArray(t, []interface{}{"foo", "123"})
1463
- // array.IsValueEqual(1, 123)
1464
- func (a * Array ) IsValueEqual (index int , value interface {}) * Array {
1465
- opChain := a .chain .enter ("IsValueEqual(%d)" , index )
1466
- defer opChain .leave ()
1467
-
1468
- if opChain .failed () {
1469
- return a
1470
- }
1471
-
1472
- if index < 0 || index >= len (a .value ) {
1473
- opChain .fail (AssertionFailure {
1474
- Type : AssertInRange ,
1475
- Actual : & AssertionValue {index },
1476
- Expected : & AssertionValue {AssertionRange {
1477
- Min : 0 ,
1478
- Max : len (a .value ) - 1 ,
1479
- }},
1480
- Errors : []error {
1481
- errors .New ("expected: valid element index" ),
1482
- },
1483
- })
1484
- return a
1485
- }
1486
-
1487
- expected , ok := canonValue (opChain , value )
1488
- if ! ok {
1489
- return a
1490
- }
1491
-
1492
- if ! reflect .DeepEqual (expected , a .value [index ]) {
1493
- opChain .fail (AssertionFailure {
1494
- Type : AssertEqual ,
1495
- Actual : & AssertionValue {a .value [index ]},
1496
- Expected : & AssertionValue {value },
1497
- Errors : []error {
1498
- fmt .Errorf (
1499
- "expected: array value at index %d is equal to given value" ,
1500
- index ),
1501
- },
1502
- })
1503
- return a
1504
- }
1505
-
1506
- return a
1507
- }
1508
-
1509
- // NotValueEqual succeeds if array's value at the given index is not equal to given value.
1510
- //
1511
- // Before comparison, both values are converted to canonical form. value should be
1512
- // map[string]interface{} or struct.
1513
- //
1514
- // Example:
1515
- //
1516
- // array := NewArray(t, []interface{}{"foo", "123"})
1517
- // array.NotValueEqual(1, 234)
1518
- func (a * Array ) NotValueEqual (index int , value interface {}) * Array {
1519
- opChain := a .chain .enter ("NotValueEqual(%d)" , index )
1520
- defer opChain .leave ()
1521
-
1522
- if opChain .failed () {
1523
- return a
1524
- }
1525
-
1526
- if index < 0 || index >= len (a .value ) {
1527
- opChain .fail (AssertionFailure {
1528
- Type : AssertInRange ,
1529
- Actual : & AssertionValue {index },
1530
- Expected : & AssertionValue {AssertionRange {
1531
- Min : 0 ,
1532
- Max : len (a .value ) - 1 ,
1533
- }},
1534
- Errors : []error {
1535
- errors .New ("expected: valid element index" ),
1536
- },
1537
- })
1538
- return a
1539
- }
1540
-
1541
- expected , ok := canonValue (opChain , value )
1542
- if ! ok {
1543
- return a
1544
- }
1545
-
1546
- if reflect .DeepEqual (expected , a .value [index ]) {
1547
- opChain .fail (AssertionFailure {
1548
- Type : AssertNotEqual ,
1549
- Actual : & AssertionValue {a .value [index ]},
1550
- Expected : & AssertionValue {value },
1551
- Errors : []error {
1552
- fmt .Errorf (
1553
- "expected: array value at index %d is not equal to given value" ,
1554
- index ),
1555
- },
1556
- })
1557
- return a
1558
- }
1559
-
1560
- return a
1561
- }
1562
-
1563
1547
// IsOrdered succeeds if every element is not less than the previous element
1564
1548
// as defined on the given `less` comparator function.
1565
1549
// For default, it will use built-in comparator function for each data type.
0 commit comments