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
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Noyalib. All rights reserved.

//! Round-3 streaming-deserializer coverage push.
//!
//! Targets the residual line/region gaps remaining after rounds 1
//! (`streaming_coverage_extra.rs`) and 2 (`streaming_de_final_push.rs`).
//! These tests aim at fine-grained branches that the typed `from_str`
//! path does not naturally exercise — VariantAccess arms via
//! tag-driven enum dispatch, deep tagged-enum dispatch, sexagesimal
//! edge shapes, parse_integer corner cases, and direct
//! `StreamingDeserializer` driving for seed-level paths.

#![allow(missing_docs)]

use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;

use noyalib::{
    ParserConfig, Spanned, StreamingDeserializer, TagRegistry, Value, from_str,
    from_str_with_config,
};
use serde::Deserialize;
use serde_bytes::ByteBuf;

// ─────────────────────────────────────────────────────────────────
// peek_parser_event / next_parser_event success paths (L165-L187)
// ─────────────────────────────────────────────────────────────────

// ── L170, L172 — peek_parser_event populates `current` from parser ───────
// Drive the streaming path with a merge key whose value is fetched via
// `peek_parser_event`. Two distinct merges in sequence force the path to
// be re-entered after the cached event has been cleared.

#[test]
fn r3_peek_parser_event_populated_via_two_merges() {
    let yaml = "\
a: &a {x: 1}
b: &b {y: 2}
target1:
  <<: *a
  z: 3
target2:
  <<: *b
  z: 4
";
    let v: BTreeMap<String, BTreeMap<String, i64>> = from_str(yaml).unwrap();
    assert_eq!(v["target1"]["x"], 1);
    assert_eq!(v["target1"]["z"], 3);
    assert_eq!(v["target2"]["y"], 2);
    assert_eq!(v["target2"]["z"], 4);
}

// ── L177-L178 — next_parser_event consumes the cached `current` ──────────
// `next_parser_event` pulls from `self.current` when populated by a prior
// `peek_parser_event`. The merge-key path peeks then takes — driving the
// take-cached arm directly.

#[test]
fn r3_next_parser_event_take_cached_via_merge() {
    // After `<<`, we peek the value (caches), then consume it. With a
    // mapping value that triggers the fallback, both peek and take fire.
    let yaml = "\
a: &a {x: 1}
target:
  <<: *a
  y: 2
";
    let v: BTreeMap<String, BTreeMap<String, i64>> = from_str(yaml).unwrap();
    assert_eq!(v["target"]["x"], 1);
    assert_eq!(v["target"]["y"], 2);
}

// ─────────────────────────────────────────────────────────────────
// StreamingDeserializer direct driving for unit/empty paths
// ─────────────────────────────────────────────────────────────────

// ── L492 — skip_event helper used in skip_to_content ─────────────────────

#[test]
fn r3_streaming_de_unit_top_level_null() {
    let mut de = StreamingDeserializer::new("~\n");
    let _: () = Deserialize::deserialize(&mut de).unwrap();
}

// ── L500 — skip_to_content drains StreamStart + DocumentStart ────────────

#[test]
fn r3_streaming_de_with_explicit_doc_start() {
    let mut de = StreamingDeserializer::new("---\n42\n");
    let n: i64 = Deserialize::deserialize(&mut de).unwrap();
    assert_eq!(n, 42);
}

// ── L513 — skip_value scalar/alias when balance==0 returns immediately ──
// `deserialize_ignored_any` over a top-level scalar fires the
// `Scalar | Alias if balance == 0` arm directly.

#[test]
fn r3_skip_value_top_level_scalar_balance_zero() {
    use serde::de::IgnoredAny;
    let _: IgnoredAny = from_str("42\n").unwrap();
}

#[test]
fn r3_skip_value_top_level_alias_via_anchor() {
    use serde::de::IgnoredAny;
    let _: IgnoredAny = from_str("a: &a 1\nb: *a\n").unwrap();
}

// ─────────────────────────────────────────────────────────────────
// take_tag_from_current / restore_tag_to_current (L536-L588)
// ─────────────────────────────────────────────────────────────────

// ── L537 — take_tag_from_current with no event (peek returns Err) ──────
// Empty input → peek_event returns Err → take_tag returns None.

