InList succeeds if whole value is equal to one of the values from given list of values (e.g. map, slice, string, etc). Before comparison, all values are converted to canonical form. If at least one value has wrong type, failure is reported. Example: value := NewValue(t, "foo") value.InList("foo
(values ...interface{})
| 832 | // value := NewValue(t, "foo") |
| 833 | // value.InList("foo", 123) |
| 834 | func (v *Value) InList(values ...interface{}) *Value { |
| 835 | opChain := v.chain.enter("InList()") |
| 836 | defer opChain.leave() |
| 837 | |
| 838 | if opChain.failed() { |
| 839 | return v |
| 840 | } |
| 841 | |
| 842 | if len(values) == 0 { |
| 843 | opChain.fail(AssertionFailure{ |
| 844 | Type: AssertUsage, |
| 845 | Errors: []error{ |
| 846 | errors.New("unexpected empty list argument"), |
| 847 | }, |
| 848 | }) |
| 849 | return v |
| 850 | } |
| 851 | |
| 852 | var isListed bool |
| 853 | for _, val := range values { |
| 854 | expected, ok := canonValue(opChain, val) |
| 855 | if !ok { |
| 856 | return v |
| 857 | } |
| 858 | |
| 859 | if reflect.DeepEqual(expected, v.value) { |
| 860 | isListed = true |
| 861 | // continue loop to check that all values are correct |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | if !isListed { |
| 866 | opChain.fail(AssertionFailure{ |
| 867 | Type: AssertBelongs, |
| 868 | Actual: &AssertionValue{v.value}, |
| 869 | Expected: &AssertionValue{AssertionList(values)}, |
| 870 | Errors: []error{ |
| 871 | errors.New("expected: value is equal to one of the values"), |
| 872 | }, |
| 873 | }) |
| 874 | } |
| 875 | |
| 876 | return v |
| 877 | } |
| 878 | |
| 879 | // NotInList succeeds if the whole value is not equal to any of the values from |
| 880 | // given list of values (e.g. map, slice, string, etc). |