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 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
|
//! Stress tests for YAML parser to ensure robustness
//!
//! These tests verify parser behavior with:
//! - Deep nesting and large documents
//! - Complex nested structures
//! - Various scalar types and values
//! - Collections (flow and block)
//! - Anchors, aliases, and merge keys
//! - Comments preservation
//! - Unicode content
//! - Document markers and directives
use yaml_edit::{Parse, YamlFile};
// ========================================
// Deep Nesting and Large Documents
// ========================================
#[test]
fn test_deeply_nested_structures() {
// Test very deep nesting
let yaml = r#"
level1:
level2:
level3:
level4:
level5: deep_value
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed the deeply nested structure correctly via API
let document = doc.document().unwrap();
let root = document.as_mapping().unwrap();
let level1 = root.get_mapping("level1").unwrap();
let level2 = level1.get_mapping("level2").unwrap();
let level3 = level2.get_mapping("level3").unwrap();
let level4 = level3.get_mapping("level4").unwrap();
assert_eq!(
level4
.get("level5")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"deep_value"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
#[test]
fn test_large_number_of_keys() {
let mut yaml = String::new();
for i in 0..1000 {
yaml.push_str(&format!("key{}: value{}\n", i, i));
}
let parse = Parse::parse_yaml(&yaml);
let doc = parse.tree();
// Verify we parsed the large mapping correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(mapping.len(), 1000);
assert_eq!(
mapping
.get("key0")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value0"
);
assert_eq!(
mapping
.get("key999")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value999"
);
assert_eq!(
mapping
.get("key250")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value250"
);
assert_eq!(
mapping
.get("key500")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value500"
);
assert_eq!(
mapping
.get("key750")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value750"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Degraded Parsing and Mixed Content
// ========================================
#[test]
fn test_mixed_valid_and_invalid() {
let yaml = r#"
# This is valid
valid_key: valid_value
valid_list:
- item1
- item2
# This has some issues but should still parse
anchor: &test
ref: *test
another: value
"#;
let parse = Parse::parse_yaml(yaml);
let doc = parse.tree();
// Verify we parsed the valid parts correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(
mapping
.get("valid_key")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"valid_value"
);
let valid_list_node = mapping.get("valid_list").unwrap();
let valid_list = valid_list_node.as_sequence().unwrap();
assert_eq!(valid_list.len(), 2);
assert_eq!(
valid_list.get(0).unwrap().as_scalar().unwrap().as_string(),
"item1"
);
assert_eq!(
valid_list.get(1).unwrap().as_scalar().unwrap().as_string(),
"item2"
);
// Verify anchor value (the anchor marker creates an empty/null scalar)
assert!(mapping.get("anchor").unwrap().as_scalar().is_some());
// Verify ref is an alias to "test"
let ref_val = mapping.get("ref").unwrap();
assert!(ref_val.is_alias());
assert_eq!(ref_val.as_alias().unwrap().name(), "test");
assert_eq!(
mapping
.get("another")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Unicode Support
// ========================================
#[test]
fn test_unicode_content() {
let yaml = r#"
english: Hello World
chinese: 你好世界
arabic: مرحبا بالعالم
emoji: 😀🎉🌟
mixed: "English 中文 العربية 🌍"
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed the unicode content correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(
mapping
.get("english")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"Hello World"
);
assert_eq!(
mapping
.get("chinese")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"你好世界"
);
assert_eq!(
mapping
.get("arabic")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"مرحبا بالعالم"
);
assert_eq!(
mapping
.get("emoji")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"😀🎉🌟"
);
assert_eq!(
mapping
.get("mixed")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"English 中文 العربية 🌍"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Complex Nested Structures
// ========================================
#[test]
fn test_complex_structures() {
let yaml = r#"
config:
database:
host: localhost
port: 5432
credentials:
username: admin
password: secret
servers:
- name: web1
ip: 192.168.1.1
services: [http, https]
- name: web2
ip: 192.168.1.2
services: [http, https, ssh]
features:
caching: true
logging:
level: info
file: /var/log/app.log
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed the complex structure correctly via API
let document = doc.document().unwrap();
let root = document.as_mapping().unwrap();
let config = root.get_mapping("config").unwrap();
// Verify database section
let database = config.get_mapping("database").unwrap();
assert_eq!(
database
.get("host")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"localhost"
);
assert_eq!(
database
.get("port")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"5432"
);
let credentials = database.get_mapping("credentials").unwrap();
assert_eq!(
credentials
.get("username")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"admin"
);
// Verify servers section
let servers = config.get_sequence("servers").unwrap();
assert_eq!(servers.len(), 2);
let web1_node = servers.get(0).unwrap();
let web1 = web1_node.as_mapping().unwrap();
assert_eq!(
web1.get("name").unwrap().as_scalar().unwrap().as_string(),
"web1"
);
assert_eq!(
web1.get("ip").unwrap().as_scalar().unwrap().as_string(),
"192.168.1.1"
);
let web1_services = web1.get_sequence("services").unwrap();
assert_eq!(web1_services.len(), 2);
// Verify features section
let features = config.get_mapping("features").unwrap();
assert_eq!(features.get("caching").unwrap().to_bool(), Some(true));
let logging = features.get_mapping("logging").unwrap();
assert_eq!(
logging
.get("level")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"info"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Scalar Types and Values
// ========================================
#[test]
fn test_empty_and_null_values() {
let yaml = r#"
empty_string: ""
null_explicit: null
null_implicit:
tilde_null: ~
empty_list: []
empty_map: {}
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed empty and null values correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(
mapping
.get("empty_string")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
""
);
assert!(mapping
.get("null_explicit")
.unwrap()
.as_scalar()
.unwrap()
.is_null());
assert!(mapping
.get("null_implicit")
.unwrap()
.as_scalar()
.unwrap()
.is_null());
assert!(mapping
.get("tilde_null")
.unwrap()
.as_scalar()
.unwrap()
.is_null());
let empty_list = mapping.get_sequence("empty_list").unwrap();
assert_eq!(empty_list.len(), 0);
let empty_map = mapping.get_mapping("empty_map").unwrap();
assert_eq!(empty_map.len(), 0);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
#[test]
fn test_various_scalars() {
let yaml = r#"
string: hello world
integer: 42
float: 3.125
boolean_true: true
boolean_false: false
hex: 0xFF
octal: 0o755
binary: 0b1010
scientific: 6.02e23
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed various scalar types correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(
mapping
.get("string")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"hello world"
);
assert_eq!(mapping.get("integer").unwrap().to_i64(), Some(42));
assert_eq!(mapping.get("float").unwrap().to_f64(), Some(3.125));
assert_eq!(mapping.get("boolean_true").unwrap().to_bool(), Some(true));
assert_eq!(mapping.get("boolean_false").unwrap().to_bool(), Some(false));
assert_eq!(mapping.get("hex").unwrap().to_i64(), Some(0xFF));
assert_eq!(mapping.get("octal").unwrap().to_i64(), Some(0o755));
assert_eq!(mapping.get("binary").unwrap().to_i64(), Some(0b1010));
assert_eq!(mapping.get("scientific").unwrap().to_f64(), Some(6.02e23));
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Comments Preservation
// ========================================
#[test]
fn test_comments_preservation() {
let yaml = r#"
# Top level comment
key1: value1 # Inline comment
# Comment block
key2: value2
# Indented comment
key3: value3 # Another inline
# Final comment
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed the keys correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(
mapping
.get("key1")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value1"
);
assert_eq!(
mapping
.get("key2")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value2"
);
assert_eq!(
mapping
.get("key3")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value3"
);
// Verify exact round-trip (comments are preserved)
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Multiline Scalars
// ========================================
#[test]
fn test_multiline_scalars() {
let yaml = r#"
literal: |
This is a literal
multiline string
with preserved newlines
folded: >
This is a folded
multiline string
that will be folded
into a single line
plain_multiline: this is a plain
multiline scalar that
continues on multiple lines
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed the multiline scalars correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
let literal = mapping
.get("literal")
.unwrap()
.as_scalar()
.unwrap()
.as_string();
assert_eq!(
literal,
"This is a literal\nmultiline string\nwith preserved newlines\n"
);
let folded = mapping
.get("folded")
.unwrap()
.as_scalar()
.unwrap()
.as_string();
assert_eq!(
folded,
"This is a folded multiline string that will be folded into a single line\n"
);
let plain_multiline = mapping
.get("plain_multiline")
.unwrap()
.as_scalar()
.unwrap()
.as_string();
assert_eq!(
plain_multiline,
"this is a plain multiline scalar that continues on multiple lines"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Flow Collections
// ========================================
#[test]
fn test_flow_collections() {
let yaml = r#"
simple_list: [1, 2, 3, 4, 5]
nested_list: [[1, 2], [3, 4], [5, 6]]
simple_map: {a: 1, b: 2, c: 3}
mixed: [{name: alice, age: 30}, {name: bob, age: 25}]
complex: {
users: [
{name: user1, roles: [admin, user]},
{name: user2, roles: [user]}
],
settings: {debug: true, timeout: 30}
}
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed the flow collections correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
let simple_list = mapping.get_sequence("simple_list").unwrap();
assert_eq!(simple_list.len(), 5);
assert!(simple_list.is_flow_style());
let nested_list = mapping.get_sequence("nested_list").unwrap();
assert_eq!(nested_list.len(), 3);
assert!(nested_list.is_flow_style());
let simple_map = mapping.get_mapping("simple_map").unwrap();
assert_eq!(simple_map.len(), 3);
assert!(simple_map.is_flow_style());
let mixed = mapping.get_sequence("mixed").unwrap();
assert_eq!(mixed.len(), 2);
let first_person_node = mixed.get(0).unwrap();
let first_person = first_person_node.as_mapping().unwrap();
assert_eq!(
first_person
.get("name")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"alice"
);
let complex = mapping.get_mapping("complex").unwrap();
let users = complex.get_sequence("users").unwrap();
let first_user_node = users.get(0).unwrap();
let first_user = first_user_node.as_mapping().unwrap();
let roles = first_user.get_sequence("roles").unwrap();
assert_eq!(
roles.get(0).unwrap().as_scalar().unwrap().as_string(),
"admin"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Anchors and Aliases
// ========================================
#[test]
fn test_anchors_and_aliases() {
let yaml = r#"
defaults: &defaults
timeout: 30
retries: 3
production:
<<: *defaults
host: prod.example.com
development:
<<: *defaults
host: dev.example.com
debug: true
template: &template
version: "1.0"
service1:
<<: *template
name: service1
service2:
<<: *template
name: service2
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed anchors and merge keys correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
// Verify anchored values
let defaults = mapping.get_mapping("defaults").unwrap();
assert_eq!(
defaults
.get("timeout")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"30"
);
let template = mapping.get_mapping("template").unwrap();
assert_eq!(
template
.get("version")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"1.0"
);
// Verify mappings with merge keys
let production = mapping.get_mapping("production").unwrap();
assert_eq!(
production
.get("host")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"prod.example.com"
);
let development = mapping.get_mapping("development").unwrap();
assert_eq!(
development
.get("host")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"dev.example.com"
);
assert_eq!(development.get("debug").unwrap().to_bool(), Some(true));
let service1 = mapping.get_mapping("service1").unwrap();
assert_eq!(
service1
.get("name")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"service1"
);
// Verify exact round-trip (preserves anchors, aliases, and merge keys)
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Document Markers and Directives
// ========================================
#[test]
fn test_document_markers() {
let yaml = r#"
%YAML 1.2
---
doc1: value1
...
---
doc2: value2
---
doc3: value3
...
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed the first document correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(
mapping
.get("doc1")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value1"
);
// Verify exact round-trip (preserves directives, document markers, and all documents)
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Special Characters in Strings
// ========================================
#[test]
fn test_special_characters_in_strings() {
let yaml = r#"
simple: "hello world"
quoted: 'single quotes'
url: "https://example.com"
"#;
let doc = YamlFile::parse(yaml).to_result().unwrap();
// Verify we parsed the special strings correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(
mapping
.get("simple")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"hello world"
);
assert_eq!(
mapping
.get("quoted")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"single quotes"
);
assert_eq!(
mapping.get("url").unwrap().as_scalar().unwrap().as_string(),
"https://example.com"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
// ========================================
// Extreme Stress Tests
// ========================================
#[test]
fn test_very_large_mapping_10k_keys() {
let mut yaml = String::new();
for i in 0..10000 {
yaml.push_str(&format!("key{}: value{}\n", i, i));
}
let parse = Parse::parse_yaml(&yaml);
let doc = parse.tree();
// Verify we parsed the large mapping correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(mapping.len(), 10000, "Should have 10000 keys");
// Spot-check various keys throughout
assert_eq!(
mapping
.get("key0")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value0"
);
assert_eq!(
mapping
.get("key2500")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value2500"
);
assert_eq!(
mapping
.get("key5000")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value5000"
);
assert_eq!(
mapping
.get("key7500")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value7500"
);
assert_eq!(
mapping
.get("key9999")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value9999"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
#[test]
fn test_very_deep_nesting_100_levels() {
// Build deeply nested structure: a: {b: {c: {...}}}
let mut yaml = String::new();
for i in 0..100 {
yaml.push_str(&format!("{}key{}: {{\n", " ".repeat(i), i));
}
yaml.push_str(&format!("{}value: deep\n", " ".repeat(100)));
for i in (0..100).rev() {
yaml.push_str(&format!("{}}}\n", " ".repeat(i)));
}
let parse = Parse::parse_yaml(&yaml);
let doc = parse.tree();
// Verify we parsed the deeply nested structure without crashing
let document = doc.document().unwrap();
let root = document.as_mapping().unwrap();
// Navigate down a few levels to verify structure (checking first 10 levels)
let mut next = root.get("key0").expect("Should have key0");
for i in 1..10 {
let key = format!("key{}", i);
let mapping = next.as_mapping().expect("Should be mapping");
next = mapping
.get(&key)
.unwrap_or_else(|| panic!("Should have {}", key));
}
// Verify we got deep enough
let last_mapping = next.as_mapping().expect("Should be mapping at level 9");
assert!(
last_mapping.contains_key("key10"),
"Should have key10 at level 10"
);
// Verify round-trip produces valid YAML
let output = doc.to_string();
let reparsed = Parse::parse_yaml(&output);
assert!(reparsed.tree().document().is_some());
}
#[test]
fn test_very_long_sequence_10k_items() {
let mut yaml = String::new();
yaml.push_str("items:\n");
for i in 0..10000 {
yaml.push_str(&format!(" - item{}\n", i));
}
let parse = Parse::parse_yaml(&yaml);
let doc = parse.tree();
// Verify we parsed the long sequence correctly via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
let items = mapping.get_sequence("items").unwrap();
assert_eq!(items.len(), 10000, "Should have 10000 items");
// Spot-check various items
assert_eq!(
items.get(0).unwrap().as_scalar().unwrap().as_string(),
"item0"
);
assert_eq!(
items.get(2500).unwrap().as_scalar().unwrap().as_string(),
"item2500"
);
assert_eq!(
items.get(5000).unwrap().as_scalar().unwrap().as_string(),
"item5000"
);
assert_eq!(
items.get(9999).unwrap().as_scalar().unwrap().as_string(),
"item9999"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
#[test]
fn test_large_document_1mb() {
// Create a document that's approximately 1MB
// Each line is ~21 bytes: "key00000: value00000\n"
// 1MB / 21 bytes ≈ 48,000 lines
let mut yaml = String::new();
for i in 0..48000 {
yaml.push_str(&format!("key{:05}: value{:05}\n", i, i));
}
// Verify size is roughly 1MB
assert!(yaml.len() > 900_000, "Should be close to 1MB");
assert!(yaml.len() < 1_100_000, "Should be close to 1MB");
let parse = Parse::parse_yaml(&yaml);
let doc = parse.tree();
// Verify we parsed the large document via API
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(mapping.len(), 48000, "Should have 48000 keys");
// Spot-check a few keys
assert_eq!(
mapping
.get("key00000")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value00000"
);
assert_eq!(
mapping
.get("key24000")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value24000"
);
assert_eq!(
mapping
.get("key47999")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value47999"
);
// Verify exact round-trip
let output = doc.to_string();
assert_eq!(output, yaml);
}
#[test]
fn test_many_comments_10k() {
let mut yaml = String::new();
for i in 0..10000 {
yaml.push_str(&format!("# Comment {}\n", i));
yaml.push_str(&format!("key{}: value{}\n", i, i));
}
let parse = Parse::parse_yaml(&yaml);
let doc = parse.tree();
// Verify we parsed correctly preserving comments
let document = doc.document().unwrap();
let mapping = document.as_mapping().unwrap();
assert_eq!(mapping.len(), 10000, "Should have 10000 keys");
// Spot-check keys
assert_eq!(
mapping
.get("key0")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value0"
);
assert_eq!(
mapping
.get("key9999")
.unwrap()
.as_scalar()
.unwrap()
.as_string(),
"value9999"
);
// Verify exact round-trip (comments are preserved)
let output = doc.to_string();
assert_eq!(output, yaml);
}
|