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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
#!/usr/bin/perl
use strict;
use warnings;
# OO version of 200_csv.t
my $tests = 275;
use Test::More;
require Test::NoWarnings;
use Spreadsheet::Read;
my $parser = Spreadsheet::Read::parses ("csv") or
plan skip_all => "No CSV parser found";
{ my $ref;
is (Spreadsheet::Read->new ("no_such_file.csv"), undef, "Open nonexisting file");
ok ($@, "No sheets read: $@"); # No such file or directory
is (Spreadsheet::Read->new ("files/empty.csv"), undef, "Open empty file");
is ($@, "files/empty.csv is empty", "No sheets read");
}
my $csv;
ok ($csv = Spreadsheet::Read->new ("files/test.csv"), "Read/Parse csv file");
ok ($csv->parses ("CSV"), "Parses CSV");
is ($csv->cr2cell (1, 1), "A1", "method cr2cell");
ok (1, "Base values");
is (ref $csv, "Spreadsheet::Read", "Return type");
is ($csv->[0]{type}, "csv", "Spreadsheet type");
is ($csv->[0]{sheets}, 1, "Sheet count");
is (ref $csv->[0]{sheet}, "HASH", "Sheet list");
is (scalar keys %{$csv->[0]{sheet}}, 1, "Sheet list count");
cmp_ok ($csv->[0]{version}, ">=", 0.01, "Parser version");
is_deeply ([$csv->cellrow (1, 1)], ["A1","B1","","D1",(undef) x 15], "row 1");
is ($csv->cellrow (1, 255), undef, "No such row 255");
is ($csv->cellrow (1, -55), undef, "No such row -55");
is ($csv->cellrow (1, 0), undef, "No such row 0");
is ($csv->cellrow (0, 1), undef, "No such sheet 0");
is ($csv->cellrow (-2, 1), undef, "Wrong sheet -2");
is ($csv->cellrow (-20, 1), undef, "No such sheet -20");
is_deeply ([$csv->row (1, 1)], ["A1","B1","","D1",(undef) x 15], "row 1");
is ($csv->row (1, 255), undef, "No such row 255");
is ($csv->row (1, -55), undef, "No such row -55");
is ($csv->row (1, 0), undef, "No such row 0");
is ($csv->row (0, 1), undef, "No such sheet 0");
is ($csv->row (-2, 1), undef, "Wrong sheet -2");
is ($csv->row (-20, 1), undef, "No such sheet -20");
is ($csv->sheet ( 0), undef, "Sheet 0");
is ($csv->sheet (255), undef, "Sheet 255");
is ($csv->sheet (-55), undef, "Sheet -55");
is ($csv->sheet ("="), undef, "Sheet '='");
is (Spreadsheet::Read::sheet (undef, 1), undef, "Don't be silly");
is (Spreadsheet::Read::sheet ($csv, -9), undef, "Don't be silly");
ok (my $sheet = $csv->sheet (1), "Sheet 1");
is ($sheet->maxrow, 5, "Last row");
is ($sheet->maxcol, 19, "Last column");
is ($sheet->cell ($sheet->maxcol, $sheet->maxrow),
"LASTFIELD", "Last field");
is_deeply ([$sheet->cellrow (1)], ["A1","B1","","D1",(undef) x 15], "row 1");
is ($sheet->cellrow (255), undef, "No such row 255");
is ($sheet->cellrow (-55), undef, "No such row -55");
is ($sheet->cellrow ( 0), undef, "No such row 0");
is_deeply ([$sheet->row (1)], ["A1","B1","","D1",(undef) x 15], "row 1");
is ($sheet->row (255), undef, "No such row 255");
is ($sheet->row (-55), undef, "No such row -55");
is ($sheet->row ( 0), undef, "No such row 0");
is_deeply ([$sheet->cellcolumn ( 1)], ["A1","A2","A3","A4",""], "col 1");
is_deeply ([$sheet->cellcolumn ("A")], ["A1","A2","A3","A4",""], "col 1");
is ($sheet->cellcolumn (255), undef, "No such col 255");
is ($sheet->cellcolumn (-55), undef, "No such col -55");
is ($sheet->cellcolumn ( 0), undef, "No such col 0");
is_deeply ([$sheet->column (1)], ["A1","A2","A3","A4",""], "col 1");
is_deeply ([$sheet->column ("A")], ["A1","A2","A3","A4",""], "col 1");
is ($sheet->column (255), undef, "No such col 255");
is ($sheet->column (-55), undef, "No such col -55");
is ($sheet->column ( 0), undef, "No such col 0");
ok (my @rows = $sheet->rows, "All rows");
is ($rows[0][0], "A1", "A1");
ok (1, "Defined fields");
foreach my $cell (qw( A1 A2 A3 A4 B1 B2 B4 C3 C4 D1 D3 )) {
my ($c, $r) = $csv->cell2cr ($cell);
is ($sheet->{cell}[$c][$r], $cell, "Unformatted cell $cell direct");
is ($sheet->{$cell}, $cell, "Formatted cell $cell direct");
is ($sheet->cell ($c, $r), $cell, "Unformatted cell $cell method");
is ($sheet->cell ($cell), $cell, "Formatted cell $cell method");
}
ok (1, "Undefined fields");
foreach my $cell (qw( B3 C1 C2 D2 D4 )) {
my ($c, $r) = $csv->cell2cr ($cell);
is ($sheet->{cell}[$c][$r], "", "Unformatted cell $cell direct");
is ($sheet->{$cell}, "", "Formatted cell $cell direct");
is ($sheet->cell ($c, $r), "", "Unformatted cell $cell method");
is ($sheet->cell ($cell), "", "Formatted cell $cell method");
}
ok ($csv = Spreadsheet::Read->new ("files/test_m.csv"), "Read/Parse M\$ csv file");
ok ($sheet = $csv->sheet (1), "Sheet 1");
ok (1, "Defined fields");
foreach my $cell (qw( A1 A2 A3 A4 B1 B2 B4 C3 C4 D1 D3 )) {
my ($c, $r) = $sheet->cell2cr ($cell);
is ($sheet->cell ($c, $r), $cell, "Unformatted cell $cell");
is ($sheet->cell ($cell), $cell, "Formatted cell $cell");
}
ok (1, "Undefined fields");
foreach my $cell (qw( B3 C1 C2 D2 D4 )) {
my ($c, $r) = $sheet->cell2cr ($cell);
is ($csv->[1]{cell}[$c][$r], "", "Unformatted cell $cell");
is ($csv->[1]{$cell}, "", "Formatted cell $cell");
}
ok ($csv = Spreadsheet::Read->new ("files/test_x.csv", sep => "=", quote => "_"),
"Read/Parse strange csv file");
ok (1, "Defined fields");
foreach my $cell (qw( A1 A2 A3 A4 B1 B2 B4 C3 C4 D1 D3 )) {
my ($c, $r) = cell2cr ($cell);
is ($csv->[1]{cell}[$c][$r], $cell, "Unformatted cell $cell");
is ($csv->[1]{$cell}, $cell, "Formatted cell $cell");
}
ok (1, "Undefined fields");
foreach my $cell (qw( B3 C1 C2 D2 D4 )) {
my ($c, $r) = cell2cr ($cell);
is ($csv->[1]{cell}[$c][$r], "", "Unformatted cell $cell");
is ($csv->[1]{$cell}, "", "Formatted cell $cell");
}
{ # RT#74976 - Error Received when reading empty sheets
foreach my $strip (0 .. 3) {
my $ref = Spreadsheet::Read->new ("files/blank.csv", strip => $strip);
ok ($ref, "File with no content - strip $strip");
}
}
# blank.csv has only one sheet with A1 filled with ' '
{ my $ref = ReadData ("files/blank.csv", clip => 0, strip => 0);
ok ($ref, "!clip strip 0");
is ($ref->[1]{maxrow}, 3, "maxrow 3");
is ($ref->[1]{maxcol}, 4, "maxcol 4");
is ($ref->[1]{cell}[1][1], " ", "(1, 1) = ' '");
is ($ref->[1]{A1}, " ", "A1 = ' '");
$ref = ReadData ("files/blank.csv", clip => 0, strip => 1);
ok ($ref, "!clip strip 1");
is ($ref->[1]{maxrow}, 3, "maxrow 3");
is ($ref->[1]{maxcol}, 4, "maxcol 4");
is ($ref->[1]{cell}[1][1], "", "blank (1, 1)");
is ($ref->[1]{A1}, "", "undef A1");
$ref = ReadData ("files/blank.csv", clip => 0, strip => 1, cells => 0);
ok ($ref, "!clip strip 1");
is ($ref->[1]{maxrow}, 3, "maxrow 3");
is ($ref->[1]{maxcol}, 4, "maxcol 4");
is ($ref->[1]{cell}[1][1], "", "blank (1, 1)");
is ($ref->[1]{A1}, undef, "undef A1");
$ref = ReadData ("files/blank.csv", clip => 0, strip => 2, rc => 0);
ok ($ref, "!clip strip 2");
is ($ref->[1]{maxrow}, 3, "maxrow 3");
is ($ref->[1]{maxcol}, 4, "maxcol 4");
is ($ref->[1]{cell}[1][1], undef, "undef (1, 1)");
is ($ref->[1]{A1}, "", "blank A1");
$ref = ReadData ("files/blank.csv", clip => 0, strip => 3, cells => 0, rc => 0);
ok ($ref, "!clip strip 3");
is ($ref->[1]{maxrow}, 3, "maxrow 3");
is ($ref->[1]{maxcol}, 4, "maxcol 4");
is ($ref->[1]{cell}[1][1], undef, "undef (1, 1)");
is ($ref->[1]{A1}, undef, "undef A1");
$ref = ReadData ("files/blank.csv", clip => 1, strip => 0);
ok ($ref, " clip strip 0");
is ($ref->[1]{maxrow}, 1, "maxrow 3");
is ($ref->[1]{maxcol}, 1, "maxcol 4");
is ($ref->[1]{cell}[1][1], " ", "(1, 1) = ' '");
is ($ref->[1]{A1}, " ", "A1 = ' '");
$ref = ReadData ("files/blank.csv", clip => 1, strip => 1);
ok ($ref, " clip strip 1");
is ($ref->[1]{maxrow}, 0, "maxrow 0");
is ($ref->[1]{maxcol}, 0, "maxcol 0");
is ($ref->[1]{cell}[1][1], undef, "undef (1, 1)");
is ($ref->[1]{A1}, undef, "undef A1");
$ref = ReadData ("files/blank.csv", clip => 1, strip => 1, cells => 0);
ok ($ref, " clip strip 1");
is ($ref->[1]{maxrow}, 0, "maxrow 0");
is ($ref->[1]{maxcol}, 0, "maxcol 0");
is ($ref->[1]{cell}[1][1], undef, "undef (1, 1)");
is ($ref->[1]{A1}, undef, "undef A1");
$ref = ReadData ("files/blank.csv", clip => 1, strip => 2, rc => 0);
ok ($ref, " clip strip 2");
is ($ref->[1]{maxrow}, 0, "maxrow 0");
is ($ref->[1]{maxcol}, 0, "maxcol 0");
is ($ref->[1]{cell}[1][1], undef, "undef (1, 1)");
is ($ref->[1]{A1}, undef, "undef A1");
$ref = ReadData ("files/blank.csv", clip => 1, strip => 3, cells => 0, rc => 0);
ok ($ref, " clip strip 3");
is ($ref->[1]{maxrow}, 0, "maxrow 0");
is ($ref->[1]{maxcol}, 0, "maxcol 0");
is ($ref->[1]{cell}[1][1], undef, "undef (1, 1)");
is ($ref->[1]{A1}, undef, "undef A1");
}
ok ($csv = Spreadsheet::Read->new ("files/test.csv"), "Read/Parse csv file");
ok ($csv->add ("files/test.csv"), "Add the same file");
is ($csv->sheets, 2, "Two sheets");
is_deeply ([ $csv->sheets ], [qw( files/test.csv files/test.csv[2] )], "Sheet names");
is_deeply ($csv->sheet ("files/test.csv"), $csv->sheet (2), "Compare sheets");
ok (my $sheet2 = $csv->sheet (2), "The new sheet");
is ($sheet2->label, "files/test.csv", "Original label");
is ($sheet2->label ("Hello"), "Hello", "New label");
ok (my $sheet3 = $csv->sheet ("Hello"), "Found by new label");
is_deeply ($sheet2, $sheet3, "Compare sheets");
ok ($csv->add ("files/test.csv", label => "Test"), "Add with label");
is_deeply ([ $csv->sheets ], [qw( files/test.csv files/test.csv[2] Test )], "Sheet names");
is ($csv->col2label (4), "D", "col2label as book method");
is ($csv->sheet (1)->col2label (27), "AA", "col2label as sheet method");
is ($csv->label2col ("D"), 4, "label2col as book method");
is ($csv->sheet (1)->label2col ("AA"), 27, "label2col as sheet method");
is_deeply ($csv->sheet (1)->range ("A2:B3"), {
A2 => "A2", A3 => "A3", B2 => "B2", B3 => "" }, "range (A2:B3)");
is_deeply ($csv->sheet (1)->range ("S5:V12"), {
S5 => "LASTFIELD" }, "range (S5:V12)");
is_deeply ($csv->sheet (1)->range (1, 2, 2, 3), {
A2 => "A2", A3 => "A3", B2 => "B2", B3 => "" }, "range (1, 2, 2, 3)");
is_deeply ($csv->sheet (1)->range (19, 5, 22, 12), {
S5 => "LASTFIELD" }, "range (19, 5, 22, 12)");
is_deeply ($csv->sheet (1)->range (-1, -1, -1, -1), {
S5 => "LASTFIELD" }, "range (-1, -1, -1, -1)");
is_deeply ($csv->sheet (1)->cellrange ("A2:B3"), [
[ "A2", "A3"], [ "B2", "" ]], "cellrange (A2:B3)");
is_deeply ($csv->sheet (1)->cellrange ("S5:V12"), [
[ "LASTFIELD" ]], "cellrange (S5:V12)");
is_deeply ($csv->sheet (1)->cellrange (1, 2, 2, 3), [
[ "A2", "A3"], [ "B2", "" ]], "cellrange (1, 2, 2, 3)");
is_deeply ($csv->sheet (1)->cellrange (19, 5, 22, 12), [
[ "LASTFIELD" ]], "cellrange (19, 5, 22, 12)");
is_deeply ($csv->sheet (1)->cellrange (-1, -1, -1, -1), [
[ "LASTFIELD" ]], "cellrange (-1, -1, -1, -1)");
unless ($ENV{AUTOMATED_TESTING}) {
Test::NoWarnings::had_no_warnings ();
$tests++;
}
done_testing ($tests);
|