parsm 0.8.1

Multi-format data processor that understands structured text better than sed or awk. Supports JSON, CSV, YAML, TOML, logfmt, and plain text with powerful filtering and templating.
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
use std::fs::File;
use std::io::Write;
use std::process::Command;
use tempfile::NamedTempFile;

/// Helper function to create a Command with proper environment setup
fn parsm_command() -> Command {
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_parsm"));
    cmd.env("RUST_LOG", "parsm=error");
    cmd
}

#[test]
fn test_csv_field_selection_by_header() {
    // Test field selection by header name on CSV with headers
    let input = "name,age,occupation\nTom,45,engineer\nAlice,30,doctor";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("name")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should extract names from the CSV
    assert!(stdout.contains("Tom"));
    assert!(stdout.contains("Alice"));
    assert!(!stdout.contains("45")); // Should not contain ages
}

#[test]
fn test_csv_field_selection_by_index() {
    // Test field selection by index on CSV without headers
    let input = "Tom,45,engineer\nAlice,30,doctor";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_0")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should extract first field (names)
    assert!(stdout.contains("Tom"));
    assert!(stdout.contains("Alice"));
    assert!(!stdout.contains("45")); // Should not contain ages
}

#[test]
fn test_csv_header_detection() {
    // Test that headers are correctly detected and skipped in output
    let input = "name,age,occupation\nTom,45,engineer\nAlice,30,doctor";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("name")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should not contain the header word "name" in output
    assert!(!stdout.contains("name"));
    // Should contain actual data
    assert!(stdout.contains("Tom"));
    assert!(stdout.contains("Alice"));
}

#[test]
fn test_csv_no_header_detection() {
    // Test CSV without headers - should not skip first row
    let input = "Tom,45,engineer\nAlice,30,doctor";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_0")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain both names (no header to skip)
    assert!(stdout.contains("Tom"));
    assert!(stdout.contains("Alice"));

    // Count lines to ensure both rows are processed
    let line_count = stdout
        .lines()
        .filter(|line| !line.trim().is_empty())
        .count();
    assert_eq!(line_count, 2);
}

#[test]
fn test_csv_template_with_headers() {
    // Test template rendering with header-based field access
    let input = "name,age,occupation\nTom,45,engineer\nAlice,30,doctor";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("[$name is $age years old]")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should render template with data
    assert!(stdout.contains("Tom is 45 years old"));
    assert!(stdout.contains("Alice is 30 years old"));
}

#[test]
fn test_csv_filter_with_headers() {
    // Test filtering with header-based field access
    let input = "name,age,occupation\nTom,45,engineer\nAlice,30,doctor\nBob,35,engineer";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("occupation == \"engineer\" {$name}")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should only contain engineers
    assert!(stdout.contains("Tom"));
    assert!(stdout.contains("Bob"));
    assert!(!stdout.contains("Alice")); // Doctor should be filtered out
}

#[test]
fn test_csv_multiple_field_selection() {
    // Test selecting different fields by index
    let input = "Alice,30,Engineer\nBob,25,Designer";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_2") // Select third field (occupation)
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(stdout.contains("Engineer"));
    assert!(stdout.contains("Designer"));
}

#[test]
fn test_csv_numeric_filtering() {
    // Test filtering CSV data with numeric comparisons
    let input = "Alice,30,Engineer\nBob,25,Designer\nCharlie,35,Manager\nDana,22,Intern";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_1 > 27") // Filter by age > 27
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should match Alice (30) and Charlie (35)
    assert!(stdout.contains("Alice"));
    assert!(stdout.contains("Charlie"));
    assert!(!stdout.contains("Bob"));
    assert!(!stdout.contains("Dana"));
}

#[test]
fn test_csv_string_filtering() {
    // Test filtering CSV data with string operations
    let input = "Alice,30,Engineer\nBob,25,Designer\nCharlie,35,Manager\nEva,28,Engineer";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_2 == \"Engineer\"") // Filter by occupation
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should match Alice and Eva (both Engineers)
    assert!(stdout.contains("Alice"));
    assert!(stdout.contains("Eva"));
    assert!(!stdout.contains("Bob"));
    assert!(!stdout.contains("Charlie"));
}

#[test]
fn test_csv_template_replacement_indexed() {
    // Test template replacement with indexed CSV fields
    let input = "Alice,30,Engineer\nBob,25,Designer";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_1 > 20 {Name: $field_0, Age: $field_1, Job: $field_2}")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(stdout.contains("Name: Alice, Age: 30, Job: Engineer"));
    assert!(stdout.contains("Name: Bob, Age: 25, Job: Designer"));
}

#[test]
fn test_csv_original_input_template() {
    // Test ${0} (original input) in templates
    let input = "Alice,30,Engineer";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_1 > 25 {Person: ${field_0} | Original: ${0}}")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert_eq!(stdout.trim(), "Person: Alice | Original: Alice,30,Engineer");
}

