Debian Codegraph @

decode_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package yaml_test
2
3 import (
4 "errors"
5 "io"
6 "math"
7 "reflect"
8 "strings"
9 "testing"
10 "time"
11
12 "go.yaml.in/yaml/v2"
13 )
14
15 var unmarshalIntTest = 123
16
17 var unmarshalTests = []struct {
18 data string
19 value interface{}
20 }{
21 {
22 "",
23 (*struct{})(nil),
24 },
25 {
26 "{}", &struct{}{},
27 }, {
28 "v: hi",
29 map[string]string{"v": "hi"},
30 }, {
31 "v: hi", map[string]interface{}{"v": "hi"},
32 }, {
33 "v: true",
34 map[string]string{"v": "true"},
35 }, {
36 "v: true",
37 map[string]interface{}{"v": true},
38 }, {
39 "v: 10",
40 map[string]interface{}{"v": 10},
41 }, {
42 "v: 0b10",
43 map[string]interface{}{"v": 2},
44 }, {
45 "v: 0xA",
46 map[string]interface{}{"v": 10},
47 }, {
48 "v: 4294967296",
49 map[string]int64{"v": 4294967296},
50 }, {
51 "v: 0.1",
52 map[string]interface{}{"v": 0.1},
53 }, {
54 "v: .1",
55 map[string]interface{}{"v": 0.1},
56 }, {
57 "v: .Inf",
58 map[string]interface{}{"v": math.Inf(+1)},
59 }, {
60 "v: -.Inf",
61 map[string]interface{}{"v": math.Inf(-1)},
62 }, {
63 "v: -10",
64 map[string]interface{}{"v": -10},
65 }, {
66 "v: -.1",
67 map[string]interface{}{"v": -0.1},
68 },
69
70 // Simple values.
71 {
72 "123",
73 &unmarshalIntTest,
74 },
75
76 // Floats from spec
77 {
78 "canonical: 6.8523e+5",
79 map[string]interface{}{"canonical": 6.8523e+5},
80 }, {
81 "expo: 685.230_15e+03",
82 map[string]interface{}{"expo": 685.23015e+03},
83 }, {
84 "fixed: 685_230.15",
85 map[string]interface{}{"fixed": 685230.15},
86 }, {
87 "neginf: -.inf",
88 map[string]interface{}{"neginf": math.Inf(-1)},
89 }, {
90 "fixed: 685_230.15",
91 map[string]float64{"fixed": 685230.15},
92 },
93 //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported
94 //{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // Equality of NaN fails.
95
96 // Bools from spec
97 {
98 "canonical: y",
99 map[string]interface{}{"canonical": true},
100 }, {
101 "answer: NO",
102 map[string]interface{}{"answer": false},
103 }, {
104 "logical: True",
105 map[string]interface{}{"logical": true},
106 }, {
107 "option: on",
108 map[string]interface{}{"option": true},
109 }, {
110 "option: on",
111 map[string]bool{"option": true},
112 },
113 // Ints from spec
114 {
115 "canonical: 685230",
116 map[string]interface{}{"canonical": 685230},
117 }, {
118 "decimal: +685_230",
119 map[string]interface{}{"decimal": 685230},
120 }, {
121 "octal: 02472256",
122 map[string]interface{}{"octal": 685230},
123 }, {
124 "hexa: 0x_0A_74_AE",
125 map[string]interface{}{"hexa": 685230},
126 }, {
127 "bin: 0b1010_0111_0100_1010_1110",
128 map[string]interface{}{"bin": 685230},
129 }, {
130 "bin: -0b101010",
131 map[string]interface{}{"bin": -42},
132 }, {
133 "bin: -0b1000000000000000000000000000000000000000000000000000000000000000",
134 map[string]interface{}{"bin": -9223372036854775808},
135 }, {
136 "decimal: +685_230",
137 map[string]int{"decimal": 685230},
138 },
139
140 //{"sexa: 190:20:30", map[string]interface{}{"sexa": 0}}, // Unsupported
141
142 // Nulls from spec
143 {
144 "empty:",
145 map[string]interface{}{"empty": nil},
146 }, {
147 "canonical: ~",
148 map[string]interface{}{"canonical": nil},
149 }, {
150 "english: null",
151 map[string]interface{}{"english": nil},
152 }, {
153 "~: null key",
154 map[interface{}]string{nil: "null key"},
155 }, {
156 "empty:",
157 map[string]*bool{"empty": nil},
158 },
159
160 // Flow sequence
161 {
162 "seq: [A,B]",
163 map[string]interface{}{"seq": []interface{}{"A", "B"}},
164 }, {
165 "seq: [A,B,C,]",
166 map[string][]string{"seq": []string{"A", "B", "C"}},
167 }, {
168 "seq: [A,1,C]",
169 map[string][]string{"seq": []string{"A", "1", "C"}},
170 }, {
171 "seq: [A,1,C]",
172 map[string][]int{"seq": []int{1}},
173 }, {
174 "seq: [A,1,C]",
175 map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
176 },
177 // Block sequence
178 {
179 "seq:\n - A\n - B",
180 map[string]interface{}{"seq": []interface{}{"A", "B"}},
181 }, {
182 "seq:\n - A\n - B\n - C",
183 map[string][]string{"seq": []string{"A", "B", "C"}},
184 }, {
185 "seq:\n - A\n - 1\n - C",
186 map[string][]string{"seq": []string{"A", "1", "C"}},
187 }, {
188 "seq:\n - A\n - 1\n - C",
189 map[string][]int{"seq": []int{1}},
190 }, {
191 "seq:\n - A\n - 1\n - C",
192 map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
193 },
194
195 // Literal block scalar
196 {
197 "scalar: | # Comment\n\n literal\n\n \ttext\n\n",
198 map[string]string{"scalar": "\nliteral\n\n\ttext\n"},
199 },
200
201 // Folded block scalar
202 {
203 "scalar: > # Comment\n\n folded\n line\n \n next\n line\n * one\n * two\n\n last\n line\n\n",
204 map[string]string{"scalar": "\nfolded line\nnext line\n * one\n * two\n\nlast line\n"},
205 },
206
207 // Map inside interface with no type hints.
208 {
209 "a: {b: c}",
210 map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": "c"}},
211 },
212
213 // Structs and type conversions.
214 {
215 "hello: world",
216 &struct{ Hello string }{"world"},
217 }, {
218 "a: {b: c}",
219 &struct{ A struct{ B string } }{struct{ B string }{"c"}},
220 }, {
221 "a: {b: c}",
222 &struct{ A *struct{ B string } }{&struct{ B string }{"c"}},
223 }, {
224 "a: {b: c}",
225 &struct{ A map[string]string }{map[string]string{"b": "c"}},
226 }, {
227 "a: {b: c}",
228 &struct{ A *map[string]string }{&map[string]string{"b": "c"}},
229 }, {
230 "a:",
231 &struct{ A map[string]string }{},
232 }, {
233 "a: 1",
234 &struct{ A int }{1},
235 }, {
236 "a: 1",
237 &struct{ A float64 }{1},
238 }, {
239 "a: 1.0",
240 &struct{ A int }{1},
241 }, {
242 "a: 1.0",
243 &struct{ A uint }{1},
244 }, {
245 "a: [1, 2]",
246 &struct{ A []int }{[]int{1, 2}},
247 }, {
248 "a: [1, 2]",
249 &struct{ A [2]int }{[2]int{1, 2}},
250 }, {
251 "a: 1",
252 &struct{ B int }{0},
253 }, {
254 "a: 1",
255 &struct {
256 B int "a"
257 }{1},
258 }, {
259 "a: y",
260 &struct{ A bool }{true},
261 },
262
263 // Some cross type conversions
264 {
265 "v: 42",
266 map[string]uint{"v": 42},
267 }, {
268 "v: -42",
269 map[string]uint{},
270 }, {
271 "v: 4294967296",
272 map[string]uint64{"v": 4294967296},
273 }, {
274 "v: -4294967296",
275 map[string]uint64{},
276 },
277
278 // int
279 {
280 "int_max: 2147483647",
281 map[string]int{"int_max": math.MaxInt32},
282 },
283 {
284 "int_min: -2147483648",
285 map[string]int{"int_min": math.MinInt32},
286 },
287 {
288 "int_overflow: 9223372036854775808", // math.MaxInt64 + 1
289 map[string]int{},
290 },
291
292 // int64
293 {
294 "int64_max: 9223372036854775807",
295 map[string]int64{"int64_max": math.MaxInt64},
296 },
297 {
298 "int64_max_base2: 0b111111111111111111111111111111111111111111111111111111111111111",
299 map[string]int64{"int64_max_base2": math.MaxInt64},
300 },
301 {
302 "int64_min: -9223372036854775808",
303 map[string]int64{"int64_min": math.MinInt64},
304 },
305 {
306 "int64_neg_base2: -0b111111111111111111111111111111111111111111111111111111111111111",
307 map[string]int64{"int64_neg_base2": -math.MaxInt64},
308 },
309 {
310 "int64_overflow: 9223372036854775808", // math.MaxInt64 + 1
311 map[string]int64{},
312 },
313
314 // uint
315 {
316 "uint_min: 0",
317 map[string]uint{"uint_min": 0},
318 },
319 {
320 "uint_max: 4294967295",
321 map[string]uint{"uint_max": math.MaxUint32},
322 },
323 {
324 "uint_underflow: -1",
325 map[string]uint{},
326 },
327
328 // uint64
329 {
330 "uint64_min: 0",
331 map[string]uint{"uint64_min": 0},
332 },
333 {
334 "uint64_max: 18446744073709551615",
335 map[string]uint64{"uint64_max": math.MaxUint64},
336 },
337 {
338 "uint64_max_base2: 0b1111111111111111111111111111111111111111111111111111111111111111",
339 map[string]uint64{"uint64_max_base2": math.MaxUint64},
340 },
341 {
342 "uint64_maxint64: 9223372036854775807",
343 map[string]uint64{"uint64_maxint64": math.MaxInt64},
344 },
345 {
346 "uint64_underflow: -1",
347 map[string]uint64{},
348 },
349
350 // float32
351 {
352 "float32_max: 3.40282346638528859811704183484516925440e+38",
353 map[string]float32{"float32_max": math.MaxFloat32},
354 },
355 {
356 "float32_nonzero: 1.401298464324817070923729583289916131280e-45",
357 map[string]float32{"float32_nonzero": math.SmallestNonzeroFloat32},
358 },
359 {
360 "float32_maxuint64: 18446744073709551615",
361 map[string]float32{"float32_maxuint64": float32(math.MaxUint64)},
362 },
363 {
364 "float32_maxuint64+1: 18446744073709551616",
365 map[string]float32{"float32_maxuint64+1": float32(math.MaxUint64 + 1)},
366 },
367
368 // float64
369 {
370 "float64_max: 1.797693134862315708145274237317043567981e+308",
371 map[string]float64{"float64_max": math.MaxFloat64},
372 },
373 {
374 "float64_nonzero: 4.940656458412465441765687928682213723651e-324",
375 map[string]float64{"float64_nonzero": math.SmallestNonzeroFloat64},
376 },
377 {
378 "float64_maxuint64: 18446744073709551615",
379 map[string]float64{"float64_maxuint64": float64(math.MaxUint64)},
380 },
381 {
382 "float64_maxuint64+1: 18446744073709551616",
383 map[string]float64{"float64_maxuint64+1": float64(math.MaxUint64 + 1)},
384 },
385
386 // Overflow cases.
387 {
388 "v: 4294967297",
389 map[string]int32{},
390 }, {
391 "v: 128",
392 map[string]int8{},
393 },
394
395 // Quoted values.
396 {
397 "'1': '\"2\"'",
398 map[interface{}]interface{}{"1": "\"2\""},
399 }, {
400 "v:\n- A\n- 'B\n\n C'\n",
401 map[string][]string{"v": []string{"A", "B\nC"}},
402 },
403
404 // Explicit tags.
405 {
406 "v: !!float '1.1'",
407 map[string]interface{}{"v": 1.1},
408 }, {
409 "v: !!float 0",
410 map[string]interface{}{"v": float64(0)},
411 }, {
412 "v: !!float -1",
413 map[string]interface{}{"v": float64(-1)},
414 }, {
415 "v: !!null ''",
416 map[string]interface{}{"v": nil},
417 }, {
418 "%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'",
419 map[string]interface{}{"v": 1},
420 },
421
422 // Non-specific tag (Issue #75)
423 {
424 "v: ! test",
425 map[string]interface{}{"v": "test"},
426 },
427
428 // Anchors and aliases.
429 {
430 "a: &x 1\nb: &y 2\nc: *x\nd: *y\n",
431 &struct{ A, B, C, D int }{1, 2, 1, 2},
432 }, {
433 "a: &a {c: 1}\nb: *a",
434 &struct {
435 A, B struct {
436 C int
437 }
438 }{struct{ C int }{1}, struct{ C int }{1}},
439 }, {
440 "a: &a [1, 2]\nb: *a",
441 &struct{ B []int }{[]int{1, 2}},
442 },
443
444 // Bug #1133337
445 {
446 "foo: ''",
447 map[string]*string{"foo": new(string)},
448 }, {
449 "foo: null",
450 map[string]*string{"foo": nil},
451 }, {
452 "foo: null",
453 map[string]string{"foo": ""},
454 }, {
455 "foo: null",
456 map[string]interface{}{"foo": nil},
457 },
458
459 // Support for ~
460 {
461 "foo: ~",
462 map[string]*string{"foo": nil},
463 }, {
464 "foo: ~",
465 map[string]string{"foo": ""},
466 }, {
467 "foo: ~",
468 map[string]interface{}{"foo": nil},
469 },
470
471 // Ignored field
472 {
473 "a: 1\nb: 2\n",
474 &struct {
475 A int
476 B int "-"
477 }{1, 0},
478 },
479
480 // Bug #1191981
481 {
482 "" +
483 "%YAML 1.1\n" +
484 "--- !!str\n" +
485 `"Generic line break (no glyph)\n\` + "\n" +
486 ` Generic line break (glyphed)\n\` + "\n" +
487 ` Line separator\u2028\` + "\n" +
488 ` Paragraph separator\u2029"` + "\n",
489 "" +
490 "Generic line break (no glyph)\n" +
491 "Generic line break (glyphed)\n" +
492 "Line separator\u2028Paragraph separator\u2029",
493 },
494
495 // Struct inlining
496 {
497 "a: 1\nb: 2\nc: 3\n",
498 &struct {
499 A int
500 C inlineB `yaml:",inline"`
501 }{1, inlineB{2, inlineC{3}}},
502 },
503
504 // Map inlining
505 {
506 "a: 1\nb: 2\nc: 3\n",
507 &struct {
508 A int
509 C map[string]int `yaml:",inline"`
510 }{1, map[string]int{"b": 2, "c": 3}},
511 },
512
513 // bug 1243827
514 {
515 "a: -b_c",
516 map[string]interface{}{"a": "-b_c"},
517 },
518 {
519 "a: +b_c",
520 map[string]interface{}{"a": "+b_c"},
521 },
522 {
523 "a: 50cent_of_dollar",
524 map[string]interface{}{"a": "50cent_of_dollar"},
525 },
526
527 // issue #295 (allow scalars with colons in flow mappings and sequences)
528 {
529 "a: {b: https://github.com/go-yaml/yaml}",
530 map[string]interface{}{"a": map[interface{}]interface{}{
531 "b": "https://github.com/go-yaml/yaml",
532 }},
533 },
534 {
535 "a: [https://github.com/go-yaml/yaml]",
536 map[string]interface{}{"a": []interface{}{"https://github.com/go-yaml/yaml"}},
537 },
538
539 // Duration
540 {
541 "a: 3s",
542 map[string]time.Duration{"a": 3 * time.Second},
543 },
544
545 // Issue #24.
546 {
547 "a: <foo>",
548 map[string]string{"a": "<foo>"},
549 },
550
551 // Base 60 floats are obsolete and unsupported.
552 {
553 "a: 1:1\n",
554 map[string]string{"a": "1:1"},
555 },
556
557 // Binary data.
558 {
559 "a: !!binary gIGC\n",
560 map[string]string{"a": "\x80\x81\x82"},
561 }, {
562 "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
563 map[string]string{"a": strings.Repeat("\x90", 54)},
564 }, {
565 "a: !!binary |\n " + strings.Repeat("A", 70) + "\n ==\n",
566 map[string]string{"a": strings.Repeat("\x00", 52)},
567 },
568
569 // Ordered maps.
570 {
571 "{b: 2, a: 1, d: 4, c: 3, sub: {e: 5}}",
572 &yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}},
573 },
574
575 // Issue #39.
576 {
577 "a:\n b:\n c: d\n",
578 map[string]struct{ B interface{} }{"a": {map[interface{}]interface{}{"c": "d"}}},
579 },
580
581 // Custom map type.
582 {
583 "a: {b: c}",
584 M{"a": M{"b": "c"}},
585 },
586
587 // Support encoding.TextUnmarshaler.
588 {
589 "a: 1.2.3.4\n",
590 map[string]textUnmarshaler{"a": textUnmarshaler{S: "1.2.3.4"}},
591 },
592 {
593 "a: 2015-02-24T18:19:39Z\n",
594 map[string]textUnmarshaler{"a": textUnmarshaler{"2015-02-24T18:19:39Z"}},
595 },
596
597 // Timestamps
598 {
599 // Date only.
600 "a: 2015-01-01\n",
601 map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
602 },
603 {
604 // RFC3339
605 "a: 2015-02-24T18:19:39.12Z\n",
606 map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, .12e9, time.UTC)},
607 },
608 {
609 // RFC3339 with short dates.
610 "a: 2015-2-3T3:4:5Z",
611 map[string]time.Time{"a": time.Date(2015, 2, 3, 3, 4, 5, 0, time.UTC)},
612 },
613 {
614 // ISO8601 lower case t
615 "a: 2015-02-24t18:19:39Z\n",
616 map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
617 },
618 {
619 // space separate, no time zone
620 "a: 2015-02-24 18:19:39\n",
621 map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
622 },
623 // Some cases not currently handled. Uncomment these when
624 // the code is fixed.
625 // {
626 // // space separated with time zone
627 // "a: 2001-12-14 21:59:43.10 -5",
628 // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
629 // },
630 // {
631 // // arbitrary whitespace between fields
632 // "a: 2001-12-14 \t\t \t21:59:43.10 \t Z",
633 // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
634 // },
635 {
636 // explicit string tag
637 "a: !!str 2015-01-01",
638 map[string]interface{}{"a": "2015-01-01"},
639 },
640 {
641 // explicit timestamp tag on quoted string
642 "a: !!timestamp \"2015-01-01\"",
643 map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
644 },
645 {
646 // explicit timestamp tag on unquoted string
647 "a: !!timestamp 2015-01-01",
648 map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
649 },
650 {
651 // quoted string that's a valid timestamp
652 "a: \"2015-01-01\"",
653 map[string]interface{}{"a": "2015-01-01"},
654 },
655 {
656 // explicit timestamp tag into interface.
657 "a: !!timestamp \"2015-01-01\"",
658 map[string]interface{}{"a": "2015-01-01"},
659 },
660 {
661 // implicit timestamp tag into interface.
662 "a: 2015-01-01",
663 map[string]interface{}{"a": "2015-01-01"},
664 },
665
666 // Encode empty lists as zero-length slices.
667 {
668 "a: []",
669 &struct{ A []int }{[]int{}},
670 },
671
672 // UTF-16-LE
673 {
674 "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n\x00",
675 M{"ñoño": "very yes"},
676 },
677 // UTF-16-LE with surrogate.
678 {
679 "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \x00=\xd8\xd4\xdf\n\x00",
680 M{"ñoño": "very yes 🟔"},
681 },
682
683 // UTF-16-BE
684 {
685 "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n",
686 M{"ñoño": "very yes"},
687 },
688 // UTF-16-BE with surrogate.
689 {
690 "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \xd8=\xdf\xd4\x00\n",
691 M{"ñoño": "very yes 🟔"},
692 },
693
694 // This *is* in fact a float number, per the spec. #171 was a mistake.
695 {
696 "a: 123456e1\n",
697 M{"a": 123456e1},
698 }, {
699 "a: 123456E1\n",
700 M{"a": 123456E1},
701 },
702 // yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes
703 {
704 "First occurrence: &anchor Foo\nSecond occurrence: *anchor\nOverride anchor: &anchor Bar\nReuse anchor: *anchor\n",
705 map[interface{}]interface{}{
706 "Reuse anchor": "Bar",
707 "First occurrence": "Foo",
708 "Second occurrence": "Foo",
709 "Override anchor": "Bar",
710 },
711 },
712 // Single document with garbage following it.
713 {
714 "---\nhello\n...\n}not yaml",
715 "hello",
716 },
717 {
718 "a: 5\n",
719 &struct{ A jsonNumberT }{"5"},
720 },
721 {
722 "a: 5.5\n",
723 &struct{ A jsonNumberT }{"5.5"},
724 },
725 {
726 `
727 a:
728 b
729 b:
730 ? a
731 : a`,
732 &M{"a": "b",
733 "b": M{
734 "a": "a",
735 }},
736 },
737 }
738
739 type M map[interface{}]interface{}
740
741 type inlineB struct {
742 B int
743 inlineC `yaml:",inline"`
744 }
745
746 type inlineC struct {
747 C int
748 }
749
750 func TestUnmarshal(t *testing.T) {
751 for i, item := range unmarshalTests {
752 t.Logf("test %d: %q", i, item.data)
753 typ := reflect.ValueOf(item.value).Type()
754 value := reflect.New(typ)
755 err := yaml.Unmarshal([]byte(item.data), value.Interface())
756 if _, ok := err.(*yaml.TypeError); !ok {
757 if err != nil {
758 t.Fatalf("Unmarshal() returned error: %v", err)
759 }
760 }
761 if !reflect.DeepEqual(value.Elem().Interface(), item.value) {
762 t.Fatalf("Unmarshal() returned %#v, want %#v (error: %v)", value.Elem().Interface(), item.value, err)
763 }
764 }
765 }
766
767 // TODO(v3): This test should also work when unmarshaling onto an interface{}.
768 func TestUnmarshalFullTimestamp(t *testing.T) {
769 // Full timestamp in same format as encoded. This is confirmed to be
770 // properly decoded by Python as a timestamp as well.
771 var str = "2015-02-24T18:19:39.123456789-03:00"
772 var tm time.Time
773 err := yaml.Unmarshal([]byte(str), &tm)
774 if err != nil {
775 t.Fatalf("Unmarshal() returned error: %v", err)
776 }
777 expected := time.Date(2015, 2, 24, 18, 19, 39, 123456789, tm.Location())
778 if !tm.Equal(expected) || tm.Location().String() != expected.Location().String() {
779 t.Fatalf("Unmarshal() returned %v, want %v", tm, expected)
780 }
781 expectedUTC := time.Date(2015, 2, 24, 21, 19, 39, 123456789, time.UTC)
782 if !tm.In(time.UTC).Equal(expectedUTC) {
783 t.Fatalf("Unmarshal() in UTC returned %v, want %v", tm.In(time.UTC), expectedUTC)
784 }
785 }
786
787 func TestDecoderSingleDocument(t *testing.T) {
788 // Test that Decoder.Decode works as expected on
789 // all the unmarshal tests.
790 for i, item := range unmarshalTests {
791 t.Logf("test %d: %q", i, item.data)
792 if item.data == "" {
793 // Behaviour differs when there's no YAML.
794 continue
795 }
796 typ := reflect.ValueOf(item.value).Type()
797 value := reflect.New(typ)
798 err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(value.Interface())
799 if _, ok := err.(*yaml.TypeError); !ok {
800 if err != nil {
801 t.Fatalf("Decode() returned error: %v", err)
802 }
803 }
804 if !reflect.DeepEqual(value.Elem().Interface(), item.value) {
805 t.Fatalf("Decode() returned %#v, want %#v", value.Elem().Interface(), item.value)
806 }
807 }
808 }
809
810 var decoderTests = []struct {
811 data string
812 values []interface{}
813 }{{
814 "",
815 nil,
816 }, {
817 "a: b",
818 []interface{}{
819 map[interface{}]interface{}{"a": "b"},
820 },
821 }, {
822 "---\na: b\n...\n",
823 []interface{}{
824 map[interface{}]interface{}{"a": "b"},
825 },
826 }, {
827 "---\n'hello'\n...\n---\ngoodbye\n...\n",
828 []interface{}{
829 "hello",
830 "goodbye",
831 },
832 }}
833
834 func TestDecoder(t *testing.T) {
835 for i, item := range decoderTests {
836 t.Logf("test %d: %q", i, item.data)
837 var values []interface{}
838 dec := yaml.NewDecoder(strings.NewReader(item.data))
839 for {
840 var value interface{}
841 err := dec.Decode(&value)
842 if err == io.EOF {
843 break
844 }
845 if err != nil {
846 t.Fatalf("Decode() returned error: %v", err)
847 }
848 values = append(values, value)
849 }
850 if !reflect.DeepEqual(values, item.values) {
851 t.Fatalf("Decode() returned %#v, want %#v", values, item.values)
852 }
853 }
854 }
855
856 type errReader struct{}
857
858 func (errReader) Read([]byte) (int, error) {
859 return 0, errors.New("some read error")
860 }
861
862 func TestDecoderReadError(t *testing.T) {
863 err := yaml.NewDecoder(errReader{}).Decode(&struct{}{})
864 if err == nil || !strings.Contains(err.Error(), `yaml: input error: some read error`) {
865 t.Fatalf("Decode() returned %v, want error containing %q", err, `yaml: input error: some read error`)
866 }
867 }
868
869 func TestUnmarshalNaN(t *testing.T) {
870 value := map[string]interface{}{}
871 err := yaml.Unmarshal([]byte("notanum: .NaN"), &value)
872 if err != nil {
873 t.Fatalf("Unmarshal() returned error: %v", err)
874 }
875 if !math.IsNaN(value["notanum"].(float64)) {
876 t.Fatalf("Unmarshal() returned %v, want NaN", value["notanum"])
877 }
878 }
879
880 var unmarshalErrorTests = []struct {
881 data, error string
882 }{
883 {"v: !!float 'error'", "yaml: cannot decode !!str `error` as a !!float"},
884 {"v: [A,", "yaml: line 1: did not find expected node content"},
885 {"v:\n- [A,", "yaml: line 2: did not find expected node content"},
886 {"a:\n- b: *,", "yaml: line 2: did not find expected alphabetic or numeric character"},
887 {"a: *b\n", "yaml: unknown anchor 'b' referenced"},
888 {"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
889 {"a: &x null\n<<:\n- *x\nb: &x {}\n", `yaml: map merge requires map or sequence of maps as the value`}, // Issue #529.
890 {"value: -", "yaml: block sequence entries are not allowed in this context"},
891 {"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
892 {"{[.]}", `yaml: invalid map key: []interface {}{"."}`},
893 {"{{.}}", `yaml: invalid map key: map[interface {}]interface {}{".":interface {}(nil)}`},
894 {"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`},
895 {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
896 {"a:\n 1:\nb\n 2:", "could not find expected ':'"},
897 {
898 "a: &a [00,00,00,00,00,00,00,00,00]\n" +
899 "b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" +
900 "c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" +
901 "d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" +
902 "e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" +
903 "f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" +
904 "g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" +
905 "h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" +
906 "i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n",
907 "yaml: document contains excessive aliasing",
908 },
909 }
910
911 func TestUnmarshalErrors(t *testing.T) {
912 for i, item := range unmarshalErrorTests {
913 t.Logf("test %d: %q", i, item.data)
914 var value interface{}
915 err := yaml.Unmarshal([]byte(item.data), &value)
916 if err == nil {
917 t.Fatalf("Unmarshal() expected error, got none. Partial unmarshal: %#v", value)
918 }
919 if !strings.Contains(err.Error(), item.error) {
920 t.Fatalf("Unmarshal() returned error %q, want containing %q. Partial unmarshal: %#v", err.Error(), item.error, value)
921 }
922
923 if strings.Contains(item.data, ":") {
924 // Repeat test with typed value.
925 var mapValue map[string]interface{}
926 err := yaml.Unmarshal([]byte(item.data), &mapValue)
927 if err == nil {
928 t.Fatalf("Unmarshal() with map[string]interface{} expected error, got none. Partial unmarshal: %#v", mapValue)
929 }
930 if !strings.Contains(err.Error(), item.error) {
931 t.Fatalf("Unmarshal() with map[string]interface{} returned error %q, want containing %q. Partial unmarshal: %#v", err.Error(), item.error, mapValue)
932 }
933 }
934 }
935 }
936
937 func TestDecoderErrors(t *testing.T) {
938 for _, item := range unmarshalErrorTests {
939 var value interface{}
940 err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(&value)
941 if err == nil {
942 t.Fatalf("Decode() expected error, got none. Partial unmarshal: %#v", value)
943 }
944 if !strings.Contains(err.Error(), item.error) {
945 t.Fatalf("Decode() returned error %q, want containing %q. Partial unmarshal: %#v", err.Error(), item.error, value)
946 }
947 }
948 }
949
950 var unmarshalerTests = []struct {
951 data, tag string
952 value interface{}
953 }{
954 {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
955 {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
956 {"_: 10", "!!int", 10},
957 {"_: null", "!!null", nil},
958 {`_: BAR!`, "!!str", "BAR!"},
959 {`_: "BAR!"`, "!!str", "BAR!"},
960 {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
961 {`_: ""`, "!!str", ""},
962 }
963
964 var unmarshalerResult = map[int]error{}
965
966 type unmarshalerType struct {
967 value interface{}
968 }
969
970 func (o *unmarshalerType) UnmarshalYAML(unmarshal func(v interface{}) error) error {
971 if err := unmarshal(&o.value); err != nil {
972 return err
973 }
974 if i, ok := o.value.(int); ok {
975 if result, ok := unmarshalerResult[i]; ok {
976 return result
977 }
978 }
979 return nil
980 }
981
982 type unmarshalerPointer struct {
983 Field *unmarshalerType "_"
984 }
985
986 type unmarshalerValue struct {
987 Field unmarshalerType "_"
988 }
989
990 func TestUnmarshalerPointerField(t *testing.T) {
991 for _, item := range unmarshalerTests {
992 obj := &unmarshalerPointer{}
993 err := yaml.Unmarshal([]byte(item.data), obj)
994 if err != nil {
995 t.Fatalf("Unmarshal() returned error: %v", err)
996 }
997 if item.value == nil {
998 if obj.Field != nil {
999 t.Fatalf("Unmarshal() returned %v, want nil", obj.Field)
1000 }
1001 } else {
1002 if obj.Field == nil {
1003 t.Fatalf("Unmarshal() returned nil, want non-nil. Value: %#v", item.value)
1004 }
1005 if !reflect.DeepEqual(obj.Field.value, item.value) {
1006 t.Fatalf("Unmarshal() returned %#v, want %#v", obj.Field.value, item.value)
1007 }
1008 }
1009 }
1010 }
1011
1012 func TestUnmarshalerValueField(t *testing.T) {
1013 for _, item := range unmarshalerTests {
1014 obj := &unmarshalerValue{}
1015 err := yaml.Unmarshal([]byte(item.data), obj)
1016 if err != nil {
1017 t.Fatalf("Unmarshal() returned error: %v", err)
1018 }
1019 if !reflect.DeepEqual(obj.Field.value, item.value) {
1020 t.Fatalf("Unmarshal() returned %#v, want %#v", obj.Field.value, item.value)
1021 }
1022 }
1023 }
1024
1025 func TestUnmarshalerWholeDocument(t *testing.T) {
1026 obj := &unmarshalerType{}
1027 err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj)
1028 if err != nil {
1029 t.Fatalf("Unmarshal() returned error: %v", err)
1030 }
1031 value, ok := obj.value.(map[interface{}]interface{})
1032 if !ok {
1033 t.Fatalf("obj.value is not a map[interface{}]interface{}. Value: %#v", obj.value)
1034 }
1035 if !reflect.DeepEqual(value["_"], unmarshalerTests[0].value) {
1036 t.Fatalf("Unmarshal() returned %#v but expected %#v", value["_"], unmarshalerTests[0].value)
1037 }
1038 }
1039
1040 func TestUnmarshalerTypeError(t *testing.T) {
1041 unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
1042 unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
1043 defer func() {
1044 delete(unmarshalerResult, 2)
1045 delete(unmarshalerResult, 4)
1046 }()
1047
1048 type T struct {
1049 Before int
1050 After int
1051 M map[string]*unmarshalerType
1052 }
1053 var v T
1054 data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}`
1055 err := yaml.Unmarshal([]byte(data), &v)
1056 expectedError := "" +
1057 "yaml: unmarshal errors:\n" +
1058 " line 1: cannot unmarshal !!str `A` into int\n" +
1059 " foo\n" +
1060 " bar\n" +
1061 " line 1: cannot unmarshal !!str `B` into int"
1062 if err == nil {
1063 t.Fatalf("Unmarshal() expected error, got none.")
1064 }
1065 if err.Error() != expectedError {
1066 t.Fatalf("Unmarshal() returned error %q, want %q", err, expectedError)
1067 }
1068 if v.M["abc"] == nil {
1069 t.Fatalf("v.M[\"abc\"] is nil, want non-nil")
1070 }
1071 if v.M["def"] != nil {
1072 t.Fatalf("v.M[\"def\"] is not nil, want nil")
1073 }
1074 if v.M["ghi"] == nil {
1075 t.Fatalf("v.M[\"ghi\"] is nil, want non-nil")
1076 }
1077 if v.M["jkl"] != nil {
1078 t.Fatalf("v.M[\"jkl\"] is not nil, want nil")
1079 }
1080
1081 if !reflect.DeepEqual(v.M["abc"].value, 1) {
1082 t.Fatalf("v.M[\"abc\"].value is %v, want %v", v.M["abc"].value, 1)
1083 }
1084 if !reflect.DeepEqual(v.M["ghi"].value, 3) {
1085 t.Fatalf("v.M[\"ghi\"].value is %v, want %v", v.M["ghi"].value, 3)
1086 }
1087 }
1088
1089 type proxyTypeError struct{}
1090
1091 func (v *proxyTypeError) UnmarshalYAML(unmarshal func(interface{}) error) error {
1092 var s string
1093 var a int32
1094 var b int64
1095 if err := unmarshal(&s); err != nil {
1096 panic(err)
1097 }
1098 if s == "a" {
1099 if err := unmarshal(&b); err == nil {
1100 panic("should have failed")
1101 }
1102 return unmarshal(&a)
1103 }
1104 if err := unmarshal(&a); err == nil {
1105 panic("should have failed")
1106 }
1107 return unmarshal(&b)
1108 }
1109
1110 func TestUnmarshalerTypeErrorProxying(t *testing.T) {
1111 type T struct {
1112 Before int
1113 After int
1114 M map[string]*proxyTypeError
1115 }
1116 var v T
1117 data := `{before: A, m: {abc: a, def: b}, after: B}`
1118 err := yaml.Unmarshal([]byte(data), &v)
1119 expectedError := "" +
1120 "yaml: unmarshal errors:\n" +
1121 " line 1: cannot unmarshal !!str `A` into int\n" +
1122 " line 1: cannot unmarshal !!str `a` into int32\n" +
1123 " line 1: cannot unmarshal !!str `b` into int64\n" +
1124 " line 1: cannot unmarshal !!str `B` into int"
1125 if err == nil {
1126 t.Fatalf("Unmarshal() expected error, got none.")
1127 }
1128 if err.Error() != expectedError {
1129 t.Fatalf("Unmarshal() returned error %q, want %q", err, expectedError)
1130 }
1131 }
1132
1133 type failingUnmarshaler struct{}
1134
1135 var failingErr = errors.New("failingErr")
1136
1137 func (ft *failingUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
1138 return failingErr
1139 }
1140
1141 func TestUnmarshalerError(t *testing.T) {
1142 err := yaml.Unmarshal([]byte("a: b"), &failingUnmarshaler{})
1143 if err != failingErr {
1144 t.Fatalf("Unmarshal() returned %v, want %v", err, failingErr)
1145 }
1146 }
1147
1148 type sliceUnmarshaler []int
1149
1150 func (su *sliceUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
1151 var slice []int
1152 err := unmarshal(&slice)
1153 if err == nil {
1154 *su = slice
1155 return nil
1156 }
1157
1158 var intVal int
1159 err = unmarshal(&intVal)
1160 if err == nil {
1161 *su = []int{intVal}
1162 return nil
1163 }
1164
1165 return err
1166 }
1167
1168 func TestUnmarshalerRetry(t *testing.T) {
1169 var su sliceUnmarshaler
1170 err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su)
1171 if err != nil {
1172 t.Fatalf("Unmarshal() returned error: %v", err)
1173 }
1174 if !reflect.DeepEqual(su, sliceUnmarshaler([]int{1, 2, 3})) {
1175 t.Fatalf("Unmarshal() returned %v, want %v", su, sliceUnmarshaler([]int{1, 2, 3}))
1176 }
1177
1178 err = yaml.Unmarshal([]byte("1"), &su)
1179 if err != nil {
1180 t.Fatalf("Unmarshal() returned error: %v", err)
1181 }
1182 if !reflect.DeepEqual(su, sliceUnmarshaler([]int{1})) {
1183 t.Fatalf("Unmarshal() returned %v, want %v", su, sliceUnmarshaler([]int{1}))
1184 }
1185 }
1186
1187 // From http://yaml.org/type/merge.html
1188 var mergeTests = `
1189 anchors:
1190 list:
1191 - &CENTER { "x": 1, "y": 2 }
1192 - &LEFT { "x": 0, "y": 2 }
1193 - &BIG { "r": 10 }
1194 - &SMALL { "r": 1 }
1195
1196 # All the following maps are equal:
1197
1198 plain:
1199 # Explicit keys
1200 "x": 1
1201 "y": 2
1202 "r": 10
1203 label: center/big
1204
1205 mergeOne:
1206 # Merge one map
1207 << : *CENTER
1208 "r": 10
1209 label: center/big
1210
1211 mergeMultiple:
1212 # Merge multiple maps
1213 << : [ *CENTER, *BIG ]
1214 label: center/big
1215
1216 override:
1217 # Override
1218 << : [ *BIG, *LEFT, *SMALL ]
1219 "x": 1
1220 label: center/big
1221
1222 shortTag:
1223 # Explicit short merge tag
1224 !!merge "<<" : [ *CENTER, *BIG ]
1225 label: center/big
1226
1227 longTag:
1228 # Explicit merge long tag
1229 !<tag:yaml.org,2002:merge> "<<" : [ *CENTER, *BIG ]
1230 label: center/big
1231
1232 inlineMap:
1233 # Inlined map
1234 << : {"x": 1, "y": 2, "r": 10}
1235 label: center/big
1236
1237 inlineSequenceMap:
1238 # Inlined map in sequence
1239 << : [ *CENTER, {"r": 10} ]
1240 label: center/big
1241 `
1242
1243 func TestMerge(t *testing.T) {
1244 var want = map[interface{}]interface{}{
1245 "x": 1,
1246 "y": 2,
1247 "r": 10,
1248 "label": "center/big",
1249 }
1250
1251 var m map[interface{}]interface{}
1252 err := yaml.Unmarshal([]byte(mergeTests), &m)
1253 if err != nil {
1254 t.Fatalf("Unmarshal() returned error: %v", err)
1255 }
1256 for name, test := range m {
1257 if name == "anchors" {
1258 continue
1259 }
1260 if !reflect.DeepEqual(test, want) {
1261 t.Errorf("test %q failed: got %#v, want %#v", name, test, want)
1262 }
1263 }
1264 }
1265
1266 func TestMergeStruct(t *testing.T) {
1267 type Data struct {
1268 X, Y, R int
1269 Label string
1270 }
1271 want := Data{1, 2, 10, "center/big"}
1272
1273 var m map[string]Data
1274 err := yaml.Unmarshal([]byte(mergeTests), &m)
1275 if err != nil {
1276 t.Fatalf("Unmarshal() returned error: %v", err)
1277 }
1278 for name, test := range m {
1279 if name == "anchors" {
1280 continue
1281 }
1282 if !reflect.DeepEqual(test, want) {
1283 t.Errorf("test %q failed: got %#v, want %#v", name, test, want)
1284 }
1285 }
1286 }
1287
1288 var unmarshalNullTests = []func() interface{}{
1289 func() interface{} { var v interface{}; v = "v"; return &v },
1290 func() interface{} { var s = "s"; return &s },
1291 func() interface{} { var s = "s"; sptr := &s; return &sptr },
1292 func() interface{} { var i = 1; return &i },
1293 func() interface{} { var i = 1; iptr := &i; return &iptr },
1294 func() interface{} { m := map[string]int{"s": 1}; return &m },
1295 func() interface{} { m := map[string]int{"s": 1}; return m },
1296 }
1297
1298 func TestUnmarshalNull(t *testing.T) {
1299 for _, test := range unmarshalNullTests {
1300 item := test()
1301 zero := reflect.Zero(reflect.TypeOf(item).Elem()).Interface()
1302 err := yaml.Unmarshal([]byte("null"), item)
1303 if err != nil {
1304 t.Fatalf("Unmarshal() returned error: %v", err)
1305 }
1306 if reflect.TypeOf(item).Kind() == reflect.Map {
1307 if !reflect.DeepEqual(reflect.ValueOf(item).Interface(), reflect.MakeMap(reflect.TypeOf(item)).Interface()) {
1308 t.Fatalf("Unmarshal() returned %v, want empty map", reflect.ValueOf(item).Interface())
1309 }
1310 } else {
1311 if !reflect.DeepEqual(reflect.ValueOf(item).Elem().Interface(), zero) {
1312 t.Fatalf("Unmarshal() returned %v, want %v", reflect.ValueOf(item).Elem().Interface(), zero)
1313 }
1314 }
1315 }
1316 }
1317
1318 func TestUnmarshalSliceOnPreset(t *testing.T) {
1319 // Issue #48.
1320 v := struct{ A []int }{[]int{1}}
1321 err := yaml.Unmarshal([]byte("a: [2]"), &v)
1322 if err != nil {
1323 t.Fatalf("Unmarshal() returned error: %v", err)
1324 }
1325 if !reflect.DeepEqual(v.A, []int{2}) {
1326 t.Fatalf("v.A is %v, want %v", v.A, []int{2})
1327 }
1328 }
1329
1330 var unmarshalStrictTests = []struct {
1331 data string
1332 value interface{}
1333 error string
1334 }{{
1335 data: "a: 1\nc: 2\n",
1336 value: struct{ A, B int }{A: 1},
1337 error: "yaml: unmarshal errors:\n line 2: field c not found in type struct { A int; B int }",
1338 }, {
1339 data: "a: 1\nb: 2\na: 3\n",
1340 value: struct{ A, B int }{A: 3, B: 2},
1341 error: "yaml: unmarshal errors:\n line 3: field a already set in type struct { A int; B int }",
1342 }, {
1343 data: "c: 3\na: 1\nb: 2\nc: 4\n",
1344 value: struct {
1345 A int
1346 inlineB `yaml:",inline"`
1347 }{
1348 A: 1,
1349 inlineB: inlineB{
1350 B: 2,
1351 inlineC: inlineC{
1352 C: 4,
1353 },
1354 },
1355 },
1356 error: "yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB \"yaml:\\\",inline\\\"\" }",
1357 }, {
1358 data: "c: 0\na: 1\nb: 2\nc: 1\n",
1359 value: struct {
1360 A int
1361 inlineB `yaml:",inline"`
1362 }{
1363 A: 1,
1364 inlineB: inlineB{
1365 B: 2,
1366 inlineC: inlineC{
1367 C: 1,
1368 },
1369 },
1370 },
1371 error: "yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB \"yaml:\\\",inline\\\"\" }",
1372 }, {
1373 data: "c: 1\na: 1\nb: 2\nc: 3\n",
1374 value: struct {
1375 A int
1376 M map[string]interface{} `yaml:",inline"`
1377 }{
1378 A: 1,
1379 M: map[string]interface{}{
1380 "b": 2,
1381 "c": 3,
1382 },
1383 },
1384 error: "yaml: unmarshal errors:\n line 4: key \"c\" already set in map",
1385 }, {
1386 data: "a: 1\n9: 2\nnull: 3\n9: 4",
1387 value: map[interface{}]interface{}{
1388 "a": 1,
1389 nil: 3,
1390 9: 4,
1391 },
1392 error: "yaml: unmarshal errors:\n line 4: key 9 already set in map",
1393 }}
1394
1395 func TestUnmarshalStrict(t *testing.T) {
1396 for i, item := range unmarshalStrictTests {
1397 t.Logf("test %d: %q", i, item.data)
1398 // First test that normal Unmarshal unmarshals to the expected value.
1399 typ := reflect.ValueOf(item.value).Type()
1400 value := reflect.New(typ)
1401 err := yaml.Unmarshal([]byte(item.data), value.Interface())
1402 if err != nil {
1403 t.Fatalf("Unmarshal() returned error: %v", err)
1404 }
1405 if !reflect.DeepEqual(value.Elem().Interface(), item.value) {
1406 t.Fatalf("Unmarshal() returned %v, want %v", value.Elem().Interface(), item.value)
1407 }
1408
1409 // Then test that UnmarshalStrict fails on the same thing.
1410 typ = reflect.ValueOf(item.value).Type()
1411 value = reflect.New(typ)
1412 err = yaml.UnmarshalStrict([]byte(item.data), value.Interface())
1413 if err == nil {
1414 t.Fatalf("UnmarshalStrict() returned no error, want %q", item.error)
1415 }
1416 if err.Error() != item.error {
1417 t.Fatalf("UnmarshalStrict() returned error %q, want %q", err, item.error)
1418 }
1419 }
1420 }
1421
1422 type textUnmarshaler struct {
1423 S string
1424 }
1425
1426 func (t *textUnmarshaler) UnmarshalText(s []byte) error {
1427 t.S = string(s)
1428 return nil
1429 }
1430
1431 func TestFuzzCrashers(t *testing.T) {
1432 cases := []string{
1433 // runtime error: index out of range
1434 "\"\\0\\\r\n",
1435
1436 // should not happen
1437 " 0: [\n] 0",
1438 "? ? \"\n\" 0",
1439 " - {\n000}0",
1440 "0:\n 0: [0\n] 0",
1441 " - \"\n000\"0",
1442 " - \"\n000\"\"",
1443 "0:\n - {\n000}0",
1444 "0:\n - \"\n000\"0",
1445 "0:\n - \"\n000\"\"",
1446
1447 // runtime error: index out of range
1448 " \ufeff\n",
1449 "? \ufeff\n",
1450 "? \ufeff:\n",
1451 "0: \ufeff\n",
1452 "? \ufeff: \ufeff\n",
1453 }
1454 for _, data := range cases {
1455 var v interface{}
1456 _ = yaml.Unmarshal([]byte(data), &v)
1457 }
1458 }
1459
1460 //var data []byte
1461 //func init() {
1462 // var err error
1463 // data, err = ioutil.ReadFile("/tmp/file.yaml")
1464 // if err != nil {
1465 // panic(err)
1466 // }
1467 //}
1468 //
1469 //func (s *S) BenchmarkUnmarshal(c *C) {
1470 // var err error
1471 // for i := 0; i < c.N; i++ {
1472 // var v map[string]interface{}
1473 // err = yaml.Unmarshal(data, &v)
1474 // }
1475 // if err != nil {
1476 // panic(err)
1477 // }
1478 //}
1479 //
1480 //func (s *S) BenchmarkMarshal(c *C) {
1481 // var v map[string]interface{}
1482 // yaml.Unmarshal(data, &v)
1483 // c.ResetTimer()
1484 // for i := 0; i < c.N; i++ {
1485 // yaml.Marshal(&v)
1486 // }
1487 //}