Debian Codegraph @

testdata/encode.yaml

raw symbols SCIP
1 # Copyright 2025 The go-yaml Project Contributors
2 # SPDX-License-Identifier: Apache-2.0
3
4 # Encode test cases for go-yaml
5 #
6 # Purpose: Tests YAML marshaling/encoding of Go values into YAML output.
7 # Validates proper formatting, quoting, type representation, null handling,
8 # special values, and YAML syntax correctness.
9 #
10 # Format: Test cases use type-as-key format:
11 # - test-type:
12 # name: Test case name
13 # ...
14 #
15 # Test Types:
16 # encode - Validates YAML marshaling of Go values
17 #
18 # Common Keys:
19 # name - Test case name (string)
20 # data - Go value to marshal (YAML representation)
21 # type - Go type of the value (e.g., "map[string]any", "[]string")
22 # want - Expected YAML output (string, typically using | for multiline)
23 #
24 # Data Types:
25 # Map types: map[string]string, map[string]any, map[string]uint64, etc.
26 # Slice types: []string, []int, []any, map[string][]string, etc.
27 # Numeric types: map[string]int, map[string]uint, map[string]float64, etc.
28 #
29 # Special Values:
30 # Constants like +Inf, -Inf, NaN are resolved via ValueRegistry during test
31 # execution. These are represented as string tokens in the data field and
32 # replaced with actual Go values (math.Inf, math.NaN) before marshaling.
33 #
34 # Note:
35 # The test framework unmarshals the 'data' into the specified 'type',
36 # then marshals it back to YAML and compares with 'want'. This ensures the
37 # value can be properly constructed from YAML representation.
38
39 # Basic string values
40 - encode:
41 name: string value in map[string]string
42 data:
43 v: hi
44 type: map[string]string
45 want: |
46 v: hi
47
48 - encode:
49 name: string value in map[string]any
50 data:
51 v: hi
52 type: map[string]any
53 want: |
54 v: hi
55
56 # String values that look like booleans
57 - encode:
58 name: string "true" in map[string]string
59 data:
60 v: 'true'
61 type: map[string]string
62 want: |
63 v: "true"
64
65 - encode:
66 name: string "false" in map[string]string
67 data:
68 v: 'false'
69 type: map[string]string
70 want: |
71 v: "false"
72
73 # Boolean values
74 - encode:
75 name: bool true in map[string]any
76 data:
77 v: true
78 type: map[string]any
79 want: |
80 v: true
81
82 - encode:
83 name: bool false in map[string]any
84 data:
85 v: false
86 type: map[string]any
87 want: |
88 v: false
89
90 # Integer values
91 - encode:
92 name: positive integer in map[string]any
93 data:
94 v: 10
95 type: map[string]any
96 want: |
97 v: 10
98
99 - encode:
100 name: negative integer in map[string]any
101 data:
102 v: -10
103 type: map[string]any
104 want: |
105 v: -10
106
107 - encode:
108 name: uint value in map[string]uint
109 data:
110 v: 42
111 type: map[string]uint
112 want: |
113 v: 42
114
115 - encode:
116 name: large int64 in map[string]any
117 data:
118 v: 4294967296
119 type: map[string]any
120 want: |
121 v: 4294967296
122
123 - encode:
124 name: large int64 in map[string]int64
125 data:
126 v: 4294967296
127 type: map[string]int64
128 want: |
129 v: 4294967296
130
131 - encode:
132 name: large uint64 in map[string]uint64
133 data:
134 v: 4294967296
135 type: map[string]uint64
136 want: |
137 v: 4294967296
138
139 # String that looks like a number
140 - encode:
141 name: string "10" in map[string]any
142 data:
143 v: '10'
144 type: map[string]any
145 want: |
146 v: "10"
147
148 # Float values
149 - encode:
150 name: float 0.1 in map[string]any
151 data:
152 v: 0.1
153 type: map[string]any
154 want: |
155 v: 0.1
156
157 - encode:
158 name: explicit float64 0.1
159 data:
160 v: 0.1
161 type: map[string]any
162 want: |
163 v: 0.1
164
165 - encode:
166 name: float32 0.99
167 data:
168 v: 0.99
169 type: map[string]any
170 want: |
171 v: 0.99
172
173 - encode:
174 name: negative float in map[string]any
175 data:
176 v: -0.1
177 type: map[string]any
178 want: |
179 v: -0.1
180
181 # Special float values
182 - encode:
183 name: positive infinity
184 data:
185 v: +Inf
186 type: map[string]any
187 want: |
188 v: .inf
189
190 - encode:
191 name: negative infinity
192 data:
193 v: -Inf
194 type: map[string]any
195 want: |
196 v: -.inf
197
198 - encode:
199 name: NaN
200 data:
201 v: NaN
202 type: map[string]any
203 want: |
204 v: .nan
205
206 # Null and empty string
207 - encode:
208 name: null value in map[string]any
209 data:
210 v: null
211 type: map[string]any
212 want: |
213 v: null
214
215 - encode:
216 name: empty string in map[string]any
217 data:
218 v: ''
219 type: map[string]any
220 want: |
221 v: ""
222
223 # String slices
224 - encode:
225 name: simple string slice
226 data:
227 v: [A, B]
228 type: map[string][]string
229 want: |
230 v:
231 - A
232 - B
233
234 - encode:
235 name: string slice with multiline element
236 data:
237 v: ['A', "B\nC"]
238 type: map[string][]string
239 want: |
240 v:
241 - A
242 - |-
243 B
244 C
245
246 # Complex nested structure
247 - encode:
248 name: nested mixed slice with map
249 data:
250 v: ['A', 1, {B: [2, 3]}]
251 type: map[string][]any
252 want: |
253 v:
254 - A
255 - 1
256 - B:
257 - 2
258 - 3
259
260 # Issue #65: Multiline strings starting with newline
261 - encode:
262 name: string starting with newline in map
263 data:
264 v: "\nhi"
265 type: map[string]any
266 want: |
267 v: |-
268
269 hi
270
271 - encode:
272 name: nested structure with leading newline
273 data:
274 v:
275 - v1: "\nhi"
276 type: map[string][]map[string]any
277 want: |
278 v:
279 - v1: |-
280
281 hi
282
283 # Nested maps
284 - encode:
285 name: nested map
286 data:
287 a:
288 b: c
289 type: map[string]any
290 want: |
291 a:
292 b: c
293
294 # Special string values requiring quoting
295 - encode:
296 name: dash string requiring quotes
297 data:
298 a: '-'
299 type: map[string]any
300 want: |
301 a: '-'
302
303 # Special characters and edge cases
304 - encode:
305 name: angle brackets in string
306 data:
307 a: '<foo>'
308 type: map[string]string
309 want: |
310 a: <foo>
311
312 - encode:
313 name: colon-separated string (base 60 float compat)
314 data:
315 a: '1:1'
316 type: map[string]string
317 want: |
318 a: '1:1'
319
320 - encode:
321 name: null byte in string
322 data:
323 a: "\x00"
324 type: map[string]string
325 want: |
326 a: "\0"
327
328 - encode:
329 name: unicode string
330 data:
331 a: '你好'
332 type: map[string]string
333 want: |
334 a: 你好
335
336 - encode:
337 name: timestamp-like string gets quoted
338 data:
339 a: '2015-02-24T18:19:39Z'
340 type: map[string]string
341 want: |
342 a: "2015-02-24T18:19:39Z"
343
344 - encode:
345 name: string containing colon-space
346 data:
347 a: 'b: c'
348 type: map[string]string
349 want: |
350 a: 'b: c'
351
352 - encode:
353 name: string with hash comment
354 data:
355 a: 'Hello #comment'
356 type: map[string]string
357 want: |
358 a: 'Hello #comment'
359
360 - encode:
361 name: unicode string with hash comment
362 data:
363 a: '你好 #comment'
364 type: map[string]string
365 want: |
366 a: '你好 #comment'
367
368 # Nested structures
369 - encode:
370 name: deeply nested maps and sequences
371 data:
372 a:
373 b:
374 - c: 1
375 d: 2
376 type: map[string]any
377 want: |
378 a:
379 b:
380 - c: 1
381 d: 2
382
383 # Tab handling
384 - encode:
385 name: string with only tabs and newlines (quoted)
386 data:
387 a: "\t\n\t\n"
388 type: map[string]string
389 want: |
390 a: "\t\n\t\n"
391
392 # Merge key edge cases
393 - encode:
394 name: merge key as regular key
395 data:
396 '<<': []
397 type: map[string]any
398 want: |
399 '<<': []
400
401 - encode:
402 name: merge key as value
403 data:
404 foo: '<<'
405 type: map[string]any
406 want: |
407 foo: '<<'
408
409 # Long strings
410 - encode:
411 name: long string no wrapping
412 data:
413 a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '
414 type: map[string]string
415 want: |
416 a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '
417
418 # Struct encoding tests
419 - encode:
420 name: empty struct
421 data: {}
422 type: testStructEmpty
423 want: |
424 {}
425
426 - encode:
427 name: struct field Hello
428 data:
429 hello: world
430 type: testStructHello
431 want: |
432 hello: world
433
434 - encode:
435 name: nested struct encoding
436 data:
437 a:
438 b: c
439 type: testStructA_NestedB
440 want: |
441 a:
442 b: c
443
444 - encode:
445 name: nested struct pointer encoding
446 data:
447 a:
448 b: c
449 type: testStructA_NestedBPtr
450 want: |
451 a:
452 b: c
453
454 - encode:
455 name: nested struct pointer null
456 data:
457 a: null
458 type: testStructA_NestedBPtr
459 want: |
460 a: null
461
462 - encode:
463 name: struct int field
464 data:
465 a: 1
466 type: testStructA_Int
467 want: |
468 a: 1
469
470 - encode:
471 name: struct int slice field
472 data:
473 a: [1, 2]
474 type: testStructA_IntSlice
475 want: |
476 a:
477 - 1
478 - 2
479
480 - encode:
481 name: struct int array field
482 data:
483 a: [1, 2]
484 type: testStructA_IntArray2
485 want: |
486 a:
487 - 1
488 - 2
489
490 - encode:
491 name: struct bool field
492 data:
493 a: true
494 type: testStructA_Bool
495 want: |
496 a: true
497
498 - encode:
499 name: struct string field true
500 data:
501 a: 'true'
502 type: testStructA_String
503 want: |
504 a: "true"
505
506 - encode:
507 name: struct string field off
508 data:
509 a: off
510 type: testStructA_String
511 want: |
512 a: 'off'
513
514 # Struct tests with yaml tags
515 - encode:
516 name: yaml tag field rename (B to a)
517 data:
518 a: 1
519 type: testStructB_Int_TagA
520 want: |
521 a: 1
522
523
524 # Scalar style encoding tests (simple cases without tabs/unicode)
525 - encode:
526 name: scalar style - just newline (double quoted)
527 data: "\n"
528 type: string
529 want: |
530 "\n"
531
532 - encode:
533 name: scalar style - hello newline world (literal)
534 data: "hello\nworld"
535 type: string
536 want: |
537 |-
538 hello
539 world
540
541 - encode:
542 name: scalar style - simple hello (plain)
543 data: hello
544 type: string
545 want: |
546 hello
547
548 - encode:
549 name: scalar style - number-like 123 (quoted)
550 data: '123'
551 type: string
552 want: |
553 "123"
554
555 - encode:
556 name: scalar style - boolean-like true (quoted)
557 data: 'true'
558 type: string
559 want: |
560 "true"
561
562 - encode:
563 name: scalar style - long multiline text (literal)
564 data: "This is a longer string\nwith multiple lines\nthat should use literal style"
565 type: string
566 want: |
567 |-
568 This is a longer string
569 with multiple lines
570 that should use literal style
571
572 - encode:
573 name: scalar style - multiline starting with space (literal with indent)
574 data: " This starts with space\nand is long enough\nfor literal style"
575 type: string
576 want: |
577 |4-
578 This starts with space
579 and is long enough
580 for literal style
581
582 - encode:
583 name: scalar style - a newline (literal 2 chars)
584 data: "a\n"
585 type: string
586 want: |
587 |
588 a
589
590 - encode:
591 name: scalar style - a newline b (literal 3 chars)
592 data: "a\nb"
593 type: string
594 want: |
595 |-
596 a
597 b
598
599 - encode:
600 name: scalar style - space a newline (literal with content)
601 data: " a\n"
602 type: string
603 want: |
604 |4
605 a
606
607 # Whitespace-only strings (all simple cases)
608 - encode:
609 name: whitespace only - just newline (double quoted)
610 data: "\n"
611 type: string
612 want: |
613 "\n"
614
615 - encode:
616 name: whitespace only - two newlines (double quoted)
617 data: "\n\n"
618 type: string
619 want: |
620 "\n\n"
621
622 - encode:
623 name: whitespace only - space newline (double quoted)
624 data: " \n"
625 type: string
626 want: |
627 " \n"
628
629 - encode:
630 name: whitespace only - tab newline (double quoted)
631 data: "\t\n"
632 type: string
633 want: |
634 "\t\n"
635
636 - encode:
637 name: whitespace only - space newline space (double quoted)
638 data: " \n "
639 type: string
640 want: |
641 " \n "
642
643 - encode:
644 name: whitespace only - newline space newline (double quoted)
645 data: "\n \n"
646 type: string
647 want: |
648 "\n \n"
649
650 - encode:
651 name: whitespace only - tab space newline tab (double quoted)
652 data: "\t \n\t"
653 type: string
654 want: |
655 "\t \n\t"
656
657 - encode:
658 name: whitespace only - multiple spaces newline spaces (double quoted)
659 data: " \n "
660 type: string
661 want: |
662 " \n "
663
664 - encode:
665 name: whitespace only - three newlines (double quoted)
666 data: "\n\n\n"
667 type: string
668 want: |
669 "\n\n\n"
670
671 - encode:
672 name: whitespace only - space tab newline space tab (double quoted)
673 data: " \t\n \t"
674 type: string
675 want: |
676 " \t\n \t"
677
678 # Whitespace with content (simple cases without leading tabs)
679 - encode:
680 name: whitespace with content - hello newline (literal)
681 data: "hello\n"
682 type: string
683 want: |
684 |
685 hello
686
687 - encode:
688 name: whitespace with content - space hello newline (literal with indent)
689 data: " hello\n"
690 type: string
691 want: |
692 |4
693 hello
694
695 - encode:
696 name: whitespace with content - space newline hello (double quoted short)
697 data: " \nhello"
698 type: string
699 want: |
700 " \nhello"
701
702 - encode:
703 name: whitespace with content - spaces hello spaces newline (double quoted trailing)
704 data: " hello \n"
705 type: string
706 want: |
707 " hello \n"
708
709 - encode:
710 name: whitespace with content - hello spaces newline (double quoted trailing)
711 data: "hello \n"
712 type: string
713 want: |
714 "hello \n"
715
716 - encode:
717 name: whitespace with content - hello newline spaces (double quoted trailing)
718 data: "hello\n "
719 type: string
720 want: |
721 "hello\n "
722
723 # WithQuotePreference tests - QuoteSingle (v4 default)
724 - encode-opts:
725 name: QuoteSingle - string "true"
726 data:
727 v: 'true'
728 type: map[string]string
729 opts:
730 required-quotes: single
731 want: |
732 v: 'true'
733
734 - encode-opts:
735 name: QuoteSingle - string "false"
736 data:
737 v: 'false'
738 type: map[string]string
739 opts:
740 required-quotes: single
741 want: |
742 v: 'false'
743
744 - encode-opts:
745 name: QuoteSingle - string "null"
746 data:
747 v: 'null'
748 type: map[string]string
749 opts:
750 required-quotes: single
751 want: |
752 v: 'null'
753
754 - encode-opts:
755 name: QuoteSingle - string "123"
756 data:
757 v: '123'
758 type: map[string]string
759 opts:
760 required-quotes: single
761 want: |
762 v: '123'
763
764 - encode-opts:
765 name: QuoteSingle - leading whitespace
766 data:
767 v: ' hello'
768 type: map[string]string
769 opts:
770 required-quotes: single
771 want: |
772 v: ' hello'
773
774 - encode-opts:
775 name: QuoteSingle - trailing whitespace
776 data:
777 v: 'hello '
778 type: map[string]string
779 opts:
780 required-quotes: single
781 want: |
782 v: 'hello '
783
784 - encode-opts:
785 name: QuoteSingle - syntax character (colon-space)
786 data:
787 v: ': hello'
788 type: map[string]string
789 opts:
790 required-quotes: single
791 want: |
792 v: ': hello'
793
794 - encode-opts:
795 name: QuoteSingle - dash string
796 data:
797 v: '-'
798 type: map[string]string
799 opts:
800 required-quotes: single
801 want: |
802 v: '-'
803
804 # WithQuotePreference tests - QuoteDouble
805 - encode-opts:
806 name: QuoteDouble - string "true"
807 data:
808 v: 'true'
809 type: map[string]string
810 opts:
811 required-quotes: double
812 want: |
813 v: "true"
814
815 - encode-opts:
816 name: QuoteDouble - string "false"
817 data:
818 v: 'false'
819 type: map[string]string
820 opts:
821 required-quotes: double
822 want: |
823 v: "false"
824
825 - encode-opts:
826 name: QuoteDouble - string "null"
827 data:
828 v: 'null'
829 type: map[string]string
830 opts:
831 required-quotes: double
832 want: |
833 v: "null"
834
835 - encode-opts:
836 name: QuoteDouble - string "123"
837 data:
838 v: '123'
839 type: map[string]string
840 opts:
841 required-quotes: double
842 want: |
843 v: "123"
844
845 - encode-opts:
846 name: QuoteDouble - leading whitespace
847 data:
848 v: ' hello'
849 type: map[string]string
850 opts:
851 required-quotes: double
852 want: |
853 v: " hello"
854
855 - encode-opts:
856 name: QuoteDouble - trailing whitespace
857 data:
858 v: 'hello '
859 type: map[string]string
860 opts:
861 required-quotes: double
862 want: |
863 v: "hello "
864
865 - encode-opts:
866 name: QuoteDouble - syntax character (colon-space)
867 data:
868 v: ': hello'
869 type: map[string]string
870 opts:
871 required-quotes: double
872 want: |
873 v: ": hello"
874
875 - encode-opts:
876 name: QuoteDouble - dash string
877 data:
878 v: '-'
879 type: map[string]string
880 opts:
881 required-quotes: double
882 want: |
883 v: "-"
884
885 # WithQuotePreference tests - QuoteLegacy (v3 behavior)
886 # Legacy: bool-like strings (representer) → double quotes
887 # Legacy: whitespace strings (emitter) → single quotes
888 - encode-opts:
889 name: QuoteLegacy - string "true" (double from representer)
890 data:
891 v: 'true'
892 type: map[string]string
893 opts:
894 required-quotes: legacy
895 want: |
896 v: "true"
897
898 - encode-opts:
899 name: QuoteLegacy - string "false" (double from representer)
900 data:
901 v: 'false'
902 type: map[string]string
903 opts:
904 required-quotes: legacy
905 want: |
906 v: "false"
907
908 - encode-opts:
909 name: QuoteLegacy - string "null" (double from representer)
910 data:
911 v: 'null'
912 type: map[string]string
913 opts:
914 required-quotes: legacy
915 want: |
916 v: "null"
917
918 - encode-opts:
919 name: QuoteLegacy - string "123" (double from representer)
920 data:
921 v: '123'
922 type: map[string]string
923 opts:
924 required-quotes: legacy
925 want: |
926 v: "123"
927
928 - encode-opts:
929 name: QuoteLegacy - leading whitespace (single from emitter)
930 data:
931 v: ' hello'
932 type: map[string]string
933 opts:
934 required-quotes: legacy
935 want: |
936 v: ' hello'
937
938 - encode-opts:
939 name: QuoteLegacy - trailing whitespace (single from emitter)
940 data:
941 v: 'hello '
942 type: map[string]string
943 opts:
944 required-quotes: legacy
945 want: |
946 v: 'hello '
947
948 - encode-opts:
949 name: QuoteLegacy - syntax character (single from emitter)
950 data:
951 v: ': hello'
952 type: map[string]string
953 opts:
954 required-quotes: legacy
955 want: |
956 v: ': hello'
957
958 - encode-opts:
959 name: QuoteLegacy - dash string (single from emitter)
960 data:
961 v: '-'
962 type: map[string]string
963 opts:
964 required-quotes: legacy
965 want: |
966 v: '-'
967
968 # Plain strings should remain unquoted regardless of quote preference
969 - encode-opts:
970 name: QuoteSingle - plain string stays plain
971 data:
972 v: hello
973 type: map[string]string
974 opts:
975 required-quotes: single
976 want: |
977 v: hello
978
979 - encode-opts:
980 name: QuoteDouble - plain string stays plain
981 data:
982 v: hello
983 type: map[string]string
984 opts:
985 required-quotes: double
986 want: |
987 v: hello
988