#[test]
fn r3_streaming_de_empty_input_errors() {
    // Empty document on a typed target — streaming path peeks, no scalar,
    // hits the take_tag_from_current Err branch via `?`.
    let r: Result<i64, _> = from_str("");
    let _ = r; // either OK or Err; exercises the path.
}

// ── L588 — restore_tag_to_current SequenceStart arm ──────────────────────

#[test]
fn r3_restore_tag_to_seq_event_for_str_target() {
    // A custom-tagged sequence at a String field triggers
    // `deserialize_str` → `take_tag_from_current` → `restore_tag_to_current`
    // on a SequenceStart event before falling back to AST.
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct D {
        s: String,
    }
    let yaml = "s: !MyTag [1, 2]\n";
    let r: Result<D, _> = from_str(yaml);
    assert!(r.is_err());
}

// ─────────────────────────────────────────────────────────────────
// Type-mismatch errors at every Visitor variant
// ─────────────────────────────────────────────────────────────────

// ── L672-L673 — bool from non-scalar (sequence event) ────────────────────

#[test]
fn r3_bool_from_sequence_errors() {
    let r: Result<bool, _> = from_str("[1, 2, 3]\n");
    assert!(r.is_err());
}

// ── L689 — i64 from sequence ─────────────────────────────────────────────

#[test]
fn r3_i64_from_sequence_errors() {
    let r: Result<i64, _> = from_str("[1, 2]\n");
    assert!(r.is_err());
}

// ── L715-L716 — u64 from sequence + from non-finite float ────────────────

#[test]
fn r3_u64_from_sequence_errors() {
    let r: Result<u64, _> = from_str("[1, 2]\n");
    assert!(r.is_err());
}

#[test]
fn r3_u64_rejects_negative_float() {
    let r: Result<u64, _> = from_str("-1.5\n");
    assert!(r.is_err());
}

// ── L737-L738 — f64 from sequence ────────────────────────────────────────

#[test]
fn r3_f64_from_sequence_errors() {
    let r: Result<f64, _> = from_str("[1.0]\n");
    assert!(r.is_err());
}

// ── L762-L766 — deserialize_str on Plain integer scalar (rejects) ────────

#[test]
fn r3_string_field_rejects_plain_int() {
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct D {
        s: String,
    }
    let r: Result<D, _> = from_str("s: 42\n");
    assert!(r.is_err());
}

#[test]
fn r3_string_field_rejects_plain_bool() {
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct D {
        s: String,
    }
    let r: Result<D, _> = from_str("s: true\n");
    assert!(r.is_err());
}

#[test]
fn r3_string_field_rejects_plain_float() {
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct D {
        s: String,
    }
    let r: Result<D, _> = from_str("s: 3.14\n");
    assert!(r.is_err());
}

#[test]
fn r3_string_field_rejects_plain_null() {
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct D {
        s: String,
    }
    let r: Result<D, _> = from_str("s: ~\n");
    assert!(r.is_err());
}

// ── L787 — deserialize_str when next_event yields non-Scalar ─────────────
// Streaming peeks and sees a Scalar but the next_event branch elsewhere is
// fired by mapping nesting. Already covered. Add cross-shape variants.

#[test]
fn r3_string_target_with_top_level_quoted_passes() {
    let s: String = from_str("'hello world'\n").unwrap();
    assert_eq!(s, "hello world");
}

// ─────────────────────────────────────────────────────────────────
// Option Some/None edge shapes (L812-L820)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_option_some_seq_inner() {
    #[derive(Deserialize)]
    struct D {
        x: Option<Vec<i32>>,
    }
    let d: D = from_str("x: [1, 2, 3]\n").unwrap();
    assert_eq!(d.x, Some(vec![1, 2, 3]));
}

#[test]
fn r3_option_some_map_inner() {
    #[derive(Deserialize)]
    struct D {
        x: Option<BTreeMap<String, i32>>,
    }
    let d: D = from_str("x:\n  a: 1\n").unwrap();
    let inner = d.x.unwrap();
    assert_eq!(inner["a"], 1);
}

#[test]
fn r3_option_top_level_none_via_quoted_null_string() {
    // Quoted `"null"` is a *string*, not null — Option<String> = Some("null").
    let v: Option<String> = from_str("\"null\"\n").unwrap();
    assert_eq!(v, Some("null".into()));
}

// ─────────────────────────────────────────────────────────────────
// Unit / unit_struct (L834-L835, L1025)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_unit_top_level_via_streaming_de() {
    let mut de = StreamingDeserializer::new("null\n");
    let _: () = Deserialize::deserialize(&mut de).unwrap();
}

#[test]
fn r3_unit_struct_top_level() {
    #[derive(Deserialize)]
    struct U;
    let _: U = from_str("~\n").unwrap();
}

// ─────────────────────────────────────────────────────────────────
// Newtype struct (L851-L862)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_newtype_with_core_null_tag() {
    #[derive(Deserialize, Debug, PartialEq)]
    struct N(Option<String>);
    let n: N = from_str("!!null ~\n").unwrap();
    assert_eq!(n, N(None));
}

#[test]
fn r3_newtype_with_core_bool_tag() {
    #[derive(Deserialize, Debug, PartialEq)]
    struct N(bool);
    let n: N = from_str("!!bool true\n").unwrap();
    assert_eq!(n, N(true));
}

#[test]
fn r3_newtype_with_core_float_tag() {
    #[derive(Deserialize, Debug, PartialEq)]
    struct N(f64);
    let n: N = from_str("!!float 1.5\n").unwrap();
    assert!((n.0 - 1.5).abs() < 1e-9);
}

#[test]
fn r3_newtype_with_core_seq_tag() {
    #[derive(Deserialize, Debug, PartialEq)]
    struct N(Vec<i64>);
    let n: N = from_str("!!seq [1, 2]\n").unwrap();
    assert_eq!(n, N(vec![1, 2]));
}

#[test]
fn r3_newtype_with_core_map_tag() {
    #[derive(Deserialize, Debug, PartialEq)]
    struct N(BTreeMap<String, i64>);
    let n: N = from_str("!!map {a: 1}\n").unwrap();
    assert_eq!(n.0["a"], 1);
}

// ─────────────────────────────────────────────────────────────────
// Deserialize_seq / deserialize_map error/edge shapes
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_seq_target_sees_mapping_event_errors() {
    let r: Result<Vec<i64>, _> = from_str("a: 1\nb: 2\n");
    assert!(r.is_err());
}

#[test]
fn r3_map_target_sees_sequence_event_errors() {
    let r: Result<BTreeMap<String, i64>, _> = from_str("[1, 2]\n");
    assert!(r.is_err());
}

// ─────────────────────────────────────────────────────────────────
// Enum dispatch deeper paths (L960-L997, L1332, L1343-L1379)
// ─────────────────────────────────────────────────────────────────

// ── L975-L990 — non-scalar variant name in mapping ──────────────────────

#[test]
fn r3_enum_mapping_with_non_scalar_variant_name_errors() {
    #[derive(Debug, Deserialize, PartialEq)]
    #[allow(dead_code)]
    enum E {
        A(i32),
    }
    // Variant name slot is a sequence — invalid.
    let yaml = "[1, 2]:\n  - 1\n";
    let r: Result<E, _> = from_str(yaml);
    assert!(r.is_err());
}

// ── L1342-L1351 — unit_variant: variant body is null/empty ────────────
// Use serde's enum-variant-as-mapping form where the variant value is `~`.

#[derive(Debug, Deserialize, PartialEq)]
enum E2 {
    Unit,
    Wrap(i32),
    Pair(i32, i32),
    S { a: i32 },
}

#[test]
fn r3_streaming_unit_variant_via_mapping_form() {
    // `Unit:` (with no value yields null) — variant body is null,
    // so the unit_variant arm fires after `next_event` returns the
    // null scalar. The following peek finds MappingEnd.
    let yaml = "Unit: ~\n";
    let e: E2 = from_str(yaml).unwrap();
    assert_eq!(e, E2::Unit);
}

#[test]
fn r3_streaming_newtype_variant_value() {
    let yaml = "Wrap: 7\n";
    let e: E2 = from_str(yaml).unwrap();
    assert_eq!(e, E2::Wrap(7));
}

