initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / contain_element_matcher_test.go
1 package matchers_test
2
3 import (
4         . "github.com/onsi/ginkgo"
5         . "github.com/onsi/gomega"
6         . "github.com/onsi/gomega/matchers"
7 )
8
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))
15
16                                 Ω([]int{1, 2}).Should(ContainElement(2))
17                                 Ω([]int{1, 2}).ShouldNot(ContainElement(3))
18
19                                 Ω(map[string]int{"foo": 1, "bar": 2}).Should(ContainElement(2))
20                                 Ω(map[int]int{3: 1, 4: 2}).ShouldNot(ContainElement(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", "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"}}))
27                         })
28                 })
29
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)))
36                         })
37
38                         It("should power through even if the matcher ever fails", func() {
39                                 Ω([]interface{}{1, 2, "3", 4}).Should(ContainElement(BeNumerically(">=", 3)))
40                         })
41
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())
47                         })
48                 })
49         })
50
51         Context("when passed a correctly typed nil", func() {
52                 It("should operate succesfully on the passed in value", func() {
53                         var nilSlice []int
54                         Ω(nilSlice).ShouldNot(ContainElement(1))
55
56                         var nilMap map[int]string
57                         Ω(nilMap).ShouldNot(ContainElement("foo"))
58                 })
59         })
60
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())
66
67                         success, err = (&ContainElementMatcher{Element: 0}).Match("abc")
68                         Ω(success).Should(BeFalse())
69                         Ω(err).Should(HaveOccurred())
70
71                         success, err = (&ContainElementMatcher{Element: 0}).Match(nil)
72                         Ω(success).Should(BeFalse())
73                         Ω(err).Should(HaveOccurred())
74                 })
75         })
76 })