tabnas-csv 0.6.5

CSV (RFC 4180) grammar plugin for the tabnas parsing engine, over the jsonic base grammar
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
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
// In-language behaviour, ported from go/csv_test.go and ts/test/csv.test.ts:
// what the shared `test/spec` fixtures cannot express (the API surface,
// streaming, typed option values, the `test/fixtures` manifest corpus with
// its engine-level `jsonicOpt`, error messages, the plugin under
// `derive`), plus the same value checks the other two suites make so the
// three cannot drift on them.

mod common;

use std::fs;
use std::sync::{Arc, Mutex};

use serde_json::{json, Value as Json};
use tabnas::Value;
use tabnas_csv::{CsvOptions, FieldOptions, RecordOptions, Stream, StreamEvent, StringOptions};

use common::{
    fixture_parser, fixtures_dir, parse_default, parse_json, parser_for, parser_with, to_json,
};

fn must(src: &str, options: Json) -> Json {
    parse_json(src, options.clone())
        .unwrap_or_else(|error| panic!("parse {src:?} with {options}: {error}"))
}

fn must_default(src: &str) -> Json {
    parse_default(src).unwrap_or_else(|error| panic!("parse {src:?}: {error}"))
}

fn code_of(src: &str, options: Json) -> String {
    match parse_json(src, options.clone()) {
        Ok(value) => panic!("parse {src:?} with {options} succeeded with {value}"),
        Err(error) => error.code,
    }
}

// ---------------------------------------------------------------------------
// The test/fixtures manifest corpus (TestFixtures / `fixtures`)
// ---------------------------------------------------------------------------

#[test]
fn fixtures() {
    let dir = fixtures_dir();
    let manifest: serde_json::Map<String, Json> = serde_json::from_str(
        &fs::read_to_string(dir.join("manifest.json")).expect("manifest.json is readable"),
    )
    .expect("manifest.json is a JSON object");
    assert!(!manifest.is_empty(), "the manifest names no fixtures");

    let mut failures = Vec::new();
    let mut judged = 0;
    for (key, entry) in &manifest {
        judged += 1;
        let name = entry["name"].as_str().unwrap_or(key);
        let csv_file = entry
            .get("csvFile")
            .and_then(Json::as_str)
            .unwrap_or(key.as_str());
        let source = fs::read_to_string(dir.join(format!("{csv_file}.csv")))
            .unwrap_or_else(|error| panic!("{name}: read {csv_file}.csv: {error}"));

        let options = entry.get("opt").cloned().unwrap_or_else(|| json!({}));
        let parser = fixture_parser(&options, entry.get("jsonicOpt"));
        let result = parser.parse(&source);

        if let Some(expected_code) = entry.get("err").and_then(Json::as_str) {
            match result {
                Ok(value) => failures.push(format!(
                    "{name}: expected error {expected_code}, parsed {}",
                    to_json(&value)
                )),
                Err(error) if error.code != expected_code => failures.push(format!(
                    "{name}: expected error {expected_code}, got {}: {error}",
                    error.code
                )),
                Err(_) => {}
            }
            continue;
        }

        let expected: Json = serde_json::from_str(
            &fs::read_to_string(dir.join(format!("{key}.json")))
                .unwrap_or_else(|error| panic!("{name}: read {key}.json: {error}")),
        )
        .unwrap_or_else(|error| panic!("{name}: {key}.json: {error}"));
        match result {
            Ok(value) => {
                let got = to_json(&value);
                if got != expected {
                    failures.push(format!("{name}:\n  got      {got}\n  expected {expected}"));
                }
            }
            Err(error) => failures.push(format!("{name}: unexpected error: {error}")),
        }
    }
    assert_eq!(judged, manifest.len());
    assert!(
        failures.is_empty(),
        "{} of {} manifest fixtures failed:\n  - {}",
        failures.len(),
        manifest.len(),
        failures.join("\n  - ")
    );
}

// ---------------------------------------------------------------------------
// The API surface (TestPlugin, TestPluginWithOptions, TestPluginEmpty,
// TestUsePlugin, TestObjectOutputIsMap)
// ---------------------------------------------------------------------------

#[test]
fn plugin() {
    assert_eq!(
        must_default("a,b\n1,2\n3,4"),
        json!([{"a":"1","b":"2"},{"a":"3","b":"4"}])
    );
}

#[test]
fn plugin_with_options() {
    assert_eq!(
        must("a,b\n1,2", json!({"object": false})),
        json!([["1", "2"]])
    );
}

#[test]
fn plugin_empty() {
    assert_eq!(must_default(""), json!([]));
}