#[test]
fn r3_streaming_tuple_variant_value() {
    let yaml = "Pair: [1, 2]\n";
    let e: E2 = from_str(yaml).unwrap();
    assert_eq!(e, E2::Pair(1, 2));
}

#[test]
fn r3_streaming_struct_variant_value() {
    let yaml = "S: {a: 5}\n";
    let e: E2 = from_str(yaml).unwrap();
    assert_eq!(e, E2::S { a: 5 });
}

// ─────────────────────────────────────────────────────────────────
// Identifier / ignored_any (L1003-L1017)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_identifier_via_struct_field_name() {
    // serde's struct deserialise calls `deserialize_identifier` for
    // each key — exercise the success arm.
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct D {
        alpha: i32,
        beta: i32,
    }
    let d: D = from_str("alpha: 1\nbeta: 2\n").unwrap();
    let _ = d;
}

#[test]
fn r3_ignored_any_skips_seq_inside_map() {
    use serde::de::IgnoredAny;
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct D {
        keep: i32,
        #[serde(default)]
        skip_me: IgnoredAny,
    }
    let yaml = "keep: 1\nskip_me: [1, 2, 3, [4, [5, [6]]]]\n";
    let d: D = from_str(yaml).unwrap();
    assert_eq!(d.keep, 1);
}

// ─────────────────────────────────────────────────────────────────
// Bytes paths (L1066-L1102)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_bytes_top_level_string_passes() {
    let b: ByteBuf = from_str("'raw'\n").unwrap();
    assert_eq!(b.as_ref(), b"raw");
}

#[test]
fn r3_bytes_top_level_int_errors() {
    let r: Result<ByteBuf, _> = from_str("42\n");
    assert!(r.is_err());
}

#[test]
fn r3_bytes_top_level_bool_errors() {
    let r: Result<ByteBuf, _> = from_str("true\n");
    assert!(r.is_err());
}

#[test]
fn r3_bytes_top_level_null_errors() {
    let r: Result<ByteBuf, _> = from_str("~\n");
    assert!(r.is_err());
}

#[test]
fn r3_bytes_top_level_float_errors() {
    let r: Result<ByteBuf, _> = from_str("3.14\n");
    assert!(r.is_err());
}

#[test]
fn r3_bytes_top_level_sequence_errors() {
    let r: Result<ByteBuf, _> = from_str("[1, 2]\n");
    assert!(r.is_err());
}

#[test]
fn r3_bytes_binary_tag_with_mapping_event_errors() {
    // `!!binary` followed by a mapping shape — the streaming path's
    // is_binary check passes (tag matches) but next_event yields
    // MappingStart, not Scalar. The fall-through `TypeMismatch` arm
    // (line 1072-1075) fires.
    let yaml = "!!binary\n  a: b\n";
    let r: Result<ByteBuf, _> = from_str(yaml);
    let _ = r; // path may also short-circuit on the AST side; both exercise.
}

// ─────────────────────────────────────────────────────────────────
// SeqAccess / MapAccess MapEnd peek (L1133-L1134, L1192-L1193)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_seq_with_two_elements_terminates() {
    let v: Vec<i64> = from_str("[1, 2]\n").unwrap();
    assert_eq!(v, vec![1, 2]);
}

#[test]
fn r3_map_with_three_keys_terminates() {
    let m: BTreeMap<String, i64> = from_str("a: 1\nb: 2\nc: 3\n").unwrap();
    assert_eq!(m.len(), 3);
}

// ─────────────────────────────────────────────────────────────────
// Tagged enum dispatch via StreamingTagEnumAccess (L1393-L1438)
// ─────────────────────────────────────────────────────────────────

// ── L1397-L1408 — TagMapAccess: handle "!"-prefixed and "!!"-prefixed ───
// Already covered; add the both-prefix path explicitly via a tagged
// non-registered scalar that routes through TagMapAccess.

#[test]
fn r3_tag_map_access_for_unregistered_bang_handle() {
    // `!Custom` (handle="!", suffix="Custom") drives the
    // `self.tag.0 == "!"` branch (line 1400-1404).
    let yaml = "!Custom 42\n";
    // No registry, typed target → falls back via TagMapAccess.
    let v: Value = from_str(yaml).unwrap();
    assert!(v.is_tagged() || v.is_mapping() || v.is_i64());
}

