4 . "github.com/onsi/ginkgo"
5 . "github.com/onsi/gomega"
6 . "github.com/onsi/gomega/matchers"
9 var _ = Describe("ContainElement", func() {
10 Context("when passed a supported type", func() {
11 Context("and expecting a non-matcher", func() {
12 It("should do the right thing", func() {
13 Ω([2]int{1, 2}).Should(ContainElement(2))
14 Ω([2]int{1, 2}).ShouldNot(ContainElement(3))
16 Ω([]int{1, 2}).Should(ContainElement(2))
17 Ω([]int{1, 2}).ShouldNot(ContainElement(3))
19 Ω(map[string]int{"foo": 1, "bar": 2}).Should(ContainElement(2))
20 Ω(map[int]int{3: 1, 4: 2}).ShouldNot(ContainElement(3))
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", "c"}}
25 Ω(arr).Should(ContainElement(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}))
26 Ω(arr).ShouldNot(ContainElement(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"b", "c"}}))
30 Context("and expecting a matcher", func() {
31 It("should pass each element through the matcher", func() {
32 Ω([]int{1, 2, 3}).Should(ContainElement(BeNumerically(">=", 3)))
33 Ω([]int{1, 2, 3}).ShouldNot(ContainElement(BeNumerically(">", 3)))
34 Ω(map[string]int{"foo": 1, "bar": 2}).Should(ContainElement(BeNumerically(">=", 2)))
35 Ω(map[string]int{"foo": 1, "bar": 2}).ShouldNot(ContainElement(BeNumerically(">", 2)))
38 It("should power through even if the matcher ever fails", func() {
39 Ω([]interface{}{1, 2, "3", 4}).Should(ContainElement(BeNumerically(">=", 3)))
42 It("should fail if the matcher fails", func() {
43 actual := []interface{}{1, 2, "3", "4"}
44 success, err := (&ContainElementMatcher{Element: BeNumerically(">=", 3)}).Match(actual)
45 Ω(success).Should(BeFalse())
46 Ω(err).Should(HaveOccurred())
51 Context("when passed a correctly typed nil", func() {
52 It("should operate succesfully on the passed in value", func() {
54 Ω(nilSlice).ShouldNot(ContainElement(1))
56 var nilMap map[int]string
57 Ω(nilMap).ShouldNot(ContainElement("foo"))
61 Context("when passed an unsupported type", func() {
62 It("should error", func() {
63 success, err := (&ContainElementMatcher{Element: 0}).Match(0)
64 Ω(success).Should(BeFalse())
65 Ω(err).Should(HaveOccurred())
67 success, err = (&ContainElementMatcher{Element: 0}).Match("abc")
68 Ω(success).Should(BeFalse())
69 Ω(err).Should(HaveOccurred())
71 success, err = (&ContainElementMatcher{Element: 0}).Match(nil)
72 Ω(success).Should(BeFalse())
73 Ω(err).Should(HaveOccurred())