#[test]
fn the_documented_stacks_agree() {
    // Every documented way of building the parser reproduces the TypeScript
    // `new Tabnas().use(jsonic).use(Csv)` result.
    let wanted = json!([{"a":"1","b":"2"}]);
    assert_eq!(
        to_json(&tabnas_csv::parse("a,b\n1,2").expect("parses")),
        wanted
    );
    assert_eq!(
        to_json(&tabnas_csv::make().parse("a,b\n1,2").expect("parses")),
        wanted
    );
    assert_eq!(
        to_json(
            &parser_with(CsvOptions::default())
                .parse("a,b\n1,2")
                .expect("parses")
        ),
        wanted
    );
    assert_eq!(
        to_json(&parser_for(json!({})).parse("a,b\n1,2").expect("parses")),
        wanted
    );

    let mut manual = tabnas_jsonic::make();
    tabnas_csv::csv(&mut manual, &CsvOptions::default()).expect("installs");
    assert_eq!(to_json(&manual.parse("a,b\n1,2").expect("parses")), wanted);
}

#[test]
fn the_plugin_bag_merges_over_the_defaults() {
    // `use_plugin` deep-merges the caller's bag over the plugin defaults,
    // so a partial bag keeps every default it does not name.
    let parser = parser_for(json!({"field": {"nonameprefix": "col"}}));
    let options = parser
        .plugin_options("csv")
        .expect("the csv options are recorded");
    let bag = to_json(options);
    assert_eq!(bag["field"]["nonameprefix"], "col");
    assert_eq!(bag["field"]["empty"], "");
    assert_eq!(bag["header"], true);
    assert_eq!(bag["strict"], true);
    assert_eq!(
        to_json(&parser.parse("a\n1,2").expect("parses")),
        json!([{"a":"1","col1":"2"}])
    );
}

#[test]
fn the_defaults_are_the_typescript_ones() {
    let defaults = serde_json::to_value(CsvOptions::default()).expect("serializes");
    assert_eq!(
        defaults,
        json!({
            "trim": null, "comment": null, "number": null, "value": null,
            "header": true, "object": true, "strict": true,
            "field": {"separation": null, "nonameprefix": "field~", "empty": "", "names": null, "exact": false},
            "record": {"separators": null, "empty": false},
            "string": {"quote": "\"", "csv": null},
        })
    );
    let round_trip =
        CsvOptions::from_value(&CsvOptions::default().to_value()).expect("round-trips");
    assert_eq!(
        serde_json::to_value(round_trip).expect("serializes"),
        defaults
    );
}

#[test]
fn installing_twice_does_not_double_the_grammar() {
    let mut parser = tabnas_jsonic::make();
    tabnas_csv::csv(&mut parser, &CsvOptions::default()).expect("installs");
    tabnas_csv::csv(&mut parser, &CsvOptions::default()).expect("a re-run is a no-op");
    assert_eq!(
        to_json(&parser.parse("a,b\n1,2").expect("parses")),
        json!([{"a":"1","b":"2"}])
    );
}

/// The IGNORE token set is what makes a row break significant: `#LN` has to
/// leave it, and `#SP` too in strict mode, or a document collapses into one
/// record. The engine merges a token set INDEX-WISE onto the one already
/// installed, so the override is spelled position for position over the
/// jsonic default and names its trailing position rather than stopping
/// short. Both halves are asserted here because a set that is merely
/// shorter than the installed one still parses the first record.
#[test]
fn the_ignore_set_keeps_only_the_tokens_the_mode_ignores() {
    let mut strict = parser_for(json!({}));
    let comment = strict.token("#CM");
    assert_eq!(strict.token_set("IGNORE"), Some(vec![comment]));

    let mut relaxed = parser_for(json!({"strict": false}));
    let space = relaxed.token("#SP");
    assert_eq!(
        relaxed.token_set("IGNORE"),
        Some(vec![space, relaxed.token("#CM")])
    );

    // A derived instance overlays the override onto the REDUCED set it
    // inherits, so the same spelling has to answer the same there.
    let child = strict.derive(|_options| {}).expect("derives");
    assert_eq!(child.token_set("IGNORE"), Some(vec![comment]));
    assert_eq!(
        to_json(&child.parse("a\nb\nc").expect("parses")),
        json!([{"a":"b"},{"a":"c"}])
    );
}

#[test]
fn a_derived_instance_rebuilds_the_grammar() {
    let parser = parser_for(json!({"object": false}));
    let child = parser.derive(|_options| {}).expect("derives");
    assert_eq!(
        to_json(&child.parse("a,b\n1,2").expect("parses")),
        json!([["1", "2"]])
    );
}

#[test]
fn object_output_is_a_map_in_document_order() {
    let value = tabnas_csv::parse("name,age\nAlice,30\nBob,25").expect("parses");
    let Value::Array(records) = &value else {
        panic!("expected an array, got {value:?}");
    };
    let Value::Object(first) = &records[0] else {
        panic!("expected an object record, got {:?}", records[0]);
    };
    let keys: Vec<&String> = first.keys().collect();
    assert_eq!(keys, ["name", "age"]);
    assert_eq!(first["name"], Value::String("Alice".to_string()));
    assert_eq!(first["age"], Value::String("30".to_string()));
}

// ---------------------------------------------------------------------------
// Behaviour (TestEmptyRecords, TestHeader, TestComma, TestDoubleQuotes,
// TestTrim, TestComment, TestNumber, TestValue, TestSeparators,
// TestRecordSeparators, TestUnstrict, TestEmptyAnyType, TestFieldExact)
// ---------------------------------------------------------------------------

