Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: support byte slice in Contains #1526

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions assert/assertion_format.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions assert/assertion_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 18 additions & 8 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,19 +893,25 @@ 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:
switch element := element.(type) {
case []byte:
if list, ok := list.([]byte); ok {
Copy link

@Antonboom Antonboom Jun 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do return false, false if !ok?

Copy link
Author

@nickajacks1 nickajacks1 Jun 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, at least with how the code is written - if list is a [][]byte, we want to fall through to the loop check on line 917.
Maybe it's valid to say that if list is neither a []byte or a [][]byte, we should return false, false, but I haven't thought very hard about that case yet.

return true, bytes.Contains(list, element)
}
}
nickajacks1 marked this conversation as resolved.
Show resolved Hide resolved
}

for i := 0; i < listValue.Len(); i++ {
Expand All @@ -921,8 +927,10 @@ func containsElement(list interface{}, element interface{}) (ok, found bool) {
// specified substring or element.
//
// assert.Contains(t, "Hello World", "World")
// assert.Contains(t, ["Hello", "World"], "World")
// assert.Contains(t, {"Hello": "World"}, "Hello")
// assert.Contains(t, []string{"Hello", "World"}, "World")
// assert.Contains(t, map[string]string{"Hello": "World"}, "Hello")
// assert.Contains(t, []byte("Hello World"), []byte("World"))
// assert.Contains(t, [][]byte{[]byte("Hello"), []byte("World")}, []byte("World"))
func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
Expand All @@ -944,8 +952,10 @@ func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bo
// specified substring or element.
//
// assert.NotContains(t, "Hello World", "Earth")
// assert.NotContains(t, ["Hello", "World"], "Earth")
// assert.NotContains(t, {"Hello": "World"}, "Earth")
// assert.NotContains(t, []string{"Hello", "World"}, "Earth")
// assert.NotContains(t, map[string]string{"Hello": "World"}, "Earth")
// assert.NotContains(t, []byte("Hello World"), []byte("Earth"))
// assert.NotContains(t, [][]byte{[]byte("Hello"), []byte("World")}, []byte("Earth"))
func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
Expand Down
34 changes: 24 additions & 10 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,30 +920,44 @@ func TestContainsNotContains(t *testing.T) {
type A struct {
Name, Value string
}
list := []string{"Foo", "Bar"}
list := []string{"Hello", "World"}

byteSliceList := [][]byte{
[]byte("Hello"), []byte("World"),
}

complexList := []*A{
{"b", "c"},
{"d", "e"},
{"g", "h"},
{"j", "k"},
}
simpleMap := map[interface{}]interface{}{"Foo": "Bar"}

interfaceList := []interface{}{"Hello World", 5, 2, []byte("Goodbye")}

simpleMap := map[interface{}]interface{}{"Hello": "World"}
var zeroMap map[interface{}]interface{}

cases := []struct {
expected interface{}
actual interface{}
result bool
}{
{"Hello World", "Hello", true},
{"Hello World", "Salut", false},
{list, "Bar", true},
{list, "Salut", false},
{"Hello World", "World", true},
{"Hello World", "Earth", false},
{[]byte("Hello World"), []byte("World"), true},
{[]byte("Hello World"), []byte("Earth"), false},
{list, "World", true},
{list, "Earth", false},
{byteSliceList, []byte("World"), true},
{byteSliceList, []byte("Earth"), false},
{complexList, &A{"g", "h"}, true},
{complexList, &A{"g", "e"}, false},
{simpleMap, "Foo", true},
{simpleMap, "Bar", false},
{interfaceList, 5, true},
{interfaceList, []byte("Goodbye"), true},
{interfaceList, "World", false},
{simpleMap, "Hello", true},
{simpleMap, "Earth", false},
{zeroMap, "Bar", false},
}

Expand All @@ -953,7 +967,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 @@ -970,7 +984,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
24 changes: 16 additions & 8 deletions require/require.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions require/require_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.