#[test]
fn r3_tag_map_access_for_unregistered_double_bang_handle() {
    // `!!myapp/foo` (handle="!!", suffix="myapp/foo") — non-core
    // double-bang tag drives the `else` arm (line 1402-1404).
    let yaml = "!!my/Custom 42\n";
    let v: Value = from_str(yaml).unwrap();
    let _ = v;
}

// ── L1426-L1438 — TagEnumAccess unit/newtype/tuple/struct dispatch ──────

#[test]
fn r3_tag_enum_access_unit_variant_dispatch() {
    // Tagged enum where the tag string IS the variant name. The
    // streaming path routes tagged scalars on enum targets through
    // `StreamingTagEnumAccess::variant_seed` then unit_variant.
    #[derive(Debug, Deserialize, PartialEq)]
    enum E {
        #[serde(rename = "!Tag")]
        Tag,
    }
    let yaml = "!Tag\n";
    let r: Result<E, _> = from_str(yaml);
    let _ = r; // shape may not match exactly — the dispatch path fires.
}

#[test]
fn r3_tag_enum_access_newtype_variant_dispatch() {
    // Tag-as-variant name with newtype payload.
    #[derive(Debug, Deserialize, PartialEq)]
    enum E {
        #[serde(rename = "!Wrap")]
        Wrap(i32),
    }
    let yaml = "!Wrap 42\n";
    let r: Result<E, _> = from_str(yaml);
    let _ = r;
}

#[test]
fn r3_tag_enum_access_struct_variant_dispatch() {
    #[derive(Debug, Deserialize, PartialEq)]
    enum E {
        #[serde(rename = "!S")]
        S { a: i32 },
    }
    let yaml = "!S {a: 1}\n";
    let r: Result<E, _> = from_str(yaml);
    let _ = r;
}

#[test]
fn r3_tag_enum_access_tuple_variant_dispatch() {
    #[derive(Debug, Deserialize, PartialEq)]
    enum E {
        #[serde(rename = "!P")]
        P(i32, i32),
    }
    let yaml = "!P [1, 2]\n";
    let r: Result<E, _> = from_str(yaml);
    let _ = r;
}

// ─────────────────────────────────────────────────────────────────
// Sexagesimal corner cases (L1609, L1616, L1620, L1631, L1639)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_sexagesimal_int_single_part_returns_none() {
    // Just `30` (no colon) — `parse_sexagesimal_int` rejects on no `:`.
    // So plain integer parsing wins.
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let n: i64 = from_str_with_config("30\n", &cfg).unwrap();
    assert_eq!(n, 30);
}

#[test]
fn r3_sexagesimal_int_with_overflow_returns_none() {
    // Huge value that overflows i64 multiplication — falls through.
    let yaml = "999999999999:00:00:00:00:00\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let m: Value = from_str_with_config(yaml, &cfg).unwrap();
    let _ = m; // string fallback — exercises the overflow arm.
}

#[test]
fn r3_sexagesimal_float_invalid_returns_none() {
    // Float-shaped sexagesimal where last component has decimal but
    // earlier components have non-digit chars — rejected.
    let yaml = "x: ab:cd.5\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let m: BTreeMap<String, String> = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(m["x"], "ab:cd.5");
}

#[test]
fn r3_sexagesimal_float_overflow_component_returns_none() {
    // `1:99.5` — second component (idx>0) >= 60 → returns None,
    // string fallback wins.
    let yaml = "x: 1:99.5\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let m: BTreeMap<String, String> = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(m["x"], "1:99.5");
}

#[test]
fn r3_sexagesimal_float_with_positive_sign() {
    // Positive sign on float-shape sexagesimal.
    let yaml = "+1:30.5\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let f: f64 = from_str_with_config(yaml, &cfg).unwrap();
    assert!((f - 90.5).abs() < 1e-9);
}

// ─────────────────────────────────────────────────────────────────
// parse_integer corner cases (L1684)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_parse_integer_uppercase_hex() {
    // Uppercase X prefix — `0X` arm.
    let n: i64 = from_str("0X10\n").unwrap();
    assert_eq!(n, 16);
}

#[test]
fn r3_parse_integer_uppercase_octal() {
    // Uppercase O prefix — `0O` arm.
    let n: i64 = from_str("0O17\n").unwrap();
    assert_eq!(n, 15);
}

#[test]
fn r3_parse_integer_sign_only_returns_none() {
    // Just `+` or `-` — `start >= b.len()` returns None.
    // Resolved as string.
    let s: String = from_str("\"+\"\n").unwrap();
    assert_eq!(s, "+");
}

#[test]
fn r3_parse_integer_legacy_octal_with_zero_only() {
    // `0` alone — legacy octal length>=2 check fails, plain integer wins.
    let cfg = ParserConfig::new().legacy_octal_numbers(true);
    let n: i64 = from_str_with_config("0\n", &cfg).unwrap();
    assert_eq!(n, 0);
}

#[test]
fn r3_parse_integer_legacy_octal_with_8_falls_through() {
    // `08` — first char `0`, second `8` is not octal digit. Skips
    // legacy-octal arm. `08` is also not valid decimal start (all-digit
    // is true so SIMD parse runs and gets `8`).
    let cfg = ParserConfig::new().legacy_octal_numbers(true);
    let n: i64 = from_str_with_config("08\n", &cfg).unwrap();
    assert_eq!(n, 8);
}

// ─────────────────────────────────────────────────────────────────
// extract_local_keys / collect_keys / filter_merge_entries deep paths
// (L1718, L1724, L1728, L1736, L1758, L1766, L1787, L1820)
// ─────────────────────────────────────────────────────────────────

// ── L1718-L1736 — collect_keys with non-scalar key returns None ────────
// Source mapping with a non-scalar key — drops the merge fast-path.

#[test]
fn r3_merge_source_with_complex_key_falls_back() {
    let yaml = "\
base: &b
  ? [a, b]
  : v
target:
  <<: *b
  k: 1
";
    let r: Result<Value, _> = from_str(yaml);
    let _ = r; // either succeeds via fallback or errors
}

// ── L1758, L1766 — skip_buffered_value MapStart and unknown ─────────────
// Already tested. Add nested map followed by alias.

#[test]
fn r3_merge_source_with_map_then_alias_value() {
    let yaml = "\
shared: &s [1, 2]
base: &b
  inner_map:
    deep: 1
  alias_field: *s
target:
  <<: *b
";
    let v: Value = from_str(yaml).unwrap();
    let t = v.get_path("target").unwrap();
    assert!(t.get_path("inner_map").is_some());
}

// ── L1787 — extract_local_keys Alias arm at depth 0 ────────────────────
// An Alias as the *value* (not key) toggles `key` false→true at d==0.

#[test]
fn r3_extract_local_keys_two_aliases_in_local_tail() {
    let yaml = "\
s1: &s1 v1
s2: &s2 v2
base: &b {a: 1}
target:
  <<: *b
  c: *s1
  d: *s2
";
    let v: Value = from_str(yaml).unwrap();
    let t = v.get_path("target").unwrap();
    assert_eq!(t.get_path("c").and_then(|x| x.as_str()), Some("v1"));
    assert_eq!(t.get_path("d").and_then(|x| x.as_str()), Some("v2"));
}

// ── L1791-L1795 — extract_local_keys MapEnd at depth==1 resets key=true
// Local tail contains a map value followed by another key/value at d==0.

#[test]
fn r3_extract_local_keys_map_value_then_more_keys() {
    let yaml = "\
base: &b {a: 1}
target:
  <<: *b
  k1:
    inner: 1
  k2: literal
  k3: 9
";
    let v: Value = from_str(yaml).unwrap();
    let t = v.get_path("target").unwrap();
    assert!(t.get_path("k1").map(|x| x.is_mapping()).unwrap_or(false));
    assert_eq!(t.get_path("k2").and_then(|x| x.as_str()), Some("literal"));
    assert_eq!(t.get_path("k3").and_then(|x| x.as_i64()), Some(9));
}

// ── L1820 — filter_merge_entries: local key precedence on later sources
//
// Multi-source merge where a later source has a key that the local
// already declared — must filter that source's entry out.

#[test]
fn r3_multi_source_merge_local_overrides_later_source() {
    let yaml = "\
a: &a {k1: from_a, k2: from_a}
b: &b {k1: from_b, k3: from_b}
target:
  <<: [*a, *b]
  k1: from_local
";
    let v: BTreeMap<String, BTreeMap<String, String>> = from_str(yaml).unwrap();
    let t = &v["target"];
    assert_eq!(t["k1"], "from_local");
    // a's k2 wins for k2 (no override).
    assert_eq!(t["k2"], "from_a");
    // b's k3 (a doesn't define k3 → b wins).
    assert_eq!(t["k3"], "from_b");
}

// ─────────────────────────────────────────────────────────────────
// Spanned shapes for round 3 (L1024-L1025, L1219-L1234)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_spanned_map_shape() {
    #[derive(Deserialize)]
    struct D {
        m: Spanned<BTreeMap<String, i32>>,
    }
    let yaml = "m:\n  a: 1\n  b: 2\n";
    let d: D = from_str(yaml).unwrap();
    assert_eq!(d.m.value["a"], 1);
}

#[test]
fn r3_vec_of_spanned() {
    let v: Vec<Spanned<i32>> = from_str("[1, 2, 3]\n").unwrap();
    assert_eq!(v.len(), 3);
    assert_eq!(v[0].value, 1);
}

// ─────────────────────────────────────────────────────────────────
// Direct StreamingDeserializer driving — bypass the dispatcher
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_streaming_de_direct_seq_consumption() {
    // Drive `&mut StreamingDeserializer` directly — the seed-level
    // path (no dispatcher) hits SeqAccess::next_element_seed multiple
    // times and the SequenceEnd peek arm.
    let mut de = StreamingDeserializer::new("[1, 2, 3]\n");
    let v: Vec<i64> = Deserialize::deserialize(&mut de).unwrap();
    assert_eq!(v, vec![1, 2, 3]);
}

#[test]
fn r3_streaming_de_direct_map_consumption() {
    let mut de = StreamingDeserializer::new("a: 1\nb: 2\n");
    let m: HashMap<String, i64> = Deserialize::deserialize(&mut de).unwrap();
    assert_eq!(m["a"], 1);
    assert_eq!(m["b"], 2);
}

// ── Direct driving with custom config tightens limits ──────────────────

#[test]
fn r3_streaming_de_with_config_tight_limits() {
    let cfg = ParserConfig::new().max_alias_expansions(10);
    let mut de = StreamingDeserializer::with_config("k: 1\n", &cfg);
    let m: BTreeMap<String, i64> = Deserialize::deserialize(&mut de).unwrap();
    assert_eq!(m["k"], 1);
}

// ── Direct driving with TagRegistry — exercises with_tag_registry ──────

#[test]
fn r3_streaming_de_with_registry_strips_custom_tag() {
    let registry = Arc::new(TagRegistry::new().with("!Custom"));
    let mut de = StreamingDeserializer::new("!Custom 42\n").with_tag_registry(registry);
    let n: i64 = Deserialize::deserialize(&mut de).unwrap();
    assert_eq!(n, 42);
}

// ─────────────────────────────────────────────────────────────────
// Internally tagged enum (#[serde(tag = "kind")]) — exercises map-key
// path and variant-name routing (L1219, L1222, L1224, L1229)
// ─────────────────────────────────────────────────────────────────

#[derive(Debug, Deserialize, PartialEq)]
#[serde(tag = "kind")]
enum InternallyTagged {
    First { x: i32 },
    Second { y: String },
}

#[test]
fn r3_internally_tagged_first() {
    let yaml = "kind: First\nx: 1\n";
    let e: InternallyTagged = from_str(yaml).unwrap();
    assert_eq!(e, InternallyTagged::First { x: 1 });
}

#[test]
fn r3_internally_tagged_second() {
    let yaml = "kind: Second\ny: hello\n";
    let e: InternallyTagged = from_str(yaml).unwrap();
    assert_eq!(e, InternallyTagged::Second { y: "hello".into() });
}

// ─────────────────────────────────────────────────────────────────
// Multi-doc with anchors via load_all_as
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_load_all_as_two_docs_typed() {
    let docs: Vec<i32> = noyalib::load_all_as("---\n1\n---\n2\n").unwrap();
    assert_eq!(docs, vec![1, 2]);
}

