noyalib 0.0.5

A pure Rust YAML library with zero unsafe code and full serde integration
Documentation
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
//! Comprehensive edge case tests for noyalib.
//!
//! Tests covering edge cases and YAML spec compliance.

// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Noyalib. All rights reserved.

use noyalib::{Value, from_str, to_string};
use serde::{Deserialize, Serialize};

// ============================================================================
// Number Edge Cases
// ============================================================================

#[test]
fn test_very_large_integers() {
    let yaml = format!("value: {}\n", i64::MAX);
    let value: Value = from_str(&yaml).unwrap();
    assert_eq!(value.get("value").unwrap().as_i64(), Some(i64::MAX));

    let yaml = format!("value: {}\n", i64::MIN);
    let value: Value = from_str(&yaml).unwrap();
    assert_eq!(value.get("value").unwrap().as_i64(), Some(i64::MIN));
}

#[test]
fn test_float_precision_edge_cases() {
    // Smallest positive denormalized float
    let yaml = "value: 5e-324\n";
    let value: Value = from_str(yaml).unwrap();
    let f = value.get("value").unwrap().as_f64().unwrap();
    assert!(f > 0.0);

    // Largest finite float
    let yaml = "value: 1.7976931348623157e+308\n";
    let value: Value = from_str(yaml).unwrap();
    let f = value.get("value").unwrap().as_f64().unwrap();
    assert!(f.is_finite());
    assert!(f > 1e307);
}

#[test]
fn test_nan_serialization() {
    use noyalib::Number;

    // NaN serializes to .nan
    let value = Value::Number(Number::Float(f64::NAN));
    let yaml = to_string(&value).unwrap();
    assert!(yaml.contains(".nan") || yaml.contains(".NaN") || yaml.contains(".NAN"));
}

#[test]
fn test_nan_parsing() {
    // NaN can be parsed when in a typed context
    use std::str::FromStr;

    use noyalib::Number;

    let n = Number::from_str(".nan").unwrap();
    assert!(n.is_nan());

    let n = Number::from_str(".NaN").unwrap();
    assert!(n.is_nan());
}

#[test]
fn test_infinity_serialization() {
    use noyalib::Number;

    // Positive infinity serializes
    let value = Value::Number(Number::Float(f64::INFINITY));
    let yaml = to_string(&value).unwrap();
    assert!(yaml.contains(".inf") || yaml.contains(".Inf"));

    // Negative infinity serializes
    let value = Value::Number(Number::Float(f64::NEG_INFINITY));
    let yaml = to_string(&value).unwrap();
    assert!(yaml.contains("-.inf") || yaml.contains("-.Inf"));
}

#[test]
fn test_infinity_parsing() {
    // Infinity can be parsed when in a typed context
    use std::str::FromStr;

    use noyalib::Number;

    assert_eq!(
        Number::from_str(".inf").unwrap(),
        Number::Float(f64::INFINITY)
    );
    assert_eq!(
        Number::from_str("+.inf").unwrap(),
        Number::Float(f64::INFINITY)
    );
    assert_eq!(
        Number::from_str("-.inf").unwrap(),
        Number::Float(f64::NEG_INFINITY)
    );
}

#[test]
fn test_octal_integer_parsing() {
    use std::str::FromStr;

    use noyalib::Number;

    assert_eq!(Number::from_str("0o777").unwrap(), Number::Integer(0o777));
    assert_eq!(Number::from_str("0O10").unwrap(), Number::Integer(8));
    assert_eq!(Number::from_str("0o0").unwrap(), Number::Integer(0));
}

#[test]
fn test_hex_integer_parsing() {
    use std::str::FromStr;

    use noyalib::Number;

    assert_eq!(Number::from_str("0xFF").unwrap(), Number::Integer(255));
    assert_eq!(Number::from_str("0x10").unwrap(), Number::Integer(16));
    assert_eq!(
        Number::from_str("0xDEADBEEF").unwrap(),
        Number::Integer(0xDEADBEEF)
    );
}

#[test]
fn test_binary_integer_parsing() {
    use std::str::FromStr;

    use noyalib::Number;

    assert_eq!(Number::from_str("0b1010").unwrap(), Number::Integer(10));
    assert_eq!(
        Number::from_str("0B11111111").unwrap(),
        Number::Integer(255)
    );
}

