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
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
my $tests;
plan tests => $tests;
use Data::Dumper;
use_ok('Spreadsheet::ParseExcel');
BEGIN { $tests += 1; }
# these tests were created based on the values received using 0.27
# to create regressions tests
{
# historically Parse returned and unblessed reference on missing file
my $excel = Spreadsheet::ParseExcel::Workbook->Parse('no_such_file.xls');
is(ref($excel), 'HASH', 'failed Parse method returns HASH ref');
isa_ok($excel->{_Excel}, 'Spreadsheet::ParseExcel',
'failed Parse method creates _Excel object');
BEGIN { $tests += 2; }
}
{
# historically Parse returned and unblessed reference on failure (e.g.
# input file is not an Excel file)
my $excel = Spreadsheet::ParseExcel::Workbook->Parse($0);
is(ref($excel), 'HASH', 'failed Parse method returns HASH ref');
isa_ok($excel->{_Excel}, 'Spreadsheet::ParseExcel',
'failed Parse method creates _Excel object');
BEGIN { $tests += 2; }
}
my $workbook_1;
{
my $workbook = Spreadsheet::ParseExcel::Workbook->Parse('sample/Excel/Test95.xls');
$workbook_1 = $workbook;
use Data::Dumper;
#diag Dumper $excel;
#_save_file('dump.txt', Dumper $excel);
is(ref($workbook), 'Spreadsheet::ParseExcel::Workbook',
'Spreadsheet::ParseExcel::Workbook created');
my $excel = $workbook->{_Excel};
isa_ok($excel, 'Spreadsheet::ParseExcel',
'Parse method creates _Excel object');
is(ref($excel->{FuncTbl}), 'HASH');
is(ref($excel->{GetContent}), 'CODE');
# meta data
is($workbook->{_CurSheet_}, 1, 'current sheet is 1');
is($workbook->{_CurSheet}, 1);
is($workbook->{Flg1904}, 0);
isa_ok($workbook->{FmtClass}, 'Spreadsheet::ParseExcel::FmtDefault');
# TODO more tests in Format
is(ref($workbook->{Format}), 'ARRAY');
my $formats = $workbook->{Format};
is(scalar(@$formats), 22);
# all but 2 are 'Spreadsheet::ParseExcel::Format' objects
is(ref($workbook->{FormatStr}), 'HASH');
is($workbook->{SheetCount}, 2);
my $fonts = $workbook->{Font};
is(ref($fonts), 'ARRAY');
is(scalar(@$fonts), 6);
is($workbook->{Version}, 1280);
is($workbook->{BIFFVersion}, 8);
is($workbook->{File}, 'sample/Excel/Test95.xls');
is($workbook->{Author}, 'kawait');
my @sheets = @{$workbook->{Worksheet}};
is (@sheets, 2, "two sheets");
is($sheets[0]->{Name}, 'Sheet1-ASC'); # Open Office shows: 'Sheet1_ASC'
is($sheets[1]->{Name}, 'Sheet1-ASC (2)'); # OO shows 'Sheet1_ASC_2_'
is($sheets[0]->{MinRow}, 0);
is($sheets[0]->{MaxRow}, 7);
#diag Dumper $sheets[0]->{Cells};
#qw(ASC Date INTEGER Float Double Formula)
is($sheets[0]->{Cells}[0][0]->{Val}, 'ASC');
#diag Dumper $sheets[0]->{Cells}[0][0];
is($sheets[1]->{MinRow}, 0);
is($sheets[1]->{MaxRow}, 5);
BEGIN { $tests += 26; }
}
eval "require IO::Scalar";
if ($@) {
ok (1, "Skipped - no IO::Scalar") for 1..6;
}
else {
{
open my $fh, '<','t/excel_files/Test95.xls';
my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($fh);
isnt($workbook, $workbook_1);
delete $workbook_1->{File}; # when give a filehandlres this field is not set
is_deeply($workbook, $workbook_1);
BEGIN { $tests += 2; }
}
# pass a reference to a scalar containing the file content
{
my $data;
if (open my $fh, '<','t/excel_files/Test95.xls') {
binmode($fh);
local $/ = undef;
$data = <$fh>;
}
my $workbook = Spreadsheet::ParseExcel::Workbook->Parse(\$data);
isnt($workbook, $workbook_1);
is_deeply($workbook, $workbook_1);
BEGIN { $tests += 2; }
}
{
open my $fh, '<','t/excel_files/Test95.xls';
binmode($fh);
my @data = <$fh>;
my $workbook = Spreadsheet::ParseExcel::Workbook->Parse(\@data);
isnt($workbook, $workbook_1);
$workbook_1->{File} = undef;
is_deeply($workbook, $workbook_1);
BEGIN { $tests += 2; }
}
}
eval "require IO::Wrap";
if ($@) {
ok (1, "Skipped - no IO::Wrap") for 1..4;
}
else {
{
open my $fh, '<','t/excel_files/Test95.xls';
my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($fh);
isnt($workbook, $workbook_1);
delete $workbook_1->{File}; # when give a filehandlres this field is not set
is_deeply($workbook, $workbook_1);
BEGIN { $tests += 2; }
}
# pass an IO::Wrap object
{
my $data;
my $fh;
if (open my $real_fh, '<','t/excel_files/Test95.xls') {
binmode($real_fh);
$fh = IO::Wrap::wraphandle($real_fh);
}
my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($fh);
isnt($workbook, $workbook_1);
is_deeply($workbook, $workbook_1);
BEGIN { $tests += 2; }
}
}
sub _save_file {
my ($file, $data) = @_;
if (open my $fh, '>', $file) {
print {$fh} $data;
}
}
|