FindAll accepts a function that returns a boolean, runs it over the object 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
(fn func(key string, value *Value) bool)
| 631 | // foundValues[0].IsEqual(101) |
| 632 | // foundValues[1].IsEqual(201) |
| 633 | func (o *Object) FindAll(fn func(key string, value *Value) bool) []*Value { |
| 634 | opChain := o.chain.enter("FindAll()") |
| 635 | defer opChain.leave() |
| 636 | |
| 637 | if opChain.failed() { |
| 638 | return []*Value{} |
| 639 | } |
| 640 | |
| 641 | if fn == nil { |
| 642 | opChain.fail(AssertionFailure{ |
| 643 | Type: AssertUsage, |
| 644 | Errors: []error{ |
| 645 | errors.New("unexpected nil function argument"), |
| 646 | }, |
| 647 | }) |
| 648 | return []*Value{} |
| 649 | } |
| 650 | |
| 651 | foundValues := make([]*Value, 0, len(o.value)) |
| 652 | |
| 653 | for _, kv := range o.sortedKV() { |
| 654 | func() { |
| 655 | valueChain := opChain.replace("FindAll[%q]", kv.key) |
| 656 | defer valueChain.leave() |
| 657 | |
| 658 | valueChain.setRoot() |
| 659 | valueChain.setSeverity(SeverityLog) |
| 660 | |
| 661 | if fn(kv.key, newValue(valueChain, kv.val)) && !valueChain.treeFailed() { |
| 662 | foundValues = append(foundValues, newValue(opChain, kv.val)) |
| 663 | } |
| 664 | }() |
| 665 | } |
| 666 | |
| 667 | return foundValues |
| 668 | } |
| 669 | |
| 670 | // NotFind accepts a function that returns a boolean, runs it over the object |
| 671 | // elelements, and checks that it does not return true for any of the elements. |