#[test]
fn empty_records() {
    assert_eq!(
        must_default("a\n1\n\n2\n3\n\n\n4\n"),
        json!([{"a":"1"},{"a":"2"},{"a":"3"},{"a":"4"}])
    );
    assert_eq!(
        must("a\n1\n\n2\n3\n\n\n4\n", json!({"record": {"empty": true}})),
        json!([{"a":"1"},{"a":""},{"a":"2"},{"a":"3"},{"a":""},{"a":""},{"a":"4"}])
    );
    assert_eq!(must_default("\n"), json!([]));
    assert_eq!(
        must_default("\r\n\r\na,b\r\nA,B\r\n\r\n"),
        json!([{"a":"A","b":"B"}])
    );
    assert_eq!(
        must("a#X\n1\n#Y\n2\n3\n\n#Z\n4\n#Q", json!({"comment": true})),
        json!([{"a":"1"},{"a":"2"},{"a":"3"},{"a":"4"}])
    );
    assert_eq!(
        must(
            "a#X\n1\n#Y\n2\n3\n\n#Z\n4\n#Q",
            json!({"comment": true, "record": {"empty": true}})
        ),
        json!([{"a":"1"},{"a":""},{"a":"2"},{"a":"3"},{"a":""},{"a":""},{"a":"4"}])
    );
}

#[test]
fn header() {
    assert_eq!(must_default("\na,b\nA,B"), json!([{"a":"A","b":"B"}]));
    assert_eq!(
        must("\na,b\nA,B", json!({"header": false})),
        json!([{"field~0":"a","field~1":"b"},{"field~0":"A","field~1":"B"}])
    );
    assert_eq!(
        must("\na,b\nA,B", json!({"header": false, "object": false})),
        json!([["a", "b"], ["A", "B"]])
    );
    assert_eq!(
        must(
            "\na,b\nA,B",
            json!({"header": false, "field": {"names": ["a", "b"]}})
        ),
        json!([{"a":"a","b":"b"},{"a":"A","b":"B"}])
    );
}

#[test]
fn comma() {
    let cases = [
        ("a\n1,", json!([{"a":"1","field~1":""}])),
        ("a\n,1", json!([{"a":"","field~1":"1"}])),
        ("a,b\n1,2,", json!([{"a":"1","b":"2","field~2":""}])),
        ("a,b\n,1,2", json!([{"a":"","b":"1","field~2":"2"}])),
        ("a\n1,\n", json!([{"a":"1","field~1":""}])),
        ("a,b\n1,2,\n", json!([{"a":"1","b":"2","field~2":""}])),
    ];
    for (src, wanted) in cases {
        assert_eq!(must_default(src), wanted, "comma {src:?}");
    }
    assert_eq!(must_default("\na"), json!([]));
    assert_eq!(must("a\n1,", json!({"object": false})), json!([["1", ""]]));
    assert_eq!(
        must("a,b\n,1,2", json!({"object": false})),
        json!([["", "1", "2"]])
    );
}

#[test]
fn double_quotes() {
    let cases = [
        ("a\n\"b\"", "b"),
        ("a\n\"\"\"b\"", "\"b"),
        ("a\n\"b\"\"\"", "b\""),
        ("a\n\"\"\"b\"\"\"", "\"b\""),
        ("a\n\"b\"\"c\"", "b\"c"),
        ("a\n\"b\"\"c\"\"d\"", "b\"c\"d"),
        ("a\n\"b\"\"c\"\"d\"\"e\"", "b\"c\"d\"e"),
        ("a\n\"\"\"\"\"b\"", "\"\"b"),
        ("a\n\"b\"\"\"\"\"", "b\"\""),
        ("a\n\"\"\"\"\"b\"\"\"\"\"", "\"\"b\"\""),
    ];
    for (src, wanted) in cases {
        assert_eq!(must_default(src), json!([{"a": wanted}]), "quotes {src:?}");
    }
}

#[test]
fn trim() {
    assert_eq!(must_default("a\n b"), json!([{"a":" b"}]));
    assert_eq!(must_default("a\nb "), json!([{"a":"b "}]));
    assert_eq!(must_default("a\n b "), json!([{"a":" b "}]));
    assert_eq!(must("a\n b", json!({"trim": true})), json!([{"a":"b"}]));
    assert_eq!(must("a\nb ", json!({"trim": true})), json!([{"a":"b"}]));
    assert_eq!(
        must("a\n b c ", json!({"trim": true})),
        json!([{"a":"b c"}])
    );
}

#[test]
fn comment() {
    assert_eq!(must_default("a\n# b"), json!([{"a":"# b"}]));
    assert_eq!(must("a\n# b", json!({"comment": true})), json!([]));
    assert_eq!(
        must("a\n b #c", json!({"comment": true})),
        json!([{"a":" b "}])
    );
    // Non-strict mode enables comment (and trim) by default.
    assert_eq!(must("a\n# b", json!({"strict": false})), json!([]));
    assert_eq!(must("a\n b ", json!({"strict": false})), json!([{"a":"b"}]));
}

