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