Skip to content

EqualValues is not panic-safe #1699

@Antonboom

Description

@Antonboom

Prerequisite

	// ConvertibleTo reports whether a value of the type is convertible to type u.
	// Even if ConvertibleTo returns true, the conversion may still panic.
	// For example, a slice of type []T is convertible to *[N]T,
	// but the conversion will panic if its length is less than N.
	ConvertibleTo(u Type) bool

Example

package main

import (
	"testing"

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

func TestEqualValues(t *testing.T) {
	a := []int{1, 2}
	b := (*[3]int)(nil)
	assert.EqualValues(t, a, b)
}

Result

=== RUN   TestEqualValues
--- FAIL: TestEqualValues (0.00s)
panic: reflect: cannot convert slice with length 2 to pointer to array with length 3 [recovered]
	panic: reflect: cannot convert slice with length 2 to pointer to array with length 3

P.S. Inspired by Antonboom/testifylint#88

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions