forked from invopop/yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaml_test.go
More file actions
617 lines (552 loc) · 14.4 KB
/
yaml_test.go
File metadata and controls
617 lines (552 loc) · 14.4 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package yaml
import (
"fmt"
"math"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
)
type MarshalTest struct {
A string
B int64
C float32
D float64
}
func TestMarshal(t *testing.T) {
f32String := strconv.FormatFloat(math.MaxFloat32, 'g', -1, 32)
f64String := strconv.FormatFloat(math.MaxFloat64, 'g', -1, 64)
s := MarshalTest{"a", math.MaxInt64, math.MaxFloat32, math.MaxFloat64}
e := []byte(fmt.Sprintf("A: a\nB: %d\nC: %s\nD: %s\n", int64(math.MaxInt64), f32String, f64String))
y, err := Marshal(s)
if err != nil {
t.Errorf("error marshaling YAML: %v", err)
}
if !reflect.DeepEqual(y, e) {
t.Errorf("marshal YAML was unsuccessful, expected: %#v, got: %#v",
string(e), string(y))
}
}
type UnmarshalString struct {
A string
B string
}
type UnmarshalStringMap struct {
A map[string]string
}
type UnmarshalNestedString struct {
A NestedString
}
type NestedString struct {
A string
}
type UnmarshalSlice struct {
A []NestedSlice
}
type NestedSlice struct {
B string
C *string
}
func TestUnmarshal(t *testing.T) {
y := []byte(``)
s1 := UnmarshalString{}
e1 := UnmarshalString{}
unmarshalEqual(t, y, &s1, &e1)
y = []byte(`{}`)
s1 = UnmarshalString{}
e1 = UnmarshalString{}
unmarshalEqual(t, y, &s1, &e1)
y = []byte("a: 1")
s1 = UnmarshalString{}
e1 = UnmarshalString{A: "1"}
unmarshalEqual(t, y, &s1, &e1)
y = []byte(`a: "1"`)
s1 = UnmarshalString{}
e1 = UnmarshalString{A: "1"}
unmarshalEqual(t, y, &s1, &e1)
y = []byte("a: true")
s1 = UnmarshalString{}
e1 = UnmarshalString{A: "true"}
unmarshalEqual(t, y, &s1, &e1)
y = []byte("a: 1")
s1 = UnmarshalString{}
e1 = UnmarshalString{A: "1"}
unmarshalEqual(t, y, &s1, &e1)
y = []byte("a:\n a: 1")
s2 := UnmarshalNestedString{}
e2 := UnmarshalNestedString{NestedString{"1"}}
unmarshalEqual(t, y, &s2, &e2)
y = []byte("a:\n - b: abc\n c: def\n - b: 123\n c: 456\n")
s3 := UnmarshalSlice{}
e3 := UnmarshalSlice{[]NestedSlice{{"abc", strPtr("def")}, {"123", strPtr("456")}}}
unmarshalEqual(t, y, &s3, &e3)
y = []byte("a:\n b: 1")
s4 := UnmarshalStringMap{}
e4 := UnmarshalStringMap{map[string]string{"b": "1"}}
unmarshalEqual(t, y, &s4, &e4)
y = []byte(`
a:
name: TestA
b:
name: TestB
`)
type NamedThing struct {
Name string `json:"name"`
}
s5 := map[string]*NamedThing{}
e5 := map[string]*NamedThing{
"a": {Name: "TestA"},
"b": {Name: "TestB"},
}
unmarshalEqual(t, y, &s5, &e5)
}
// TestUnmarshalNonStrict tests that we parse ambiguous YAML without error.
func TestUnmarshalNonStrict(t *testing.T) {
for _, tc := range []struct {
yaml []byte
want UnmarshalString
}{
{
yaml: []byte("a: 1"),
want: UnmarshalString{A: "1"},
},
{
// Order does not matter.
yaml: []byte("b: 1\na: 2"),
want: UnmarshalString{A: "2", B: "1"},
},
{
// Unknown field get ignored.
yaml: []byte("a: 1\nunknownField: 2"),
want: UnmarshalString{A: "1"},
},
{
// Unknown fields get ignored.
yaml: []byte("unknownOne: 2\na: 1\nunknownTwo: 2"),
want: UnmarshalString{A: "1"},
},
{
// In YAML, `YES` is no longer Boolean true.
yaml: []byte("a: YES"),
want: UnmarshalString{A: "YES"},
},
} {
s := UnmarshalString{}
unmarshalEqual(t, tc.yaml, &s, &tc.want)
}
}
// prettyFunctionName converts a slice of JSONOpt function pointers to a human
// readable string representation.
func prettyFunctionName(opts []JSONOpt) []string {
var r []string
for _, o := range opts {
r = append(r, runtime.FuncForPC(reflect.ValueOf(o).Pointer()).Name())
}
return r
}
func unmarshalEqual(t *testing.T, y []byte, s, e interface{}, opts ...JSONOpt) { //nolint:unparam
t.Helper()
err := Unmarshal(y, s, opts...)
if err != nil {
t.Errorf("Unmarshal(%#q, s, %v) = %v", string(y), prettyFunctionName(opts), err)
return
}
if !reflect.DeepEqual(s, e) {
t.Errorf("Unmarshal(%#q, s, %v) = %+#v; want %+#v", string(y), prettyFunctionName(opts), s, e)
}
}
// TestUnmarshalErrors tests that we return an error on ambiguous YAML.
func TestUnmarshalErrors(t *testing.T) {
for _, tc := range []struct {
yaml []byte
wantErr string
}{
{
// Declaring `a` twice produces an error.
yaml: []byte("a: 1\na: 2"),
wantErr: `key "a" already defined`,
},
{
// Not ignoring first declaration of A with wrong type.
yaml: []byte("a: [1,2,3]\na: value-of-a"),
wantErr: `key "a" already defined`,
},
{
// Declaring field `true` twice.
yaml: []byte("true: string-value-of-yes\ntrue: 1"),
wantErr: `key "true" already defined`,
},
} {
s := UnmarshalString{}
err := Unmarshal(tc.yaml, &s)
if tc.wantErr != "" && err == nil {
t.Errorf("Unmarshal(%#q, &s) = nil; want error", string(tc.yaml))
continue
}
if tc.wantErr == "" && err != nil {
t.Errorf("Unmarshal(%#q, &s) = %v; want no error", string(tc.yaml), err)
continue
}
// We only expect errors during unmarshalling YAML.
if tc.wantErr != "" && !strings.Contains(err.Error(), tc.wantErr) {
t.Errorf("Unmarshal(%#q, &s) = %v; want err contains %#q", string(tc.yaml), err, tc.wantErr)
}
}
}
type Case struct {
input string
output string
// By default we test that reversing the output == input. But if there is a
// difference in the reversed output, you can optionally specify it here.
reverse *string
}
type RunType int
const (
RunTypeJSONToYAML RunType = iota
RunTypeYAMLToJSON
)
func TestJSONToYAML(t *testing.T) {
cases := []Case{
{
`{"t":"a"}`,
"t: a\n",
nil,
}, {
`{"t":null}`,
"t: null\n",
nil,
},
}
runCases(t, RunTypeJSONToYAML, cases)
}
func TestYAMLToJSON(t *testing.T) {
cases := []Case{
{
"t: a\n",
`{"t":"a"}`,
nil,
}, {
"t: \n",
`{"t":null}`,
strPtr("t: null\n"),
}, {
"t: null\n",
`{"t":null}`,
nil,
},
{
"true: yes\n",
`{"true":"yes"}`,
strPtr("\"true\": \"yes\"\n"),
},
{
"false: yes\n",
`{"false":"yes"}`,
strPtr("\"false\": \"yes\"\n"),
},
{
"1: a\n",
`{"1":"a"}`,
strPtr("\"1\": a\n"),
}, {
"1000000000000000000000000000000000000: a\n",
`{"1e+36":"a"}`,
strPtr("\"1e+36\": a\n"),
}, {
"1e+36: a\n",
`{"1e+36":"a"}`,
strPtr("\"1e+36\": a\n"),
}, {
"\"1e+36\": a\n",
`{"1e+36":"a"}`,
nil,
}, {
"\"1.2\": a\n",
`{"1.2":"a"}`,
nil,
}, {
"- t: a\n",
`[{"t":"a"}]`,
nil,
}, {
"- t: a\n" +
"- t:\n" +
" b: 1\n" +
" c: 2\n",
`[{"t":"a"},{"t":{"b":1,"c":2}}]`,
nil,
}, {
`[{t: a}, {t: {b: 1, c: 2}}]`,
`[{"t":"a"},{"t":{"b":1,"c":2}}]`,
strPtr("- t: a\n" +
"- t:\n" +
" b: 1\n" +
" c: 2\n"),
}, {
"- t: \n",
`[{"t":null}]`,
strPtr("- t: null\n"),
}, {
"- t: null\n",
`[{"t":null}]`,
nil,
},
}
// Cases that should produce errors.
_ = []Case{
{
"~: a",
`{"null":"a"}`,
nil,
}, {
"a: !!binary gIGC\n",
"{\"a\":\"\x80\x81\x82\"}",
nil,
},
}
runCases(t, RunTypeYAMLToJSON, cases)
}
func runCases(t *testing.T, runType RunType, cases []Case) {
var f func([]byte) ([]byte, error)
var invF func([]byte) ([]byte, error)
var msg string
var invMsg string
if runType == RunTypeJSONToYAML {
f = JSONToYAML
invF = YAMLToJSON
msg = "JSON to YAML"
invMsg = "YAML back to JSON"
} else {
f = YAMLToJSON
invF = JSONToYAML
msg = "YAML to JSON"
invMsg = "JSON back to YAML"
}
for _, c := range cases {
// Convert the string.
t.Logf("converting %s\n", c.input)
output, err := f([]byte(c.input))
if err != nil {
t.Errorf("Failed to convert %s, input: `%s`, err: %v", msg, c.input, err)
}
// Check it against the expected output.
if string(output) != c.output {
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
msg, c.input, c.output, string(output))
}
// Set the string that we will compare the reversed output to.
reverse := c.input
// If a special reverse string was specified, use that instead.
if c.reverse != nil {
reverse = *c.reverse
}
// Reverse the output.
input, err := invF(output)
if err != nil {
t.Errorf("Failed to convert %s, input: `%s`, err: %v", invMsg, string(output), err)
}
// Check the reverse is equal to the input (or to *c.reverse).
if string(input) != reverse {
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
invMsg, string(output), reverse, string(input))
}
}
}
// To be able to easily fill in the *Case.reverse string above.
func strPtr(s string) *string {
return &s
}
func TestYAMLToJSONDuplicateFields(t *testing.T) {
data := []byte("foo: bar\nfoo: baz\n")
if _, err := YAMLToJSON(data); err == nil {
t.Error("expected YAMLtoJSON to fail on duplicate field names")
}
}
func TestExtractOrigins_Map(t *testing.T) {
input := map[string]any{
"__origin__": map[string]any{"key": "root"},
"info": map[string]any{
"__origin__": map[string]any{"key": "info"},
"title": "Test",
},
}
tree := extractOrigins(input, "")
if tree == nil {
t.Fatal("expected non-nil tree")
}
// Root origin extracted
if tree.Origin == nil {
t.Fatal("expected root origin")
}
if tree.Origin.(map[string]any)["key"] != "root" {
t.Error("wrong root origin")
}
// __origin__ removed from map
if _, ok := input["__origin__"]; ok {
t.Error("__origin__ not removed from root map")
}
infoMap := input["info"].(map[string]any)
if _, ok := infoMap["__origin__"]; ok {
t.Error("__origin__ not removed from info map")
}
// Child tree extracted
infoTree := tree.Fields["info"]
if infoTree == nil {
t.Fatal("expected info child tree")
}
if infoTree.Origin.(map[string]any)["key"] != "info" {
t.Error("wrong info origin")
}
}
func TestExtractOrigins_Slice(t *testing.T) {
input := map[string]any{
"items": []any{
map[string]any{
"__origin__": map[string]any{"key": "item0"},
"name": "first",
},
"scalar",
map[string]any{
"__origin__": map[string]any{"key": "item2"},
"name": "third",
},
},
}
tree := extractOrigins(input, "")
if tree == nil {
t.Fatal("expected non-nil tree")
}
itemsTree := tree.Fields["items"]
if itemsTree == nil {
t.Fatal("expected items child tree")
}
if len(itemsTree.Items) != 3 {
t.Fatalf("expected 3 items, got %d", len(itemsTree.Items))
}
if itemsTree.Items[0] == nil || itemsTree.Items[0].Origin == nil {
t.Error("expected origin for item 0")
}
if itemsTree.Items[1] != nil {
t.Error("expected nil tree for scalar item 1")
}
if itemsTree.Items[2] == nil || itemsTree.Items[2].Origin == nil {
t.Error("expected origin for item 2")
}
// __origin__ removed from slice elements
item0 := input["items"].([]any)[0].(map[string]any)
if _, ok := item0["__origin__"]; ok {
t.Error("__origin__ not removed from item 0")
}
}
func TestExtractOrigins_Nil(t *testing.T) {
tree := extractOrigins("scalar", "")
if tree != nil {
t.Error("expected nil tree for scalar")
}
tree = extractOrigins(map[string]any{"a": "b"}, "")
if tree != nil {
t.Error("expected nil tree for map without __origin__")
}
}
type OriginTreeTestStruct struct {
Info struct {
Title string `json:"title"`
Version string `json:"version"`
} `json:"info"`
}
func TestUnmarshalWithOriginTree(t *testing.T) {
yamlData := []byte("info:\n title: Test\n version: v1\n")
var out OriginTreeTestStruct
tree, err := UnmarshalWithOriginTree(yamlData, &out, OriginOpt{Enabled: true, File: "test.yaml"})
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
// Struct populated correctly
if out.Info.Title != "Test" {
t.Errorf("expected title Test, got %s", out.Info.Title)
}
// Origin tree returned — root mapping now gets __origin__ too (yaml3 document() fix)
if tree == nil {
t.Fatal("expected non-nil origin tree")
}
// Root mapping must have origin set
if tree.Origin == nil {
t.Fatal("expected root origin to be set (yaml3 document() now injects __origin__ for root mappings)")
}
// Info subtree with origin
infoTree := tree.Fields["info"]
if infoTree == nil {
t.Fatal("expected info subtree")
}
if infoTree.Origin == nil {
t.Fatal("expected info origin")
}
// Compact format: []any{file, key_name, key_line, key_col, nf, ...}
originSeq, ok := infoTree.Origin.([]any)
if !ok {
t.Fatalf("expected []any origin, got %T", infoTree.Origin)
}
if len(originSeq) < 5 {
t.Errorf("expected at least 5 elements in origin sequence, got %d", len(originSeq))
}
if originSeq[0] != "test.yaml" {
t.Errorf("expected file 'test.yaml' at index 0, got %v", originSeq[0])
}
if originSeq[1] != "info" {
t.Errorf("expected key_name 'info' at index 1, got %v", originSeq[1])
}
}
// TestUnmarshalWithOriginTree_StandaloneSchema verifies that a YAML document
// that IS itself a schema (no wrapping key — the $ref pattern) produces a
// non-nil root Origin in the OriginTree. This exercises the yaml3 fix that
// injects __origin__ for the root mapping in document().
func TestUnmarshalWithOriginTree_StandaloneSchema(t *testing.T) {
// Simulates data/ref-chain-example/base/schemas/pet.yaml
yamlData := []byte("type: object\nrequired:\n - id\n - name\n")
var out map[string]any
tree, err := UnmarshalWithOriginTree(yamlData, &out, OriginOpt{Enabled: true, File: "pet.yaml"})
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if tree == nil {
t.Fatal("expected non-nil origin tree")
}
// Root schema Origin must now be set (yaml3 document() fix)
if tree.Origin == nil {
t.Fatal("expected root origin to be set for standalone schema ($ref-root fix)")
}
// Compact format: [file, key_name, key_line, key_col, nf, fields..., ns, seqs...]
originSeq, ok := tree.Origin.([]any)
if !ok {
t.Fatalf("expected []any origin, got %T", tree.Origin)
}
if len(originSeq) < 6 {
t.Errorf("expected at least 6 elements, got %d", len(originSeq))
}
if originSeq[0] != "pet.yaml" {
t.Errorf("expected file 'pet.yaml', got %v", originSeq[0])
}
// key_name must be empty string (synthetic root key has no name)
if originSeq[1] != "" {
t.Errorf("expected empty key_name for root, got %v", originSeq[1])
}
// 'required' sequence items must be in the tree for source tracking
if tree.Fields != nil {
t.Errorf("expected no sub-fields in a standalone scalar-only schema, but got: %v", tree.Fields)
}
}
func TestUnmarshalWithOriginTree_Disabled(t *testing.T) {
yamlData := []byte("info:\n title: Test\n")
var out OriginTreeTestStruct
tree, err := UnmarshalWithOriginTree(yamlData, &out, OriginOpt{Enabled: false})
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if tree != nil {
t.Error("expected nil tree when origin is disabled")
}
if out.Info.Title != "Test" {
t.Errorf("expected title Test, got %s", out.Info.Title)
}
}