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

Function containsElement

assert/assertions.go:917–952  ·  view source on GitHub ↗

containsElement try loop over the list check if the list includes the element. return (false, false) if impossible. return (true, false) if element was not found. return (true, true) if element was found.

(list interface{}, element interface{})

Source from the content-addressed store, hash-verified

915// return (true, false) if element was not found.
916// return (true, true) if element was found.
917func containsElement(list interface{}, element interface{}) (ok, found bool) {
918 listValue := reflect.ValueOf(list)
919 listType := reflect.TypeOf(list)
920 if listType == nil {
921 return false, false
922 }
923 listKind := listType.Kind()
924 defer func() {
925 if e := recover(); e != nil {
926 ok = false
927 found = false
928 }
929 }()
930
931 if listKind == reflect.String {
932 elementValue := reflect.ValueOf(element)
933 return true, strings.Contains(listValue.String(), elementValue.String())
934 }
935
936 if listKind == reflect.Map {
937 mapKeys := listValue.MapKeys()
938 for i := 0; i < len(mapKeys); i++ {
939 if ObjectsAreEqual(mapKeys[i].Interface(), element) {
940 return true, true
941 }
942 }
943 return true, false
944 }
945
946 for i := 0; i < listValue.Len(); i++ {
947 if ObjectsAreEqual(listValue.Index(i).Interface(), element) {
948 return true, true
949 }
950 }
951 return true, false
952}
953
954// Contains asserts that the specified string, list(array, slice...) or map contains the
955// specified substring or element.

Callers 5

Test_containsElementFunction · 0.85
ContainsFunction · 0.85
NotContainsFunction · 0.85
SubsetFunction · 0.85
NotSubsetFunction · 0.85

Calls 4

ObjectsAreEqualFunction · 0.85
ContainsMethod · 0.45
StringMethod · 0.45
LenMethod · 0.45

Tested by 1

Test_containsElementFunction · 0.68