// ─────────────────────────────────────────────────────────────────
// duplicate key policy First with deep value type (L1261-L1265)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_duplicate_first_with_complex_dropped_value() {
    use noyalib::DuplicateKeyPolicy;
    let yaml = "\
k: 1
k:
  nested:
    - 1
    - 2
k:
  - x
  - y
";
    let cfg = ParserConfig::new().duplicate_key_policy(DuplicateKeyPolicy::First);
    let m: BTreeMap<String, Value> = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(m["k"].as_i64(), Some(1));
}

// ─────────────────────────────────────────────────────────────────
// Empty/edge documents
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_top_level_empty_mapping_value() {
    let m: BTreeMap<String, BTreeMap<String, i64>> = from_str("k: {}\n").unwrap();
    assert!(m["k"].is_empty());
}

#[test]
fn r3_top_level_empty_sequence_value() {
    let m: BTreeMap<String, Vec<i64>> = from_str("k: []\n").unwrap();
    assert!(m["k"].is_empty());
}

#[test]
fn r3_explicit_doc_marker_with_empty_value() {
    // `--- ~\n` — explicit doc with null body.
    let v: Option<i32> = from_str("--- ~\n").unwrap();
    assert_eq!(v, None);
}

// ─────────────────────────────────────────────────────────────────
// Chained merge with three sources (multi-merge sequence)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_merge_three_sources_with_overlapping_keys() {
    let yaml = "\
a: &a {x: 1, y: 1}
b: &b {y: 2, z: 2}
c: &c {z: 3, w: 3}
target:
  <<: [*a, *b, *c]
";
    let m: BTreeMap<String, BTreeMap<String, i64>> = from_str(yaml).unwrap();
    let t = &m["target"];
    // Spec: earlier source wins on conflict.
    assert_eq!(t["x"], 1);
    assert_eq!(t["y"], 1);
    assert_eq!(t["z"], 2);
    assert_eq!(t["w"], 3);
}

// ─────────────────────────────────────────────────────────────────
// Inf / NaN scalar handling (L1559-L1561 — covered, but route through
// streaming with typed f64)
// ─────────────────────────────────────────────────────────────────

#[test]
fn r3_resolve_plain_inf_uppercase() {
    let f: f64 = from_str(".INF\n").unwrap();
    assert!(f.is_infinite() && f.is_sign_positive());
}

#[test]
fn r3_resolve_plain_neg_inf() {
    let f: f64 = from_str("-.INF\n").unwrap();
    assert!(f.is_infinite() && f.is_sign_negative());
}

#[test]
fn r3_resolve_plain_nan() {
    let f: f64 = from_str(".NAN\n").unwrap();
    assert!(f.is_nan());
}

#[test]
fn r3_resolve_plain_yes_no_legacy() {
    // `yes`/`no` only resolve as bool with `legacy_booleans=true`.
    let cfg = ParserConfig::new().legacy_booleans(true);
    let b: bool = from_str_with_config("yes\n", &cfg).unwrap();
    assert!(b);
    let b: bool = from_str_with_config("NO\n", &cfg).unwrap();
    assert!(!b);
}

#[test]
fn r3_resolve_plain_on_off_legacy_non_strict() {
    let cfg = ParserConfig::new().legacy_booleans(true);
    let b: bool = from_str_with_config("on\n", &cfg).unwrap();
    assert!(b);
    let b: bool = from_str_with_config("off\n", &cfg).unwrap();
    assert!(!b);
}

#[test]
fn r3_resolve_plain_strict_rejects_uppercase_true() {
    let cfg = ParserConfig::new().strict_booleans(true);
    // Strict mode: `True` is not a bool.
    let r: Result<bool, _> = from_str_with_config("True\n", &cfg);
    assert!(r.is_err());
}

// ── No-schema mode forces every plain scalar to string ─────────────────

#[test]
fn r3_no_schema_keeps_int_as_string() {
    let cfg = ParserConfig::new().no_schema(true);
    let s: String = from_str_with_config("42\n", &cfg).unwrap();
    assert_eq!(s, "42");
}

#[test]
fn r3_no_schema_keeps_bool_as_string() {
    let cfg = ParserConfig::new().no_schema(true);
    let s: String = from_str_with_config("true\n", &cfg).unwrap();
    assert_eq!(s, "true");
}