#[test]
fn number() {
    assert_eq!(must_default("a\n1"), json!([{"a":"1"}]));
    assert_eq!(must("a\n1", json!({"number": true})), json!([{"a":1.0}]));
    assert_eq!(
        must("a\n1e2", json!({"number": true})),
        json!([{"a":100.0}])
    );
    assert_eq!(must_default("a\n1e2"), json!([{"a":"1e2"}]));
    assert_eq!(
        must("a\n1e2", json!({"strict": false})),
        json!([{"a":100.0}])
    );
}

// A numeric header cell becomes a column NAME, and the canonical
// TypeScript names it with JavaScript's object-key coercion, which is
// `String(number)`. Rust's own f64 formatting disagrees in three places
// that reach a key: negative zero keeps its sign (`-0`), the exponent
// thresholds at 1e21 and 1e-7 are never taken, and a large integral float
// prints its exact binary value (`123456789012345683968`) rather than its
// shortest round-tripping digits.
//
// Those cases now live in `test/spec/number.tsv`, where all three
// runtimes run them. They were a Rust-only test while the Go port named
// every non-string header cell "", which also collapsed the columns onto
// one key; that is fixed, so the coverage moved to the shared fixture
// rather than staying in one runtime.
//
// An ARRAY header cell is named the same way, through
// `Array.prototype.toString`, which is `join(',')`. Those cases live in
// `test/spec/unstrict.tsv`, again in all three runtimes.

// DIVERGENCE (see ../../DIVERGENCE.md): an OBJECT header cell.
//
// The canonical TypeScript throws a raw JavaScript
// `TypeError: Cannot convert object to primitive value`, because a jsonic
// object is allocated with a null prototype and so has no `toString`.
// This port cannot raise a JavaScript `TypeError`, so it refuses the
// document with the engine's inherited `unexpected` code instead of
// inventing a column name the canonical never produces. That is why the
// case cannot be a shared fixture row: the canonical does not fail with a
// parse error, it throws.
//
// Asserted in both directions, so a repair fails as loudly as a
// regression: the parse must FAIL, and with the code recorded. Before
// this, the document parsed and the column was named `{"x":1.0}`, a JSON
// render of the cell.
#[test]
fn an_object_header_cell_refuses_the_document() {
    for src in [
        "a,{x:1}\nx,y",
        "a,[{x:1}]\nx,y",
        "a,[1,{x:1}]\nx,y",
        "{x:1},a\nx,y",
        // The cell is the whole header.
        "{x:1}\nx",
        // A SHORT data row still runs the name loop to the header's
        // length, filling the missing cell with `field.empty`, so the name
        // is still taken. `field.exact` is off here; with it on the length
        // check wins instead, which is
        // `field_exact_uses_a_header_that_was_never_converted`.
        "a,{x:1}\nx",
        // The first data record is where it stops.
        "a,{x:1}\nx,y\np,q",
    ] {
        assert_eq!(
            code_of(src, json!({"strict": false})),
            "unexpected",
            "{src}"
        );
    }
}

// The same cell is ordinary text in strict mode, where no field body is
// parsed, so the refusal above cannot reach a default-options document.
#[test]
fn an_object_header_cell_is_text_in_strict_mode() {
    assert_eq!(
        must_default("a,{x:1}\nx,y"),
        json!([{"a": "x", "{x:1}": "y"}])
    );
}

// The refusal above must reach only a column name that is actually BUILT.
// The canonical keeps the header row RAW (`ctx.u.fields = r.child.node`)
// and applies ToPropertyKey in one place, `obj[fields[fI]] = ...`, which
// runs only under `object: true` and only once there is a record to key.
//
// Refusing at the header row instead was too early, and these are the
// documents it wrongly refused: measured against the canonical runtime,
// which answers `[["x","y"]]` for the first three and `[]` for the rest,
// while this port answered `unexpected` for every one of them.
#[test]
fn the_object_refusal_reaches_only_a_built_column_name() {
    // `object: false` never applies ToPropertyKey at all: the header is
    // kept for the field COUNT and later rows come back as arrays.
    for src in ["a,{x:1}\nx,y", "{x:1},a\nx,y", "a,[1,{x:1}]\nx,y"] {
        assert_eq!(
            must(src, json!({"strict": false, "object": false})),
            json!([["x", "y"]]),
            "{src}"
        );
    }
    // A header-only document has no record to key, under either shape.
    for options in [
        json!({"strict": false}),
        json!({"strict": false, "object": false}),
    ] {
        for src in ["a,{x:1}", "a,{x:1}\n"] {
            assert_eq!(must(src, options.clone()), json!([]), "{src} {options}");
        }
    }
    // `header: false`: there is no header row, so the object is a field
    // VALUE and no name is ever taken from it.
    assert_eq!(
        must("a,{x:1}\nx,y", json!({"strict": false, "header": false})),
        json!([{"field~0": "a", "field~1": {"x": 1.0}}, {"field~0": "x", "field~1": "y"}])
    );
    assert_eq!(
        must(
            "a,{x:1}\nx,y",
            json!({"strict": false, "header": false, "object": false})
        ),
        json!([["a", {"x": 1.0}], ["x", "y"]])
    );
    assert_eq!(
        must(
            "a,{x:1}\nx,y",
            json!({"strict": false, "header": false, "field": {"names": ["p", "q"]}})
        ),
        json!([{"p": "a", "q": {"x": 1.0}}, {"p": "x", "q": "y"}])
    );
}

