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

EqualExportedValues panic when struct contains array #1404

Closed
olitez opened this issue Jun 21, 2023 · 1 comment · Fixed by #1473
Closed

EqualExportedValues panic when struct contains array #1404

olitez opened this issue Jun 21, 2023 · 1 comment · Fixed by #1473
Labels
assert.EqualValues About equality bug pkg-assert Change related to package testify/assert

Comments

@olitez
Copy link

olitez commented Jun 21, 2023

Hi

package main

import (
	"github.com/stretchr/testify/assert"
)

type Foo struct {
	Array [2]int
}

func main() {
	a := Foo{[2]int{1, 2}}
	b := Foo{[2]int{2, 1}}
	assert.EqualExportedValues(nil, a, b)
}

Will panic with "panic: reflect.MakeSlice of non-slice type"

Reason is there in assertion.go:113 :

case reflect.Array, reflect.Slice:
	result := reflect.MakeSlice(expectedType, expectedValue.Len(), expectedValue.Len())

reflect.MakeSlice only work with slice, not with Array. Fix could be follow:

case reflect.Array, reflect.Slice:
	result := reflect.New(expectedType).Elem()
	if expectedKind == reflect.Slice {
		result = reflect.MakeSlice(expectedType, expectedValue.Len(), expectedValue.Len())
	}

Or even carry reflect.Array to different switch case

@dolmen dolmen changed the title EqualExportedValues panic when struct contain array EqualExportedValues panic when struct contains array Jul 5, 2023
@dolmen dolmen added pkg-assert Change related to package testify/assert bug labels Jul 5, 2023
@dolmen
Copy link
Collaborator

dolmen commented Jul 5, 2023

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

Successfully merging a pull request may close this issue.

2 participants