onsi / gomega

Ginkgo's Preferred Matcher Library
http://onsi.github.io/gomega/
MIT License
2.16k stars 282 forks source link

Expect to MatchFields across MatchElements #736

Closed dschveninger closed 7 months ago

dschveninger commented 7 months ago

I have a slice of a structure.

It("slice of a structure", func() {
    testData := []TestStructure{
        {Name: "John", Status: true, Address: "123 Main St"},
        {Name: "Jane", Status: true, Address: "456 Elm St"},
        {Name: "Bob", Status: false, Address: "789 Oak St"},
        {Name: "Alice", Status: false, Address: "101 Pine St"},
        {Name: "Mike", Status: true, Address: "202 Maple St"},
    }
    Expect(testData).To(Not(BeEmpty()), "The slice should not be empty")
    Expect(testData).To(Each(HaveField("Status", BeTrue())), "Each element's Status should be true")
})

and I want to make sure all Status are true. Is there a way to do this with one Expect?

I could not get this from https://[onsi.github.io/gomega/#putting-it-all-together-testing-complex-structures](https://onsi.github.io/gomega/#putting-it-all-together-testing-complex-structures).

Expect(testData).To(Each(HaveField("Status", BeTrue())), "Each element's Status should be true") was suggest by ChatGPT4, lol

thediveo commented 7 months ago

https://onsi.github.io/gomega/#haveeachelement-interface

dschveninger commented 7 months ago

@thediveo thank you to point to this.

Here is the example for others

var _ = Describe("GoMeaga Expects examples", Label("resource1"), func() {
    Context("SUCCESSFUL test", func() {
...
        It("slice of a structure", func() {
            testData := []TestStructure{
                {Name: "John", Status: true, Address: "123 Main St"},
                {Name: "Jane", Status: true, Address: "456 Elm St"},
                {Name: "Bob", Status: true, Address: "789 Oak St"},
                {Name: "Alice", Status: true, Address: "101 Pine St"},
                {Name: "Mike", Status: true, Address: "202 Maple St"},
            }
            Expect(testData).To(Not(BeEmpty()), "The slice should not be empty")
            Expect(testData).Should(HaveEach(MatchFields(IgnoreExtras, Fields{
                "Status":    Equal(true),
            })))
        })
    })
    Context("FAILING test", func() {
...
               /*
        This show you how to check a slice of a structure type but the message and an example of a status message to help review the failure
        */
        It("slice of a structure", func() {
            testData := []TestStructure{
                {Name: "John", Status: true, Address: "123 Main St"},
                {Name: "Jane", Status: true, Address: "456 Elm St"},
                {Name: "Bob", Status: true, Address: "789 Oak St"},
                {Name: "Alice", Status: true, Address: "101 Pine St"},
                {Name: "Mike", Status: false, Address: "202 Maple St"},
            }
            Expect(testData).To(Not(BeEmpty()), "The slice should not be empty")
            Expect(testData).Should(HaveEach(MatchFields(IgnoreExtras, Fields{
                "Status":    Equal(true),
            })), "At least one structure in the slice Status: is false")
        })
    })
})
thediveo commented 7 months ago

In your case HaveField("Status", BeTrue()) might be shorter and better readable compared to gstruct.