// ============================================================================
// String Edge Cases
// ============================================================================

#[test]
fn test_multiline_literal_string() {
    let yaml = r#"
description: |
  This is a multi-line
  literal string that
  preserves newlines.
"#;
    let value: Value = from_str(yaml).unwrap();
    let desc = value.get("description").unwrap().as_str().unwrap();
    assert!(desc.contains("This is a multi-line"));
    assert!(desc.contains("literal string"));
}

#[test]
fn test_multiline_folded_string() {
    let yaml = r#"
description: >
  This is a folded
  string that becomes
  a single line.
"#;
    let value: Value = from_str(yaml).unwrap();
    let desc = value.get("description").unwrap().as_str().unwrap();
    // Folded strings join lines with spaces
    assert!(desc.contains("This is a folded"));
}

#[test]
fn test_strings_with_special_characters() {
    let yaml = r#"
quoted_single: 'has ''escaped'' quotes'
quoted_double: "has \"escaped\" quotes and\nnewline"
backslash: "path\\to\\file"
"#;
    let value: Value = from_str(yaml).unwrap();

    let single = value.get("quoted_single").unwrap().as_str().unwrap();
    assert!(single.contains("'escaped'"));

    let double = value.get("quoted_double").unwrap().as_str().unwrap();
    assert!(double.contains("\"escaped\""));

    let backslash = value.get("backslash").unwrap().as_str().unwrap();
    assert!(backslash.contains("\\"));
}

#[test]
fn test_unicode_strings() {
    let yaml = r#"
emoji: "Hello 👋 World 🌍"
chinese: "你好世界"
japanese: "こんにちは"
arabic: "مرحبا"
mixed: "Hello 世界 مرحبا"
"#;
    let value: Value = from_str(yaml).unwrap();

    assert_eq!(
        value.get("emoji").unwrap().as_str().unwrap(),
        "Hello 👋 World 🌍"
    );
    assert_eq!(value.get("chinese").unwrap().as_str().unwrap(), "你好世界");
    assert_eq!(
        value.get("japanese").unwrap().as_str().unwrap(),
        "こんにちは"
    );
    assert_eq!(value.get("arabic").unwrap().as_str().unwrap(), "مرحبا");
}

#[test]
fn test_empty_string() {
    let yaml = "value: ''\n";
    let value: Value = from_str(yaml).unwrap();
    assert_eq!(value.get("value").unwrap().as_str(), Some(""));

    let yaml = "value: \"\"\n";
    let value: Value = from_str(yaml).unwrap();
    assert_eq!(value.get("value").unwrap().as_str(), Some(""));
}

#[test]
fn test_strings_that_look_like_other_types() {
    let yaml = r#"
bool_like: "true"
number_like: "123"
null_like: "null"
float_like: "3.125"
"#;
    let value: Value = from_str(yaml).unwrap();

    // All should be strings due to quotes
    assert_eq!(value.get("bool_like").unwrap().as_str(), Some("true"));
    assert_eq!(value.get("number_like").unwrap().as_str(), Some("123"));
    assert_eq!(value.get("null_like").unwrap().as_str(), Some("null"));
    assert_eq!(value.get("float_like").unwrap().as_str(), Some("3.125"));
}

// ============================================================================
// Collection Edge Cases
// ============================================================================

#[test]
fn test_empty_sequence() {
    let yaml = "items: []\n";
    let value: Value = from_str(yaml).unwrap();
    let items = value.get("items").unwrap().as_sequence().unwrap();
    assert!(items.is_empty());
}

#[test]
fn test_empty_mapping() {
    let yaml = "config: {}\n";
    let value: Value = from_str(yaml).unwrap();
    let config = value.get("config").unwrap().as_mapping().unwrap();
    assert!(config.is_empty());
}

#[test]
fn test_deeply_nested_structure() {
    // Create a deeply nested structure (10 levels)
    let yaml = r#"
level1:
  level2:
    level3:
      level4:
        level5:
          level6:
            level7:
              level8:
                level9:
                  level10:
                    value: "deep"
"#;
    let value: Value = from_str(yaml).unwrap();
    let deep = value
        .get_path("level1.level2.level3.level4.level5.level6.level7.level8.level9.level10.value")
        .unwrap();
    assert_eq!(deep.as_str(), Some("deep"));
}

#[test]
fn test_large_sequence() {
    // Create a sequence with 1000 items
    let items: Vec<i32> = (0..1000).collect();
    let yaml = to_string(&items).unwrap();
    let parsed: Vec<i32> = from_str(&yaml).unwrap();
    assert_eq!(parsed.len(), 1000);
    assert_eq!(parsed[0], 0);
    assert_eq!(parsed[999], 999);
}

#[test]
fn test_large_mapping() {
    use std::collections::BTreeMap;

    // Create a mapping with 100 keys
    let mut map: BTreeMap<String, i32> = BTreeMap::new();
    for i in 0..100 {
        let _ = map.insert(format!("key_{i}"), i);
    }
    let yaml = to_string(&map).unwrap();
    let parsed: BTreeMap<String, i32> = from_str(&yaml).unwrap();
    assert_eq!(parsed.len(), 100);
    assert_eq!(parsed.get("key_50"), Some(&50));
}

#[test]
fn test_duplicate_keys_in_mapping() {
    // Default policy is Last: last occurrence wins.
    let yaml = r#"
name: first
value: 1
name: second
"#;
    let result: Value = from_str(yaml).unwrap();
    assert_eq!(result.get("name").unwrap().as_str(), Some("second"));
    assert_eq!(result.get("value").unwrap().as_i64(), Some(1));

    // With DuplicateKeyPolicy::Error, duplicate keys are rejected.
    use noyalib::{DuplicateKeyPolicy, ParserConfig, from_str_with_config};
    let config = ParserConfig::new().duplicate_key_policy(DuplicateKeyPolicy::Error);
    let result: Result<Value, _> = from_str_with_config(yaml, &config);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("duplicate") || err.contains("Duplicate"),
        "got: {err}"
    );
}

#[test]
fn test_mixed_type_sequence() {
    let yaml = r#"
items:
  - 1
  - "two"
  - true
  - null
  - 3.125
  - [nested, list]
  - key: value
"#;
    let value: Value = from_str(yaml).unwrap();
    let items = value.get("items").unwrap().as_sequence().unwrap();
    assert_eq!(items.len(), 7);
    assert_eq!(items[0].as_i64(), Some(1));
    assert_eq!(items[1].as_str(), Some("two"));
    assert_eq!(items[2].as_bool(), Some(true));
    assert!(items[3].is_null());
    assert!(items[4].as_f64().is_some());
    assert!(items[5].is_sequence());
    assert!(items[6].is_mapping());
}

// ============================================================================
// YAML Spec Compliance
// ============================================================================

#[test]
fn test_null_representations() {
    let yaml = r#"
null1: null
null2: ~
null3:
"#;
    let value: Value = from_str(yaml).unwrap();
    assert!(value.get("null1").unwrap().is_null());
    assert!(value.get("null2").unwrap().is_null());
    assert!(value.get("null3").unwrap().is_null());
}

#[test]
fn test_boolean_variations() {
    // yaml-rust2 uses YAML 1.1 which recognizes true/false (lowercase)
    let yaml = r#"
true1: true
false1: false
"#;
    let value: Value = from_str(yaml).unwrap();
    assert_eq!(value.get("true1").unwrap().as_bool(), Some(true));
    assert_eq!(value.get("false1").unwrap().as_bool(), Some(false));

    // Capitalized variants may be parsed as strings depending on the YAML version
    let yaml_caps = r#"
true2: True
true3: TRUE
false2: False
false3: FALSE
"#;
    let value: Value = from_str(yaml_caps).unwrap();
    // Test that parsing doesn't fail - the values may be bool or string
    assert!(value.get("true2").is_some());
    assert!(value.get("true3").is_some());
    assert!(value.get("false2").is_some());
    assert!(value.get("false3").is_some());
}

