-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: master
Are you sure you want to change the base?
Conversation
6db7a93
to
84e42a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably worth updating the examples
Here:
Lines 879 to 884 in 84e42a9
// Contains asserts that the specified string, list(array, slice...) or map contains the | |
// specified substring or element. | |
// | |
// assert.Contains(t, "Hello World", "World") | |
// assert.Contains(t, ["Hello", "World"], "World") | |
// assert.Contains(t, {"Hello": "World"}, "Hello") |
And here:
Lines 902 to 907 in 84e42a9
// NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the | |
// specified substring or element. | |
// | |
// assert.NotContains(t, "Hello World", "Earth") | |
// assert.NotContains(t, ["Hello", "World"], "Earth") | |
// assert.NotContains(t, {"Hello": "World"}, "Earth") |
So, that the godoc reflects that []byte
is now supported.
4be271c
to
5583f5d
Compare
Would this change be more useful to more people if when eg: Contains(t, []int{1,2,3,4,5,6,7,8,9}, []int{4,5,6}) // -> true
Contains(t, []int{1,2,3,4,5,6,7,8,9}, []int{3,5,7}) // -> false
Contains(t, []byte("My hovercraft is full of eels"), []byte("hovercraft")) // -> true
Contains(t, [...]byte("I am an array"), [...]byte("arr")) // -> true
Contains(t, []any{"hi", []byte{}, false, 9}, []byte{}) // -> true |
Yeah actually I think I was looking for that functionality before and was disappointed to find that it didn't exist. |
@nickajacks1 Let's keep this PR focused on |
05facad
to
ee6de9c
Compare
ee6de9c
to
30bccad
Compare
case reflect.Slice: | ||
switch element := element.(type) { | ||
case []byte: | ||
if list, ok := list.([]byte); ok { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a shame not to go with the generic subslice within slice approach first as it solves this case too. But this does not preclude doing that.
@dolmen are you satisfied with the changes you requested?
Summary
Update
assert.Contains
andassert.NotContains
to check if a byte slice contains another byte slice.Changes
If both the list and element arguments are byte slices, use
bytes.Contains
to check if element is a subslice. This mirrors the way strings are treated byassert.Contains
.Motivation
The change makes
Contains
more consistent and predictable. One could argue that developers could easily cast byte slices to strings, but the fact that that Contains does not treat strings and byte slices the same way breaks the principle of least astonishment (and probably would warrant a new rule intestifylint
).