babbel_yaml 0.1.1

Fast, modular YAML 1.2 parser and emitter with anchors, aliases, and tags
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
// =====================================================================================
//  File: document_structure_tests.rs
//  Location: library/src/internal_tests/
// -------------------------------------------------------------------------------------
//  Purpose:
//      Internal tests for YAML document structure parsing in the babbel_yaml crate.
//      These tests validate correct handling of multi-document streams, document boundaries,
//      and structural edge cases, ensuring compliance with the YAML specification.
//
//  Context:
//      - Part of the babbel_yaml project, a Rust YAML parser/serializer.
//      - Focuses on document boundaries, multi-document parsing, and structure validation.
//      - Ensures robust handling of complex YAML document layouts and edge cases.
//
// -------------------------------------------------------------------------------------
//  Test Coverage:
//      - Multi-document streams and boundaries
//      - Document start and end markers
//      - Nested and complex document structures
//      - Edge cases for empty and malformed documents
//      - Compliance with YAML document structure rules
// =====================================================================================

#[cfg(test)]
mod tests {
    use crate::nodes::node::{BlockStyle, QuoteType};
    use crate::test_helpers::{assert_nodes_eq, parse_yaml};
    use crate::{BufferSource, Node, Node::Document, Numeric, parse};
    use std::collections::HashMap;

    #[test]
    fn test_parse_multi_document() {
        let mut source = BufferSource::new(
            b"key1: value1\n---\nkey2: value2\n---\nkey3: value3\nkey4: value4\n",
        );
        let result = parse(&mut source).unwrap();
        let expected = Node::Documents(vec![
            Document(vec![Node::Mapping(vec![(
                Node::Str("key1".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Str("value1".to_string(), QuoteType::Unquoted, BlockStyle::None),
            )])]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("key2".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Str("value2".to_string(), QuoteType::Unquoted, BlockStyle::None),
            )])]),
            Document(vec![Node::Mapping(vec![
                (
                    Node::Str("key3".to_string(), QuoteType::Unquoted, BlockStyle::None),
                    Node::Str("value3".to_string(), QuoteType::Unquoted, BlockStyle::None),
                ),
                (
                    Node::Str("key4".to_string(), QuoteType::Unquoted, BlockStyle::None),
                    Node::Str("value4".to_string(), QuoteType::Unquoted, BlockStyle::None),
                ),
            ])]),
        ]);

        assert_eq!(result, expected);
    }

    #[test]
    fn test_parse_header_comments() {
        let mut source = BufferSource::new(
            b"# Header comment 1\n# Header comment 2\n# Header comment 3\nkey: value\n",
        );
        let result = parse(&mut source).unwrap();

        assert_eq!(
            result,
            Node::Documents(vec![Document(vec![{
                let mut map = HashMap::new();
                map.insert(
                    "key".to_string(),
                    Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
                );
                let mut pairs = Vec::new();
                for (k, v) in map.into_iter() {
                    let value = match v {
                        Node::Mapping(p) => Node::Mapping(p),
                        other => other,
                    };
                    pairs.push((Node::Str(k, QuoteType::Unquoted, BlockStyle::None), value));
                }
                Node::Mapping(pairs)
            }])])
        );
    }

    #[test]
    fn test_parse_document_end_marker() {
        let input = b"key: value\n---";
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_end_marker_with_trailing_content() {
        let input = b"key: value\n---\nother: 123";
        let expected = Node::Documents(vec![
            Document(vec![Node::Mapping(vec![(
                Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
            )])]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("other".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(123)),
            )])]),
        ]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_end_marker_with_comments() {
        let input = b"# Comment before\nkey: value\n# Comment after\n---\n# Comment in between\nother: 123\n# Final comment";
        let expected = Node::Documents(vec![
            Document(vec![Node::Mapping(vec![(
                Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
            )])]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("other".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(123)),
            )])]),
        ]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_end_marker_only() {
        let input = b"---";
        let expected = Node::Documents(vec![Document(vec![])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_multiple_document_end_markers() {
        let input = b"key: value\n---\n---\nother: 1";
        // The parser merges empty documents, so only non-empty documents are present
        let expected = Node::Documents(vec![
            Document(vec![Node::Mapping(vec![(
                Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
            )])]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("other".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(1)),
            )])]),
        ]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_empty_document_end_marker() {
        let input = b"...";
        let expected = Node::Documents(vec![Document(vec![])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_start_marker() {
        let input = b"---\nkey: value";
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_start_and_end_markers() {
        let input = b"---\nkey: value\n...";
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_multiple_documents_with_start_markers() {
        let input = b"---\nfirst: 1\n---\nsecond: 2\n---\nthird: 3";
        let expected = Node::Documents(vec![
            Document(vec![Node::Mapping(vec![(
                Node::Str("first".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(1)),
            )])]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("second".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(2)),
            )])]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("third".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(3)),
            )])]),
        ]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_documents_with_mixed_markers() {
        let input = b"---\nfirst: 1\n...\n---\nsecond: 2\n---\nthird: 3\n...";
        let expected = Node::Documents(vec![
            Document(vec![Node::Mapping(vec![(
                Node::Str("first".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(1)),
            )])]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("second".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(2)),
            )])]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("third".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Number(Numeric::Integer(3)),
            )])]),
        ]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_empty_documents_between_markers() {
        let input = b"---\n---\nkey: value\n---";
        // The parser merges empty documents, so only the non-empty document is present
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_with_yaml_version_directive() {
        let input = b"# YAML version would be %YAML 1.2 here\n---\nkey: value";
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_with_tag_directive() {
        let input =
            b"# TAG directive would be %TAG ! tag:example.com,2000:app/ here\n---\nkey: value";
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_with_multiple_directives() {
        let input = b"# Would have %YAML 1.2 and %TAG ! tag:example.com,2000:app/ directives here\n---\nkey: value";
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_with_comments_around_markers() {
        let input = b"# Before first marker\n---\n# After first marker\nkey: value\n# Before end marker\n...\n# After end marker";
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_documents_with_different_content_types() {
        let input = b"---\n\"scalar document\"\n---\n- item1\n- item2\n---\nkey: value";
        let expected = Node::Documents(vec![
            Document(vec![Node::Str(
                "scalar document".to_string(),
                QuoteType::Double,
                BlockStyle::None,
            )]),
            Document(vec![Node::Array(vec![
                Node::Str("item1".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Str("item2".to_string(), QuoteType::Unquoted, BlockStyle::None),
            ])]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
            )])]),
        ]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_with_whitespace_only_content() {
        let input = b"---\n   \n  \n  \n---\nkey: value";
        let expected = Node::Documents(vec![
            Document(vec![]),
            Document(vec![Node::Mapping(vec![(
                Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
                Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
            )])]),
        ]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_with_trailing_spaces_on_markers() {
        let input = b"---   \nkey: value\n...   ";
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_document_markers_with_comments_inline() {
        let input = b"--- # Start of document\nkey: value\n# End of document";
        let expected = Node::Documents(vec![Document(vec![Node::Mapping(vec![(
            Node::Str("key".to_string(), QuoteType::Unquoted, BlockStyle::None),
            Node::Str("value".to_string(), QuoteType::Unquoted, BlockStyle::None),
        )])])]);
        let actual = parse_yaml(input);
        assert_nodes_eq(&actual, &expected);
    }

    #[test]
    fn test_parse_documents_with_complex_nested_content() {
        let input = b"---\nusers:\n  - name: alice\n    roles: [admin, user]\n  - name: bob\n    roles: [user]\nconfig:\n  database:\n    host: localhost\n    port: 5432\n---\nservers:\n  web:\n    - host: web1.example.com\n    - host: web2.example.com";
        // Only check that the parse succeeds and the structure is as expected
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            assert!(!docs.is_empty(), "Should have at least one document");
            let mut found_users = false;
            let mut found_servers = false;
            for doc in docs {
                if let Document(nodes) = doc {
                    for node in nodes {
                        if let Node::Mapping(pairs) = node {
                            for (k, _) in pairs {
                                if let Node::Str(s, _, _) = k {
                                    if s == "users" {
                                        found_users = true;
                                    }
                                    if s == "servers" {
                                        found_servers = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            assert!(found_users, "Should find 'users' mapping");
            assert!(found_servers, "Should find 'servers' mapping");
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_trailing_empty_document_after_end_and_start_markers() {
        // Pattern: <content>"\n...\n---\n" should produce a trailing empty document
        let mut source = BufferSource::new(b"---\nkey: value\n...\n---\n");
        let result = parse(&mut source).unwrap();

        if let Node::Documents(docs) = &result {
            assert_eq!(
                docs.len(),
                2,
                "Expected two documents including trailing empty one"
            );
            if let Document(nodes) = &docs[1] {
                assert!(nodes.is_empty(), "Second document should be empty");
            } else {
                panic!("Second entry should be a Document node");
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_parse_document_with_boolean_and_null_values() {
        let input = b"---\n\
            enabled: true\n\
            disabled: false\n\
            empty: null\n\
            missing: ~\n\
            ---\n\
            yes: yes\n\
            no: no\n\
            on: on\n\
            off: off";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            assert!(!docs.is_empty(), "Should have at least one document");
            let mut found_enabled = false;
            let mut found_disabled = false;
            let mut found_empty = false;
            let mut found_missing = false;
            let mut found_yes = false;
            let mut found_no = false;
            let mut found_on = false;
            let mut found_off = false;
            for doc in docs {
                if let Document(nodes) = doc {
                    for node in nodes {
                        if let Node::Mapping(pairs) = node {
                            for (k, v) in pairs {
                                if let Node::Str(s, _, _) = k {
                                    match s.as_str() {
                                        "enabled" => {
                                            if let Node::Boolean(true) = v {
                                                found_enabled = true;
                                            }
                                        }
                                        "disabled" => {
                                            if let Node::Boolean(false) = v {
                                                found_disabled = true;
                                            }
                                        }
                                        "empty" => {
                                            if let Node::None = v {
                                                found_empty = true;
                                            }
                                        }
                                        "missing" => {
                                            if let Node::None = v {
                                                found_missing = true;
                                            }
                                        }
                                        "yes" => {
                                            if let Node::Str(val, _, _) = v {
                                                if val == "yes" {
                                                    found_yes = true;
                                                }
                                            }
                                        }
                                        "no" => {
                                            if let Node::Str(val, _, _) = v {
                                                if val == "no" {
                                                    found_no = true;
                                                }
                                            }
                                        }
                                        "on" => {
                                            if let Node::Str(val, _, _) = v {
                                                if val == "on" {
                                                    found_on = true;
                                                }
                                            }
                                        }
                                        "off" => {
                                            if let Node::Str(val, _, _) = v {
                                                if val == "off" {
                                                    found_off = true;
                                                }
                                            }
                                        }
                                        _ => {}
                                    }
                                }
                            }
                        }
                    }
                }
            }
            assert!(found_enabled, "Should find enabled: true");
            assert!(found_disabled, "Should find disabled: false");
            assert!(found_empty, "Should find empty: null");
            assert!(found_missing, "Should find missing: ~");
            assert!(found_yes, "Should find yes: yes");
            assert!(found_no, "Should find no: no");
            assert!(found_on, "Should find on: on");
            assert!(found_off, "Should find off: off");
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_parse_document_with_numeric_values() {
        let input = b"---\n\
            integer: 42\n\
            negative: -123\n\
            float: 3.14159\n\
            scientific: 1.23e+4\n\
            binary: 0b1010\n\
            octal: 0o755\n\
            hex: 0xFF";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            assert_eq!(docs.len(), 1);
            if let Document(nodes) = &docs[0] {
                assert!(!nodes.is_empty());
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_parse_document_with_anchor_and_alias() {
        let input = b"---\n\
                        default: &default\n\
                            host: localhost\n\
                            port: 8080\n\
                        development:\n\
                            config: *default\n\
                            debug: true\n\
                        production:\n\
                            config: *default\n\
                            debug: false";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            assert_eq!(docs.len(), 1);
            if let Document(nodes) = &docs[0] {
                assert!(!nodes.is_empty());
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_parse_document_with_literal_and_folded_strings() {
        let input = b"---\n\
                        literal: |\n\
                            Line 1\n\
                            Line 2\n\
                            Line 3\n\
                        folded: >\n\
                            This is a very long\n\
                            line that will be\n\
                            folded into a single\n\
                            paragraph.\n\
                        mixed:\n\
                            - |\n\
                                Literal in array\n\
                            - >\n\
                                Folded in array";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            assert_eq!(docs.len(), 1);
            if let Document(nodes) = &docs[0] {
                assert!(!nodes.is_empty());
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_parse_document_with_unicode_content() {
        let input = b"---\n\
            greeting: \"\xE4\xBD\xA0\xE5\xA5\xBD\"  # Hello in Chinese\n\
            emoji: \"\xF0\x9F\x91\x8B\"  # Wave emoji\n\
            mixed: [\"\xC2\xA1Hola!\", \"world\", \"\xF0\x9F\x8C\x8D\"]  # Spanish + world emoji";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            assert_eq!(docs.len(), 1);
            if let Document(nodes) = &docs[0] {
                assert!(!nodes.is_empty());
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_parse_document_with_tags() {
        let input = b"---\n\
            timestamp: !!timestamp 2001-12-14t21:59:43.10-05:00\n\
            integer: !!int \"123\"\n\
            float: !!float \"456.789\"\n\
            binary: !!binary |\n\
              R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\n\
              OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\n\
              +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\n\
              AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            assert_eq!(docs.len(), 1);
            if let Document(nodes) = &docs[0] {
                assert!(!nodes.is_empty());
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_parse_document_with_tag_directives() {
        let input = b"%TAG !e! tag:example.com,2000:app/\n\
            ---\n\
            item: !e!custom value";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            assert_eq!(docs.len(), 1);
            if let Document(nodes) = &docs[0] {
                assert!(!nodes.is_empty());
                // Check that the tag was resolved
                if let Node::Mapping(pairs) = &nodes[0] {
                    if let Node::Tagged(_, tag) = &pairs[0].1 {
                        // Tag is not resolved by this parser; it should remain as the literal '!e!custom'
                        assert_eq!(tag, "!e!custom");
                    } else {
                        panic!("Expected tagged node");
                    }
                }
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_parse_document_with_yaml_version() {
        let input = b"%YAML 1.2\n\
            ---\n\
            key: value";
        let actual = parse_yaml(input);
        // Accept parse success as pass
        if let Node::Documents(docs) = &actual {
            assert!(!docs.is_empty());
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_yaml_11_boolean_values() {
        let input = b"%YAML 1.1\n\
            ---\n\
            yes_value: yes\n\
            no_value: no\n\
            on_value: on\n\
            off_value: off";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            if let Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    // Check yes -> true
                    if let Some((_, Node::Boolean(true))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(s, _, _) if s == "yes_value"))
                    {
                        // Passed
                    } else {
                        panic!("Expected 'yes' to be parsed as boolean true in YAML 1.1");
                    }

                    // Check no -> false
                    if let Some((_, Node::Boolean(false))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(s, _, _) if s == "no_value"))
                    {
                        // Passed
                    } else {
                        panic!("Expected 'no' to be parsed as boolean false in YAML 1.1");
                    }

                    // Check on -> true
                    if let Some((_, Node::Boolean(true))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(s, _, _) if s == "on_value"))
                    {
                        // Passed
                    } else {
                        panic!("Expected 'on' to be parsed as boolean true in YAML 1.1");
                    }

                    // Check off -> false
                    if let Some((_, Node::Boolean(false))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(s, _, _) if s == "off_value"))
                    {
                        // Passed
                    } else {
                        panic!("Expected 'off' to be parsed as boolean false in YAML 1.1");
                    }
                }
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_yaml_12_boolean_values_strict() {
        let input = b"%YAML 1.2\n\
            ---\n\
            yes_value: yes\n\
            no_value: no\n\
            true_value: true\n\
            false_value: false";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            if let Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    // Check yes remains string
                    if let Some((_, Node::Str(s, _, _))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(key, _, _) if key == "yes_value"))
                    {
                        assert_eq!(s, "yes", "Expected 'yes' to remain a string in YAML 1.2");
                    } else {
                        panic!("Expected 'yes' to be parsed as string in YAML 1.2");
                    }

                    // Check no remains string
                    if let Some((_, Node::Str(s, _, _))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(key, _, _) if key == "no_value"))
                    {
                        assert_eq!(s, "no", "Expected 'no' to remain a string in YAML 1.2");
                    } else {
                        panic!("Expected 'no' to be parsed as string in YAML 1.2");
                    }

                    // Check true/false still work
                    if let Some((_, Node::Boolean(true))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(key, _, _) if key == "true_value"))
                    {
                        // Passed
                    } else {
                        panic!("Expected 'true' to be parsed as boolean in YAML 1.2");
                    }

                    if let Some((_, Node::Boolean(false))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(key, _, _) if key == "false_value"))
                    {
                        // Passed
                    } else {
                        panic!("Expected 'false' to be parsed as boolean in YAML 1.2");
                    }
                }
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_yaml_11_octal_numbers() {
        let input = b"%YAML 1.1\n\
            ---\n\
            permissions: 0755";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            if let Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    if let Some((_, Node::Number(Numeric::Integer(i)))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(s, _, _) if s == "permissions"))
                    {
                        assert_eq!(
                            *i, 493,
                            "Expected 0755 (octal) to be parsed as 493 (decimal) in YAML 1.1"
                        );
                    } else {
                        panic!("Expected '0755' to be parsed as octal number in YAML 1.1");
                    }
                }
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_yaml_12_octal_numbers_require_0o_prefix() {
        let input = b"%YAML 1.2\n\
            ---\n\
            permissions: 0o755";
        let actual = parse_yaml(input);
        if let Node::Documents(docs) = &actual {
            if let Document(nodes) = &docs[0] {
                if let Node::Mapping(pairs) = &nodes[0] {
                    if let Some((_, Node::Number(Numeric::Integer(i)))) = pairs
                        .iter()
                        .find(|(k, _)| matches!(k, Node::Str(s, _, _) if s == "permissions"))
                    {
                        assert_eq!(
                            *i, 493,
                            "Expected 0o755 (octal) to be parsed as 493 (decimal) in YAML 1.2"
                        );
                    } else {
                        panic!("Expected '0o755' to be parsed as octal number in YAML 1.2");
                    }
                }
            }
        } else {
            panic!("Expected Documents node");
        }
    }

    #[test]
    fn test_rhx7_yaml_directive_without_doc_end_marker_should_error() {
        // RHX7: A %YAML directive appearing mid-stream without a preceding
        // '...' (document-end) marker is invalid YAML.
        let yaml = "key: value\n%YAML 1.2\n---\n";
        let result = crate::parse_with_config(yaml, crate::parser::config::ParserConfig::strict());
        assert!(
            result.is_err(),
            "RHX7: '%YAML' directive without preceding '...' should error, got: {:?}",
            result
        );
    }

    #[test]
    fn test_7lbh_multiline_double_quoted_implicit_key_should_error() {
        // 7LBH: A double-quoted scalar that literally spans multiple source lines
        // used as an implicit block mapping key must be rejected per YAML spec §8.1.1.
        // The key "c\n d": 1 (with a real newline inside) is invalid.
        // But "a\nb": 1 (escape sequence "\n") should succeed since it is one line.
        let yaml_invalid = "\"a\\nb\": 1\n\"c\n d\": 1\n";
        let result_invalid = crate::parse_with_config(yaml_invalid, crate::parser::config::ParserConfig::strict());
        assert!(
            result_invalid.is_err(),
            "7LBH: multiline double-quoted implicit key should error, got: {:?}",
            result_invalid
        );

        // Escape-only case must still succeed
        let yaml_valid = "\"a\\nb\": 1\n";
        let result_valid = crate::parse_with_config(yaml_valid, crate::parser::config::ParserConfig::strict());
        assert!(
            result_valid.is_ok(),
            "7LBH: single-line double-quoted key with escape should succeed, got: {:?}",
            result_valid
        );
    }
}