added support for string type
[govpp.git] / vendor / github.com / onsi / gomega / gstruct / elements_test.go
1 package gstruct_test
2
3 import (
4         . "github.com/onsi/ginkgo"
5         . "github.com/onsi/gomega"
6         . "github.com/onsi/gomega/gstruct"
7 )
8
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"}
14         empty := []string{}
15         var nils []string
16
17         It("should strictly match all elements", func() {
18                 m := MatchAllElements(id, Elements{
19                         "b": Equal("b"),
20                         "a": Equal("a"),
21                 })
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")
27
28                 m = MatchAllElements(id, Elements{
29                         "a": Equal("a"),
30                         "b": Equal("fail"),
31                 })
32                 Ω(allElements).ShouldNot(m, "should run nested matchers")
33
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")
38         })
39
40         It("should ignore extra elements", func() {
41                 m := MatchElements(id, IgnoreExtras, Elements{
42                         "b": Equal("b"),
43                         "a": Equal("a"),
44                 })
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")
50         })
51
52         It("should ignore missing elements", func() {
53                 m := MatchElements(id, IgnoreMissing, Elements{
54                         "a": Equal("a"),
55                         "b": Equal("b"),
56                 })
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")
62         })
63
64         It("should ignore missing and extra elements", func() {
65                 m := MatchElements(id, IgnoreMissing|IgnoreExtras, Elements{
66                         "a": Equal("a"),
67                         "b": Equal("b"),
68                 })
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")
74
75                 m = MatchElements(id, IgnoreExtras|IgnoreMissing, Elements{
76                         "a": Equal("a"),
77                         "b": Equal("fail"),
78                 })
79                 Ω(allElements).ShouldNot(m, "should run nested matchers")
80         })
81
82         Context("with elements that share a key", func() {
83                 nonUniqueID := func(element interface{}) string {
84                         return element.(string)[0:1]
85                 }
86
87                 allElements := []string{"a123", "a213", "b321"}
88                 includingBadElements := []string{"a123", "b123", "b5555"}
89                 extraElements := []string{"a123", "b1234", "c345"}
90                 missingElements := []string{"b123", "b1234", "b1345"}
91
92                 It("should strictly allow multiple matches", func() {
93                         m := MatchElements(nonUniqueID, AllowDuplicates, Elements{
94                                 "a": ContainSubstring("1"),
95                                 "b": ContainSubstring("1"),
96                         })
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")
102                 })
103
104                 It("should ignore missing", func() {
105                         m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreMissing, Elements{
106                                 "a": ContainSubstring("1"),
107                                 "b": ContainSubstring("1"),
108                         })
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")
114                 })
115
116                 It("should ignore extras", func() {
117                         m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreExtras, Elements{
118                                 "a": ContainSubstring("1"),
119                                 "b": ContainSubstring("1"),
120                         })
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")
126                 })
127
128                 It("should ignore missing and extras", func() {
129                         m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreExtras|IgnoreMissing, Elements{
130                                 "a": ContainSubstring("1"),
131                                 "b": ContainSubstring("1"),
132                         })
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")
138                 })
139         })
140 })
141
142 func id(element interface{}) string {
143         return element.(string)
144 }