#[test]
fn test_yes_no_on_off_booleans() {
    // YAML 1.1 style (may be treated as strings in YAML 1.2)
    let yaml = r#"
yes_val: yes
no_val: no
on_val: on
off_val: off
"#;
    let value: Value = from_str(yaml).unwrap();
    // yaml-rust2 uses YAML 1.1 which recognizes yes/no/on/off as booleans
    let yes = value.get("yes_val").unwrap();
    let no = value.get("no_val").unwrap();
    // These might be booleans or strings depending on parser
    assert!(yes.is_bool() || yes.is_string());
    assert!(no.is_bool() || no.is_string());
}

#[test]
fn test_flow_vs_block_style() {
    // Flow style (inline)
    let flow_yaml = "{name: test, items: [1, 2, 3]}";
    let flow: Value = from_str(flow_yaml).unwrap();

    // Block style (indented)
    let block_yaml = r#"
name: test
items:
  - 1
  - 2
  - 3
"#;
    let block: Value = from_str(block_yaml).unwrap();

    // Should produce equivalent values
    assert_eq!(
        flow.get("name").unwrap().as_str(),
        block.get("name").unwrap().as_str()
    );
    assert_eq!(
        flow.get("items").unwrap().as_sequence().unwrap().len(),
        block.get("items").unwrap().as_sequence().unwrap().len()
    );
}

// ============================================================================
// Error Handling Edge Cases
// ============================================================================

#[test]
fn test_malformed_yaml_unclosed_bracket() {
    let yaml = "[1, 2, 3";
    let result: Result<Value, _> = from_str(yaml);
    assert!(result.is_err());
}

#[test]
fn test_malformed_yaml_bad_indentation() {
    let yaml = r#"
name: test
  badly: indented
"#;
    let result: Result<Value, _> = from_str(yaml);
    // This may succeed with yaml-rust2 by treating it as a multi-line string
    // or may fail depending on strictness
    // The test documents current behavior
    let _ = result;
}

#[test]
fn test_type_mismatch_error() {
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Typed {
        count: i32,
    }

    let yaml = "count: not_a_number\n";
    let result: Result<Typed, _> = from_str(yaml);
    assert!(result.is_err());
}

#[test]
fn test_missing_required_field_error() {
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Required {
        name: String,
        required_field: i32,
    }

    let yaml = "name: test\n";
    let result: Result<Required, _> = from_str(yaml);
    assert!(result.is_err());
    let err = result.unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("required_field") || msg.contains("missing"));
}

#[test]
fn test_error_location() {
    use noyalib::Location;

    // Test location calculation
    let source = "line1\nline2\nline3";
    let loc = Location::from_index(source, 0);
    assert_eq!(loc.line(), 1);
    assert_eq!(loc.column(), 1);

    let loc = Location::from_index(source, 6);
    assert_eq!(loc.line(), 2);
    assert_eq!(loc.column(), 1);

    let loc = Location::from_index(source, 12);
    assert_eq!(loc.line(), 3);
    assert_eq!(loc.column(), 1);
}

// ============================================================================
// Serialization Edge Cases
// ============================================================================

#[test]
fn test_serialize_option_none() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct WithOption {
        name: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        optional: Option<i32>,
    }

    let value = WithOption {
        name: "test".to_string(),
        optional: None,
    };
    let yaml = to_string(&value).unwrap();
    assert!(!yaml.contains("optional"));
}

#[test]
fn test_serialize_option_some() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct WithOption {
        name: String,
        optional: Option<i32>,
    }

    let value = WithOption {
        name: "test".to_string(),
        optional: Some(42),
    };
    let yaml = to_string(&value).unwrap();
    assert!(yaml.contains("optional"));
    assert!(yaml.contains("42"));
}

#[test]
fn test_serialize_unit_struct() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct UnitStruct;

    let value = UnitStruct;
    let yaml = to_string(&value).unwrap();
    let parsed: UnitStruct = from_str(&yaml).unwrap();
    assert_eq!(value, parsed);
}

#[test]
fn test_serialize_newtype_struct() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Wrapper(i32);

    let value = Wrapper(42);
    let yaml = to_string(&value).unwrap();
    let parsed: Wrapper = from_str(&yaml).unwrap();
    assert_eq!(value, parsed);
}

#[test]
fn test_serialize_tuple_struct() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Point(i32, i32);

    let value = Point(10, 20);
    let yaml = to_string(&value).unwrap();
    let parsed: Point = from_str(&yaml).unwrap();
    assert_eq!(value, parsed);
}

#[test]
fn test_serialize_enum_variants() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    enum MyEnum {
        Unit,
        Newtype(i32),
        Tuple(i32, String),
        Struct { x: i32, y: i32 },
    }

    let unit = MyEnum::Unit;
    let newtype = MyEnum::Newtype(42);
    let tuple = MyEnum::Tuple(1, "hello".to_string());
    let struct_var = MyEnum::Struct { x: 10, y: 20 };

    // Test each variant
    let yaml = to_string(&unit).unwrap();
    let parsed: MyEnum = from_str(&yaml).unwrap();
    assert_eq!(unit, parsed);

    let yaml = to_string(&newtype).unwrap();
    let parsed: MyEnum = from_str(&yaml).unwrap();
    assert_eq!(newtype, parsed);

    let yaml = to_string(&tuple).unwrap();
    let parsed: MyEnum = from_str(&yaml).unwrap();
    assert_eq!(tuple, parsed);

    let yaml = to_string(&struct_var).unwrap();
    let parsed: MyEnum = from_str(&yaml).unwrap();
    assert_eq!(struct_var, parsed);
}

// ============================================================================
// Value Type Edge Cases
// ============================================================================

#[test]
fn test_value_equality_edge_cases() {
    // Same value different representations
    let v1: Value = from_str("42").unwrap();
    let v2 = Value::from(42);
    assert_eq!(v1, v2);

    // Null comparisons
    let null1 = Value::Null;
    let null2: Value = from_str("null").unwrap();
    assert_eq!(null1, null2);
}

#[test]
fn test_value_ordering_edge_cases() {
    use std::cmp::Ordering;

    // Null < Bool
    assert!(Value::Null < Value::Bool(false));

    // Bool < Number
    assert!(Value::Bool(true) < Value::from(0));

    // Number < String
    assert!(Value::from(999) < Value::from("a"));

    // String < Sequence
    assert!(Value::from("zzz") < Value::Sequence(vec![]));

    // Sequence < Mapping
    assert!(Value::Sequence(vec![]) < Value::Mapping(Default::default()));

    // Test within same type
    assert_eq!(Value::from(1).cmp(&Value::from(2)), Ordering::Less);
    assert_eq!(Value::from("a").cmp(&Value::from("b")), Ordering::Less);
}

#[test]
fn test_value_hash_consistency() {
    use std::collections::HashSet;
    use std::hash::{Hash, Hasher};

    fn hash_value(v: &Value) -> u64 {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        v.hash(&mut hasher);
        hasher.finish()
    }

    // Equal values should have equal hashes
    let v1 = Value::from(42);
    let v2 = Value::from(42);
    assert_eq!(v1, v2);
    assert_eq!(hash_value(&v1), hash_value(&v2));

    // Values can be used as hash set keys
    let mut set: HashSet<Value> = HashSet::new();
    let _ = set.insert(Value::from(1));
    let _ = set.insert(Value::from("test"));
    let _ = set.insert(Value::Null);
    assert_eq!(set.len(), 3);
}

// ============================================================================
// Anchor and Alias Edge Cases
// ============================================================================

#[test]
fn test_anchor_with_scalar() {
    let yaml = r#"
value: &anchor 42
copy: *anchor
"#;
    let value: Value = from_str(yaml).unwrap();
    assert_eq!(value.get("value").unwrap().as_i64(), Some(42));
    assert_eq!(value.get("copy").unwrap().as_i64(), Some(42));
}

#[test]
fn test_anchor_with_sequence() {
    let yaml = r#"
items: &items
  - a
  - b
  - c
copy: *items
"#;
    let value: Value = from_str(yaml).unwrap();
    let items = value.get("items").unwrap().as_sequence().unwrap();
    let copy = value.get("copy").unwrap().as_sequence().unwrap();
    assert_eq!(items.len(), copy.len());
    assert_eq!(items.len(), 3);
}

