/
contain_elements_matcher_test.go
152 lines (127 loc) · 5.89 KB
/
contain_elements_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("ContainElements", func() {
Context("with a slice", func() {
It("should do the right thing", func() {
Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("foo", "bar", "baz"))
Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("bar"))
Expect([]string{"foo", "bar", "baz"}).Should(ContainElements())
Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements("baz", "bar", "foo", "foo"))
})
})
Context("with an array", func() {
It("should do the right thing", func() {
Expect([3]string{"foo", "bar", "baz"}).Should(ContainElements("foo", "bar", "baz"))
Expect([3]string{"foo", "bar", "baz"}).Should(ContainElements("bar"))
Expect([3]string{"foo", "bar", "baz"}).Should(ContainElements())
Expect([3]string{"foo", "bar", "baz"}).ShouldNot(ContainElements("baz", "bar", "foo", "foo"))
})
})
Context("with a map", func() {
It("should apply to the values", func() {
Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ContainElements("foo", "bar", "baz"))
Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ContainElements("bar"))
Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ContainElements())
Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).ShouldNot(ContainElements("baz", "bar", "foo", "foo"))
})
})
Context("with anything else", func() {
It("should error", func() {
failures := InterceptGomegaFailures(func() {
Expect("foo").Should(ContainElements("f", "o", "o"))
})
Expect(failures).Should(HaveLen(1))
})
})
Context("when passed matchers", func() {
It("should pass if the matchers pass", func() {
Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("foo", MatchRegexp("^ba"), "baz"))
Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("foo", MatchRegexp("^ba")))
Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements("foo", MatchRegexp("^ba"), MatchRegexp("foo")))
Expect([]string{"foo", "bar", "baz"}).Should(ContainElements("foo", MatchRegexp("^ba"), MatchRegexp("^ba")))
Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements("foo", MatchRegexp("^ba"), MatchRegexp("turducken")))
})
It("should not depend on the order of the matchers", func() {
Expect([][]int{{1, 2}, {2}}).Should(ContainElements(ContainElement(1), ContainElement(2)))
Expect([][]int{{1, 2}, {2}}).Should(ContainElements(ContainElement(2), ContainElement(1)))
})
Context("when a matcher errors", func() {
It("should soldier on", func() {
Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements(BeFalse(), "foo", "bar"))
Expect([]interface{}{"foo", "bar", false}).Should(ContainElements(BeFalse(), ContainSubstring("foo"), "bar"))
})
})
})
Context("when passed exactly one argument, and that argument is a slice", func() {
It("should match against the elements of that argument", func() {
Expect([]string{"foo", "bar", "baz"}).Should(ContainElements([]string{"foo", "baz"}))
Expect([]string{"foo", "bar", "baz"}).ShouldNot(ContainElements([]string{"foo", "nope"}))
})
})
Describe("FailureMessage", func() {
It("prints missing elements", func() {
failures := InterceptGomegaFailures(func() {
Expect([]int{2}).Should(ContainElements(1, 2, 3))
})
expected := "Expected\n.*\\[2\\]\nto contain elements\n.*\\[1, 2, 3\\]\nthe missing elements were\n.*\\[1, 3\\]"
Expect(failures).To(ContainElements(MatchRegexp(expected)))
})
When("expected was specified as an array", func() {
It("flattens the array in the expectation message", func() {
failures := InterceptGomegaFailures(func() {
Expect([]string{"A", "B", "C"}).To(ContainElements([]string{"A", "D"}))
})
expected := `Expected\n.*\["A", "B", "C"\]\nto contain elements\n.*: \["A", "D"\]\nthe missing elements were\n.*\["D"\]`
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})
It("flattens the array in the negated expectation message", func() {
failures := InterceptGomegaFailures(func() {
Expect([]string{"A", "B"}).NotTo(ContainElements([]string{"A", "B"}))
})
expected := `Expected\n.*\["A", "B"\]\nnot to contain elements\n.*: \["A", "B"\]`
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})
})
When("the expected values are the same type", func() {
It("uses that type for the expectation slice", func() {
failures := InterceptGomegaFailures(func() {
Expect([]string{"A", "B"}).To(ContainElements("A", "B", "C"))
})
expected := `to contain elements
\s*<\[\]string \| len:3, cap:3>: \["A", "B", "C"\]
the missing elements were
\s*<\[\]string \| len:1, cap:1>: \["C"\]`
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})
It("uses that type for the negated expectation slice", func() {
failures := InterceptGomegaFailures(func() {
Expect([]uint64{1, 2}).NotTo(ContainElements(uint64(1), uint64(2)))
})
expected := `not to contain elements\n\s*<\[\]uint64 \| len:2, cap:2>: \[1, 2\]`
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})
})
When("the expected values are different types", func() {
It("uses interface{} for the expectation slice", func() {
failures := InterceptGomegaFailures(func() {
Expect([]interface{}{1, true}).To(ContainElements(1, "C"))
})
expected := `to contain elements
\s*<\[\]interface {} \| len:2, cap:2>: \[<int>1, <string>"C"\]
the missing elements were
\s*<\[\]string \| len:1, cap:1>: \["C"\]`
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})
It("uses interface{} for the negated expectation slice", func() {
failures := InterceptGomegaFailures(func() {
Expect([]interface{}{1, "B"}).NotTo(ContainElements(1, "B"))
})
expected := `not to contain elements\n\s*<\[\]interface {} \| len:2, cap:2>: \[<int>1, <string>"B"\]`
Expect(failures).To(ConsistOf(MatchRegexp(expected)))
})
})
})
})