Every runs the passed function on all the elements in the array. If assertion inside function fails, the original Array is marked failed. Every will execute the function for all values in the array irrespective of assertion failures for some values in the array. Example: array := NewArray(t, []
(fn func(index int, value *Value))
| 392 | // value.String().NotEmpty() |
| 393 | // }) |
| 394 | func (a *Array) Every(fn func(index int, value *Value)) *Array { |
| 395 | opChain := a.chain.enter("Every()") |
| 396 | defer opChain.leave() |
| 397 | |
| 398 | if opChain.failed() { |
| 399 | return a |
| 400 | } |
| 401 | |
| 402 | if fn == nil { |
| 403 | opChain.fail(AssertionFailure{ |
| 404 | Type: AssertUsage, |
| 405 | Errors: []error{ |
| 406 | errors.New("unexpected nil function argument"), |
| 407 | }, |
| 408 | }) |
| 409 | return a |
| 410 | } |
| 411 | |
| 412 | for index, element := range a.value { |
| 413 | func() { |
| 414 | valueChain := opChain.replace("Every[%d]", index) |
| 415 | defer valueChain.leave() |
| 416 | |
| 417 | fn(index, newValue(valueChain, element)) |
| 418 | }() |
| 419 | } |
| 420 | |
| 421 | return a |
| 422 | } |
| 423 | |
| 424 | // Filter accepts a function that returns a boolean. The function is ran |
| 425 | // over the array elements. If the function returns true, the element passes |