#[test]
fn test_merge_key_basic() {
    let yaml = r#"
defaults: &defaults
  timeout: 30
  retries: 3

server:
  <<: *defaults
  host: localhost
"#;
    let value: Value = from_str(yaml).unwrap();
    let server = value.get("server").unwrap();
    assert_eq!(server.get("host").unwrap().as_str(), Some("localhost"));
    assert_eq!(server.get("timeout").unwrap().as_i64(), Some(30));
    assert_eq!(server.get("retries").unwrap().as_i64(), Some(3));
}

#[test]
fn test_merge_key_override() {
    let yaml = r#"
defaults: &defaults
  timeout: 30
  retries: 3

server:
  <<: *defaults
  timeout: 60
"#;
    let value: Value = from_str(yaml).unwrap();
    let server = value.get("server").unwrap();
    // Local value should override merged value
    assert_eq!(server.get("timeout").unwrap().as_i64(), Some(60));
    assert_eq!(server.get("retries").unwrap().as_i64(), Some(3));
}

// ============================================================================
// Multi-Document Edge Cases
// ============================================================================

#[test]
fn test_load_all_empty() {
    use noyalib::load_all;

    let yaml = "";
    let docs: Vec<_> = load_all(yaml).unwrap().filter_map(Result::ok).collect();
    assert!(docs.is_empty());
}

#[test]
fn test_load_all_single_document() {
    use noyalib::load_all;

    let yaml = "value: 42";
    let docs: Vec<_> = load_all(yaml).unwrap().filter_map(Result::ok).collect();
    assert_eq!(docs.len(), 1);
    assert_eq!(docs[0].get("value").unwrap().as_i64(), Some(42));
}

#[test]
fn test_load_all_multiple_documents() {
    use noyalib::load_all;

    let yaml = r#"
---
name: doc1
---
name: doc2
---
name: doc3
"#;
    let docs: Vec<_> = load_all(yaml).unwrap().filter_map(Result::ok).collect();
    assert_eq!(docs.len(), 3);
    assert_eq!(docs[0].get("name").unwrap().as_str(), Some("doc1"));
    assert_eq!(docs[1].get("name").unwrap().as_str(), Some("doc2"));
    assert_eq!(docs[2].get("name").unwrap().as_str(), Some("doc3"));
}

// ============================================================================
// PathAccess Edge Cases
// ============================================================================

#[test]
fn test_path_with_array_index() {
    let yaml = r#"
items:
  - name: first
  - name: second
  - name: third
"#;
    let value: Value = from_str(yaml).unwrap();
    assert_eq!(
        value.get_path("items[0].name").unwrap().as_str(),
        Some("first")
    );
    assert_eq!(
        value.get_path("items[1].name").unwrap().as_str(),
        Some("second")
    );
    assert_eq!(
        value.get_path("items[2].name").unwrap().as_str(),
        Some("third")
    );
}

#[test]
fn test_path_nonexistent() {
    let yaml = "name: test\n";
    let value: Value = from_str(yaml).unwrap();
    assert!(value.get_path("nonexistent").is_none());
    assert!(value.get_path("name.nested").is_none());
    assert!(value.get_path("items[0]").is_none());
}

#[test]
fn test_path_empty_string() {
    let yaml = "name: test\n";
    let value: Value = from_str(yaml).unwrap();
    // Empty path should return None (no segments)
    let result = value.get_path("");
    assert!(result.is_none() || result == Some(&value));
}

// ============================================================================
// Config Edge Cases
// ============================================================================

#[test]
fn test_serializer_config_document_markers() {
    use noyalib::{SerializerConfig, to_string_with_config};

    let config = SerializerConfig::new()
        .document_start(true)
        .document_end(true);

    let value = Value::from("test");
    let yaml = to_string_with_config(&value, &config).unwrap();
    assert!(yaml.starts_with("---"));
    assert!(yaml.contains("..."));
}

#[test]
fn test_serializer_config_indent() {
    use noyalib::{Mapping, SerializerConfig, to_string_with_config};

    let config = SerializerConfig::new().indent(4);

    let mut map = Mapping::new();
    let _ = map.insert("key", Value::from("value"));
    let value = Value::Mapping(map);

    let yaml = to_string_with_config(&value, &config).unwrap();
    // With 4-space indent, nested content would have 4 spaces
    // For simple maps, just verify it doesn't error
    assert!(yaml.contains("key"));
}