#[test]
fn test_csv_complex_boolean_logic() {
    // Test complex boolean logic with CSV data
    let input = "Alice,30,Engineer,true\nBob,22,Designer,false\nCharlie,35,Manager,false\nDana,24,Admin,true";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_1 > 25 && field_3? {Found: $field_0 ($field_2)}")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should only match Alice (age > 25 && active == true)
    assert!(stdout.contains("Found: Alice"));
    assert!(!stdout.contains("Bob"));
    assert!(!stdout.contains("Charlie"));
    assert!(!stdout.contains("Dana"));
}

#[test]
fn test_csv_nonexistent_field() {
    // Test accessing non-existent fields
    let input = "Alice,30,Engineer";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_5") // Field that doesn't exist
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Non-existent field should return empty or be handled gracefully
    assert_eq!(stdout.trim(), "");
}

#[test]
fn test_csv_quoted_fields() {
    // Test CSV with quoted fields containing commas
    let input = r#""Smith, John",35,"Senior Engineer, Tech Lead""#;

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_1") // Select age field
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert_eq!(stdout.trim(), "35");
}

#[test]
fn test_csv_empty_fields() {
    // Test CSV with empty fields
    let input = "Alice,,Engineer\n,25,Designer\nCharlie,35,\n";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_0") // Select first field
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    let lines: Vec<&str> = stdout.trim().split('\n').collect();
    assert_eq!(lines.len(), 3);
    assert_eq!(lines[0], "Alice");
    assert_eq!(lines[1], ""); // Empty field
    assert_eq!(lines[2], "Charlie");
}

#[test]
fn test_csv_numeric_comparisons() {
    // Test various numeric comparisons
    let test_cases = vec![
        ("Alice,30,5000", "field_1 > 25", true),
        ("Bob,22,3000", "field_1 < 25", true),
        ("Charlie,30,5000", "field_2 >= 5000", true),
        ("Dana,28,4500", "field_2 <= 4000", false),
        ("Eve,35,6000", "field_1 == 35", true),
        ("Frank,25,3500", "field_1 != 30", true),
    ];

    for (input, filter, should_match) in test_cases {
        let mut file = NamedTempFile::new().expect("create temp file");
        write!(file, "{input}").expect("write temp file");

        let output = parsm_command()
            .arg(format!("{filter} {{Match: $field_0}}"))
            .stdin(File::open(file.path()).unwrap())
            .output()
            .expect("run parsm");

        assert!(output.status.success(), "parsm failed for input: {input}");
        let stdout = String::from_utf8_lossy(&output.stdout);

        if should_match {
            assert!(
                stdout.contains("Match:"),
                "Expected match for: {input} with filter: {filter}"
            );
        } else {
            assert_eq!(
                stdout.trim(),
                "",
                "Expected no match for: {input} with filter: {filter}"
            );
        }
    }
}

#[test]
fn test_csv_different_row_lengths() {
    // Test CSV rows with different number of fields
    let input = "Alice,30\nBob,25,Designer,Manager\nCharlie,35,Engineer";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_1") // Select second field (age)
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    let lines: Vec<&str> = stdout.trim().split('\n').collect();
    assert_eq!(lines.len(), 3);
    assert_eq!(lines[0], "30");
    assert_eq!(lines[1], "25");
    assert_eq!(lines[2], "35");
}

#[test]
fn test_csv_single_column() {
    // Test CSV with only one column
    let input = "Alice\nBob\nCharlie";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_0") // Select only field
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    // This might not be detected as CSV, but if it is, should work
    if output.status.success() {
        let stdout = String::from_utf8_lossy(&output.stdout);
        if !stdout.trim().is_empty() {
            let lines: Vec<&str> = stdout.trim().split('\n').collect();
            assert!(lines.len() <= 3); // May or may not be detected as CSV
        }
    }
}

#[test]
fn test_csv_malformed_input() {
    // Test with malformed CSV (unbalanced quotes)
    let input = r#"Alice,30,"Engineer
Bob,25,Designer"#;

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_0 == \"Bob\"")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    // Should handle malformed CSV gracefully without crashing
    assert!(
        output.status.success(),
        "parsm should handle malformed CSV gracefully"
    );
}

#[test]
fn test_csv_braced_field_syntax() {
    // Test ${field_N} syntax in templates
    let input = "Alice,30,Engineer";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("field_1 > 25 {Name: ${field_0}, Age: ${field_1}, Job: ${field_2}}")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert_eq!(stdout.trim(), "Name: Alice, Age: 30, Job: Engineer");
}

