NotFind accepts a function that returns a boolean, runs it over the object elelements, and checks that it does not return true for any of the elements. If there are any failed assertions in the predicate function, the element is skipped without causing test failure. If the predicate function did n
(fn func(key string, value *Value) bool)
| 691 | // return num.Raw() > 100 // check element value |
| 692 | // }) // succeeds |
| 693 | func (o *Object) NotFind(fn func(key string, value *Value) bool) *Object { |
| 694 | opChain := o.chain.enter("NotFind()") |
| 695 | defer opChain.leave() |
| 696 | |
| 697 | if opChain.failed() { |
| 698 | return o |
| 699 | } |
| 700 | |
| 701 | if fn == nil { |
| 702 | opChain.fail(AssertionFailure{ |
| 703 | Type: AssertUsage, |
| 704 | Errors: []error{ |
| 705 | errors.New("unexpected nil function argument"), |
| 706 | }, |
| 707 | }) |
| 708 | return o |
| 709 | } |
| 710 | |
| 711 | for _, kv := range o.sortedKV() { |
| 712 | found := false |
| 713 | |
| 714 | func() { |
| 715 | valueChain := opChain.replace("NotFind[%q]", kv.key) |
| 716 | defer valueChain.leave() |
| 717 | |
| 718 | valueChain.setRoot() |
| 719 | valueChain.setSeverity(SeverityLog) |
| 720 | |
| 721 | if fn(kv.key, newValue(valueChain, kv.val)) && !valueChain.treeFailed() { |
| 722 | found = true |
| 723 | } |
| 724 | }() |
| 725 | |
| 726 | if found { |
| 727 | opChain.fail(AssertionFailure{ |
| 728 | Type: AssertNotContainsElement, |
| 729 | Expected: &AssertionValue{kv.val}, |
| 730 | Actual: &AssertionValue{o.value}, |
| 731 | Errors: []error{ |
| 732 | errors.New("expected: none of the object elements match predicate"), |
| 733 | fmt.Errorf("element with key %q matches predicate", kv.key), |
| 734 | }, |
| 735 | }) |
| 736 | return o |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | return o |
| 741 | } |
| 742 | |
| 743 | // IsEmpty succeeds if object is empty. |
| 744 | // |