FindAll accepts a function that returns a boolean, runs it over the array elements, and returns all the elements on which it returned true. If there are any failed assertions in the predicate function, the element is skipped without causing test failure. If no elements were found, empty slice is r
(fn func(index int, value *Value) bool)
| 599 | // foundValues[0].IsEqual(101) |
| 600 | // foundValues[1].IsEqual(201) |
| 601 | func (a *Array) FindAll(fn func(index int, value *Value) bool) []*Value { |
| 602 | opChain := a.chain.enter("FindAll()") |
| 603 | defer opChain.leave() |
| 604 | |
| 605 | if opChain.failed() { |
| 606 | return []*Value{} |
| 607 | } |
| 608 | |
| 609 | if fn == nil { |
| 610 | opChain.fail(AssertionFailure{ |
| 611 | Type: AssertUsage, |
| 612 | Errors: []error{ |
| 613 | errors.New("unexpected nil function argument"), |
| 614 | }, |
| 615 | }) |
| 616 | return []*Value{} |
| 617 | } |
| 618 | |
| 619 | foundValues := make([]*Value, 0, len(a.value)) |
| 620 | |
| 621 | for index, element := range a.value { |
| 622 | func() { |
| 623 | valueChain := opChain.replace("FindAll[%d]", index) |
| 624 | defer valueChain.leave() |
| 625 | |
| 626 | valueChain.setRoot() |
| 627 | valueChain.setSeverity(SeverityLog) |
| 628 | |
| 629 | if fn(index, newValue(valueChain, element)) && !valueChain.treeFailed() { |
| 630 | foundValues = append(foundValues, newValue(opChain, element)) |
| 631 | } |
| 632 | }() |
| 633 | } |
| 634 | |
| 635 | return foundValues |
| 636 | } |
| 637 | |
| 638 | // NotFind accepts a function that returns a boolean, runs it over the array |
| 639 | // elelements, and checks that it does not return true for any of the elements. |