Skip to content

Commit

Permalink
assert: support byte slice in Contains
Browse files Browse the repository at this point in the history
  • Loading branch information
nickajacks1 committed Jan 27, 2024
1 parent 9b9a3b4 commit 84e42a9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 11 additions & 4 deletions assert/assertions.go
Expand Up @@ -845,19 +845,26 @@ func containsElement(list interface{}, element interface{}) (ok, found bool) {
}
}()

if listKind == reflect.String {
switch listKind {
case reflect.String:
elementValue := reflect.ValueOf(element)
return true, strings.Contains(listValue.String(), elementValue.String())
}

if listKind == reflect.Map {
case reflect.Map:
mapKeys := listValue.MapKeys()
for i := 0; i < len(mapKeys); i++ {
if ObjectsAreEqual(mapKeys[i].Interface(), element) {
return true, true
}
}
return true, false
case reflect.Slice:
elementValue := reflect.ValueOf(element)
if elementValue.Kind() != reflect.Slice ||
elementValue.Type().Elem().Kind() != reflect.Uint8 ||
listType.Elem().Kind() != reflect.Uint8 {
break
}
return true, bytes.Contains(listValue.Bytes(), elementValue.Bytes())
}

for i := 0; i < listValue.Len(); i++ {
Expand Down
12 changes: 10 additions & 2 deletions assert/assertions_test.go
Expand Up @@ -875,6 +875,10 @@ func TestContainsNotContains(t *testing.T) {
{"g", "h"},
{"j", "k"},
}
byteSliceList := [][]byte{
[]byte("Foo"), []byte("Bar"),
}

simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
var zeroMap map[interface{}]interface{}

Expand All @@ -885,8 +889,12 @@ func TestContainsNotContains(t *testing.T) {
}{
{"Hello World", "Hello", true},
{"Hello World", "Salut", false},
{[]byte("Hello World"), []byte("Hello"), true},
{[]byte("Hello World"), []byte("Salut"), false},
{list, "Bar", true},
{list, "Salut", false},
{byteSliceList, []byte("Bar"), true},
{byteSliceList, []byte("Salut"), false},
{complexList, &A{"g", "h"}, true},
{complexList, &A{"g", "e"}, false},
{simpleMap, "Foo", true},
Expand All @@ -900,7 +908,7 @@ func TestContainsNotContains(t *testing.T) {
res := Contains(mockT, c.expected, c.actual)

if res != c.result {
if res {
if c.result {
t.Errorf("Contains(%#v, %#v) should return true:\n\t%#v contains %#v", c.expected, c.actual, c.expected, c.actual)
} else {
t.Errorf("Contains(%#v, %#v) should return false:\n\t%#v does not contain %#v", c.expected, c.actual, c.expected, c.actual)
Expand All @@ -917,7 +925,7 @@ func TestContainsNotContains(t *testing.T) {
// NotContains should be inverse of Contains. If it's not, something is wrong
if res == Contains(mockT, c.expected, c.actual) {
if res {
t.Errorf("NotContains(%#v, %#v) should return true:\n\t%#v does not contains %#v", c.expected, c.actual, c.expected, c.actual)
t.Errorf("NotContains(%#v, %#v) should return true:\n\t%#v does not contain %#v", c.expected, c.actual, c.expected, c.actual)
} else {
t.Errorf("NotContains(%#v, %#v) should return false:\n\t%#v contains %#v", c.expected, c.actual, c.expected, c.actual)
}
Expand Down

0 comments on commit 84e42a9

Please sign in to comment.