MCPcopy
hub / github.com/stretchr/testify / NotSubset

Function NotSubset

assert/assertions.go:1075–1132  ·  view source on GitHub ↗

NotSubset asserts that the list (array, slice, or map) does NOT contain all elements given in the subset (array, slice, or map). Map elements are key-value pairs unless compared with an array or slice where only the map key is evaluated. assert.NotSubset(t, [1, 3, 4], [1, 2]) assert.NotSubset(t,

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

Source from the content-addressed store, hash-verified

1073// assert.NotSubset(t, [1, 3, 4], {1: "one", 2: "two"})
1074// assert.NotSubset(t, {"x": 1, "y": 2}, ["z"])
1075func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) {
1076 if h, ok := t.(tHelper); ok {
1077 h.Helper()
1078 }
1079 if subset == nil {
1080 return Fail(t, "nil is the empty set which is a subset of every set", msgAndArgs...)
1081 }
1082
1083 listKind := reflect.TypeOf(list).Kind()
1084 if listKind != reflect.Array && listKind != reflect.Slice && listKind != reflect.Map {
1085 return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...)
1086 }
1087
1088 subsetKind := reflect.TypeOf(subset).Kind()
1089 if subsetKind != reflect.Array && subsetKind != reflect.Slice && subsetKind != reflect.Map {
1090 return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...)
1091 }
1092
1093 if subsetKind == reflect.Map && listKind == reflect.Map {
1094 subsetMap := reflect.ValueOf(subset)
1095 actualMap := reflect.ValueOf(list)
1096
1097 for _, k := range subsetMap.MapKeys() {
1098 ev := subsetMap.MapIndex(k)
1099 av := actualMap.MapIndex(k)
1100
1101 if !av.IsValid() {
1102 return true
1103 }
1104 if !ObjectsAreEqual(ev.Interface(), av.Interface()) {
1105 return true
1106 }
1107 }
1108
1109 return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...)
1110 }
1111
1112 subsetList := reflect.ValueOf(subset)
1113 if subsetKind == reflect.Map {
1114 keys := make([]interface{}, subsetList.Len())
1115 for idx, key := range subsetList.MapKeys() {
1116 keys[idx] = key.Interface()
1117 }
1118 subsetList = reflect.ValueOf(keys)
1119 }
1120 for i := 0; i < subsetList.Len(); i++ {
1121 element := subsetList.Index(i).Interface()
1122 ok, found := containsElement(list, element)
1123 if !ok {
1124 return Fail(t, fmt.Sprintf("%q could not be applied builtin len()", list), msgAndArgs...)
1125 }
1126 if !found {
1127 return true
1128 }
1129 }
1130
1131 return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...)
1132}

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