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: handle array case in copyExportedFields #1404 #1436

Closed
wants to merge 1 commit into from
Closed

assert: handle array case in copyExportedFields #1404 #1436

wants to merge 1 commit into from

Conversation

qerdcv
Copy link
Contributor

@qerdcv qerdcv commented Jul 29, 2023

Summary

Implemented reflect.Array handling in the copyExportedFields to prevent panic when trying to make a slice from an array

Changes

Creating new array and filling it with exported values following the example from reflect.Slice.

I've decide to copy paste some code from the reflect.Slice case to make the code more readable, straightforward and avoid mutating the existing case for reflect.Slice

Motivation

Fixing a bug described in the issue #1404

Related issues

#1404

@qerdcv
Copy link
Contributor Author

qerdcv commented Jul 29, 2023

@glesica
@boyan-soubachov

Сheck it please, when you have time and inspiration

@dolmen dolmen added bug pkg-assert Change related to package testify/assert labels Jul 31, 2023
assert/assertions.go Outdated Show resolved Hide resolved
for i := 0; i < expectedValue.Len(); i++ {
index := expectedValue.Index(i)
if isNil(index) {
continue
Copy link
Collaborator

Choose a reason for hiding this comment

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

This case is not covered by the test suite.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

I added one more test for nil-value, but it failed. I fixed it by passing an actual interface{} to the isNil function.

continue
}
unexportedRemoved := copyExportedFields(index.Interface())
result.Index(i).Set(reflect.ValueOf(unexportedRemoved))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
result.Index(i).Set(reflect.ValueOf(unexportedRemoved))
index.Set(reflect.ValueOf(unexportedRemoved))

Nested{[2]int{1, 2}, [2]int{3, 4}},
},
value2: S7{
Nested{[2]interface{}{nil, 2}, [2]int{3, 4}},
Copy link
Collaborator

Choose a reason for hiding this comment

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

As nil is a special value which means anything matches, it would be better to use a concrete value here. Or better: add more test cases to validate that anything matches, not just that nil matches anything.

@dolmen dolmen changed the title #1404 Handle array case in copyExportedFields assert: handle array case in copyExportedFields #1404 Aug 8, 2023
@dolmen
Copy link
Collaborator

dolmen commented Oct 16, 2023

Issue #1404 has been fixed by #1473.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pkg-assert Change related to package testify/assert rejected/duplicate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants