File: 03_file.t

package info (click to toggle)
libtext-csv-encoded-perl 0.25-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 288 kB
  • sloc: perl: 434; makefile: 2
file content (142 lines) | stat: -rw-r--r-- 2,349 bytes parent folder | download | duplicates (4)
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
use Encode;
use utf8;

my $csv  = Text::CSV::Encoded->new({
    encoding_in  => 'utf8',
    encoding_out => 'shiftjis',
});
my $file = sprintf('sample/test_%s.csv' , $csv->backend =~ /PP/ ? 'pp' : 'xs' );



open (my $fh, "sample/utf8.csv") or die $!;
open (my $fh2, ">$file") or die $!;


#
# column_names & getline_hr
#

$csv->column_names( $csv->getline($fh) );

while( my $hr = $csv->getline_hr( $fh ) ) {
    $csv->print( $fh2, [ $hr->{text} ] );
    $fh2->print("\n");
}

close($fh);
close($fh2);


my $checker = Text::CSV->new({ binary => 1});

open ($fh,  $file) or die $!;
open ($fh2,  "sample/sjis.csv") or die $!;

<$fh2>;

$csv->encoding_in( 'shiftjis' );


while( 1 ) {
    my $row = $csv->getline( $fh );
    $csv->eof and last;
    is( Encode::encode( 'shiftjis', $row->[0] ), $checker->getline( $fh2 )->[1] );
}

close($fh);
close($fh2);


# convert directly

# shiftjis

open ($fh,  $file) or die $!;
open ($fh2,  "sample/sjis.csv") or die $!;

<$fh2>;

$csv->encoding_in( 'shiftjis' );
$csv->encoding( 'shiftjis' );

while( 1 ) {
    my $row = $csv->getline( $fh );
    $csv->eof and last;
    is( $row->[0], $checker->getline( $fh2 )->[1] );
}

close($fh);
close($fh2);


# utf8

open ($fh,  $file) or die $!;
open ($fh2,  "sample/utf8.csv") or die $!;

<$fh2>;

$csv->encoding_in( 'shiftjis' );
$csv->encoding( 'utf8' );

while( 1 ) {
    my $row = $csv->getline( $fh );
    $csv->eof and last;
    my $string = $checker->getline( $fh2 )->[1];
    $string = encode_utf8( $string ) if ( $csv->automatic_UTF8 );
    is( $row->[0], $string );
}

close($fh);
close($fh2);


# unicode

open ($fh,  $file) or die $!;
open ($fh2,  "sample/utf8.csv") or die $!;

<$fh2>;

$csv->encoding_in( 'shiftjis' );
$csv->encoding( undef );

while( 1 ) {
    my $row = $csv->getline( $fh );
    $csv->eof and last;
    my $data = $checker->getline( $fh2 )->[1];
    is( $row->[0], utf8::is_utf8 ($data) ? $data : Encode::decode_utf8( $data ) );
}

close($fh);
close($fh2);


#
# bind_columns
#

my ( $id, $text );
$csv->bind_columns( \$id, \$text );

$csv->encoding_in( 'utf8' );
$csv->encoding( 'shiftjis' );

open ($fh,  $file) or die $!;
open ($fh2,  "sample/utf8.csv") or die $!;

<$fh2>;

while( my $col = $csv->getline( $fh2 ) ) {
    is( $text, $checker->getline( $fh )->[0] );
}

close($fh);
close($fh2);



unlink( $file ) or warn $!;

1;