Skip to content

EqualExportedValues panic when struct contains array  #1404

@olitez

Description

@olitez

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions