-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslicer_test.go
329 lines (321 loc) · 6.91 KB
/
slicer_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright 2020 VMware, Inc.
* Copyright 2023 SteelBridgeLabs, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*
* Changes:
* - Changed package name from github.com/vmware-labs/yamlpath to github.com/SteelBridgeLabs/jsonpath
* - Removed YAML implementation and added JSON implementation
*/
package jsonpath
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSlicer(t *testing.T) {
cases := []struct {
name string
index string
length int
expected []int
expectedErr string
focus bool // if true, run only tests with focus set to true
}{
{
name: "index",
index: "3",
length: 10,
expected: []int{3},
expectedErr: "",
},
{
name: "range",
index: "1:3",
length: 10,
expected: []int{1, 2},
expectedErr: "",
},
{
name: "range with step",
index: "1:6:2",
length: 10,
expected: []int{1, 3, 5},
expectedErr: "",
},
{
name: "wildcard",
index: "*",
length: 4,
expected: []int{0, 1, 2, 3},
expectedErr: "",
},
{
name: "range with everything omitted, short form",
index: ":",
length: 4,
expected: []int{0, 1, 2, 3},
expectedErr: "",
}, {
name: "range with everything omitted, long form",
index: "::",
length: 4,
expected: []int{0, 1, 2, 3},
expectedErr: "",
},
{
name: "range with start omitted",
index: ":2",
length: 10,
expected: []int{0, 1},
expectedErr: "",
},
{
name: "range with start and end omitted",
index: "::2",
length: 10,
expected: []int{0, 2, 4, 6, 8},
expectedErr: "",
},
{
name: "last index",
index: "-1",
length: 4,
expected: []int{3},
expectedErr: "",
},
{
name: "overflowed index",
index: "4",
length: 4,
expected: []int{},
expectedErr: "",
},
{
name: "underflowed index",
index: "-5",
length: 4,
expected: []int{},
expectedErr: "",
},
{
name: "negative step with default start and end",
index: "::-1",
length: 4,
expected: []int{3, 2, 1, 0},
expectedErr: "",
},
{
name: "negative step with default start",
index: ":0:-1",
length: 4,
expected: []int{3, 2, 1},
expectedErr: "",
},
{
name: "negative step with default end",
index: "2::-1",
length: 4,
expected: []int{2, 1, 0},
expectedErr: "",
},
{
name: "larger negative step",
index: "::-2",
length: 4,
expected: []int{3, 1},
expectedErr: "",
},
{
name: "negative range with default step",
index: "-1:-3",
length: 10,
expected: []int{},
expectedErr: "",
},
{
name: "negative range with negative step",
index: "-1:-3:-1",
length: 10,
expected: []int{9, 8},
expectedErr: "",
},
{
name: "negative range with larger negative step",
index: "-1:-6:-2",
length: 10,
expected: []int{9, 7, 5},
expectedErr: "",
},
{
name: "larger negative range with larger negative step",
index: "-1:-7:-2",
length: 10,
expected: []int{9, 7, 5},
expectedErr: "",
},
{
name: "negative from, positive to",
index: "-5:7",
length: 10,
expected: []int{5, 6},
expectedErr: "",
},
{
name: "negative from",
index: "-2:",
length: 10,
expected: []int{8, 9},
expectedErr: "",
},
{
name: "positive from, negative to",
index: "1:-1",
length: 10,
expected: []int{1, 2, 3, 4, 5, 6, 7, 8},
expectedErr: "",
},
{
name: "negative from, positive to, negative step",
index: "-1:1:-1",
length: 10,
expected: []int{9, 8, 7, 6, 5, 4, 3, 2},
expectedErr: "",
},
{
name: "positive from, negative to, negative step",
index: "7:-5:-1",
length: 10,
expected: []int{7, 6},
expectedErr: "",
},
{
name: "too many colons",
index: "1:2:3:4",
length: 10,
expectedErr: "malformed array index, too many colons",
},
{
name: "non-integer array index",
index: "1:2:a",
length: 10,
expectedErr: "non-integer array index",
},
{
name: "zero step",
index: "1:2:0",
length: 10,
expectedErr: "array index step value must be non-zero",
},
{
name: "empty range",
index: "2:2",
length: 10,
expected: []int{},
},
{
name: "union",
index: "0,2",
length: 10,
expected: []int{0, 2},
},
{
name: "union with whitespace",
index: " 0 , 1 ",
length: 10,
expected: []int{0, 1},
},
{
name: "union with duplicated results (deviation from comparison project consensus)",
index: "1,0,1",
length: 3,
expected: []int{1, 0, 1},
expectedErr: "",
},
{
name: "union with wildcard and index",
index: "*,1",
length: 3,
expectedErr: "error in union member 0: wildcard cannot be used in union",
},
{
name: "default indices with empty array",
index: ":",
length: 0,
expected: []int{},
},
{
name: "negative step with empty array",
index: "::-1",
length: 0,
expected: []int{},
},
{
name: "empty string",
index: "",
length: 10,
expectedErr: "array index missing",
},
{
name: "maximal range with positive step",
index: "0:10",
length: 10,
expected: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
},
{
name: "maximal range with negative step",
index: "9:0:-1",
length: 10,
expected: []int{9, 8, 7, 6, 5, 4, 3, 2, 1},
},
{
name: "excessively large to value",
index: "2:113667776004",
length: 10,
expected: []int{2, 3, 4, 5, 6, 7, 8, 9},
},
{
name: "excessively small from value",
index: "-113667776004:1",
length: 10,
expected: []int{0},
},
{
name: "excessively large from value with negative step",
index: "113667776004:0:-1",
length: 10,
expected: []int{9, 8, 7, 6, 5, 4, 3, 2, 1},
},
{
name: "excessively small to value with negative step",
index: "3:-113667776004:-1",
length: 10,
expected: []int{3, 2, 1, 0},
},
}
focussed := false
for _, tc := range cases {
if tc.focus {
focussed = true
break
}
}
for _, tc := range cases {
if focussed && !tc.focus {
continue
}
actual, err := slice(tc.index, tc.length)
t.Run(tc.name, func(t *testing.T) {
if tc.expectedErr == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.expectedErr)
return
}
require.Equal(t, tc.expected, actual)
})
}
if focussed {
t.Fatalf("testcase(s) still focussed")
}
}