Debian Codegraph @

internal/libyaml/testdata/emitter.yaml

raw symbols SCIP
1 # Copyright 2025 The go-yaml Project Contributors
2 # SPDX-License-Identifier: Apache-2.0
3
4 # Emitter test cases for libyaml emitter
5 #
6 # Purpose: Tests the YAML emitter that converts events into YAML text output.
7 #
8 # Format: Test cases use type-as-key format:
9 # - test-type:
10 # name: Test case name
11 # ...
12 #
13 # Test Types:
14 # emit - Emit events to string, verify output contains expected strings
15 # emit-config - Emit events with specific emitter configuration
16 # roundtrip - Parse input YAML, emit it back, verify output contains expected strings
17 # emit-writer - Emit events to io.Writer, verify output contains expected strings
18 #
19 # Common Keys:
20 # name - Test case name (string)
21 # data - List of event specifications to emit (for emit, emit-config, emit-writer)
22 # conf - Emitter configuration map (for emit-config type only):
23 # * canonical - Boolean, emit in canonical YAML form
24 # * indent - Integer (2-9), indentation width in spaces
25 # * width - Integer, line width (-1 for unlimited, or positive number)
26 # * unicode - Boolean, enable Unicode character output
27 # yaml - Input YAML string (for roundtrip type only)
28 # want - Expected result (format varies by test type)
29 #
30 # Event Specification Format (for data lists):
31 # Simplified (type only): - EVENT_TYPE
32 # Detailed (with fields): - EVENT_TYPE:
33 # encoding: ... (for STREAM_START_EVENT)
34 # value: ... (for SCALAR_EVENT)
35 # anchor: ... (for SCALAR_EVENT, ALIAS_EVENT)
36 # tag: ... (for SCALAR_EVENT)
37 # implicit: ... (for various events)
38 # style: ... (PLAIN_SCALAR_STYLE, SINGLE_QUOTED_SCALAR_STYLE,
39 # DOUBLE_QUOTED_SCALAR_STYLE, LITERAL_SCALAR_STYLE,
40 # FOLDED_SCALAR_STYLE, BLOCK_SEQUENCE_STYLE,
41 # FLOW_SEQUENCE_STYLE, BLOCK_MAPPING_STYLE,
42 # FLOW_MAPPING_STYLE)
43
44 # Simple emit tests
45 - emit:
46 name: Simple scalar
47 data:
48 - STREAM_START_EVENT:
49 encoding: UTF8_ENCODING
50 - DOCUMENT_START_EVENT:
51 implicit: true
52 - SCALAR_EVENT:
53 value: hello
54 implicit: true
55 style: PLAIN_SCALAR_STYLE
56 - DOCUMENT_END_EVENT:
57 implicit: true
58 - STREAM_END_EVENT
59 want: hello
60
61 - emit:
62 name: Simple mapping
63 data:
64 - STREAM_START_EVENT:
65 encoding: UTF8_ENCODING
66 - DOCUMENT_START_EVENT:
67 implicit: true
68 - MAPPING_START_EVENT:
69 implicit: true
70 style: BLOCK_MAPPING_STYLE
71 - SCALAR_EVENT:
72 value: key
73 implicit: true
74 style: PLAIN_SCALAR_STYLE
75 - SCALAR_EVENT:
76 value: value
77 implicit: true
78 style: PLAIN_SCALAR_STYLE
79 - MAPPING_END_EVENT
80 - DOCUMENT_END_EVENT:
81 implicit: true
82 - STREAM_END_EVENT
83 want:
84 - key
85 - value
86
87 - emit:
88 name: Block sequence
89 data:
90 - STREAM_START_EVENT:
91 encoding: UTF8_ENCODING
92 - DOCUMENT_START_EVENT:
93 implicit: true
94 - SEQUENCE_START_EVENT:
95 implicit: true
96 style: BLOCK_SEQUENCE_STYLE
97 - SCALAR_EVENT:
98 value: item1
99 implicit: true
100 style: PLAIN_SCALAR_STYLE
101 - SCALAR_EVENT:
102 value: item2
103 implicit: true
104 style: PLAIN_SCALAR_STYLE
105 - SEQUENCE_END_EVENT
106 - DOCUMENT_END_EVENT:
107 implicit: true
108 - STREAM_END_EVENT
109 want:
110 - item1
111 - item2
112 - '-'
113
114 - emit:
115 name: Flow sequence
116 data:
117 - STREAM_START_EVENT:
118 encoding: UTF8_ENCODING
119 - DOCUMENT_START_EVENT:
120 implicit: true
121 - SEQUENCE_START_EVENT:
122 implicit: true
123 style: FLOW_SEQUENCE_STYLE
124 - SCALAR_EVENT:
125 value: '1'
126 implicit: true
127 style: PLAIN_SCALAR_STYLE
128 - SCALAR_EVENT:
129 value: '2'
130 implicit: true
131 style: PLAIN_SCALAR_STYLE
132 - SCALAR_EVENT:
133 value: '3'
134 implicit: true
135 style: PLAIN_SCALAR_STYLE
136 - SEQUENCE_END_EVENT
137 - DOCUMENT_END_EVENT:
138 implicit: true
139 - STREAM_END_EVENT
140 want:
141 - '['
142 - ']'
143
144 - emit:
145 name: Flow mapping
146 data:
147 - STREAM_START_EVENT:
148 encoding: UTF8_ENCODING
149 - DOCUMENT_START_EVENT:
150 implicit: true
151 - MAPPING_START_EVENT:
152 implicit: true
153 style: FLOW_MAPPING_STYLE
154 - SCALAR_EVENT:
155 value: a
156 implicit: true
157 style: PLAIN_SCALAR_STYLE
158 - SCALAR_EVENT:
159 value: '1'
160 implicit: true
161 style: PLAIN_SCALAR_STYLE
162 - SCALAR_EVENT:
163 value: b
164 implicit: true
165 style: PLAIN_SCALAR_STYLE
166 - SCALAR_EVENT:
167 value: '2'
168 implicit: true
169 style: PLAIN_SCALAR_STYLE
170 - MAPPING_END_EVENT
171 - DOCUMENT_END_EVENT:
172 implicit: true
173 - STREAM_END_EVENT
174 want:
175 - '{'
176 - '}'
177
178 - emit:
179 name: Explicit document
180 data:
181 - STREAM_START_EVENT:
182 encoding: UTF8_ENCODING
183 - DOCUMENT_START_EVENT:
184 implicit: false
185 - SCALAR_EVENT:
186 value: value
187 implicit: true
188 style: PLAIN_SCALAR_STYLE
189 - DOCUMENT_END_EVENT:
190 implicit: false
191 - STREAM_END_EVENT
192 want:
193 - '---'
194 - '...'
195
196 - emit:
197 name: Anchor and alias
198 data:
199 - STREAM_START_EVENT:
200 encoding: UTF8_ENCODING
201 - DOCUMENT_START_EVENT:
202 implicit: true
203 - SEQUENCE_START_EVENT:
204 implicit: true
205 style: BLOCK_SEQUENCE_STYLE
206 - SCALAR_EVENT:
207 anchor: myanchor
208 value: value
209 implicit: true
210 style: PLAIN_SCALAR_STYLE
211 - ALIAS_EVENT:
212 anchor: myanchor
213 - SEQUENCE_END_EVENT
214 - DOCUMENT_END_EVENT:
215 implicit: true
216 - STREAM_END_EVENT
217 want:
218 - '&myanchor'
219 - '*myanchor'
220
221 - emit:
222 name: Tag
223 data:
224 - STREAM_START_EVENT:
225 encoding: UTF8_ENCODING
226 - DOCUMENT_START_EVENT:
227 implicit: true
228 - SCALAR_EVENT:
229 tag: 'tag:yaml.org,2002:str'
230 value: value
231 implicit: false
232 style: PLAIN_SCALAR_STYLE
233 - DOCUMENT_END_EVENT:
234 implicit: true
235 - STREAM_END_EVENT
236 want: '!!str'
237
238 - emit:
239 name: Single quoted scalar
240 data:
241 - STREAM_START_EVENT:
242 encoding: UTF8_ENCODING
243 - DOCUMENT_START_EVENT:
244 implicit: true
245 - SCALAR_EVENT:
246 value: quoted value
247 implicit: true
248 style: SINGLE_QUOTED_SCALAR_STYLE
249 - DOCUMENT_END_EVENT:
250 implicit: true
251 - STREAM_END_EVENT
252 want: "'"
253
254 - emit:
255 name: Double quoted scalar
256 data:
257 - STREAM_START_EVENT:
258 encoding: UTF8_ENCODING
259 - DOCUMENT_START_EVENT:
260 implicit: true
261 - SCALAR_EVENT:
262 value: quoted value
263 implicit: true
264 style: DOUBLE_QUOTED_SCALAR_STYLE
265 - DOCUMENT_END_EVENT:
266 implicit: true
267 - STREAM_END_EVENT
268 want: '"'
269
270 - emit:
271 name: Literal scalar
272 data:
273 - STREAM_START_EVENT:
274 encoding: UTF8_ENCODING
275 - DOCUMENT_START_EVENT:
276 implicit: true
277 - MAPPING_START_EVENT:
278 implicit: true
279 style: BLOCK_MAPPING_STYLE
280 - SCALAR_EVENT:
281 value: key
282 implicit: true
283 style: PLAIN_SCALAR_STYLE
284 - SCALAR_EVENT:
285 value: "line1\nline2\n"
286 implicit: true
287 style: LITERAL_SCALAR_STYLE
288 - MAPPING_END_EVENT
289 - DOCUMENT_END_EVENT:
290 implicit: true
291 - STREAM_END_EVENT
292 want: '|'
293
294 - emit:
295 name: Folded scalar
296 data:
297 - STREAM_START_EVENT:
298 encoding: UTF8_ENCODING
299 - DOCUMENT_START_EVENT:
300 implicit: true
301 - MAPPING_START_EVENT:
302 implicit: true
303 style: BLOCK_MAPPING_STYLE
304 - SCALAR_EVENT:
305 value: key
306 implicit: true
307 style: PLAIN_SCALAR_STYLE
308 - SCALAR_EVENT:
309 value: "folded text\n"
310 implicit: true
311 style: FOLDED_SCALAR_STYLE
312 - MAPPING_END_EVENT
313 - DOCUMENT_END_EVENT:
314 implicit: true
315 - STREAM_END_EVENT
316 want: '>'
317
318 # Config tests
319 - emit-config:
320 name: Canonical mode
321 conf:
322 canonical: true
323 data:
324 - STREAM_START_EVENT:
325 encoding: UTF8_ENCODING
326 - DOCUMENT_START_EVENT:
327 implicit: false
328 - SCALAR_EVENT:
329 value: value
330 implicit: true
331 style: PLAIN_SCALAR_STYLE
332 - DOCUMENT_END_EVENT:
333 implicit: false
334 - STREAM_END_EVENT
335 want: ---
336
337 - emit-config:
338 name: Custom indent
339 conf:
340 indent: 4
341 data:
342 - STREAM_START_EVENT:
343 encoding: UTF8_ENCODING
344 - DOCUMENT_START_EVENT:
345 implicit: true
346 - MAPPING_START_EVENT:
347 implicit: true
348 style: BLOCK_MAPPING_STYLE
349 - SCALAR_EVENT:
350 value: key
351 implicit: true
352 style: PLAIN_SCALAR_STYLE
353 - MAPPING_START_EVENT:
354 implicit: true
355 style: BLOCK_MAPPING_STYLE
356 - SCALAR_EVENT:
357 value: nested
358 implicit: true
359 style: PLAIN_SCALAR_STYLE
360 - SCALAR_EVENT:
361 value: value
362 implicit: true
363 style: PLAIN_SCALAR_STYLE
364 - MAPPING_END_EVENT
365 - MAPPING_END_EVENT
366 - DOCUMENT_END_EVENT:
367 implicit: true
368 - STREAM_END_EVENT
369 want: key
370
371 - emit-config:
372 name: Custom width
373 conf:
374 width: 40
375 data:
376 - STREAM_START_EVENT:
377 encoding: UTF8_ENCODING
378 - DOCUMENT_START_EVENT:
379 implicit: true
380 - SCALAR_EVENT:
381 value: short
382 implicit: true
383 style: PLAIN_SCALAR_STYLE
384 - DOCUMENT_END_EVENT:
385 implicit: true
386 - STREAM_END_EVENT
387 want: short
388
389 - emit-config:
390 name: Unicode mode
391 conf:
392 unicode: true
393 data:
394 - STREAM_START_EVENT:
395 encoding: UTF8_ENCODING
396 - DOCUMENT_START_EVENT:
397 implicit: true
398 - SCALAR_EVENT:
399 value: "unicode: \u00e9"
400 implicit: true
401 style: PLAIN_SCALAR_STYLE
402 - DOCUMENT_END_EVENT:
403 implicit: true
404 - STREAM_END_EVENT
405 want: unicode
406
407 - emit:
408 name: Multiple documents
409 data:
410 - STREAM_START_EVENT:
411 encoding: UTF8_ENCODING
412 - DOCUMENT_START_EVENT:
413 implicit: false
414 - SCALAR_EVENT:
415 value: doc1
416 implicit: true
417 style: PLAIN_SCALAR_STYLE
418 - DOCUMENT_END_EVENT:
419 implicit: false
420 - DOCUMENT_START_EVENT:
421 implicit: false
422 - SCALAR_EVENT:
423 value: doc2
424 implicit: true
425 style: PLAIN_SCALAR_STYLE
426 - DOCUMENT_END_EVENT:
427 implicit: false
428 - STREAM_END_EVENT
429 want: ---
430
431 - emit:
432 name: Nested structures
433 data:
434 - STREAM_START_EVENT:
435 encoding: UTF8_ENCODING
436 - DOCUMENT_START_EVENT:
437 implicit: true
438 - MAPPING_START_EVENT:
439 implicit: true
440 style: BLOCK_MAPPING_STYLE
441 - SCALAR_EVENT:
442 value: parent
443 implicit: true
444 style: PLAIN_SCALAR_STYLE
445 - SEQUENCE_START_EVENT:
446 implicit: true
447 style: BLOCK_SEQUENCE_STYLE
448 - MAPPING_START_EVENT:
449 implicit: true
450 style: BLOCK_MAPPING_STYLE
451 - SCALAR_EVENT:
452 value: child
453 implicit: true
454 style: PLAIN_SCALAR_STYLE
455 - SCALAR_EVENT:
456 value: value
457 implicit: true
458 style: PLAIN_SCALAR_STYLE
459 - MAPPING_END_EVENT
460 - SEQUENCE_END_EVENT
461 - MAPPING_END_EVENT
462 - DOCUMENT_END_EVENT:
463 implicit: true
464 - STREAM_END_EVENT
465 want:
466 - parent
467 - child
468
469 # Roundtrip test
470 - roundtrip:
471 name: Roundtrip
472 yaml: |
473 key: value
474 list:
475 - item1
476 - item2
477 want:
478 - key
479 - value
480 - item1
481
482 # Writer test
483 - emit-writer:
484 name: Writer
485 data:
486 - STREAM_START_EVENT:
487 encoding: UTF8_ENCODING
488 - DOCUMENT_START_EVENT:
489 implicit: true
490 - SCALAR_EVENT:
491 value: test
492 implicit: true
493 style: PLAIN_SCALAR_STYLE
494 - DOCUMENT_END_EVENT:
495 implicit: true
496 - STREAM_END_EVENT
497 want: test
498
499 # Emitter API tests (from api.yaml)
500 - api-new:
501 name: New emitter
502 with: NewEmitter
503 test:
504 - nil: [buffer, false]
505 - len: [buffer, 128] # output_buffer_size
506 - nil: [raw-buffer, false]
507 - nil: [states, false]
508 - nil: [events, false]
509 - eq: [best-width, -1]
510
511 - api-method:
512 name: Emitter set canonical true
513 with: NewEmitter
514 call: [SetCanonical, true]
515 test:
516 - eq: [canonical, true]
517
518 - api-method:
519 name: Emitter set canonical false
520 with: NewEmitter
521 call: [SetCanonical, false]
522 test:
523 - eq: [canonical, false]
524
525 - api-method:
526 name: Emitter set indent 2
527 with: NewEmitter
528 call: [SetIndent, 2]
529 test:
530 - eq: [BestIndent, 2]
531
532 - api-method:
533 name: Emitter set indent 5
534 with: NewEmitter
535 call: [SetIndent, 5]
536 test:
537 - eq: [BestIndent, 5]
538
539 - api-method:
540 name: Emitter set indent clamp low
541 with: NewEmitter
542 call: [SetIndent, 1]
543 test:
544 - eq: [BestIndent, 2]
545
546 - api-method:
547 name: Emitter set indent clamp high
548 with: NewEmitter
549 call: [SetIndent, 10]
550 test:
551 - eq: [BestIndent, 2]
552
553 - api-method:
554 name: Emitter set width 80
555 with: NewEmitter
556 call: [SetWidth, 80]
557 test:
558 - eq: [best-width, 80]
559
560 - api-method:
561 name: Emitter set width -1
562 with: NewEmitter
563 call: [SetWidth, -1]
564 test:
565 - eq: [best-width, -1]
566
567 - api-method:
568 name: Emitter set width clamp
569 with: NewEmitter
570 call: [SetWidth, -10]
571 test:
572 - eq: [best-width, -1]
573
574 - api-method:
575 name: Emitter set unicode true
576 with: NewEmitter
577 call: [SetUnicode, true]
578 test:
579 - eq: [unicode, true]
580
581 - api-method:
582 name: Emitter set unicode false
583 with: NewEmitter
584 call: [SetUnicode, false]
585 test:
586 - eq: [unicode, false]
587
588 - api-method:
589 name: Emitter set line break
590 with: NewEmitter
591 call: [SetLineBreak, LN_BREAK]
592 test:
593 - eq: [line-break, LN_BREAK]
594
595 - api-panic:
596 name: Emitter set encoding twice
597 with: NewEmitter
598 init: [SetEncoding, UTF8_ENCODING]
599 call: [SetEncoding, UTF16LE_ENCODING]
600 want: must set the output encoding only once
601
602 - api-delete:
603 name: Emitter delete
604 with: NewEmitter
605 test:
606 - nil: [output-buffer, true]
607 - len: [buffer, 0]
608
609 # Event constructor tests
610 - api-new-event:
611 name: New stream start event
612 call: [NewStreamStartEvent, UTF8_ENCODING]
613 test:
614 - eq: [Type, STREAM_START_EVENT]
615 - eq: [encoding, UTF8_ENCODING]
616
617 - api-new-event:
618 name: New stream end event
619 call: [NewStreamEndEvent]
620 test:
621 - eq: [Type, STREAM_END_EVENT]
622
623 - api-new-event:
624 name: New document end event
625 call: [NewDocumentEndEvent, false]
626 test:
627 - eq: [Type, DOCUMENT_END_EVENT]
628 - eq: [Implicit, false]
629
630 - api-new-event:
631 name: New alias event
632 byte: true
633 call: [NewAliasEvent, myanchor]
634 test:
635 - eq: [Type, ALIAS_EVENT]
636 - eq: [Anchor, myanchor]
637
638 - api-new-event:
639 name: New sequence end event
640 call: [NewSequenceEndEvent]
641 test:
642 - eq: [Type, SEQUENCE_END_EVENT]
643
644 - api-new-event:
645 name: New mapping end event
646 call: [NewMappingEndEvent]
647 test:
648 - eq: [Type, MAPPING_END_EVENT]