// `field.exact` is measured against the LENGTH of the header row, which
// the canonical reads off a header it has not converted. So it fires, and
// fires first, on a header holding a cell that can never be named, under
// either result shape. Both codes are what the canonical raises.
#[test]
fn field_exact_uses_a_header_that_was_never_converted() {
    for options in [
        json!({"strict": false, "object": false, "field": {"exact": true}}),
        json!({"strict": false, "field": {"exact": true}}),
    ] {
        assert_eq!(
            code_of("a,{x:1}\nx,y,z", options.clone()),
            "csv_extra_field",
            "{options}"
        );
        assert_eq!(
            code_of("a,{x:1}\nx", options.clone()),
            "csv_missing_field",
            "{options}"
        );
    }
}

// `field.empty` is dropped into a syntactically empty cell, and the header
// row is a row like any other, so an OPTION value can name a column
// without ever passing the lexer. The expectations are the canonical
// runtime's.
//
// This one guards a defect this port does NOT have, and it passes against
// the code before the fix above as well as after. The Go port needed the
// same widening as a repair: there an option value keeps whatever numeric
// type the caller spelled it with (`42` is an `int`), and a conversion
// that handled only the lexer's `float64` refused the document. Here
// `Value::from_json` folds every JSON number into the one
// `Value::Number(f64)` the lexer produces, and a typed caller can spell
// `empty` only as a `serde_json::Value`, so an option value reaches the
// conversion in the same variant a parsed one does.
#[test]
fn field_empty_names_a_column_from_an_option_value() {
    for empty in [json!(42), json!(42.0)] {
        assert_eq!(
            must(",a\nx,y", json!({"field": {"empty": empty}})),
            json!([{"42": "x", "a": "y"}]),
            "{empty}"
        );
    }
    for (empty, name) in [
        (json!("e"), "e"),
        (json!(true), "true"),
        (json!(null), "null"),
        (json!(1.5), "1.5"),
        (json!([1, 2]), "1,2"),
    ] {
        assert_eq!(
            must(",a\nx,y", json!({"field": {"empty": empty}})),
            json!([{name: "x", "a": "y"}]),
            "{empty}"
        );
    }
}

// An OBJECT reaches the conversion from an option value too, and there the
// canonical does NOT throw. The option merge rebuilds a plain source
// object onto `Object.prototype` on the way into the bag, so `String`
// gives it "[object Object]" whatever the caller wrote, an object made
// with `Object.create(null)` included. A PARSED cell is allocated with a
// null prototype and inherits no `toString` at all, which is what throws.
//
// This port refused BOTH until now, which was a port defect and not an
// impossibility. What separates the two is PROVENANCE, and provenance is
// not on the value: an option object and a parsed object are both
// `Value::Object`. It is taken where the options are READ instead, and
// carried to the name site, which is what `from_option` is. Every
// expectation below is the canonical runtime's own output, measured on
// 2026-09-21.
//
// `field.empty` reaches this in the DEFAULT mode, because the value is
// dropped into a syntactically empty cell before any rule runs. It is the
// one route by which the object divergence is not bounded by strict mode.
#[test]
fn an_object_option_value_names_the_column_as_the_canonical_does() {
    for empty in [json!({"q": 1}), json!({}), json!([{"q": 1}]), json!([{}])] {
        assert_eq!(
            must(",a\nx,y", json!({"field": {"empty": empty}})),
            json!([{"[object Object]": "x", "a": "y"}]),
            "{empty}"
        );
    }
    // Non-strict, where a parsed object could also reach the name site,
    // and does not in this document.
    assert_eq!(
        must(",a\nq,r", json!({"strict": false, "field": {"empty": {}}})),
        json!([{"[object Object]": "q", "a": "r"}])
    );
}

