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
|
#!/usr/bin/perl
use strict;
use warnings;
#use Test::More "no_plan";
use Test::More tests => 21;
BEGIN {
use_ok "Text::CSV_XS", ("csv");
plan skip_all => "Cannot load Text::CSV_XS" if $@;
require "./t/util.pl";
}
my $tfni = "_92test-i.csv"; END { -f $tfni and unlink $tfni } # CRNL
my $tfnn = "_92test-n.csv"; END { -f $tfnn and unlink $tfnn } # CRNL + NL
my $tfno = "_92test-o.csv"; END { -f $tfno and unlink $tfno } # out
my $data =
"foo,bar,baz,quux\r\n".
"1,2,3,25\r\n".
"2,a b,,14\r\n";
open my $fhi, ">", $tfni or die "$tfni: $!";
print $fhi $data;
close $fhi;
open my $fhn, ">", $tfnn or die "$tfnn: $!";
{ my $d = $data;
$d =~ s/5\r\n/5\n/;
print $fhn $d;
}
close $fhn;
ok (my $aoa = csv (in => $tfni), "Read default data");;
{ my ($I, $O, @W);
ok (my $co = Text::CSV_XS->new ({
eol => "\n",
auto_diag => 1,
callbacks => {
before_print => sub {
warn ++$O, "\n";
$_[1][3] =~ s/x$/y/ or $_[1][3] *= 4;
},
},
}), "Create external CSV object");
open my $fho, ">", $tfno or die "$tfno: $!\n";
{ local $SIG{__WARN__} = sub { push @W => @_ };
csv (
in => $tfni,
out => undef,
callbacks => {
after_parse => sub {
warn ++$I, "\n";
$co->print ($fho, $_[1]);
},
},
);
}
close $tfno;
chomp @W;
is ("@W", "1 1 2 2 3 3", "Old-fashioned streaming");
}
# Basic straight-forward streaming, no filters/modifiers
unlink $tfno if -e $tfno;
csv (in => $tfni, out => $tfno, quote_space => 0);
ok (-s $tfno, "FILE -> FILE");
is_deeply (csv (in => $tfno), $aoa, "Data is equal");
unlink $tfno if -e $tfno;
open my $fho, ">", $tfno;
csv (in => $tfni, out => $fho, quote_space => 0);
close $fho;
ok (-s $tfno, "FILE -> FH");
is_deeply (csv (in => $tfno), $aoa, "Data is equal");
unlink $tfno if -e $tfno;
open $fhi, "<", $tfni;
csv (in => $fhi, out => $tfno, quote_space => 0);
close $fhi;
ok (-s $tfno, "FH -> FILE");
is_deeply (csv (in => $tfno), $aoa, "Data is equal");
unlink $tfno if -e $tfno;
open $fhi, "<", $tfni;
open $fho, ">", $tfno;
csv (in => $fhi, out => $fho, quote_space => 0);
close $fho;
close $fhi;
ok (-s $tfno, "FH -> FH");
is_deeply (csv (in => $tfno), $aoa, "Data is equal");
unlink $tfno if -e $tfno;
my @W;
eval {
local $SIG{__WARN__} = sub { push @W => @_ };
csv (in => $tfnn, out => $tfno, quote_space => 0);
};
like ($W[0], qr{\b2016 - EOL\b}, "Inconsistent use of EOL");
ok (-s $tfno, "FH -> FILE (NL => CRNL)");
is_deeply (csv (in => $tfno), $aoa, "Data is equal");
is (do { local (@ARGV, $/) = ($tfno); <> }, $data, "Consistent CRNL");
unlink $tfno if -e $tfno;
csv (
in => $tfni,
out => $tfno,
quote_space => 0,
after_parse => sub { $_[1][1] .= "X" },
);
ok (-s $tfno, "With after_parse");
my @new = map { my @x = @$_; $x[1] .= "X"; \@x } @$aoa;
is_deeply (csv (in => $tfno), \@new, "Data is equal");
# Prove streaming behavior
my $io = "";
unlink $tfno if -e $tfno;
csv (
in => $tfni,
out => $tfno,
on_in => sub { $io .= "I" },
callbacks => { before_print => sub { $io .= "O" }},
);
ok (-s $tfno, "FILE -> FILE");
is_deeply (csv (in => $tfno), $aoa, "Data is equal");
like ($io, qr{^(?:IO)+\z}, "IOIOIO...");
|