OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / stretchr / testify / vendor / github.com / stretchr / objx / codegen / template.txt
1 /*
2         {4} ({1} and []{1})
3         --------------------------------------------------
4 */
5
6 // {4} gets the value as a {1}, returns the optionalDefault
7 // value or a system default object if the value is the wrong type.
8 func (v *Value) {4}(optionalDefault ...{1}) {1} {
9         if s, ok := v.data.({1}); ok {
10                 return s
11         }
12         if len(optionalDefault) == 1 {
13                 return optionalDefault[0]
14         }
15         return {3}
16 }
17
18 // Must{4} gets the value as a {1}.
19 //
20 // Panics if the object is not a {1}.
21 func (v *Value) Must{4}() {1} {
22         return v.data.({1})
23 }
24
25 // {4}Slice gets the value as a []{1}, returns the optionalDefault
26 // value or nil if the value is not a []{1}.
27 func (v *Value) {4}Slice(optionalDefault ...[]{1}) []{1} {
28         if s, ok := v.data.([]{1}); ok {
29                 return s
30         }
31         if len(optionalDefault) == 1 {
32                 return optionalDefault[0]
33         }
34         return nil
35 }
36
37 // Must{4}Slice gets the value as a []{1}.
38 //
39 // Panics if the object is not a []{1}.
40 func (v *Value) Must{4}Slice() []{1} {
41         return v.data.([]{1})
42 }
43
44 // Is{4} gets whether the object contained is a {1} or not.
45 func (v *Value) Is{4}() bool {
46   _, ok := v.data.({1})
47   return ok
48 }
49
50 // Is{4}Slice gets whether the object contained is a []{1} or not.
51 func (v *Value) Is{4}Slice() bool {
52         _, ok := v.data.([]{1})
53         return ok
54 }
55
56 // Each{4} calls the specified callback for each object
57 // in the []{1}.
58 //
59 // Panics if the object is the wrong type.
60 func (v *Value) Each{4}(callback func(int, {1}) bool) *Value {
61
62         for index, val := range v.Must{4}Slice() {
63                 carryon := callback(index, val)
64                 if carryon == false {
65                         break
66                 }
67         }
68
69         return v
70
71 }
72
73 // Where{4} uses the specified decider function to select items
74 // from the []{1}.  The object contained in the result will contain
75 // only the selected items.
76 func (v *Value) Where{4}(decider func(int, {1}) bool) *Value {
77
78         var selected []{1}
79
80         v.Each{4}(func(index int, val {1}) bool {
81                 shouldSelect := decider(index, val)
82                 if shouldSelect == false {
83                         selected = append(selected, val)
84                 }
85                 return true
86         })
87
88         return &Value{data:selected}
89
90 }
91
92 // Group{4} uses the specified grouper function to group the items
93 // keyed by the return of the grouper.  The object contained in the
94 // result will contain a map[string][]{1}.
95 func (v *Value) Group{4}(grouper func(int, {1}) string) *Value {
96
97         groups := make(map[string][]{1})
98
99         v.Each{4}(func(index int, val {1}) bool {
100                 group := grouper(index, val)
101                 if _, ok := groups[group]; !ok {
102                         groups[group] = make([]{1}, 0)
103                 }
104                 groups[group] = append(groups[group], val)
105                 return true
106         })
107
108         return &Value{data:groups}
109
110 }
111
112 // Replace{4} uses the specified function to replace each {1}s
113 // by iterating each item.  The data in the returned result will be a
114 // []{1} containing the replaced items.
115 func (v *Value) Replace{4}(replacer func(int, {1}) {1}) *Value {
116
117         arr := v.Must{4}Slice()
118         replaced := make([]{1}, len(arr))
119
120         v.Each{4}(func(index int, val {1}) bool {
121                 replaced[index] = replacer(index, val)
122                 return true
123         })
124
125         return &Value{data:replaced}
126
127 }
128
129 // Collect{4} uses the specified collector function to collect a value
130 // for each of the {1}s in the slice.  The data returned will be a
131 // []interface{}.
132 func (v *Value) Collect{4}(collector func(int, {1}) interface{}) *Value {
133
134         arr := v.Must{4}Slice()
135         collected := make([]interface{}, len(arr))
136
137         v.Each{4}(func(index int, val {1}) bool {
138                 collected[index] = collector(index, val)
139                 return true
140         })
141
142         return &Value{data:collected}
143 }
144
145 // ************************************************************
146 // TESTS
147 // ************************************************************
148
149 func Test{4}(t *testing.T) {
150
151   val := {1}( {2} )
152         m := map[string]interface{}{"value": val, "nothing": nil}
153         assert.Equal(t, val, New(m).Get("value").{4}())
154         assert.Equal(t, val, New(m).Get("value").Must{4}())
155         assert.Equal(t, {1}({3}), New(m).Get("nothing").{4}())
156         assert.Equal(t, val, New(m).Get("nothing").{4}({2}))
157
158         assert.Panics(t, func() {
159                 New(m).Get("age").Must{4}()
160         })
161
162 }
163
164 func Test{4}Slice(t *testing.T) {
165
166   val := {1}( {2} )
167         m := map[string]interface{}{"value": []{1}{ val }, "nothing": nil}
168         assert.Equal(t, val, New(m).Get("value").{4}Slice()[0])
169         assert.Equal(t, val, New(m).Get("value").Must{4}Slice()[0])
170         assert.Equal(t, []{1}(nil), New(m).Get("nothing").{4}Slice())
171         assert.Equal(t, val, New(m).Get("nothing").{4}Slice( []{1}{ {1}({2}) } )[0])
172
173         assert.Panics(t, func() {
174                 New(m).Get("nothing").Must{4}Slice()
175         })
176
177 }
178
179 func TestIs{4}(t *testing.T) {
180
181         var v *Value
182
183         v = &Value{data: {1}({2})}
184         assert.True(t, v.Is{4}())
185
186         v = &Value{data: []{1}{ {1}({2}) }}
187         assert.True(t, v.Is{4}Slice())
188
189 }
190
191 func TestEach{4}(t *testing.T) {
192
193         v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
194         count := 0
195         replacedVals := make([]{1}, 0)
196         assert.Equal(t, v, v.Each{4}(func(i int, val {1}) bool {
197
198                 count++
199                 replacedVals = append(replacedVals, val)
200
201                 // abort early
202                 if i == 2 {
203                         return false
204                 }
205
206                 return true
207
208         }))
209
210         assert.Equal(t, count, 3)
211         assert.Equal(t, replacedVals[0], v.Must{4}Slice()[0])
212         assert.Equal(t, replacedVals[1], v.Must{4}Slice()[1])
213         assert.Equal(t, replacedVals[2], v.Must{4}Slice()[2])
214
215 }
216
217 func TestWhere{4}(t *testing.T) {
218
219         v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
220
221         selected := v.Where{4}(func(i int, val {1}) bool {
222                 return i%2==0
223         }).Must{4}Slice()
224
225         assert.Equal(t, 3, len(selected))
226
227 }
228
229 func TestGroup{4}(t *testing.T) {
230
231         v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
232
233         grouped := v.Group{4}(func(i int, val {1}) string {
234                 return fmt.Sprintf("%v", i%2==0)
235         }).data.(map[string][]{1})
236
237         assert.Equal(t, 2, len(grouped))
238         assert.Equal(t, 3, len(grouped["true"]))
239         assert.Equal(t, 3, len(grouped["false"]))
240
241 }
242
243 func TestReplace{4}(t *testing.T) {
244
245         v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
246
247         rawArr := v.Must{4}Slice()
248
249         replaced := v.Replace{4}(func(index int, val {1}) {1} {
250                 if index < len(rawArr)-1 {
251                         return rawArr[index+1]
252                 }
253                 return rawArr[0]
254         })
255
256         replacedArr := replaced.Must{4}Slice()
257         if assert.Equal(t, 6, len(replacedArr)) {
258                 assert.Equal(t, replacedArr[0], rawArr[1])
259                 assert.Equal(t, replacedArr[1], rawArr[2])
260                 assert.Equal(t, replacedArr[2], rawArr[3])
261                 assert.Equal(t, replacedArr[3], rawArr[4])
262                 assert.Equal(t, replacedArr[4], rawArr[5])
263                 assert.Equal(t, replacedArr[5], rawArr[0])
264         }
265
266 }
267
268 func TestCollect{4}(t *testing.T) {
269
270         v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
271
272         collected := v.Collect{4}(func(index int, val {1}) interface{} {
273                 return index
274         })
275
276         collectedArr := collected.MustInterSlice()
277         if assert.Equal(t, 6, len(collectedArr)) {
278                 assert.Equal(t, collectedArr[0], 0)
279                 assert.Equal(t, collectedArr[1], 1)
280                 assert.Equal(t, collectedArr[2], 2)
281                 assert.Equal(t, collectedArr[3], 3)
282                 assert.Equal(t, collectedArr[4], 4)
283                 assert.Equal(t, collectedArr[5], 5)
284         }
285
286 }