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
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 119;
BEGIN {
use_ok "Text::CSV_XS", ();
plan skip_all => "Cannot load Text::CSV_XS" if $@;
}
my $tfn = "_66test.csv"; END { -f $tfn and unlink $tfn; }
ok (my $csv = Text::CSV_XS->new, "new");
is ($csv->formula, "none", "default");
is ($csv->formula (1), "die", "die");
is ($csv->formula ("die"), "die", "die");
is ($csv->formula (2), "croak", "croak");
is ($csv->formula ("croak"), "croak", "croak");
is ($csv->formula (3), "diag", "diag");
is ($csv->formula ("diag"), "diag", "diag");
is ($csv->formula (4), "empty", "empty");
is ($csv->formula ("empty"), "empty", "empty");
is ($csv->formula (""), "empty", "explicit empty");
is ($csv->formula (5), "undef", "undef");
is ($csv->formula ("undef"), "undef", "undef");
is ($csv->formula (undef), "undef", "explicit undef");
is ($csv->formula (sub { }), "cb", "callback");
is ($csv->formula (0), "none", "none");
is ($csv->formula ("none"), "none", "none");
is ($csv->formula_handling, "none", "default");
is ($csv->formula_handling ("DIE"), "die", "die");
is ($csv->formula_handling ("CROAK"), "croak", "croak");
is ($csv->formula_handling ("DIAG"), "diag", "diag");
is ($csv->formula_handling ("EMPTY"), "empty", "empty");
is ($csv->formula_handling ("UNDEF"), "undef", "undef");
is ($csv->formula_handling ("NONE"), "none", "none");
foreach my $f (-1, 9, "xxx", "DIAX", [], {}) {
eval { $csv->formula ($f); };
like ($@, qr/\bformula-handling '\Q$f\E' is not supported/, "$f in invalid");
}
my %f = qw(
0 none none none
1 die die die
2 croak croak croak
3 diag diag diag
4 empty empty empty
5 undef undef undef
);
foreach my $f (sort keys %f) {
ok (my $p = Text::CSV_XS->new ({ formula => $f }), "new with $f");
is ($p->formula, $f{$f}, "Set to $f{$f}");
}
eval { Text::CSV_XS->new ({ formula => "xxx" }); };
like ($@, qr/\bformula-handling 'xxx' is not supported/, "xxx is invalid");
# TODO : $csv->formula (sub { 42; });
# Parser
my @data = split m/\n/ => <<"EOC";
a,b,c
1,2,3
=1+2,3,4
1,=2+3,4
1,2,=3+4
EOC
sub parse {
my $f = shift;
my @d;
ok (my $csv = Text::CSV_XS->new ({ formula => $f }), "new $f");
#diag ("Formula: ". $csv->formula);
for (@data) {
$csv->parse ($_);
push @d, [ $csv->fields ];
}
\@d;
} # parse
is_deeply (parse (0), [
[ "a", "b", "c", ],
[ "1", "2", "3", ],
[ "=1+2", "3", "4", ],
[ "1", "=2+3", "4", ],
[ "1", "2", "=3+4", ],
], "Default");
my $r = eval { parse (1) };
is ($r, undef, "Die on formulas");
is ($@, "Formulas are forbidden\n", "Message");
$@ = undef;
$r = eval { parse (2) };
is ($r, undef, "Croak on formulas");
is ($@, "Formulas are forbidden\n", "Message");
$@ = undef;
my @m;
local $SIG{__WARN__} = sub { push @m, @_ };
is_deeply (parse (3), [
[ "a", "b", "c", ],
[ "1", "2", "3", ],
[ "=1+2", "3", "4", ],
[ "1", "=2+3", "4", ],
[ "1", "2", "=3+4", ],
], "Default");
is ($@, undef, "Legal with warnings");
is_deeply (\@m, [
"Field 1 in record 3 contains formula '=1+2'\n",
"Field 2 in record 4 contains formula '=2+3'\n",
"Field 3 in record 5 contains formula '=3+4'\n",
], "Warnings");
@m = ();
is_deeply (parse (4), [
[ "a", "b", "c", ],
[ "1", "2", "3", ],
[ "", "3", "4", ],
[ "1", "", "4", ],
[ "1", "2", "", ],
], "Empty");
is_deeply (parse (5), [
[ "a", "b", "c", ],
[ "1", "2", "3", ],
[ undef, "3", "4", ],
[ "1", undef, "4", ],
[ "1", "2", undef, ],
], "Undef");
for ([ "Callback return", sub { 42; } ],
[ "Callback assign", sub { $_ = 42; } ],
[ "Callback subst", sub { s/.*/42/; $_ } ], # s///r requires 5.13.2
) {
my ($msg, $cb) = @$_;
is_deeply (parse ($cb), [
[ "a", "b", "c", ],
[ "1", "2", "3", ],
[ "42", "3", "4", ],
[ "1", "42", "4", ],
[ "1", "2", "42", ],
], $msg);
}
is_deeply (parse (sub { eval { s{^=([-+*/0-9()]+)$}{$1}ee }; $_ }), [
[ "a", "b", "c", ],
[ "1", "2", "3", ],
[ "3", "3", "4", ],
[ "1", "5", "4", ],
[ "1", "2", "7", ],
], "Callback calculations");
{ @m = ();
ok (my $csv = Text::CSV_XS->new ({ formula => 3 }), "new 3 hr");
ok ($csv->column_names ("code", "value", "desc"), "Set column names");
ok ($csv->parse ("1,=2+3,4"), "Parse");
is_deeply (\@m,
[ qq{Field 2 (column: 'value') contains formula '=2+3'\n} ],
"Warning for HR");
}
# Writer
sub writer {
my $f = shift;
ok (my $csv = Text::CSV_XS->new ({
formula_handling => $f, quote_empty => 1 }), "new $f");
ok ($csv->combine ("1", "=2+3", "4"), "combine $f");
$csv->string;
} # writer
@m = ();
is ( writer (0), q{1,=2+3,4}, "Out 0");
is (eval { writer (1) }, undef, "Out 1");
is (eval { writer (2) }, undef, "Out 2");
is ( writer (3), q{1,=2+3,4}, "Out 3");
is ( writer (4), q{1,"",4}, "Out 4");
is ( writer (5), q{1,,4}, "Out 5");
is_deeply (\@m, [ "Field 1 contains formula '=2+3'\n" ], "Warning 3");
@m = ();
is ( writer ("none"), q{1,=2+3,4}, "Out none");
is (eval { writer ("die") }, undef, "Out die");
is (eval { writer ("croak") }, undef, "Out croak");
is ( writer ("diag"), q{1,=2+3,4}, "Out diag");
is ( writer ("empty"), q{1,"",4}, "Out empty");
is ( writer ("undef"), q{1,,4}, "Out undef");
is_deeply (\@m, [ "Field 1 contains formula '=2+3'\n" ], "Warning diag");
open my $fh, ">", $tfn;
printf $fh <<"EOC";
1,2,3
=1+2,3,4
1,=12-6,5
1,2,=4+(9-1)/2
EOC
close $fh;
is_deeply (Text::CSV_XS::csv (in => $tfn,
formula => sub { eval { s{^=([-+*/0-9()]+)$}{$1}ee }; $_ }),
[[1,2,3],[3,3,4],[1,6,5],[1,2,8]], "Formula calc from csv function");
|