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
|
#!/usr/bin/perl
use strict;
use warnings;
my $tests = 301;
use Test::More;
require Test::NoWarnings;
BEGIN { $ENV{SPREADSHEET_READ_ODS} = "Spreadsheet::ReadSXC"; }
use Spreadsheet::Read;
my $parser = Spreadsheet::Read::parses ("ods") or
plan skip_all => "No SXC parser found";
my $pv = $parser->VERSION;
$pv >= "0.25" and
plan skip_all => "Use Spreadsheet::ParseODS instead please";
diag ("# Parser: $parser-$pv");
my $content;
{ local $/;
open my $xml, "<", "files/content.xml" or die "files/content.xml: $!\n";
binmode $xml;
$content = <$xml>;
close $xml;
}
{ my $ref;
$ref = ReadData ("no_such_file.ods");
ok (!defined $ref, "Nonexistent file");
# Too noisy
#eval { $ref = ReadData ("files/empty.ods") };
#ok (!defined $ref, "Empty file");
#like ($@, qr/too short/);
}
my @base = (
[ "files/test.ods", "Read/Parse ods file" ],
[ "files/content.xml", "Read/Parse xml file" ],
[ $content, "Parse xml data" ],
);
if ($parser->VERSION > 0.24) {
open my $fh, "<", "files/test.ods" or die "files/test.ods: $!\n";
push @base => [ $fh, "Parse ods file handle" ];
$tests += 100;
}
foreach my $base (@base) {
my ($txt, $msg) = @$base;
my $sxc;
my @options = ref $txt ? (parser => "ods") : ();
ok ($sxc = ReadData ($txt, @options), $msg);
ok (1, "Base values");
is (ref $sxc, "ARRAY", "Return type");
is ($sxc->[0]{type}, "sxc", "Spreadsheet type");
is ($sxc->[0]{sheets}, 2, "Sheet count");
is (ref $sxc->[0]{sheet}, "HASH", "Sheet list");
is (scalar keys %{$sxc->[0]{sheet}},
2, "Sheet list count");
# This should match the version required in Makefile.PL's PREREQ_PM
cmp_ok ($sxc->[0]{version}, ">=", 0.12, "Parser version");
ok (1, "Sheet 1");
# Simple sheet with cells filled with the cell label:
# -- -- -- --
# A1 B1 D1
# A2 B2
# A3 C3 D3
# A4 B4 C4
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 ($sxc->[1]{cell}[$c][$r], $cell, "Unformatted cell $cell");
is ($sxc->[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 ($sxc->[1]{cell}[$c][$r], undef, "Unformatted cell $cell");
is ($sxc->[1]{$cell}, undef, "Formatted cell $cell");
}
ok (1, "Nonexistent fields");
foreach my $cell (qw( A9 X6 B17 AB4 BE33 )) {
my ($c, $r) = cell2cr ($cell);
is ($sxc->[1]{cell}[$c][$r], undef, "Unformatted cell $cell");
is ($sxc->[1]{$cell}, undef, "Formatted cell $cell");
}
ok (1, "Sheet 2");
# Sheet with merged cells and notes/annotations
# x x x
# x x
# x x x
ok (1, "Defined fields");
foreach my $cell (qw( A1 C1 E1 B2 D2 A3 C3 E3 )) {
my ($c, $r) = cell2cr ($cell);
is ($sxc->[2]{cell}[$c][$r], "x", "Unformatted cell $cell");
is ($sxc->[2]{$cell}, "x", "Formatted cell $cell");
}
ok (1, "Undefined fields");
foreach my $cell (qw( B1 D1 A2 C2 E2 B3 D3 )) {
my ($c, $r) = cell2cr ($cell);
is ($sxc->[2]{cell}[$c][$r], undef, "Unformatted cell $cell");
is ($sxc->[2]{$cell}, undef, "Formatted cell $cell");
}
ok (1, "Nonexistent fields");
foreach my $cell (qw( A9 X6 B17 AB4 BE33 )) {
my ($c, $r) = cell2cr ($cell);
is ($sxc->[2]{cell}[$c][$r], undef, "Unformatted cell $cell");
is ($sxc->[2]{$cell}, undef, "Formatted cell $cell");
}
# Sheet order
ok (exists $sxc->[0]{sheet}{Sheet1}, "Sheet labels in metadata");
my @sheets = map { $sxc->[$_]{label} } 1 .. $sxc->[0]{sheets};
SKIP: {
$sxc->[0]{version} < 0.20 and
skip "Not supported", 1;
is ("@sheets", "@{['Sheet1','Second Sheet']}", "Sheet order");
}
}
unless ($ENV{AUTOMATED_TESTING}) {
Test::NoWarnings::had_no_warnings ();
$tests++;
}
done_testing ($tests);
|