IsEqualUnordered succeeds if array is equal to another array, ignoring element order. Before comparison, both arrays are converted to canonical form. Example: array := NewArray(t, []interface{}{"foo", 123}) array.IsEqualUnordered([]interface{}{123, "foo"})
(value interface{})
| 851 | // array := NewArray(t, []interface{}{"foo", 123}) |
| 852 | // array.IsEqualUnordered([]interface{}{123, "foo"}) |
| 853 | func (a *Array) IsEqualUnordered(value interface{}) *Array { |
| 854 | opChain := a.chain.enter("IsEqualUnordered()") |
| 855 | defer opChain.leave() |
| 856 | |
| 857 | if opChain.failed() { |
| 858 | return a |
| 859 | } |
| 860 | |
| 861 | expected, ok := canonArray(opChain, value) |
| 862 | if !ok { |
| 863 | return a |
| 864 | } |
| 865 | |
| 866 | for _, element := range expected { |
| 867 | expectedCount := countElement(expected, element) |
| 868 | actualCount := countElement(a.value, element) |
| 869 | |
| 870 | if actualCount != expectedCount { |
| 871 | if expectedCount == 1 && actualCount == 0 { |
| 872 | opChain.fail(AssertionFailure{ |
| 873 | Type: AssertContainsElement, |
| 874 | Actual: &AssertionValue{a.value}, |
| 875 | Expected: &AssertionValue{element}, |
| 876 | Reference: &AssertionValue{value}, |
| 877 | Errors: []error{ |
| 878 | errors.New("expected: array contains element from reference array"), |
| 879 | }, |
| 880 | }) |
| 881 | } else { |
| 882 | opChain.fail(AssertionFailure{ |
| 883 | Type: AssertNotContainsElement, |
| 884 | Actual: &AssertionValue{a.value}, |
| 885 | Expected: &AssertionValue{element}, |
| 886 | Reference: &AssertionValue{value}, |
| 887 | Errors: []error{ |
| 888 | fmt.Errorf( |
| 889 | "expected: element occurs %d time(s), as in reference array,"+ |
| 890 | " but it occurs %d time(s)", |
| 891 | expectedCount, |
| 892 | actualCount), |
| 893 | }, |
| 894 | }) |
| 895 | } |
| 896 | return a |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | for _, element := range a.value { |
| 901 | expectedCount := countElement(expected, element) |
| 902 | actualCount := countElement(a.value, element) |
| 903 | |
| 904 | if actualCount != expectedCount { |
| 905 | if expectedCount == 0 && actualCount == 1 { |
| 906 | opChain.fail(AssertionFailure{ |
| 907 | Type: AssertNotContainsElement, |
| 908 | Actual: &AssertionValue{a.value}, |
| 909 | Expected: &AssertionValue{element}, |
| 910 | Reference: &AssertionValue{value}, |