Bug 31590: Remove Text::CSV::Unicode
[koha-ffzg.git] / xt / author / Text_CSV_Various.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
16
17 #This test demonstrates why Koha uses the CSV parser and configration
18 #it does.  Specifically, the test is for Unicode compliance in text
19 #parsing and data.  This test requires other modules that Koha doesn't
20 #actually use, in order to compare.  Therefore, running this test is not
21 #necessary to test your Koha installation.
22
23 use Modern::Perl;
24 use open OUT=>':encoding(UTF-8)', ':std';
25 use utf8;
26
27 use Test::More tests => 21;
28 use Text::CSV;
29 use Text::CSV_XS;
30
31 sub pretty_line {
32         my $max = 54;
33         (@_) or return "#" x $max . "\n";
34         my $phrase = "  " . shift() . "  ";
35         my $half = "#" x (($max - length($phrase))/2);
36         return $half . $phrase . $half . "\n";
37 }
38
39 my ($csv, $bin, %parsers);
40
41 foreach( qw( Text::CSV Text::CSV_XS )) {
42     ok($csv = $_->new(),            $_ . '->new()');
43     ok($bin = $_->new({binary=>1}), $_ . '->new({binary=>1})');
44     $csv and $parsers{$_} = $csv;
45     $bin and $parsers{$_ . " (binary)"} = $bin;
46 }
47
48 my $lines = [
49     {description=>"010D: LATIN SMALL LETTER C WITH CARON",     character=>'č', line=>'field1,second field,field3,do_we_have_a_č_problem?, f!fth field ,lastfield'},
50     {description=>"0117: LATIN SMALL LETTER E WITH DOT ABOVE", character=>'ė', line=>'field1,second field,field3,do_we_have_a_ė_problem?, f!fth field ,lastfield'},
51 ];
52
53 ok( scalar(keys %parsers)>0 && scalar(@$lines)>0,
54     sprintf "Testing %d lines with  %d parsers.",
55          scalar(@$lines), scalar(keys %parsers) );
56
57 foreach my $key (sort keys %parsers) {
58     my $parser = $parsers{$key};
59     print "Testing parser $key version " . ($parser->version||'?') . "\n";
60 }
61
62 my $i = 0;
63 foreach my $line (@$lines) {
64     print pretty_line("Line " . ++$i);
65     print pretty_line($line->{description} . ': ' . $line->{character});
66     foreach my $key (sort keys %parsers) {
67         my $parser = $parsers{$key};
68         my ($status, $count, @fields);
69         $status = $parser->parse( $line->{line} );
70         if( $status ) {
71             ok($status, "parse ($key)");
72             @fields = $parser->fields;
73             $count = scalar(@fields);
74             is( $count, 6, "Number of fields ($count of 6)");
75             my $j = 0;
76             foreach my $f (@fields) {
77                 $j++;
78                 print "\t field $j: $f\n";
79             }
80         } else {
81             ok(! $status, "parse ($key) fails as expected"); #FIXME We never hit this line
82         }
83     }
84 }