// The bound on the fix above, asserted so it fails on over-reach as
// loudly as on regression. A PARSED object still refuses, and it refuses
// in the very parse where an option object names the column beside it: if
// provenance were read off the value again, the first case here would
// start naming a column the canonical throws on. The canonical throws
// `TypeError: Cannot convert object to primitive value` for it.
//
// The last two are the value route rather than the name route. An option
// object that never names a column is a cell value like any other, and
// the canonical returns it.
#[test]
fn a_parsed_object_is_refused_beside_an_option_object() {
    assert_eq!(
        code_of(
            ",{x:1}\nq,r",
            json!({"strict": false, "field": {"empty": {}}})
        ),
        "unexpected"
    );
    // And when the parsed object EQUALS the option object. Provenance is
    // the allocation, not the contents: an option `{x:1}` compares equal
    // to a parsed `{x:1}`, and only one of them may be named.
    assert_eq!(
        code_of(
            ",{x:1}\nq,r",
            json!({"strict": false, "field": {"empty": {"x": 1}}})
        ),
        "unexpected"
    );
    assert_eq!(
        must(",a\nx,y", json!({"object": false, "field": {"empty": {}}})),
        json!([["x", "y"]])
    );
    assert_eq!(
        must("h1,h2\nx,", json!({"field": {"empty": {}}})),
        json!([{"h1": "x", "h2": {}}])
    );
}

// `field.names` is the other source of a field list. The canonical takes
// any value and converts it where it builds the name, so `[1, 2]` names
// columns "1" and "2" and an object element names "[object Object]". This
// port types the option as `Option<Vec<String>>`, so a non-string element
// is refused when the OPTIONS are read, before any document is parsed.
// The Go port keeps the element and converts it at the name site, which
// is what the canonical does; widening this one is a breaking change to a
// public type, so `DIVERGENCE.md` carries it instead.
#[test]
fn field_names_are_strings_in_this_port_alone() {
    for names in [json!([1, 2]), json!([{"q": 1}]), json!([true, null])] {
        let mut parser = tabnas_jsonic::make();
        let outcome = parser.use_plugin(
            tabnas_csv::plugin(),
            Some(Value::from_json(
                &json!({"header": false, "field": {"names": names}}),
            )),
        );
        match outcome {
            Ok(_) => panic!("field.names {names} installed"),
            Err(error) => assert!(
                error.to_string().contains("expected a string"),
                "{names}: {error}"
            ),
        }
    }
    // A string list is the supported spelling, and names the same columns
    // the canonical names for `["1", "2"]`.
    assert_eq!(
        must(
            "x,y",
            json!({"header": false, "field": {"names": ["1", "2"]}})
        ),
        json!([{"1": "x", "2": "y"}])
    );
}

// The same spelling reaches a field body, through the text rules that
// concatenate a token value with the text around it (the TypeScript
// `'' + r.o0.val`). Checked against ts/src/csv.ts on the same inputs.
#[test]
fn a_number_in_field_text_is_spelled_the_way_javascript_spells_it() {
    for (src, field) in [
        ("a\n-0 x", "0 x"),
        ("a\n1e21 x", "1e+21 x"),
        ("a\n1e-7 z", "1e-7 z"),
    ] {
        assert_eq!(
            must(src, json!({"number": true})),
            json!([{ "a": field }]),
            "field text for {src:?}"
        );
    }
}

#[test]
fn value() {
    assert_eq!(must_default("a\ntrue"), json!([{"a":"true"}]));
    assert_eq!(must("a\ntrue", json!({"value": true})), json!([{"a":true}]));
    assert_eq!(
        must("a\nfalse", json!({"value": true})),
        json!([{"a":false}])
    );
    assert_eq!(must("a\nnull", json!({"value": true})), json!([{"a":null}]));
}

#[test]
fn separators() {
    assert_eq!(
        must(
            "a|b|c\nA|B|C\nAA|BB|CC",
            json!({"field": {"separation": "|"}})
        ),
        json!([{"a":"A","b":"B","c":"C"},{"a":"AA","b":"BB","c":"CC"}])
    );
    assert_eq!(
        must("a~~b~~c\nA~~B~~C", json!({"field": {"separation": "~~"}})),
        json!([{"a":"A","b":"B","c":"C"}])
    );
}

#[test]
fn record_separators() {
    assert_eq!(
        must(
            "a,b,c%A,B,C%AA,BB,CC",
            json!({"record": {"separators": "%"}})
        ),
        json!([{"a":"A","b":"B","c":"C"},{"a":"AA","b":"BB","c":"CC"}])
    );
}

#[test]
fn unstrict() {
    assert_eq!(
        must("a,b\n1,2", json!({"strict": false})),
        json!([{"a":1.0,"b":2.0}])
    );
    assert_eq!(
        must("a,b,c\ntrue,[1,2],{x:1}", json!({"strict": false})),
        json!([{"a":true,"b":[1.0,2.0],"c":{"x":1.0}}])
    );
    let src = "a,b,c\ntrue,[1,2],{x:{y:\"q\\\"w\"}}\n x , 'y\\'y', \"z\\\"z\"\n";
    assert_eq!(
        must(src, json!({"strict": false})),
        json!([
            {"a":true,"b":[1.0,2.0],"c":{"x":{"y":"q\"w"}}},
            {"a":"x","b":"y'y","c":"z\"z"},
        ])
    );
    // Trailing content after a complete embedded value is a syntax error.
    assert_eq!(code_of("a\n{x:1}y", json!({"strict": false})), "unexpected");
}