#[test]
fn test_csv_headers_all_data_rows() {
    // Test that with headers, all data rows (not header) are processed
    let input = "Name,Age,Job\nAlice,30,Engineer\nBob,25,Designer\nCharlie,35,Manager";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("name") // Select by header name
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should not contain the header word "Name"
    assert!(!stdout.contains("Name"));
    // Should contain all data rows
    assert!(stdout.contains("Alice"));
    assert!(stdout.contains("Bob"));
    assert!(stdout.contains("Charlie"));

    let line_count = stdout
        .lines()
        .filter(|line| !line.trim().is_empty())
        .count();
    assert_eq!(line_count, 3); // Only data rows, not header
}

/// Test CSV forced format parsing with --csv flag
#[test]
fn test_csv_forced_format() {
    let test_cases = vec![
        // Comma-separated data that might be detected as other formats, but force CSV parsing
        (
            "name,age,occupation\nAlice,30,Engineer",
            r#""name""#,
            "Alice",
        ),
        ("name,age,occupation\nAlice,30,Engineer", r#""age""#, "30"),
        ("Alice,30,Engineer", r#""field_0""#, "Alice"),
        ("Alice,30,Engineer", r#""field_1""#, "30"),
        ("Alice,30,Engineer", r#""field_2""#, "Engineer"),
        // Comma-separated with quotes, ensure it's parsed as CSV
        ("\"Smith, John\",30,Engineer", r#""field_0""#, "Smith, John"),
        ("\"Smith, John\",30,Engineer", r#""field_1""#, "30"),
        // Test template with forced CSV
        (
            "Alice,30,Engineer",
            r#"{Name: ${1}, Age: ${2}}"#,
            "Name: Alice, Age: 30",
        ),
        (
            "Alice,30,Engineer",
            r#"{${field_0} is ${field_1} years old}"#,
            "Alice is 30 years old",
        ),
    ];

    for (input, expression, expected) in test_cases {
        let mut file = NamedTempFile::new().expect("create temp file");
        write!(file, "{input}").expect("write temp file");

        let output = parsm_command()
            .arg("--csv")
            .arg(expression)
            .stdin(File::open(file.path()).unwrap())
            .output()
            .expect("run parsm");

        assert!(
            output.status.success(),
            "CSV forced format failed for input '{}' with expression '{}': {:?}",
            input,
            expression,
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        let result = stdout.trim();
        assert_eq!(
            result, expected,
            "Failed for CSV forced input '{input}' with expression '{expression}'",
        );
    }
}

/// Test CSV forced format filtering with --csv flag
#[test]
fn test_csv_forced_format_filtering() {
    let test_cases = vec![
        ("Alice,30,Engineer", "field_1 > \"25\"", true),
        ("Bob,20,Student", "field_1 > \"25\"", false),
        ("Alice,30,Engineer", r#"field_0 == "Alice""#, true),
        ("Bob,20,Student", r#"field_0 == "Alice""#, false),
        ("Alice,30,Engineer", r#"field_2 *= "Eng""#, true),
        ("Bob,20,Student", r#"field_2 *= "Eng""#, false),
    ];

    for (input, filter, should_match) in test_cases {
        let mut file = NamedTempFile::new().expect("create temp file");
        write!(file, "{input}").expect("write temp file");

        let output = parsm_command()
            .arg("--csv")
            .arg(filter)
            .arg(r#"{match}"#)
            .stdin(File::open(file.path()).unwrap())
            .output()
            .expect("run parsm");

        assert!(
            output.status.success(),
            "CSV forced format filtering failed for input '{}' with filter '{}': {:?}",
            input,
            filter,
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        let result = stdout.trim();

        if should_match {
            assert_eq!(
                result, "match",
                "Expected match for CSV forced filter '{filter}' with input '{input}'",
            );
        } else {
            assert_eq!(
                result, "",
                "Expected empty output for CSV forced filter '{filter}' with input '{input}'",
            );
        }
    }
}

/// Test multiline CSV output with consistent handling
#[test]
fn test_csv_multiline_output() {
    // Test with a multiline CSV file that has headers
    let input = "name,age,occupation\nAlice,30,Engineer\nBob,25,Designer\nCharlie,35,Manager";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    // Test default output (should preserve original lines)
    let output = parsm_command()
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Default output should be the original lines
    let lines: Vec<&str> = stdout.trim().split('\n').collect();
    assert_eq!(lines.len(), 4); // Header + 3 data rows
    assert_eq!(lines[0], "name,age,occupation");
    assert_eq!(lines[1], "Alice,30,Engineer");
    assert_eq!(lines[2], "Bob,25,Designer");
    assert_eq!(lines[3], "Charlie,35,Manager");

    // Test filtering functionality on multiline CSV
    let filter_output = parsm_command()
        .arg("age > 27") // Filter rows where age > 27
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(
        filter_output.status.success(),
        "parsm filter failed: {filter_output:?}"
    );
    let filter_stdout = String::from_utf8_lossy(&filter_output.stdout);

    // Should only contain rows with age > 27 (Alice and Charlie)
    assert!(filter_stdout.contains("Alice,30,Engineer"));
    assert!(filter_stdout.contains("Charlie,35,Manager"));
    assert!(!filter_stdout.contains("Bob,25,Designer"));

    // Test header-based filtering with --csv flag
    // Note: Currently, with --csv flag, we need to use field_N syntax instead of header names
    let header_filter_output = parsm_command()
        .arg("--csv")
        .arg("field_1 > 27") // Using field index instead of header name
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(
        header_filter_output.status.success(),
        "parsm header filter failed: {header_filter_output:?}"
    );
    let header_filter_stdout = String::from_utf8_lossy(&header_filter_output.stdout);

    // Should only contain rows with age > 27 (Alice and Charlie)
    assert!(header_filter_stdout.contains("Alice,30,Engineer"));
    assert!(header_filter_stdout.contains("Charlie,35,Manager"));
    assert!(!header_filter_stdout.contains("Bob,25,Designer"));
}

/// Test multiline CSV output with consistent handling
#[test]
fn test_csv_multiline_output_field() {
    // Test with a multiline CSV file that has headers
    let input = "name,age,occupation\nAlice,30,Engineer\nBob,25,Designer\nCharlie,35,Manager";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    // Test default output (should preserve original lines)
    let output = parsm_command()
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Default output should be the original lines
    let lines: Vec<&str> = stdout.trim().split('\n').collect();
    assert_eq!(lines.len(), 4); // Header + 3 data rows
    assert_eq!(lines[0], "name,age,occupation");
    assert_eq!(lines[1], "Alice,30,Engineer");
    assert_eq!(lines[2], "Bob,25,Designer");
    assert_eq!(lines[3], "Charlie,35,Manager");

    // Test filtering functionality on multiline CSV
    let filter_output = parsm_command()
        .arg("field_1 > 27") // Filter rows where age > 27
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(
        filter_output.status.success(),
        "parsm filter failed: {filter_output:?}"
    );
    let filter_stdout = String::from_utf8_lossy(&filter_output.stdout);

    // Should only contain rows with age > 27 (Alice and Charlie)
    assert!(filter_stdout.contains("Alice,30,Engineer"));
    assert!(filter_stdout.contains("Charlie,35,Manager"));
    assert!(!filter_stdout.contains("Bob,25,Designer"));

    // Test header-based filtering with --csv flag
    // Note: Currently, with --csv flag, we need to use field_N syntax instead of header names
    let header_filter_output = parsm_command()
        .arg("--csv")
        .arg("field_1 > 27") // Using field index instead of header name
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(
        header_filter_output.status.success(),
        "parsm header filter failed: {header_filter_output:?}"
    );
    let header_filter_stdout = String::from_utf8_lossy(&header_filter_output.stdout);

    // Should only contain rows with age > 27 (Alice and Charlie)
    assert!(header_filter_stdout.contains("Alice,30,Engineer"));
    assert!(header_filter_stdout.contains("Charlie,35,Manager"));
    assert!(!header_filter_stdout.contains("Bob,25,Designer"));
}

/// Regression test for multiline CSV output functionality
///
/// This test verifies that the multiline CSV output functionality works correctly,
/// focusing on the default behavior of outputting the original input and ensuring
/// that filtering works properly.
#[test]
fn test_csv_output_regression() {
    // Create a simple CSV file without complex quoting
    let input = "name,age,active\nAlice,30,true\nBob,25,false\nCharlie,35,true";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    // Test default output (should preserve original input)
    let output = parsm_command()
        .arg("--csv")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(
        output.status.success(),
        "parsm default output failed: {output:?}"
    );
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Default output should match original input
    assert_eq!(stdout.trim(), input);

    // Test filtering by field index
    let filter_output = parsm_command()
        .arg("--csv")
        .arg("field_2 == \"true\"")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(
        filter_output.status.success(),
        "parsm filter failed: {filter_output:?}"
    );
    let filter_stdout = String::from_utf8_lossy(&filter_output.stdout);

    // Should only include rows with active=true
    assert!(filter_stdout.contains("Alice,30,true"));
    assert!(filter_stdout.contains("Charlie,35,true"));
    assert!(!filter_stdout.contains("Bob,25,false"));

    // Test field selection
    let field_output = parsm_command()
        .arg("--csv")
        .arg("field_0")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(
        field_output.status.success(),
        "parsm field selection failed: {field_output:?}"
    );
    let field_stdout = String::from_utf8_lossy(&field_output.stdout);

    // Should extract just the names
    assert_eq!(
        field_stdout.trim().lines().collect::<Vec<&str>>().join(","),
        "Alice,Bob,Charlie"
    );
}