Subset asserts that the specified list(array, slice...) or map contains all elements given in the specified subset list(array, slice...) or map. assert.Subset(t, [1, 2, 3], [1, 2]) assert.Subset(t, {"x": 1, "y": 2}, {"x": 1})
(t TestingT, list, subset interface{}, msgAndArgs ...interface{})
| 969 | // assert.Subset(t, [1, 2, 3], [1, 2]) |
| 970 | // assert.Subset(t, {"x": 1, "y": 2}, {"x": 1}) |
| 971 | func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) { |
| 972 | if h, ok := t.(tHelper); ok { |
| 973 | h.Helper() |
| 974 | } |
| 975 | if subset == nil { |
| 976 | return true // we consider nil to be equal to the nil set |
| 977 | } |
| 978 | |
| 979 | listKind := reflect.TypeOf(list).Kind() |
| 980 | if listKind != reflect.Array && listKind != reflect.Slice && listKind != reflect.Map { |
| 981 | return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...) |
| 982 | } |
| 983 | |
| 984 | subsetKind := reflect.TypeOf(subset).Kind() |
| 985 | if subsetKind != reflect.Array && subsetKind != reflect.Slice && listKind != reflect.Map { |
| 986 | return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...) |
| 987 | } |
| 988 | |
| 989 | if subsetKind == reflect.Map && listKind == reflect.Map { |
| 990 | subsetMap := reflect.ValueOf(subset) |
| 991 | actualMap := reflect.ValueOf(list) |
| 992 | |
| 993 | for _, k := range subsetMap.MapKeys() { |
| 994 | ev := subsetMap.MapIndex(k) |
| 995 | av := actualMap.MapIndex(k) |
| 996 | |
| 997 | if !av.IsValid() { |
| 998 | return Fail(t, fmt.Sprintf("%#v does not contain %#v", list, subset), msgAndArgs...) |
| 999 | } |
| 1000 | if !ObjectsAreEqual(ev.Interface(), av.Interface()) { |
| 1001 | return Fail(t, fmt.Sprintf("%#v does not contain %#v", list, subset), msgAndArgs...) |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | return true |
| 1006 | } |
| 1007 | |
| 1008 | subsetList := reflect.ValueOf(subset) |
| 1009 | for i := 0; i < subsetList.Len(); i++ { |
| 1010 | element := subsetList.Index(i).Interface() |
| 1011 | ok, found := containsElement(list, element) |
| 1012 | if !ok { |
| 1013 | return Fail(t, fmt.Sprintf("%#v could not be applied builtin len()", list), msgAndArgs...) |
| 1014 | } |
| 1015 | if !found { |
| 1016 | return Fail(t, fmt.Sprintf("%#v does not contain %#v", list, element), msgAndArgs...) |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | return true |
| 1021 | } |
| 1022 | |
| 1023 | // NotSubset asserts that the specified list(array, slice...) or map does NOT |
| 1024 | // contain all elements given in the specified subset list(array, slice...) or |
searching dependent graphs…