#[test]
fn empty_takes_any_value() {
    assert_eq!(
        must("a,b,c\n1,,3", json!({"field": {"empty": null}})),
        json!([{"a":"1","b":null,"c":"3"}])
    );
    assert_eq!(
        must("a,b\n1,", json!({"field": {"empty": false}})),
        json!([{"a":"1","b":false}])
    );
    assert_eq!(
        must("a,b\n1,", json!({"field": {"empty": 42}})),
        json!([{"a":"1","b":42.0}])
    );
    // And through the typed options.
    let typed = parser_with(CsvOptions {
        field: FieldOptions {
            empty: json!(false),
            ..Default::default()
        },
        ..Default::default()
    });
    assert_eq!(
        to_json(&typed.parse("a,b\n1,").expect("parses")),
        json!([{"a":"1","b":false}])
    );
}

#[test]
fn field_exact() {
    let exact = json!({"field": {"exact": true}});
    assert_eq!(code_of("a,b\n1,2,3", exact.clone()), "csv_extra_field");
    assert_eq!(code_of("a,b\n1", exact.clone()), "csv_missing_field");
    assert_eq!(must("a,b\n1,2", exact), json!([{"a":"1","b":"2"}]));
}

#[test]
fn field_exact_error_messages_interpolate_their_placeholders() {
    // The error and hint templates use {braces}; the engine's injector
    // fills them from the details the plugin supplies with the bad token.
    // Row counts from 1 and includes the header, so the second data row
    // reports as row 3, the line number an editor would show.
    let plain = |message: String| -> String {
        let mut out = String::new();
        let mut chars = message.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '\x1b' && chars.peek() == Some(&'[') {
                for code in chars.by_ref() {
                    if code == 'm' {
                        break;
                    }
                }
            } else {
                out.push(c);
            }
        }
        out
    };
    let message = |src: &str| -> String {
        match parse_json(src, json!({"field": {"exact": true}})) {
            Ok(value) => panic!("{src:?} parsed: {value}"),
            Err(error) => plain(error.to_string()),
        }
    };

    let extra = message("a,b\n1,2,3");
    assert!(extra.contains("unexpected extra field value: 3"), "{extra}");
    assert!(
        extra.contains("Row 2 has too many fields (the first of which is: 3)"),
        "{extra}"
    );
    assert!(extra.contains("Only 2"), "{extra}");

    let missing = message("a,b\n1");
    assert!(
        missing.contains("Row 2 has too few fields. 2 fields per row are expected."),
        "{missing}"
    );

    let later = message("a,b\n1,2\n3,4,5");
    assert!(later.contains("Row 3 has too many fields"), "{later}");

    for text in [&extra, &missing, &later] {
        for name in ["fsrc", "row", "len"] {
            assert!(
                !text.contains(&format!("{{{name}}}")),
                "no {{{name}}} left: {text}"
            );
            assert!(
                !text.contains(&format!("${name}")),
                "no ${name} left: {text}"
            );
        }
    }
}

// ---------------------------------------------------------------------------
// Streaming (TestStream / `stream`)
// ---------------------------------------------------------------------------

#[test]
fn stream() {
    let events = Arc::new(Mutex::new(Vec::new()));
    let sink = Arc::clone(&events);
    let parser = parser_with(CsvOptions {
        stream: Some(Stream::new(move |event| {
            sink.lock().expect("the event log").push(event);
        })),
        ..Default::default()
    });

    let result = parser.parse("a,b\n1,2\n3,4\n5,6").expect("parses");
    // Streamed records are not stored in the result.
    assert_eq!(to_json(&result), json!([]));

    let events = events.lock().expect("the event log");
    assert_eq!(events.first(), Some(&StreamEvent::Start));
    assert_eq!(events.last(), Some(&StreamEvent::End));
    let records: Vec<Json> = events
        .iter()
        .filter_map(|event| match event {
            StreamEvent::Record(record) => Some(to_json(record)),
            _ => None,
        })
        .collect();
    assert_eq!(
        records,
        vec![
            json!({"a":"1","b":"2"}),
            json!({"a":"3","b":"4"}),
            json!({"a":"5","b":"6"}),
        ]
    );
    assert_eq!(events.len(), 5);
}

// ---------------------------------------------------------------------------
// Record key order (TestRecordsKeepTheHeaderOrder / `records keep the header
// order`)
// ---------------------------------------------------------------------------

/// The keys of each record, in order. `json!` equality ignores key order,
/// as an `IndexMap` compares as a map, so the keys are read out.
fn record_keys(value: &Json) -> Vec<Vec<String>> {
    value
        .as_array()
        .expect("an array of records")
        .iter()
        .map(|record| {
            record
                .as_object()
                .expect("a record object")
                .keys()
                .cloned()
                .collect()
        })
        .collect()
}

/// A record lists its fields in HEADER order, then any unnamed extras in
/// column order. The header is not alphabetical, so header order and
/// sorted order differ. The Go and TypeScript suites hold the same cases;
/// the Go port once built a record as a `map[string]any` and lost the
/// order.
#[test]
fn records_keep_the_header_order() {
    assert_eq!(
        record_keys(&must_default("name,age,city\nAda,36,Paris\nLin,28,Oslo")),
        vec![vec!["name", "age", "city"], vec!["name", "age", "city"]]
    );
    assert_eq!(
        record_keys(&must_default("b,a\n1,2,3,4")),
        vec![vec!["b", "a", "field~2", "field~3"]]
    );

    // A repeated name keeps its FIRST place and takes the LAST value.
    let repeated = must_default("b,a,b\n1,2,3");
    assert_eq!(record_keys(&repeated), vec![vec!["b", "a"]]);
    assert_eq!(
        serde_json::to_string(&repeated).expect("serialises"),
        r#"[{"b":"3","a":"2"}]"#
    );

    // A streamed record is the same ordered record.
    let events = Arc::new(Mutex::new(Vec::new()));
    let sink = Arc::clone(&events);
    let parser = parser_with(CsvOptions {
        stream: Some(Stream::new(move |event| {
            sink.lock().expect("the event log").push(event);
        })),
        ..Default::default()
    });
    parser.parse("name,age,city\nAda,36,Paris").expect("parses");
    let streamed: Vec<Json> = events
        .lock()
        .expect("the event log")
        .iter()
        .filter_map(|event| match event {
            StreamEvent::Record(record) => Some(to_json(record)),
            _ => None,
        })
        .collect();
    assert_eq!(
        record_keys(&Json::Array(streamed)),
        vec![vec!["name", "age", "city"]]
    );
}

// A quote the RFC 4180 matcher cannot fire for leaves the jsonic string
// matcher to read the quotes, because every runtime keeps that matcher ON
// in strict mode: `string.quote: ""` reads `"x y"` as the string `x y`,
// and `string.quote: "\"\""` fails the document. Those two, and the
// plain `'x y'` and `` `x y` `` the jsonic matcher takes under the
// default quote, are the rest of the degenerate-quote table.
//
// They now live in `test/spec/double-quote.tsv`, where all three runtimes
// run them. They were a Rust-only test while the Go port switched the
// jsonic matcher off in strict mode, which answered them with the text as
// written; that is fixed, so the coverage moved to the shared fixture
// rather than staying in one runtime.

// ---------------------------------------------------------------------------
// Typed options
// ---------------------------------------------------------------------------

#[test]
fn typed_options_match_the_bag() {
    let typed = parser_with(CsvOptions {
        header: false,
        object: false,
        trim: Some(true),
        field: FieldOptions {
            separation: Some("|".to_string()),
            ..Default::default()
        },
        record: RecordOptions {
            separators: Some("%".to_string()),
            empty: false,
        },
        string: StringOptions {
            quote: "'".to_string(),
            csv: None,
        },
        ..Default::default()
    });
    let bag = json!({
        "header": false, "object": false, "trim": true,
        "field": {"separation": "|"}, "record": {"separators": "%"}, "string": {"quote": "'"},
    });
    let src = "a|'b|c' |d%e|f|g";
    assert_eq!(
        to_json(&typed.parse(src).expect("parses")),
        must(src, bag.clone())
    );
    assert_eq!(must(src, bag), json!([["a", "b|c", "d"], ["e", "f", "g"]]));
}

#[test]
fn field_names_through_typed_options() {
    let parser = parser_with(CsvOptions {
        header: false,
        field: FieldOptions {
            names: Some(vec!["x".to_string(), "y".to_string()]),
            exact: true,
            ..Default::default()
        },
        ..Default::default()
    });
    assert_eq!(
        to_json(&parser.parse("1,2\n3,4").expect("parses")),
        json!([{"x":"1","y":"2"},{"x":"3","y":"4"}])
    );
    assert_eq!(parser.parse("1,2,3").unwrap_err().code, "csv_extra_field");
}

// ---------------------------------------------------------------------------
// Prototype pollution (ts/test/prototype-pollution.test.ts)
// ---------------------------------------------------------------------------

#[test]
fn a_header_column_named_proto_is_an_ordinary_key() {
    // A Rust map has no prototype chain, so the hazard the TypeScript
    // test guards against cannot arise; what is pinned is that the column
    // survives as an ordinary key rather than being dropped.
    assert_eq!(
        must_default("__proto__,b\n1,2\n"),
        json!([{"__proto__":"1","b":"2"}])
    );
    assert_eq!(
        must_default("constructor,b\n1,2\n"),
        json!([{"constructor":"1","b":"2"}])
    );
}

// ---------------------------------------------------------------------------
// The shared default parser under threads
// ---------------------------------------------------------------------------

#[test]
fn parse_is_safe_to_share_across_threads() {
    let handles: Vec<_> = (0..8)
        .map(|index| {
            std::thread::spawn(move || {
                for row in 0..20 {
                    let src = format!("a,b\n{index},{row}");
                    let value = tabnas_csv::parse(&src).expect("parses");
                    assert_eq!(
                        to_json(&value),
                        json!([{"a": index.to_string(), "b": row.to_string()}])
                    );
                }
            })
        })
        .collect();
    for handle in handles {
        handle.join().expect("a thread panicked");
    }
}