...

Source file src/github.com/onsi/gomega/matchers/be_element_of_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("BeElementOf", func() {
    10  	When("passed a supported type", func() {
    11  		It("should do the right thing", func() {
    12  			Expect(2).Should(BeElementOf([2]int{1, 2}))
    13  			Expect(3).ShouldNot(BeElementOf([2]int{1, 2}))
    14  
    15  			Expect(2).Should(BeElementOf([]int{1, 2}))
    16  			Expect(3).ShouldNot(BeElementOf([]int{1, 2}))
    17  
    18  			Expect(2).Should(BeElementOf(1, 2))
    19  			Expect(3).ShouldNot(BeElementOf(1, 2))
    20  
    21  			Expect("abc").Should(BeElementOf("abc"))
    22  			Expect("abc").ShouldNot(BeElementOf("def"))
    23  
    24  			Expect("abc").ShouldNot(BeElementOf())
    25  			Expect(7).ShouldNot(BeElementOf(nil))
    26  
    27  			arr := make([]myCustomType, 2)
    28  			arr[0] = myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}
    29  			arr[1] = myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "c"}}
    30  			Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).Should(BeElementOf(arr))
    31  			Expect(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"b", "c"}}).ShouldNot(BeElementOf(arr))
    32  		})
    33  	})
    34  
    35  	When("passed a correctly typed nil", func() {
    36  		It("should operate successfully on the passed in value", func() {
    37  			var nilSlice []int
    38  			Expect(1).ShouldNot(BeElementOf(nilSlice))
    39  
    40  			var nilMap map[int]string
    41  			Expect("foo").ShouldNot(BeElementOf(nilMap))
    42  		})
    43  	})
    44  
    45  	When("passed an unsupported type", func() {
    46  		It("should error", func() {
    47  			success, err := (&BeElementOfMatcher{Elements: []interface{}{0}}).Match(nil)
    48  			Expect(success).Should(BeFalse())
    49  			Expect(err).Should(HaveOccurred())
    50  
    51  			success, err = (&BeElementOfMatcher{Elements: nil}).Match(nil)
    52  			Expect(success).Should(BeFalse())
    53  			Expect(err).Should(HaveOccurred())
    54  		})
    55  	})
    56  
    57  	It("builds failure message", func() {
    58  		actual := BeElementOf(1, 2).FailureMessage(123)
    59  		Expect(actual).To(Equal("Expected\n    <int>: 123\nto be an element of\n    <[]int | len:2, cap:2>: [1, 2]"))
    60  	})
    61  
    62  	It("builds negated failure message", func() {
    63  		actual := BeElementOf(1, 2).NegatedFailureMessage(123)
    64  		Expect(actual).To(Equal("Expected\n    <int>: 123\nnot to be an element of\n    <[]int | len:2, cap:2>: [1, 2]"))
    65  	})
    66  })
    67  

View as plain text