MCPcopy Create free account
hub / github.com/stretchr/testify / NotSubset

Function NotSubset

assert/assertions.go:1030–1080  ·  view source on GitHub ↗

NotSubset asserts that the specified list(array, slice...) or map does NOT contain all elements given in the specified subset list(array, slice...) or map. assert.NotSubset(t, [1, 3, 4], [1, 2]) assert.NotSubset(t, {"x": 1, "y": 2}, {"z": 3})

(t TestingT, list, subset interface{}, msgAndArgs ...interface{})

Source from the content-addressed store, hash-verified

1028// assert.NotSubset(t, [1, 3, 4], [1, 2])
1029// assert.NotSubset(t, {"x": 1, "y": 2}, {"z": 3})
1030func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) {
1031 if h, ok := t.(tHelper); ok {
1032 h.Helper()
1033 }
1034 if subset == nil {
1035 return Fail(t, "nil is the empty set which is a subset of every set", msgAndArgs...)
1036 }
1037
1038 listKind := reflect.TypeOf(list).Kind()
1039 if listKind != reflect.Array && listKind != reflect.Slice && listKind != reflect.Map {
1040 return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...)
1041 }
1042
1043 subsetKind := reflect.TypeOf(subset).Kind()
1044 if subsetKind != reflect.Array && subsetKind != reflect.Slice && listKind != reflect.Map {
1045 return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...)
1046 }
1047
1048 if subsetKind == reflect.Map && listKind == reflect.Map {
1049 subsetMap := reflect.ValueOf(subset)
1050 actualMap := reflect.ValueOf(list)
1051
1052 for _, k := range subsetMap.MapKeys() {
1053 ev := subsetMap.MapIndex(k)
1054 av := actualMap.MapIndex(k)
1055
1056 if !av.IsValid() {
1057 return true
1058 }
1059 if !ObjectsAreEqual(ev.Interface(), av.Interface()) {
1060 return true
1061 }
1062 }
1063
1064 return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...)
1065 }
1066
1067 subsetList := reflect.ValueOf(subset)
1068 for i := 0; i < subsetList.Len(); i++ {
1069 element := subsetList.Index(i).Interface()
1070 ok, found := containsElement(list, element)
1071 if !ok {
1072 return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...)
1073 }
1074 if !found {
1075 return true
1076 }
1077 }
1078
1079 return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...)
1080}
1081
1082// ElementsMatch asserts that the specified listA(array, slice...) is equal to specified
1083// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements,

Callers 5

NotSubsetFunction · 0.92
NotSubsetfFunction · 0.70
TestSubsetNotSubsetFunction · 0.70
TestNotSubsetNilFunction · 0.70
NotSubsetMethod · 0.70

Calls 5

ObjectsAreEqualFunction · 0.85
containsElementFunction · 0.85
FailFunction · 0.70
HelperMethod · 0.65
LenMethod · 0.45

Tested by 2

TestSubsetNotSubsetFunction · 0.56
TestNotSubsetNilFunction · 0.56