...

Source file src/github.com/onsi/gomega/matchers/have_each_matcher_test.go

Documentation: github.com/onsi/gomega/matchers

     1  package matchers_test
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo/v2"
     5  	. "github.com/onsi/gomega"
     6  	. "github.com/onsi/gomega/matchers"
     7  )
     8  
     9  var _ = Describe("HaveEach", func() {
    10  	When("passed a supported type", func() {
    11  		Context("and expecting a non-matcher", func() {
    12  			It("should do the right thing", func() {
    13  				Expect([2]int{2, 2}).Should(HaveEach(2))
    14  				Expect([2]int{2, 3}).ShouldNot(HaveEach(3))
    15  
    16  				Expect([]int{2, 2}).Should(HaveEach(2))
    17  				Expect([]int{1, 2}).ShouldNot(HaveEach(3))
    18  
    19  				Expect(map[string]int{"foo": 2, "bar": 2}).Should(HaveEach(2))
    20  				Expect(map[int]int{3: 3, 4: 2}).ShouldNot(HaveEach(3))
    21  
    22  				arr := make([]myCustomType, 2)
    23  				arr[0] = myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}
    24  				arr[1] = myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}
    25  				Expect(arr).Should(HaveEach(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}))
    26  				Expect(arr).ShouldNot(HaveEach(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"b", "c"}}))
    27  
    28  				// ...and finaaaaaly, let's eat our own documentation ;)
    29  				Expect([]string{"Foo", "FooBar"}).Should(HaveEach(ContainSubstring("Foo")))
    30  				Expect([]string{"Foo", "FooBar"}).ShouldNot(HaveEach(ContainSubstring("Bar")))
    31  			})
    32  		})
    33  
    34  		Context("and expecting a matcher", func() {
    35  			It("should pass each element through the matcher", func() {
    36  				Expect([]int{1, 2, 3}).Should(HaveEach(BeNumerically(">=", 1)))
    37  				Expect([]int{1, 2, 3}).ShouldNot(HaveEach(BeNumerically(">", 1)))
    38  				Expect(map[string]int{"foo": 1, "bar": 2}).Should(HaveEach(BeNumerically(">=", 1)))
    39  				Expect(map[string]int{"foo": 1, "bar": 2}).ShouldNot(HaveEach(BeNumerically(">=", 2)))
    40  			})
    41  
    42  			It("should not power through if the matcher ever fails", func() {
    43  				actual := []interface{}{1, 2, "3", 4}
    44  				success, err := (&HaveEachMatcher{Element: BeNumerically(">=", 1)}).Match(actual)
    45  				Expect(success).Should(BeFalse())
    46  				Expect(err).Should(HaveOccurred())
    47  			})
    48  
    49  			It("should fail if the matcher fails", func() {
    50  				actual := []interface{}{1, 2, "3", "4"}
    51  				success, err := (&HaveEachMatcher{Element: BeNumerically(">=", 1)}).Match(actual)
    52  				Expect(success).Should(BeFalse())
    53  				Expect(err).Should(HaveOccurred())
    54  			})
    55  		})
    56  	})
    57  
    58  	When("passed an empty supported type or correctly typed nil", func() {
    59  		It("should error", func() {
    60  			success, err := (&HaveEachMatcher{Element: []int{}}).Match(42)
    61  			Expect(success).Should(BeFalse())
    62  			Expect(err).Should(HaveOccurred())
    63  
    64  			var nilSlice []int
    65  			success, err = (&HaveEachMatcher{Element: nilSlice}).Match(1)
    66  			Expect(success).Should(BeFalse())
    67  			Expect(err).Should(HaveOccurred())
    68  
    69  			var nilMap map[int]string
    70  			success, err = (&HaveEachMatcher{Element: nilMap}).Match(1)
    71  			Expect(success).Should(BeFalse())
    72  			Expect(err).Should(HaveOccurred())
    73  
    74  			// again, we eat our own documentation food here...
    75  			Expect([]int{}).To(Or(BeEmpty(), HaveEach(42)))
    76  			Expect([]int{1}).NotTo(Or(BeEmpty(), HaveEach(42)))
    77  		})
    78  	})
    79  
    80  	When("passed an unsupported type", func() {
    81  		It("should error", func() {
    82  			success, err := (&HaveEachMatcher{Element: 0}).Match(0)
    83  			Expect(success).Should(BeFalse())
    84  			Expect(err).Should(HaveOccurred())
    85  
    86  			success, err = (&HaveEachMatcher{Element: 0}).Match("abc")
    87  			Expect(success).Should(BeFalse())
    88  			Expect(err).Should(HaveOccurred())
    89  
    90  			success, err = (&HaveEachMatcher{Element: 0}).Match(nil)
    91  			Expect(success).Should(BeFalse())
    92  			Expect(err).Should(HaveOccurred())
    93  		})
    94  	})
    95  })
    96  

View as plain text