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

HaveExactElements: false positive when passed nothing #771

Closed
pohly opened this issue Jul 18, 2024 · 5 comments
Closed

HaveExactElements: false positive when passed nothing #771

pohly opened this issue Jul 18, 2024 · 5 comments

Comments

@pohly
Copy link

pohly commented Jul 18, 2024

@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.

@pohly
Copy link
Author

pohly commented Jul 18, 2024

The reason for using HaveExactElements instead of BeEmpty is that the set of expected elements is dynamic.

@onsi
Copy link
Owner

onsi commented Jul 18, 2024

yes looks like a bug to me. i'll try to prioritize this and get to it soon. i'm a bit behind on ginkgo/gomega stuff these days.

@pohly
Copy link
Author

pohly commented Jul 19, 2024

Might be worth checking whether ConsistOf has the same problem.

@pohly
Copy link
Author

pohly commented Jul 19, 2024

ConsistOf works:

go test .
--- FAIL: TestHaveExactElements (0.00s)
    foo_test.go:13: 
        Expected
            <[]string | len:1, cap:1>: ["test"]
        to consist of
            <[]interface {} | len:0, cap:0>: []
        the extra elements were
            <[]string | len:1, cap:1>: ["test"]
FAIL
FAIL	k8s.io/kubernetes/foo	0.006s
FAIL

BeEmpty produces better output:

go test .
--- FAIL: TestHaveExactElements (0.00s)
    foo_test.go:13: 
        Expected
            <[]string | len:1, cap:1>: ["test"]
        to be empty
FAIL
FAIL	k8s.io/kubernetes/foo	0.006s
FAIL

pohly added a commit to pohly/kubernetes that referenced this issue Jul 19, 2024
Relax checking of results: the order does not matter.
This also avoids onsi/gomega#771
@onsi onsi closed this as completed in 878940c Jul 25, 2024
@onsi
Copy link
Owner

onsi commented Jul 25, 2024

alrighty i've fixed this now. will cut a release once CI goes green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants