forked from kubernetes-sigs/structured-merge-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoset_test.go
283 lines (271 loc) · 6.92 KB
/
toset_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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package typed_test
import (
"fmt"
"testing"
"sigs.k8s.io/structured-merge-diff/v3/fieldpath"
"sigs.k8s.io/structured-merge-diff/v3/typed"
"sigs.k8s.io/structured-merge-diff/v3/value"
)
type objSetPair struct {
object typed.YAMLObject
set *fieldpath.Set
}
type fieldsetTestCase struct {
name string
rootTypeName string
schema typed.YAMLObject
pairs []objSetPair
}
var (
// Short names for readable test cases.
_NS = fieldpath.NewSet
_P = fieldpath.MakePathOrDie
_KBF = fieldpath.KeyByFields
_V = value.NewValueInterface
)
var fieldsetCases = []fieldsetTestCase{{
name: "simple pair",
rootTypeName: "stringPair",
schema: `types:
- name: stringPair
map:
fields:
- name: key
type:
scalar: string
- name: value
type:
namedType: __untyped_atomic_
- name: __untyped_atomic_
scalar: untyped
list:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
map:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
`,
pairs: []objSetPair{
{`{"key":"foo","value":1}`, _NS(_P("key"), _P("value"))},
{`{"key":"foo","value":{"a": "b"}}`, _NS(_P("key"), _P("value"))},
{`{"key":"foo","value":null}`, _NS(_P("key"), _P("value"))},
{`{"key":"foo"}`, _NS(_P("key"))},
{`{"key":"foo","value":true}`, _NS(_P("key"), _P("value"))},
},
}, {
name: "struct grab bag",
rootTypeName: "myStruct",
schema: `types:
- name: myStruct
map:
fields:
- name: numeric
type:
scalar: numeric
- name: string
type:
scalar: string
- name: bool
type:
scalar: boolean
- name: setStr
type:
list:
elementType:
scalar: string
elementRelationship: associative
- name: setBool
type:
list:
elementType:
scalar: boolean
elementRelationship: associative
- name: setNumeric
type:
list:
elementType:
scalar: numeric
elementRelationship: associative
- name: color
type:
map:
fields:
- name: R
type:
scalar: numeric
- name: G
type:
scalar: numeric
- name: B
type:
scalar: numeric
elementRelationship: atomic
- name: arbitraryWavelengthColor
type:
map:
elementType:
scalar: numeric
elementRelationship: atomic
- name: args
type:
list:
elementType:
map:
fields:
- name: key
type:
scalar: string
- name: value
type:
scalar: string
elementRelationship: atomic
`,
pairs: []objSetPair{
{`{"numeric":1}`, _NS(_P("numeric"))},
{`{"numeric":3.14159}`, _NS(_P("numeric"))},
{`{"string":"aoeu"}`, _NS(_P("string"))},
{`{"bool":true}`, _NS(_P("bool"))},
{`{"bool":false}`, _NS(_P("bool"))},
{`{"setStr":["a","b","c"]}`, _NS(
_P("setStr", _V("a")),
_P("setStr", _V("b")),
_P("setStr", _V("c")),
)},
{`{"setBool":[true,false]}`, _NS(
_P("setBool", _V(true)),
_P("setBool", _V(false)),
)},
{`{"setNumeric":[1,2,3,3.14159]}`, _NS(
_P("setNumeric", _V(1)),
_P("setNumeric", _V(2)),
_P("setNumeric", _V(3)),
_P("setNumeric", _V(3.14159)),
)},
{`{"color":{}}`, _NS(_P("color"))},
{`{"color":null}`, _NS(_P("color"))},
{`{"color":{"R":255,"G":0,"B":0}}`, _NS(_P("color"))},
{`{"arbitraryWavelengthColor":{}}`, _NS(_P("arbitraryWavelengthColor"))},
{`{"arbitraryWavelengthColor":null}`, _NS(_P("arbitraryWavelengthColor"))},
{`{"arbitraryWavelengthColor":{"IR":255}}`, _NS(_P("arbitraryWavelengthColor"))},
{`{"args":[]}`, _NS(_P("args"))},
{`{"args":null}`, _NS(_P("args"))},
{`{"args":[null]}`, _NS(_P("args"))},
{`{"args":[{"key":"a","value":"b"},{"key":"c","value":"d"}]}`, _NS(_P("args"))},
},
}, {
name: "associative list",
rootTypeName: "myRoot",
schema: `types:
- name: myRoot
map:
fields:
- name: list
type:
namedType: myList
- name: atomicList
type:
namedType: mySequence
- name: myList
list:
elementType:
namedType: myElement
elementRelationship: associative
keys:
- key
- id
- name: mySequence
list:
elementType:
scalar: string
elementRelationship: atomic
- name: myElement
map:
fields:
- name: key
type:
scalar: string
- name: id
type:
scalar: numeric
- name: value
type:
namedType: myValue
- name: bv
type:
scalar: boolean
- name: nv
type:
scalar: numeric
- name: myValue
map:
elementType:
scalar: string
`,
pairs: []objSetPair{
{`{"list":[]}`, _NS()},
{`{"list":[{"key":"a","id":1,"value":{"a":"a"}}]}`, _NS(
_P("list", _KBF("key", "a", "id", 1)),
_P("list", _KBF("key", "a", "id", 1), "key"),
_P("list", _KBF("key", "a", "id", 1), "id"),
_P("list", _KBF("key", "a", "id", 1), "value", "a"),
)},
{`{"list":[{"key":"a","id":1},{"key":"a","id":2},{"key":"b","id":1}]}`, _NS(
_P("list", _KBF("key", "a", "id", 1)),
_P("list", _KBF("key", "a", "id", 2)),
_P("list", _KBF("key", "b", "id", 1)),
_P("list", _KBF("key", "a", "id", 1), "key"),
_P("list", _KBF("key", "a", "id", 1), "id"),
_P("list", _KBF("key", "a", "id", 2), "key"),
_P("list", _KBF("key", "a", "id", 2), "id"),
_P("list", _KBF("key", "b", "id", 1), "key"),
_P("list", _KBF("key", "b", "id", 1), "id"),
)},
{`{"atomicList":["a","a","a"]}`, _NS(_P("atomicList"))},
},
}}
func (tt fieldsetTestCase) test(t *testing.T) {
parser, err := typed.NewParser(tt.schema)
if err != nil {
t.Fatalf("failed to create schema: %v", err)
}
for i, v := range tt.pairs {
v := v
t.Run(fmt.Sprintf("%v-%v", tt.name, i), func(t *testing.T) {
t.Parallel()
tv, err := parser.Type(tt.rootTypeName).FromYAML(v.object)
if err != nil {
t.Errorf("failed to parse object: %v", err)
}
fs, err := tv.ToFieldSet()
if err != nil {
t.Fatalf("got validation errors: %v", err)
}
if !fs.Equals(v.set) {
t.Errorf("wanted\n%s\ngot\n%s\n", v.set, fs)
}
})
}
}
func TestToFieldSet(t *testing.T) {
for _, tt := range fieldsetCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
tt.test(t)
})
}
}