4 . "github.com/onsi/ginkgo"
5 . "github.com/onsi/gomega"
6 . "github.com/onsi/gomega/gstruct"
9 var _ = Describe("Slice", func() {
10 allElements := []string{"a", "b"}
11 missingElements := []string{"a"}
12 extraElements := []string{"a", "b", "c"}
13 duplicateElements := []string{"a", "a", "b"}
17 It("should strictly match all elements", func() {
18 m := MatchAllElements(id, Elements{
22 Ω(allElements).Should(m, "should match all elements")
23 Ω(missingElements).ShouldNot(m, "should fail with missing elements")
24 Ω(extraElements).ShouldNot(m, "should fail with extra elements")
25 Ω(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
26 Ω(nils).ShouldNot(m, "should fail with an uninitialized slice")
28 m = MatchAllElements(id, Elements{
32 Ω(allElements).ShouldNot(m, "should run nested matchers")
34 m = MatchAllElements(id, Elements{})
35 Ω(empty).Should(m, "should handle empty slices")
36 Ω(allElements).ShouldNot(m, "should handle only empty slices")
37 Ω(nils).Should(m, "should handle nil slices")
40 It("should ignore extra elements", func() {
41 m := MatchElements(id, IgnoreExtras, Elements{
45 Ω(allElements).Should(m, "should match all elements")
46 Ω(missingElements).ShouldNot(m, "should fail with missing elements")
47 Ω(extraElements).Should(m, "should ignore extra elements")
48 Ω(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
49 Ω(nils).ShouldNot(m, "should fail with an uninitialized slice")
52 It("should ignore missing elements", func() {
53 m := MatchElements(id, IgnoreMissing, Elements{
57 Ω(allElements).Should(m, "should match all elements")
58 Ω(missingElements).Should(m, "should ignore missing elements")
59 Ω(extraElements).ShouldNot(m, "should fail with extra elements")
60 Ω(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
61 Ω(nils).Should(m, "should ignore an uninitialized slice")
64 It("should ignore missing and extra elements", func() {
65 m := MatchElements(id, IgnoreMissing|IgnoreExtras, Elements{
69 Ω(allElements).Should(m, "should match all elements")
70 Ω(missingElements).Should(m, "should ignore missing elements")
71 Ω(extraElements).Should(m, "should ignore extra elements")
72 Ω(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
73 Ω(nils).Should(m, "should ignore an uninitialized slice")
75 m = MatchElements(id, IgnoreExtras|IgnoreMissing, Elements{
79 Ω(allElements).ShouldNot(m, "should run nested matchers")
82 Context("with elements that share a key", func() {
83 nonUniqueID := func(element interface{}) string {
84 return element.(string)[0:1]
87 allElements := []string{"a123", "a213", "b321"}
88 includingBadElements := []string{"a123", "b123", "b5555"}
89 extraElements := []string{"a123", "b1234", "c345"}
90 missingElements := []string{"b123", "b1234", "b1345"}
92 It("should strictly allow multiple matches", func() {
93 m := MatchElements(nonUniqueID, AllowDuplicates, Elements{
94 "a": ContainSubstring("1"),
95 "b": ContainSubstring("1"),
97 Ω(allElements).Should(m, "should match all elements")
98 Ω(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
99 Ω(extraElements).ShouldNot(m, "should reject with extra keys")
100 Ω(missingElements).ShouldNot(m, "should reject with missing keys")
101 Ω(nils).ShouldNot(m, "should fail with an uninitialized slice")
104 It("should ignore missing", func() {
105 m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreMissing, Elements{
106 "a": ContainSubstring("1"),
107 "b": ContainSubstring("1"),
109 Ω(allElements).Should(m, "should match all elements")
110 Ω(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
111 Ω(extraElements).ShouldNot(m, "should reject with extra keys")
112 Ω(missingElements).Should(m, "should allow missing keys")
113 Ω(nils).Should(m, "should allow an uninitialized slice")
116 It("should ignore extras", func() {
117 m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreExtras, Elements{
118 "a": ContainSubstring("1"),
119 "b": ContainSubstring("1"),
121 Ω(allElements).Should(m, "should match all elements")
122 Ω(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
123 Ω(extraElements).Should(m, "should allow extra keys")
124 Ω(missingElements).ShouldNot(m, "should reject missing keys")
125 Ω(nils).ShouldNot(m, "should reject an uninitialized slice")
128 It("should ignore missing and extras", func() {
129 m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreExtras|IgnoreMissing, Elements{
130 "a": ContainSubstring("1"),
131 "b": ContainSubstring("1"),
133 Ω(allElements).Should(m, "should match all elements")
134 Ω(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
135 Ω(extraElements).Should(m, "should allow extra keys")
136 Ω(missingElements).Should(m, "should allow missing keys")
137 Ω(nils).Should(m, "should allow an uninitialized slice")
142 func id(element interface{}) string {
143 return element.(string)