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
|
# --
# CSV.t - CSV tests
# Copyright (C) 2001-2007 OTRS GmbH, http://otrs.org/
# --
# $Id: CSV.t,v 1.6.2.1 2007/10/19 06:14:31 tr Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --
use Kernel::System::CSV;
$Self->{CSVObject} = Kernel::System::CSV->new(%{$Self});
my $CSV = $Self->{CSVObject}->Array2CSV(
Head => ['RowA', 'RowB', ],
Data => [
[1,4],
[7,3],
[1,9],
[34,4],
],
);
$Self->True(
$CSV eq
'"RowA";"RowB";
"1";"4";
"7";"3";
"1";"9";
"34";"4";
',
'GenerateCSV()',
);
my $Array = $Self->{CSVObject}->CSV2Array(
String => '"field1";"field2";"field3";'."\n".'"2";"3";"4";'."\n",
Separator => ';',
Quote => '"',
);
$Self->True(
($Array->[0][0] eq 'field1' && $Array->[0][2] eq 'field3' &&
$Array->[1][1] eq '3' && $#{$Array} eq 1 && $#{$Array->[1]} eq 2),
'CSV2Array() - with quote "',
);
$Array = $Self->{CSVObject}->CSV2Array(
String => 'field1;field2;field3;'."\n".'2;3;4;'."\n",
Separator => ';',
Quote => '',
);
$Self->True(
($Array->[0][0] eq 'field1' && $Array->[0][2] eq 'field3' &&
$Array->[1][1] eq '3' && $#{$Array} eq 1 && $#{$Array->[1]} eq 2),
'CSV2Array() without quote "',
);
# Working with CSVString with \n
my $String = '"field1";"field2";"field3";'."\n".'"a'."\n" .'b";"FirstLine'."\n" .'SecondLine";"4";'."\n";
$Array = $Self->{CSVObject}->CSV2Array(
String => $String,
Separator => ';',
Quote => '"',
);
$Self->True(
($Array->[0][0] eq 'field1' && $Array->[0][2] eq 'field3' &&
$Array->[1][0] eq "a\nb" && $#{$Array} eq 1 && $#{$Array->[1]} eq 2),
'CSV2Array() - with included \n',
);
# -------------------------------------------------
# tests because of the douple "" problem bug# 2263
# -------------------------------------------------
my $TextWithNewLine = "Hallo guys,\nhere was a newline. And again.\n";
my @TableData = (
['<a href="/sirios-cvs-utf8/index.pl?Action=AgentStats&Subaction=Overview" class="navitem">bersicht</a>' , '"'],
['4""4', 'asdf"SDF'],
['"a"', "xxx"],
[34,$TextWithNewLine],
);
$CSV = $Self->{CSVObject}->Array2CSV(
Head => ['RowA', 'RowB', ],
Data => \@TableData,
);
$Self->True(
$CSV eq
'"RowA";"RowB";
"<a href=""/sirios-cvs-utf8/index.pl?Action=AgentStats&Subaction=Overview"" class=""navitem"">bersicht</a>";"""";
"4""""4";"asdf""SDF";
"""a""";"xxx";
"34";"' . $TextWithNewLine . '";
',
'GenerateCSV() with ""',
);
my $ArrayRef = $Self->{CSVObject}->CSV2Array(
String => $CSV,
Separator => ';',
Quote => '"',
);
shift @{$ArrayRef};
for my $Row (0..$#TableData) {
for my $Column (0..$#{$TableData[0]}) {
print $TableData[$Row][$Column] . " <-> " . $ArrayRef->[$Row][$Column] . "\n";
$Self->Is(
$TableData[$Row][$Column],
$ArrayRef->[$Row][$Column],
'CSV2Array() with " in content',
);
}
}
1;
|