Debian Codegraph @

path_test.go

raw symbols SCIP
1 package yaml_test
2
3 import (
4 "fmt"
5 "log"
6 "reflect"
7 "strings"
8 "testing"
9
10 "github.com/goccy/go-yaml"
11 "github.com/goccy/go-yaml/parser"
12 )
13
14 func builder() *yaml.PathBuilder { return &yaml.PathBuilder{} }
15
16 func TestPathBuilder(t *testing.T) {
17 tests := []struct {
18 expected string
19 path *yaml.Path
20 }{
21 {
22 expected: `$.a.b[0]`,
23 path: builder().Root().Child("a").Child("b").Index(0).Build(),
24 },
25 {
26 expected: `$.'a.b'.'c*d'`,
27 path: builder().Root().Child("a.b").Child("c*d").Build(),
28 },
29 {
30 expected: `$.'a.b-*'.c`,
31 path: builder().Root().Child("a.b-*").Child("c").Build(),
32 },
33 {
34 expected: `$.'a'.b`,
35 path: builder().Root().Child("'a'").Child("b").Build(),
36 },
37 {
38 expected: `$.'a.b'.c`,
39 path: builder().Root().Child("'a.b'").Child("c").Build(),
40 },
41 }
42 for _, test := range tests {
43 t.Run(test.expected, func(t *testing.T) {
44 expected := test.expected
45 got := test.path.String()
46 if expected != got {
47 t.Fatalf("failed to build path. expected:[%q] but got:[%q]", expected, got)
48 }
49 })
50 }
51 }
52
53 func TestPath(t *testing.T) {
54 yml := `
55 store:
56 book:
57 - author: john
58 price: 10
59 - author: ken
60 price: 12
61 bicycle:
62 color: red
63 price: 19.95
64 bicycle*unicycle:
65 price: 20.25
66 `
67 tests := []struct {
68 name string
69 path *yaml.Path
70 expected interface{}
71 }{
72 {
73 name: "$.store.book[0].author",
74 path: builder().Root().Child("store").Child("book").Index(0).Child("author").Build(),
75 expected: "john",
76 },
77 {
78 name: "$.store.book[1].price",
79 path: builder().Root().Child("store").Child("book").Index(1).Child("price").Build(),
80 expected: uint64(12),
81 },
82 {
83 name: "$.store.book[*].author",
84 path: builder().Root().Child("store").Child("book").IndexAll().Child("author").Build(),
85 expected: []interface{}{"john", "ken"},
86 },
87 {
88 name: "$.store.book[*]",
89 path: builder().Root().Child("store").Child("book").IndexAll().Build(),
90 expected: []interface{}{
91 map[string]interface{}{
92 "author": "john",
93 "price": uint64(10),
94 },
95 map[string]interface{}{
96 "author": "ken",
97 "price": uint64(12),
98 },
99 },
100 },
101 {
102 name: "$..book[*]",
103 path: builder().Root().Recursive("book").IndexAll().Build(),
104 expected: []interface{}{
105 []interface{}{
106 map[string]interface{}{
107 "author": "john",
108 "price": uint64(10),
109 },
110 map[string]interface{}{
111 "author": "ken",
112 "price": uint64(12),
113 },
114 },
115 },
116 },
117 {
118 name: "$.store.book[0]",
119 path: builder().Root().Child("store").Child("book").Index(0).Build(),
120 expected: map[string]interface{}{"author": "john", "price": uint64(10)},
121 },
122 {
123 name: "$..author",
124 path: builder().Root().Recursive("author").Build(),
125 expected: []interface{}{"john", "ken"},
126 },
127 {
128 name: "$.store.bicycle.price",
129 path: builder().Root().Child("store").Child("bicycle").Child("price").Build(),
130 expected: float64(19.95),
131 },
132 {
133 name: `$.store.'bicycle*unicycle'.price`,
134 path: builder().Root().Child("store").Child(`bicycle*unicycle`).Child("price").Build(),
135 expected: float64(20.25),
136 },
137 {
138 name: "$",
139 path: builder().Root().Build(),
140 expected: map[string]interface{}{
141 "store": map[string]interface{}{
142 "book": []interface{}{
143 map[string]interface{}{
144 "author": "john",
145 "price": uint64(10),
146 },
147 map[string]interface{}{
148 "author": "ken",
149 "price": uint64(12),
150 },
151 },
152 "bicycle": map[string]interface{}{
153 "color": "red",
154 "price": 19.95,
155 },
156 "bicycle*unicycle": map[string]interface{}{
157 "price": 20.25,
158 },
159 },
160 },
161 },
162 }
163 t.Run("PathString", func(t *testing.T) {
164 for _, test := range tests {
165 t.Run(test.name, func(t *testing.T) {
166 path, err := yaml.PathString(test.name)
167 if err != nil {
168 t.Fatalf("%+v", err)
169 }
170 if test.name != path.String() {
171 t.Fatalf("expected %s but actual %s", test.name, path.String())
172 }
173 })
174 }
175 })
176 t.Run("string", func(t *testing.T) {
177 for _, test := range tests {
178 t.Run(test.name, func(t *testing.T) {
179 if test.name != test.path.String() {
180 t.Fatalf("expected %s but actual %s", test.name, test.path.String())
181 }
182 })
183 }
184 })
185 t.Run("read", func(t *testing.T) {
186 for _, test := range tests {
187 t.Run(test.name, func(t *testing.T) {
188 var v interface{}
189 if err := test.path.Read(strings.NewReader(yml), &v); err != nil {
190 t.Fatalf("%+v", err)
191 }
192 if !reflect.DeepEqual(test.expected, v) {
193 t.Fatalf("expected %v(%T). but actual %v(%T)", test.expected, test.expected, v, v)
194 }
195 })
196 }
197 })
198 t.Run("filter", func(t *testing.T) {
199 var target interface{}
200 if err := yaml.Unmarshal([]byte(yml), &target); err != nil {
201 t.Fatalf("failed to unmarshal: %+v", err)
202 }
203 for _, test := range tests {
204 t.Run(test.name, func(t *testing.T) {
205 var v interface{}
206 if err := test.path.Filter(target, &v); err != nil {
207 t.Fatalf("%+v", err)
208 }
209 if !reflect.DeepEqual(test.expected, v) {
210 t.Fatalf("expected %v(%T). but actual %v(%T)", test.expected, test.expected, v, v)
211 }
212 })
213 }
214 })
215 }
216
217 func TestPath_ReservedKeyword(t *testing.T) {
218 tests := []struct {
219 name string
220 path string
221 src string
222 expected interface{}
223 failure bool
224 }{
225 {
226 name: "quoted path",
227 path: `$.'a.b.c'.foo`,
228 src: `
229 a.b.c:
230 foo: bar
231 `,
232 expected: "bar",
233 },
234 {
235 name: "contains quote key",
236 path: `$.a'b`,
237 src: `a'b: 10`,
238 expected: uint64(10),
239 },
240 {
241 name: "escaped quote",
242 path: `$.'alice\'s age'`,
243 src: `alice's age: 10`,
244 expected: uint64(10),
245 },
246 {
247 name: "directly use white space",
248 path: `$.a b`,
249 src: `a b: 10`,
250 expected: uint64(10),
251 },
252 {
253 name: "empty quoted key",
254 path: `$.''`,
255 src: `a: 10`,
256 failure: true,
257 },
258 {
259 name: "unterminated quote",
260 path: `$.'abcd`,
261 src: `abcd: 10`,
262 failure: true,
263 },
264 }
265 for _, test := range tests {
266 t.Run(test.name, func(t *testing.T) {
267 path, err := yaml.PathString(test.path)
268 if test.failure {
269 if err == nil {
270 t.Fatal("expected error")
271 }
272 return
273 } else {
274 if err != nil {
275 t.Fatalf("%+v", err)
276 }
277 }
278 file, err := parser.ParseBytes([]byte(test.src), 0)
279 if err != nil {
280 t.Fatal(err)
281 }
282 var v interface{}
283 if err := path.Read(file, &v); err != nil {
284 t.Fatalf("%+v", err)
285 }
286 if v != test.expected {
287 t.Fatalf("failed to get value. expected:[%v] but got:[%v]", test.expected, v)
288 }
289 })
290 }
291 }
292
293 func TestPath_Invalid(t *testing.T) {
294 tests := []struct {
295 path string
296 src string
297 }{
298 {
299 path: "$.wrong",
300 src: "foo: bar",
301 },
302 }
303 for _, test := range tests {
304 path, err := yaml.PathString(test.path)
305 if err != nil {
306 t.Fatal(err)
307 }
308 t.Run("path.Read", func(t *testing.T) {
309 file, err := parser.ParseBytes([]byte(test.src), 0)
310 if err != nil {
311 t.Fatal(err)
312 }
313 var v interface{}
314 err = path.Read(file, &v)
315 if err == nil {
316 t.Fatal("expected error")
317 }
318 if !yaml.IsNotFoundNodeError(err) {
319 t.Fatalf("unexpected error %s", err)
320 }
321 })
322 t.Run("path.ReadNode", func(t *testing.T) {
323 file, err := parser.ParseBytes([]byte(test.src), 0)
324 if err != nil {
325 t.Fatal(err)
326 }
327 _, err = path.ReadNode(file)
328 if err == nil {
329 t.Fatal("expected error")
330 }
331 if !yaml.IsNotFoundNodeError(err) {
332 t.Fatalf("unexpected error %s", err)
333 }
334 })
335 }
336 }
337
338 func TestPath_ReadNode(t *testing.T) {
339 tests := []struct {
340 name string
341 path string
342 src string
343 expected interface{}
344 }{
345 {
346 name: "nested array sequence",
347 path: `$.a.b[0].c`,
348 src: `
349 a:
350 b:
351 - c: 123
352 e: |
353 Line1
354 Line2
355 `,
356 expected: uint64(123),
357 },
358 {
359 name: "nested array sequence issue#281",
360 path: `$..a.c`,
361 src: `
362 s:
363 - a:
364 b: u1
365 c: get1
366 d: i1
367 - w:
368 c: bad
369 e:
370 - a:
371 b: u2
372 c: get2
373 d: i2
374 `,
375 // The expected values are
376 // - get1
377 // - get2
378 expected: []interface{}{
379 map[string]interface{}{
380 "b": "u1",
381 "c": "get1",
382 "d": "i1",
383 },
384 map[string]interface{}{
385 "b": "u2",
386 "c": "get2",
387 "d": "i2",
388 },
389 },
390 },
391 {
392 name: "nested array sequence issue#281",
393 path: `$..c`,
394 src: `
395 s:
396 - a:
397 b: u1
398 c: get1
399 d: i1
400 - w:
401 c: bad
402 e:
403 - a:
404 b: u2
405 c: get2
406 d: i2
407 `,
408 expected: []interface{}{"get1", "bad", "get2"},
409 },
410 {
411 name: "nested array sequence issue#281",
412 path: `$.s[0].a.c`,
413 src: `
414 s:
415 - a:
416 b: u1
417 c: get1
418 d: i1
419 - w:
420 c: bad
421 e:
422 - a:
423 b: u2
424 c: get2
425 d: i2
426 `,
427 expected: "get1",
428 },
429 {
430 name: "nested array sequence issue#281",
431 path: "$.s[*].a.c",
432 src: `
433 s:
434 - a:
435 b: u1
436 c: get1
437 d: i1
438 - w:
439 c: bad
440 e:
441 - a:
442 b: u2
443 c: get2
444 d: i2
445 `,
446 expected: []interface{}{"get1"},
447 },
448 }
449 for _, test := range tests {
450 path, err := yaml.PathString(test.path)
451 if err != nil {
452 t.Fatal(err)
453 }
454 t.Run(fmt.Sprintf("path.ReadNode %s path %s", test.name, test.path), func(t *testing.T) {
455 file, err := parser.ParseBytes([]byte(test.src), 0)
456 if err != nil {
457 t.Fatal(err)
458 }
459 n, err := path.ReadNode(file)
460 if err != nil {
461 t.Fatal("expected error", err)
462 }
463 var v interface{}
464 err = yaml.Unmarshal([]byte(n.String()), &v)
465 if err != nil {
466 t.Fatal(err)
467 }
468 if !reflect.DeepEqual(test.expected, v) {
469 t.Fatalf("expected %v(%T) but got %v(%T)", test.expected, test.expected, v, v)
470 }
471 })
472 }
473 }
474
475 func TestPath_Merge(t *testing.T) {
476 tests := []struct {
477 path string
478 dst string
479 src string
480 expected string
481 }{
482 {
483 path: "$.c",
484 dst: `
485 a: 1
486 b: 2
487 c:
488 d: 3
489 e: 4
490 `,
491 src: `
492 f: 5
493 g: 6
494 `,
495 expected: `
496 a: 1
497 b: 2
498 c:
499 d: 3
500 e: 4
501 f: 5
502 g: 6
503 `,
504 },
505 {
506 path: "$.a.b",
507 dst: `
508 a:
509 b:
510 - 1
511 - 2
512 `,
513 src: `
514 - 3
515 - map:
516 - 4
517 - 5
518 `,
519 expected: `
520 a:
521 b:
522 - 1
523 - 2
524 - 3
525 - map:
526 - 4
527 - 5
528 `,
529 },
530 }
531 for _, test := range tests {
532 t.Run(test.path, func(t *testing.T) {
533 path, err := yaml.PathString(test.path)
534 if err != nil {
535 t.Fatalf("%+v", err)
536 }
537 t.Run("FromReader", func(t *testing.T) {
538 file, err := parser.ParseBytes([]byte(test.dst), 0)
539 if err != nil {
540 t.Fatalf("%+v", err)
541 }
542 if err := path.MergeFromReader(file, strings.NewReader(test.src)); err != nil {
543 t.Fatalf("%+v", err)
544 }
545 actual := "\n" + file.String()
546 if test.expected != actual {
547 t.Fatalf("expected: %q. but got %q", test.expected, actual)
548 }
549 })
550 t.Run("FromFile", func(t *testing.T) {
551 file, err := parser.ParseBytes([]byte(test.dst), 0)
552 if err != nil {
553 t.Fatalf("%+v", err)
554 }
555 src, err := parser.ParseBytes([]byte(test.src), 0)
556 if err != nil {
557 t.Fatalf("%+v", err)
558 }
559 if err := path.MergeFromFile(file, src); err != nil {
560 t.Fatalf("%+v", err)
561 }
562 actual := "\n" + file.String()
563 if test.expected != actual {
564 t.Fatalf("expected: %q. but got %q", test.expected, actual)
565 }
566 })
567 t.Run("FromNode", func(t *testing.T) {
568 file, err := parser.ParseBytes([]byte(test.dst), 0)
569 if err != nil {
570 t.Fatalf("%+v", err)
571 }
572 src, err := parser.ParseBytes([]byte(test.src), 0)
573 if err != nil {
574 t.Fatalf("%+v", err)
575 }
576 if len(src.Docs) == 0 {
577 t.Fatalf("failed to parse")
578 }
579 if err := path.MergeFromNode(file, src.Docs[0]); err != nil {
580 t.Fatalf("%+v", err)
581 }
582 actual := "\n" + file.String()
583 if test.expected != actual {
584 t.Fatalf("expected: %q. but got %q", test.expected, actual)
585 }
586 })
587 })
588 }
589 }
590
591 func TestPath_Replace(t *testing.T) {
592 tests := []struct {
593 path string
594 dst string
595 src string
596 expected string
597 }{
598 {
599 path: "$.a",
600 dst: `
601 a: 1
602 b: 2
603 `,
604 src: `3`,
605 expected: `
606 a: 3
607 b: 2
608 `,
609 },
610 {
611 path: "$.b",
612 dst: `
613 b: 1
614 c: 2
615 `,
616 src: `
617 d: e
618 f:
619 g: h
620 i: j
621 `,
622 expected: `
623 b:
624 d: e
625 f:
626 g: h
627 i: j
628 c: 2
629 `,
630 },
631 {
632 path: "$.a.b[0]",
633 dst: `
634 a:
635 b:
636 - hello
637 c: 2
638 `,
639 src: `world`,
640 expected: `
641 a:
642 b:
643 - world
644 c: 2
645 `,
646 },
647
648 {
649 path: "$.books[*].author",
650 dst: `
651 books:
652 - name: book_a
653 author: none
654 - name: book_b
655 author: none
656 pictures:
657 - name: picture_a
658 author: none
659 - name: picture_b
660 author: none
661 building:
662 author: none
663 `,
664 src: `ken`,
665 expected: `
666 books:
667 - name: book_a
668 author: ken
669 - name: book_b
670 author: ken
671 pictures:
672 - name: picture_a
673 author: none
674 - name: picture_b
675 author: none
676 building:
677 author: none
678 `,
679 },
680 {
681 path: "$..author",
682 dst: `
683 books:
684 - name: book_a
685 author: none
686 - name: book_b
687 author: none
688 pictures:
689 - name: picture_a
690 author: none
691 - name: picture_b
692 author: none
693 building:
694 author: none
695 `,
696 src: `ken`,
697 expected: `
698 books:
699 - name: book_a
700 author: ken
701 - name: book_b
702 author: ken
703 pictures:
704 - name: picture_a
705 author: ken
706 - name: picture_b
707 author: ken
708 building:
709 author: ken
710 `,
711 },
712 }
713 for _, test := range tests {
714 t.Run(test.path, func(t *testing.T) {
715 path, err := yaml.PathString(test.path)
716 if err != nil {
717 t.Fatalf("%+v", err)
718 }
719 t.Run("WithReader", func(t *testing.T) {
720 file, err := parser.ParseBytes([]byte(test.dst), 0)
721 if err != nil {
722 t.Fatalf("%+v", err)
723 }
724 if err := path.ReplaceWithReader(file, strings.NewReader(test.src)); err != nil {
725 t.Fatalf("%+v", err)
726 }
727 actual := "\n" + file.String()
728 if test.expected != actual {
729 t.Fatalf("expected: %q. but got %q", test.expected, actual)
730 }
731 })
732 t.Run("WithFile", func(t *testing.T) {
733 file, err := parser.ParseBytes([]byte(test.dst), 0)
734 if err != nil {
735 t.Fatalf("%+v", err)
736 }
737 src, err := parser.ParseBytes([]byte(test.src), 0)
738 if err != nil {
739 t.Fatalf("%+v", err)
740 }
741 if err := path.ReplaceWithFile(file, src); err != nil {
742 t.Fatalf("%+v", err)
743 }
744 actual := "\n" + file.String()
745 if test.expected != actual {
746 t.Fatalf("expected: %q. but got %q", test.expected, actual)
747 }
748 })
749 t.Run("WithNode", func(t *testing.T) {
750 file, err := parser.ParseBytes([]byte(test.dst), 0)
751 if err != nil {
752 t.Fatalf("%+v", err)
753 }
754 src, err := parser.ParseBytes([]byte(test.src), 0)
755 if err != nil {
756 t.Fatalf("%+v", err)
757 }
758 if len(src.Docs) == 0 {
759 t.Fatalf("failed to parse")
760 }
761 if err := path.ReplaceWithNode(file, src.Docs[0]); err != nil {
762 t.Fatalf("%+v", err)
763 }
764 actual := "\n" + file.String()
765 if test.expected != actual {
766 t.Fatalf("expected: %q. but got %q", test.expected, actual)
767 }
768 })
769 })
770 }
771 }
772
773 func TestInvalidPath(t *testing.T) {
774 tests := []struct {
775 name string
776 path string
777 }{
778 {
779 name: "missing root with dot",
780 path: ".foo",
781 },
782 {
783 name: "missing root with index",
784 path: "foo[0]",
785 },
786 {
787 name: "missing root with recursive",
788 path: "..foo",
789 },
790 }
791 for _, test := range tests {
792 t.Run(test.name, func(t *testing.T) {
793 if _, err := yaml.PathString(test.path); err == nil {
794 t.Fatal("expected error")
795 }
796 })
797 }
798 }
799
800 func ExamplePath_AnnotateSource() {
801 yml := `
802 a: 1
803 b: "hello"
804 `
805 var v struct {
806 A int
807 B string
808 }
809 if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
810 panic(err)
811 }
812 if v.A != 2 {
813 // output error with YAML source
814 path, err := yaml.PathString("$.a")
815 if err != nil {
816 log.Fatal(err)
817 }
818 source, err := path.AnnotateSource([]byte(yml), false)
819 if err != nil {
820 log.Fatal(err)
821 }
822 fmt.Printf("a value expected 2 but actual %d:\n%s\n", v.A, string(source))
823 }
824 // OUTPUT:
825 // a value expected 2 but actual 1:
826 // > 2 | a: 1
827 // ^
828 // 3 | b: "hello"
829 }
830
831 func ExamplePath_AnnotateSource_withComment() {
832 yml := `
833 # This is my document
834 doc:
835 # This comment should be line 3
836 map:
837 # And below should be line 5
838 - value1
839 - value2
840 other: value3
841 `
842 path, err := yaml.PathString("$.doc.map[0]")
843 if err != nil {
844 log.Fatal(err)
845 }
846 msg, err := path.AnnotateSource([]byte(yml), false)
847 if err != nil {
848 log.Fatal(err)
849 }
850 fmt.Println(string(msg))
851 // OUTPUT:
852 // 4 | # This comment should be line 3
853 // 5 | map:
854 // 6 | # And below should be line 5
855 // > 7 | - value1
856 // ^
857 // 8 | - value2
858 // 9 | other: value3
859 }
860
861 func ExamplePathString() {
862 yml := `
863 store:
864 book:
865 - author: john
866 price: 10
867 - author: ken
868 price: 12
869 bicycle:
870 color: red
871 price: 19.95
872 `
873 path, err := yaml.PathString("$.store.book[*].author")
874 if err != nil {
875 log.Fatal(err)
876 }
877 var authors []string
878 if err := path.Read(strings.NewReader(yml), &authors); err != nil {
879 log.Fatal(err)
880 }
881 fmt.Println(authors)
882 // OUTPUT:
883 // [john ken]
884 }