Skip to content

HaveExactElements: false positive when passed nothing #771

@pohly

Description

@pohly

@bart0sh found the following odd behavior:

package foo

import (
	"testing"

	"github.com/onsi/gomega"
)

func TestHaveExactElements(t *testing.T) {
	var expected []any = nil
	actual := []string{"test"}

	gomega.NewWithT(t).Expect(actual).To(gomega.HaveExactElements(expected...)) // Same as HaveExactElements().
	if len(expected) != len(actual) {
		t.Error("unexpected non-nil results", expected, actual)
	}
}

Output:

--- FAIL: TestHaveExactElements (0.00s)
    foo_test.go:15: HaveExactElements did not detect a length mismatch: the actual list has 1 elements, expected were 0
FAIL
FAIL	k8s.io/kubernetes/foo	0.007s
FAIL

I think this is a bug. HaveExactElements should report all extra elements in the actual slice. So if there is nothing to match against, any non-empty slice should cause a failure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions