blib/lib/Test/Excel.pm

filter: Statementstmt 93.0% 256 / 275 Branchbran 80.1% 117 / 146 Conditioncond 68.9% 133 / 193 MC/DCmcdc 38.3% 36 / 94 Subroutinesub 100% 25 / 25 Podpod 66.6% 4 / 6 totaltotal 77.2% 571 / 739 CC 147 SCAR 43.2
CC
147
Cov
82%
compare_excel
132.2
_validate_rule
47.4
_parse
18.6
CRAP 74.9
↑ Summary
1 package Test::Excel;
2  
3 7 $Test::Excel::VERSION = '1.54';
4 7 $Test::Excel::AUTHORITY = 'cpan:MANWAR';
5  
6 =head1 NAME
7  
8 Test::Excel - Interface to test and compare Excel files (.xls/.xlsx).
9  
10 =head1 VERSION
11  
12 Version 1.54
13  
14 =cut
15  
16 7 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Test::Builder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use Spreadsheet::Read;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 7 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 our @ISA = qw(Exporter);
27 7 our @EXPORT = qw(cmp_excel compare_excel cmp_excel_ok cmp_excel_not_ok);
28  
29 7 $|=1;
30  
31 7 my $ALMOST_ZERO = 10**-16;
32 7 my $IGNORE = 1;
33 7 my $SPECIAL_CASE = 2;
34 7 my $REGEX_CASE = 3;
35 7 my $MAX_ERRORS_PER_SHEET = 0;
36 7 my $TESTER = Test::Builder->new;
37  
38 =head1 DESCRIPTION
39  
40 This module is meant to be used for testing custom generated Excel files, it
41 provides interfaces to compare_excel two Excel files if they are I<visually> same.
42 It now supports Excel files with the extensions C<.xls> or C<.xlsx>.
43  
44 =head1 SYNOPSIS
45  
46 Using as unit test as below:
47  
48 use strict; use warnings;
49 use Test::More tests => 2;
50 use Test::Excel;
51  
52 cmp_excel_ok("foo.xls", "foo.xls");
53  
54 cmp_excel_not_ok("foo.xls", "bar.xls");
55  
56 done_testing();
57  
58 Using as standalone as below:
59  
60 use strict; use warnings;
61 use Test::Excel;
62  
63 if (compare_excel("foo.xls", "foo.xls")) {
64 print "Excels are similar.\n";
65 }
66 else {
67 print "Excels aren't similar.\n";
68 }
69  
70 =head1 METHODS
71  
72 =head2 cmp_excel($got, $exp, \%rule, $message)
73  
74 This function will tell you whether the two Excel files are "visually" different,
75 ignoring differences in embedded fonts / images and metadata. Both C<$got> and
76 C<$exp> can be either instance of L<Spreadsheet::Read> / file path (which is in
77 turn passed to the L<Spreadsheet::Read> constructor).
78 This one is for use in TEST MODE.
79  
80 use strict; use warnings;
81 use Test::More tests => 1;
82 use Test::Excel;
83  
84 cmp_excel('foo.xls', 'bar.xls', {}, 'EXCELs are identical.');
85  
86 done_testing();
87  
88 =head2 cmp_excel_ok($got, $exp, \%rule, $message)
89  
90 Test OK if excel files are identical. Same as C<cmp_excel()>.
91  
92 =head2 cmp_excel_not_ok($got, $exp, \%rule, $message)
93  
94 Test OK if excel files are NOT identical.
95  
96 =cut
97  
98 14 ▶sub cmp_excel {
Subroutine cmp_excel: called CC=1 SCAR=0.0
Pod documented
99 14 my ($got, $exp, $rule, $message) = @_;
100  
101 14 my $status = compare_excel($got, $exp, $rule);
102 7 $TESTER->ok($status, $message);
103 }
104  
105 1 ▶sub cmp_excel_ok {
Subroutine cmp_excel_ok: called CC=1 SCAR=0.0
Pod documented
106 1 my ($got, $exp, $rule, $message) = @_;
107  
108 1 my $status = compare_excel($got, $exp, $rule);
109 1 $TESTER->ok($status, $message);
110 }
111  
112 1 ▶sub cmp_excel_not_ok {
Subroutine cmp_excel_not_ok: called CC=2 SCAR=7.4
Pod documented
113 1 my ($got, $exp, $rule, $message) = @_;
114  
115 1 my $status = compare_excel($got, $exp, $rule);
116 1 ▶ if ($status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 0) { } 1 0 1
117 1 $TESTER->ok(1, $message);
118 }
119 else {
120 0 $TESTER->ok(0, $message);
121 }
122 }
123  
124 =head2 compare_excel($got, $exp, \%rule)
125  
126 Same as C<cmp_excel_ok()> but ideal for non-TEST MODE.
127 This function will tell you whether the two Excel files are "visually" different,
128 ignoring differences in embedded fonts / images and metadata. Both C<$got> and
129 C<$exp> can be either instance of L<Spreadsheet::Read> / file path (which is in
130 turn passed to the L<Spreadsheet::Read> constructor).
131  
132 use strict; use warnings;
133 use Test::Excel;
134  
135 print "EXCELs are identical.\n" if compare_excel("foo.xls", "bar.xls");
136  
137 =cut
138  
139 45 ▶sub compare_excel {
Subroutine compare_excel: called CC=84 SCAR=48.8
Pod documented
140 45 my ($got, $exp, $rule) = @_;
141  
142 52 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
143 52 my ($error) = @_;
144 52 ▶ warn $error unless ($error =~ /Use of uninitialized value/);
Branch: 1 branch
BranchTrueFalseTotal
unless $error =~ /Use of uninitialized value/ 0 52 52
145 45 };
146  
147 45 ▶ die("ERROR: Unable to locate file [$got][$!].\n") unless (-f $got);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $got 2 43 45
148 43 ▶ die("ERROR: Unable to locate file [$exp][$!].\n") unless (-f $exp);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $exp 2 41 43
149  
150 41 _log_message("INFO: Excel comparison [$got] [$exp]\n");
151 41 ▶ unless (blessed($got) && $got->isa('Spreadsheet::Read')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($got) and $got->isa("Spreadsheet::Read")) 41 0 41
Condition33%1/3
builtin::blessed($got) and $got->isa("Spreadsheet::Read")
!ll&&!rl&&r
4100
MC/DC0%0/2
builtin::blessed($got) and $got->isa("Spreadsheet::Read")
builtin::blessed($got) $got->isa("Spreadsheet::Read")
Truth table33%1/3
A and B
A= builtin::blessed($got)B= $got->isa("Spreadsheet::Read")
ABresult
0X0
100
111
152 41 ▶ $got = Spreadsheet::Read->new($got)
Condition50%1/2
'Spreadsheet::Read'->new($got) || die("ERROR: Couldn't create Spreadsheet::Read instance with: [$got]\n")
l!l
410
MC/DC0%0/1
'Spreadsheet::Read'->new($got) || die("ERROR: Couldn't create Spreadsheet::Read instance with: [$got]\n")
'Spreadsheet::Read'->new($got)
Truth table50%1/2
A
A= 'Spreadsheet::Read'->new($got)
Aresult
11
00
153 || die("ERROR: Couldn't create Spreadsheet::Read instance with: [$got]\n");
154 }
155  
156 41 ▶ unless (blessed($exp) && $exp->isa('Spreadsheet::Read')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($exp) and $exp->isa("Spreadsheet::Read")) 41 0 41
Condition33%1/3
builtin::blessed($exp) and $exp->isa("Spreadsheet::Read")
!ll&&!rl&&r
4100
MC/DC0%0/2
builtin::blessed($exp) and $exp->isa("Spreadsheet::Read")
builtin::blessed($exp) $exp->isa("Spreadsheet::Read")
Truth table33%1/3
A and B
A= builtin::blessed($exp)B= $exp->isa("Spreadsheet::Read")
ABresult
0X0
100
111
157 41 ▶ $exp = Spreadsheet::Read->new($exp)
Condition50%1/2
'Spreadsheet::Read'->new($exp) || die("ERROR: Couldn't create Spreadsheet::Read instance with: [$exp]\n")
l!l
410
MC/DC0%0/1
'Spreadsheet::Read'->new($exp) || die("ERROR: Couldn't create Spreadsheet::Read instance with: [$exp]\n")
'Spreadsheet::Read'->new($exp)
Truth table50%1/2
A
A= 'Spreadsheet::Read'->new($exp)
Aresult
11
00
158 || die("ERROR: Couldn't create Spreadsheet::Read instance with: [$exp]\n");
159 }
160  
161 41 _validate_rule($rule);
162  
163 30 my $spec = _get_hashval($rule, 'spec');
164 30 my $error_limit = _get_hashval($rule, 'error_limit');
165 30 my $sheet = _get_hashval($rule, 'sheet');
166 30 my @gotWorkSheets = $got->sheets();
167 30 my @expWorkSheets = $exp->sheets();
168  
169 30 ▶ $spec = _parse($spec) if defined $spec;
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec 16 14 30
170 29 ▶ $error_limit = $MAX_ERRORS_PER_SHEET unless defined $error_limit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error_limit 26 3 29
171  
172 29 ▶ if (@gotWorkSheets != @expWorkSheets) {
Branch: 1 branch
BranchTrueFalseTotal
if (@gotWorkSheets != @expWorkSheets) 1 28 29
173 1 my $error = 'ERROR: Sheets count mismatch. ';
174 1 $error .= 'Got: [' . @gotWorkSheets .
175 '] exp: [' . @expWorkSheets . "]\n";
176 1 _log_message($error);
177 1 return 0;
178 }
179  
180 28 my @sheets;
181 28 my $status = 1;
182 28 ▶ @sheets = split(/\|/, $sheet) if defined $sheet;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sheet 6 22 28
183  
184 28 ▶ for (my $i = 0; $i < @gotWorkSheets; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @gotWorkSheets) 50 23 73
185 50 my $error_on_sheet = 0;
186 50 my $gotWorkSheet = $gotWorkSheets[$i];
187 50 my $expWorkSheet = $expWorkSheets[$i];
188 50 my $gotSheetName = $gotWorkSheet;
189 50 my $expSheetName = $expWorkSheet;
190  
191 50 ▶ unless (exists $spec->{ALL}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $spec->{"ALL"}) 24 26 50
192 24 ▶ if (uc($gotSheetName) ne uc($expSheetName)) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $gotSheetName ne uc $expSheetName) 0 24 24
193 0 my $error = "ERROR: Sheetname mismatch. Got: [$gotSheetName] exp: [$expSheetName].\n";
194 0 _log_message($error);
195 0 return 0;
196 }
197 }
198  
199 50 my $got_sheet = $got->sheet($gotSheetName);
200 50 my $exp_sheet = $exp->sheet($expSheetName);
201 50 my ($gotRowMin, $gotRowMax) = (1, $got_sheet->maxrow);
202 50 my ($gotColMin, $gotColMax) = (1, $got_sheet->maxcol);
203 50 my ($expRowMin, $expRowMax) = (1, $exp_sheet->maxrow);
204 50 my ($expColMin, $expColMax) = (1, $exp_sheet->maxcol);
205  
206 50 _log_message("INFO: [$gotSheetName]:[$gotRowMin][$gotColMin]:[$gotRowMax][$gotColMax]\n");
207 50 _log_message("INFO: [$expSheetName]:[$expRowMin][$expColMin]:[$expRowMax][$expColMax]\n");
208  
209 50 ▶ if (defined($gotRowMax) && defined($expRowMax) && ($gotRowMax != $expRowMax)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gotRowMax and defined $expRowMax and $gotRowMax != $expRowMax) 0 50 50
Condition33%2/6
defined $gotRowMax and defined $expRowMax and $gotRowMax != $expRowMax
!ll&&!rl&&r
0500
defined $gotRowMax and defined $expRowMax
!ll&&!rl&&r
0050
MC/DC0%0/3
defined $gotRowMax and defined $expRowMax and $gotRowMax != $expRowMax
defined $gotRowMax defined $expRowMax $gotRowMax != $expRowMax
Truth table25%1/4
A and B and C
A= defined $gotRowMaxB= defined $expRowMaxC= $gotRowMax != $expRowMax
ABCresult
0XX0
10X0
1100
1111
210 0 my $error = "ERROR: Max row counts mismatch in sheet [$gotSheetName]. ";
211 0 $error .= "Got[$gotRowMax] Expected: [$expRowMax]\n";
212 0 _log_message($error);
213 0 return 0;
214 }
215  
216 50 ▶ if (defined($gotColMax) && defined($expColMax) && ($gotColMax != $expColMax)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gotColMax and defined $expColMax and $gotColMax != $expColMax) 0 50 50
Condition33%2/6
defined $gotColMax and defined $expColMax and $gotColMax != $expColMax
!ll&&!rl&&r
0500
defined $gotColMax and defined $expColMax
!ll&&!rl&&r
0050
MC/DC0%0/3
defined $gotColMax and defined $expColMax and $gotColMax != $expColMax
defined $gotColMax defined $expColMax $gotColMax != $expColMax
Truth table25%1/4
A and B and C
A= defined $gotColMaxB= defined $expColMaxC= $gotColMax != $expColMax
ABCresult
0XX0
10X0
1100
1111
217 0 my $error = "ERROR: Max column counts mismatch in sheet [$gotSheetName]. ";
218 0 $error .= "Got[$gotColMax] Expected: [$expColMax]\n";
219 0 _log_message($error);
220 0 return 0;
221 }
222  
223 50 my ($swap);
224 50 ▶ for (my $row = $gotRowMin; $row <= $gotRowMax; $row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row <= $gotRowMax) 287 45 332
225 287 ▶ for (my $col = $gotColMin; $col <= $gotColMax; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col <= $gotColMax) 1879 282 2161
226 1879 my $gotData = $got_sheet->cell($col, $row);
227 1879 my $expData = $exp_sheet->cell($col, $row);
228  
229 next if (defined $spec
230 &&
231 (( exists $spec->{ALL}
232 && exists $spec->{ALL}->{$col}
233 && exists $spec->{ALL}->{$col}->{$row}
234 && exists $spec->{ALL}->{$col}->{$row}->{$IGNORE}
235 )
236 ||
237 ( exists $spec->{uc($gotSheetName)}
238 && exists $spec->{uc($gotSheetName)}->{$col}
239 && exists $spec->{uc($gotSheetName)}->{$col}->{$row}
240 1879 ▶ && exists $spec->{uc($gotSheetName)}->{$col}->{$row}->{$IGNORE}
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec and exists $spec->{"ALL"} && exists $spec->{"ALL"}{$col} && exists $spec->{"ALL"}{$col}{$row} && exists $spec->{"ALL"}{$col}{$row}{$IGNORE} || exists $$spec{uc $gotSheetName} && exists $$spec{uc $gotSheetName}{$col} && exists $$spec{uc $gotSheetName}{$col}{$row} && exists $$spec{uc $gotSheetName}{$col}{$row}{$IGNORE} 20 1859 1879
Condition95%23/24
defined $spec and exists $spec->{"ALL"} && exists $spec->{"ALL"}{$col} && exists $spec->{"ALL"}{$col}{$row} && exists $spec->{"ALL"}{$col}{$row}{$IGNORE} || exists $$spec{uc $gotSheetName} && exists $$spec{uc $gotSheetName}{$col} && exists $$spec{uc $gotSheetName}{$col}{$row} && exists $$spec{uc $gotSheetName}{$col}{$row}{$IGNORE}
!ll&&!rl&&r
0185920
exists $spec->{"ALL"} and exists $spec->{"ALL"}{$col} and exists $spec->{"ALL"}{$col}{$row} and exists $spec->{"ALL"}{$col}{$row}{$IGNORE} or exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$IGNORE}
l!l&&r!l&&!r
8121859
exists $spec->{"ALL"} and exists $spec->{"ALL"}{$col} and exists $spec->{"ALL"}{$col}{$row} and exists $spec->{"ALL"}{$col}{$row}{$IGNORE}
!ll&&!rl&&r
186838
exists $spec->{"ALL"} and exists $spec->{"ALL"}{$col} and exists $spec->{"ALL"}{$col}{$row}
!ll&&!rl&&r
1862611
exists $spec->{"ALL"} and exists $spec->{"ALL"}{$col}
!ll&&!rl&&r
147438817
exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$IGNORE}
!ll&&!rl&&r
18114812
exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row}
!ll&&!rl&&r
166814360
exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col}
!ll&&!rl&&r
1527141203
MC/DC88%8/9
defined $spec and exists $spec->{"ALL"} && exists $spec->{"ALL"}{$col} && exists $spec->{"ALL"}{$col}{$row} && exists $spec->{"ALL"}{$col}{$row}{$IGNORE} || exists $$spec{uc $gotSheetName} && exists $$spec{uc $gotSheetName}{$col} && exists $$spec{uc $gotSheetName}{$col}{$row} && exists $$spec{uc $gotSheetName}{$col}{$row}{$IGNORE}
defined $spec exists $spec->{"ALL"} exists $spec->{"ALL"}{$col} exists $spec->{"ALL"}{$col}{$row} exists $spec->{"ALL"}{$col}{$row}{$IGNORE} exists $$spec{uc $gotSheetName} exists $$spec{uc $gotSheetName}{$col} exists $$spec{uc $gotSheetName}{$col}{$row} exists $$spec{uc $gotSheetName}{$col}{$row}{$IGNORE}
Truth table50%11/22
A and B and C and D and E or F and G and H and I
A= defined $specB= exists $spec->{"ALL"}C= exists $spec->{"ALL"}{$col}D= exists $spec->{"ALL"}{$col}{$row}E= exists $spec->{"ALL"}{$col}{$row}{$IGNORE}F= exists $$spec{uc $gotSheetName}G= exists $$spec{uc $gotSheetName}{$col}H= exists $$spec{uc $gotSheetName}{$col}{$row}I= exists $$spec{uc $gotSheetName}{$col}{$row}{$IGNORE}
ABCDEFGHIresult
0XXXXXXXX0
10XXX0XXX0
10XXX10XX0
10XXX110X0
10XXX11100
110XX0XXX0
110XX10XX0
110XX110X0
110XX11100
1110X0XXX0
1110X10XX0
1110X110X0
1110X11100
111100XXX0
1111010XX0
11110110X0
1111011100
11111XXXX1
10XXX11111
110XX11111
1110X11111
1111011111
241 ))
242 );
243  
244 1859 ▶ if (defined $gotData && defined $expData) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gotData and defined $expData) 1787 72 1859
Condition66%2/3
defined $gotData and defined $expData
!ll&&!rl&&r
7201787
MC/DC50%1/2
defined $gotData and defined $expData
defined $gotData defined $expData
Truth table66%2/3
A and B
A= defined $gotDataB= defined $expData
ABresult
0X0
100
111
245 # Number like data?
246 1787 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($gotData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ and $expData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/) { } 1555 232 1787
Condition66%2/3
$gotData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ and $expData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
!ll&&!rl&&r
23201555
MC/DC50%1/2
$gotData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ and $expData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
$gotData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ $expData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
Truth table66%2/3
A and B
A= $gotData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/B= $expData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
ABresult
0X0
100
111
247 ($gotData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/)
248 &&
249 ($expData =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/)
250 ) {
251 1555 ▶ if (($gotData < $ALMOST_ZERO) && ($expData < $ALMOST_ZERO)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gotData < $ALMOST_ZERO and $expData < $ALMOST_ZERO) { } 0 1555 1555
Condition33%1/3
$gotData < $ALMOST_ZERO and $expData < $ALMOST_ZERO
!ll&&!rl&&r
155500
MC/DC0%0/2
$gotData < $ALMOST_ZERO and $expData < $ALMOST_ZERO
$gotData < $ALMOST_ZERO $expData < $ALMOST_ZERO
Truth table33%1/3
A and B
A= $gotData < $ALMOST_ZEROB= $expData < $ALMOST_ZERO
ABresult
0X0
100
111
252 # Can be treated as the same.
253 0 next;
254 }
255 else {
256 1555 ▶ if (defined $rule && scalar(keys %$rule)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule and scalar keys %$rule) { } 101 1454 1555
Condition100%2/2
defined $rule and scalar keys %$rule
!ll
21553
MC/DC100%1/1
defined $rule and scalar keys %$rule
defined $rule
Truth table100%2/2
A
A= defined $rule
Aresult
00
11
257 101 my $compare_with;
258 101 my $difference = abs($expData - $gotData) / abs($expData);
259  
260 101 ▶ if (( defined $spec
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$SPECIAL_CASE} or @sheets and grep /$gotSheetName/, @sheets) { } 65 36 101
Condition83%15/18
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$SPECIAL_CASE} or @sheets and grep /$gotSheetName/, @sheets
l!l&&r!l&&!r
402536
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$SPECIAL_CASE}
!ll&&!rl&&r
61040
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row}
!ll&&!rl&&r
124940
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col}
!ll&&!rl&&r
12089
defined $spec and exists $$spec{uc $gotSheetName}
!ll&&!rl&&r
01289
@sheets and grep /$gotSheetName/, @sheets
!ll&&!rl&&r
241225
MC/DC42%3/7
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$SPECIAL_CASE} or @sheets and grep /$gotSheetName/, @sheets
defined $spec exists $$spec{uc $gotSheetName} exists $$spec{uc $gotSheetName}{$col} exists $$spec{uc $gotSheetName}{$col}{$row} exists $$spec{uc $gotSheetName}{$col}{$row}{$SPECIAL_CASE} @sheets grep /$gotSheetName/, @sheets
Truth table25%4/16
A and B and C and D and E or F and G
A= defined $specB= exists $$spec{uc $gotSheetName}C= exists $$spec{uc $gotSheetName}{$col}D= exists $$spec{uc $gotSheetName}{$col}{$row}E= exists $$spec{uc $gotSheetName}{$col}{$row}{$SPECIAL_CASE}F= @sheetsG= grep /$gotSheetName/, @sheets
ABCDEFGresult
11111XX1
0XXXX111
10XXX111
110XX111
1110X111
11110111
0XXXX0X0
0XXXX100
10XXX0X0
10XXX100
110XX0X0
110XX100
1110X0X0
1110X100
111100X0
11110100
261 && exists $spec->{uc($gotSheetName)}
262 && exists $spec->{uc($gotSheetName)}->{$col}
263 && exists $spec->{uc($gotSheetName)}->{$col}->{$row}
264 && exists $spec->{uc($gotSheetName)}->{$col}->{$row}->{$SPECIAL_CASE}
265 )
266 ||
267 (@sheets && grep(/$gotSheetName/,@sheets))
268 ) {
269  
270 65 _log_message("INFO: [NUMBER]:[$gotSheetName]:[SPC][".
271 ($row)."][".($col)."]:[$gotData][$expData] ... ");
272 65 $compare_with = $rule->{sheet_tolerance};
273 }
274 else {
275 36 _log_message("INFO: [NUMBER]:[$gotSheetName]:[STD][".(
276 $row)."][".($col)."]:[$gotData][$expData] ... ");
277 36 ▶ $compare_with = $rule->{tolerance} || 0;
Condition50%1/2
$rule->{'tolerance'} || 0
l!l
360
MC/DC0%0/1
$rule->{'tolerance'} || 0
$rule->{'tolerance'}
Truth table50%1/2
A
A= $rule->{'tolerance'}
Aresult
11
00
278 }
279  
280 101 ▶ if (defined $compare_with && ($compare_with < $difference)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $compare_with and $compare_with < $difference) { } 8 93 101
Condition66%2/3
defined $compare_with and $compare_with < $difference
!ll&&!rl&&r
0938
MC/DC50%1/2
defined $compare_with and $compare_with < $difference
defined $compare_with $compare_with < $difference
Truth table66%2/3
A and B
A= defined $compare_withB= $compare_with < $difference
ABresult
0X0
100
111
281 8 _log_message("[FAIL]\n");
282 8 $difference = sprintf("%02f", $difference);
283 8 $status = 0;
284 }
285 else {
286 93 $status = 1;
287 93 _log_message("[PASS]\n");
288 }
289 }
290 else {
291 1454 _log_message("INFO: [NUMBER]:[$gotSheetName]:[N/A][".
292 ($row)."][".($col)."]:[$gotData][$expData] ... ");
293 1454 ▶ if ($expData != $gotData) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expData != $gotData) { } 2 1452 1454
294 2 _log_message("[FAIL]\n");
295 2 return 0;
296 }
297 else {
298 1452 $status = 1;
299 1452 _log_message("[PASS]\n");
300 }
301 }
302 }
303 }
304 else {
305 # Is it regex?
306 232 ▶ if (( defined $spec
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$REGEX_CASE} or exists $spec->{'ALL'}{$col} and exists $spec->{'ALL'}{$col}{$row} and exists $spec->{'ALL'}{$col}{$row}{$REGEX_CASE} or @sheets and grep /$gotSheetName/, @sheets) { } 60 172 232
Condition88%24/27
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$REGEX_CASE} or exists $spec->{"ALL"}{$col} and exists $spec->{"ALL"}{$col}{$row} and exists $spec->{"ALL"}{$col}{$row}{$REGEX_CASE} or @sheets and grep /$gotSheetName/, @sheets
l!l&&r!l&&!r
1149172
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$REGEX_CASE} or exists $spec->{"ALL"}{$col} and exists $spec->{"ALL"}{$col}{$row} and exists $spec->{"ALL"}{$col}{$row}{$REGEX_CASE}
l!l&&r!l&&!r
83221
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$REGEX_CASE}
!ll&&!rl&&r
22408
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row}
!ll&&!rl&&r
174508
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col}
!ll&&!rl&&r
5312158
defined $spec and exists $$spec{uc $gotSheetName}
!ll&&!rl&&r
053179
exists $spec->{"ALL"}{$col} and exists $spec->{"ALL"}{$col}{$row} and exists $spec->{"ALL"}{$col}{$row}{$REGEX_CASE}
!ll&&!rl&&r
22103
exists $spec->{"ALL"}{$col} and exists $spec->{"ALL"}{$col}{$row}
!ll&&!rl&&r
21563
@sheets and grep /$gotSheetName/, @sheets
!ll&&!rl&&r
1522049
MC/DC70%7/10
defined $spec and exists $$spec{uc $gotSheetName} and exists $$spec{uc $gotSheetName}{$col} and exists $$spec{uc $gotSheetName}{$col}{$row} and exists $$spec{uc $gotSheetName}{$col}{$row}{$REGEX_CASE} or exists $spec->{"ALL"}{$col} and exists $spec->{"ALL"}{$col}{$row} and exists $spec->{"ALL"}{$col}{$row}{$REGEX_CASE} or @sheets and grep /$gotSheetName/, @sheets
defined $spec exists $$spec{uc $gotSheetName} exists $$spec{uc $gotSheetName}{$col} exists $$spec{uc $gotSheetName}{$col}{$row} exists $$spec{uc $gotSheetName}{$col}{$row}{$REGEX_CASE} exists $spec->{"ALL"}{$col} exists $spec->{"ALL"}{$col}{$row} exists $spec->{"ALL"}{$col}{$row}{$REGEX_CASE} @sheets grep /$gotSheetName/, @sheets
Truth table21%11/51
A and B and C and D and E or F and G and H or I and J
A= defined $specB= exists $$spec{uc $gotSheetName}C= exists $$spec{uc $gotSheetName}{$col}D= exists $$spec{uc $gotSheetName}{$col}{$row}E= exists $$spec{uc $gotSheetName}{$col}{$row}{$REGEX_CASE}F= exists $spec->{"ALL"}{$col}G= exists $spec->{"ALL"}{$col}{$row}H= exists $spec->{"ALL"}{$col}{$row}{$REGEX_CASE}I= @sheetsJ= grep /$gotSheetName/, @sheets
ABCDEFGHIJresult
11111XXXXX1
0XXXX111XX1
10XXX111XX1
110XX111XX1
1110X111XX1
11110111XX1
0XXXX0XX111
0XXXX10X111
0XXXX110111
10XXX0XX111
10XXX10X111
10XXX110111
110XX0XX111
110XX10X111
110XX110111
1110X0XX111
1110X10X111
1110X110111
111100XX111
1111010X111
11110110111
0XXXX0XX0X0
0XXXX0XX100
0XXXX10X0X0
0XXXX10X100
0XXXX1100X0
0XXXX110100
10XXX0XX0X0
10XXX0XX100
10XXX10X0X0
10XXX10X100
10XXX1100X0
10XXX110100
110XX0XX0X0
110XX0XX100
110XX10X0X0
110XX10X100
110XX1100X0
110XX110100
1110X0XX0X0
1110X0XX100
1110X10X0X0
1110X10X100
1110X1100X0
1110X110100
111100XX0X0
111100XX100
1111010X0X0
1111010X100
111101100X0
11110110100
307 && exists $spec->{uc($gotSheetName)}
308 && exists $spec->{uc($gotSheetName)}->{$col}
309 && exists $spec->{uc($gotSheetName)}->{$col}->{$row}
310 && exists $spec->{uc($gotSheetName)}->{$col}->{$row}->{$REGEX_CASE}
311 )
312 ||
313 ( exists $spec->{ALL}->{$col}
314 && exists $spec->{ALL}->{$col}->{$row}
315 && exists $spec->{ALL}->{$col}->{$row}->{$REGEX_CASE}
316 )
317 ||
318 (@sheets && grep(/$gotSheetName/,@sheets))
319 ) {
320 60 my $exp = qr{$spec->{uc($gotSheetName)}->{$col}->{$row}->{$REGEX_CASE}};
321 60 ▶ if (($gotData =~ /$exp/i) && ($expData =~ /$exp/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gotData =~ /$exp/i and $expData =~ /$exp/i) { } 59 1 60
Condition66%2/3
$gotData =~ /$exp/i and $expData =~ /$exp/i
!ll&&!rl&&r
1059
MC/DC50%1/2
$gotData =~ /$exp/i and $expData =~ /$exp/i
$gotData =~ /$exp/i $expData =~ /$exp/i
Truth table66%2/3
A and B
A= $gotData =~ /$exp/iB= $expData =~ /$exp/i
ABresult
0X0
100
111
322 59 $status = 1;
323 59 _log_message("INFO: [REGEX]:[$gotSheetName]:[".
324 ($row)."][".($col)."]:[$gotData][$expData] ... [PASS]\n");
325 }
326 else {
327 1 _log_message("INFO: [REGEX]:[$gotSheetName]:[$expData][$gotData][$exp] ... [FAIL]\n");
328 1 $status = 0;
329 }
330 }
331 else {
332 # String like data?
333 172 ▶ if (uc($gotData) ne uc($expData)) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $gotData ne uc $expData) { } 10 162 172
334 10 _log_message("INFO: [STRING]:[$gotSheetName]:[$expData][$gotData] ... [FAIL]\n");
335 10 ▶ if (defined $rule) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule) { } 10 0 10
336 10 $error_on_sheet++;
337 10 $status = 0;
338 }
339 else {
340 0 return 0;
341 }
342 }
343 else {
344 162 $status = 1;
345 162 _log_message("INFO: [STRING]:[$gotSheetName]:[STD][".
346 ($row)."][".($col)."]:[$gotData][$expData] ... [PASS]\n");
347 }
348 }
349 }
350  
351 1785 ▶ if ( exists $rule->{swap_check}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{'swap_check'} and defined $rule->{'swap_check'} and $rule->{'swap_check'}) { } 62 1723 1785
Condition66%4/6
exists $rule->{"swap_check"} and defined $rule->{"swap_check"} and $rule->{"swap_check"}
!ll&&!rl&&r
1723062
exists $rule->{"swap_check"} and defined $rule->{"swap_check"}
!ll&&!rl&&r
1723062
MC/DC33%1/3
exists $rule->{"swap_check"} and defined $rule->{"swap_check"} and $rule->{"swap_check"}
exists $rule->{"swap_check"} defined $rule->{"swap_check"} $rule->{"swap_check"}
Truth table50%2/4
A and B and C
A= exists $rule->{"swap_check"}B= defined $rule->{"swap_check"}C= $rule->{"swap_check"}
ABCresult
0XX0
10X0
1100
1111
352 && defined $rule->{swap_check}
353 && $rule->{swap_check}
354 ) {
355 62 ▶ if ($status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 0) 16 46 62
356 16 $error_on_sheet++;
357 16 push @{$swap->{exp}->{_number_to_letter($col)}}, $expData;
358 16 push @{$swap->{got}->{_number_to_letter($col)}}, $gotData;
359  
360 16 ▶ if (($error_on_sheet >= $error_limit)
Branch: 1 branch
BranchTrueFalseTotal
if ($error_on_sheet >= $error_limit and $error_on_sheet % 2 == 0 and not _is_swapping($swap)) 0 16 16
Condition83%5/6
$error_on_sheet >= $error_limit and $error_on_sheet % 2 == 0 and not _is_swapping($swap)
!ll&&!rl&&r
1150
$error_on_sheet >= $error_limit and $error_on_sheet % 2 == 0
!ll&&!rl&&r
745
MC/DC0%0/3
$error_on_sheet >= $error_limit and $error_on_sheet % 2 == 0 and not _is_swapping($swap)
$error_on_sheet >= $error_limit $error_on_sheet % 2 == 0 not _is_swapping($swap)
Truth table75%3/4
A and B and not(C)
A= $error_on_sheet >= $error_limitB= $error_on_sheet % 2 == 0C= not _is_swapping($swap)
ABCresult
0XX0
10X0
1100
1111
361 && ($error_on_sheet % 2 == 0)
362 && !_is_swapping($swap)) {
363 0 _log_message("ERROR: Max error per sheet reached.[$error_on_sheet]\n");
364 0 return $status;
365 }
366 }
367 }
368 else {
369 1723 ▶ return $status if ($status == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $status == 0 3 1720 1723
370 }
371 }
372 } # col
373  
374 282 ▶ if (($error_on_sheet > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($error_on_sheet > 0 and $error_on_sheet >= $error_limit and $error_on_sheet % 2 == 0 and not _is_swapping($swap)) 0 282 282
Condition88%8/9
$error_on_sheet > 0 and $error_on_sheet >= $error_limit and $error_on_sheet % 2 == 0 and not _is_swapping($swap)
!ll&&!rl&&r
27750
$error_on_sheet > 0 and $error_on_sheet >= $error_limit and $error_on_sheet % 2 == 0
!ll&&!rl&&r
27525
$error_on_sheet > 0 and $error_on_sheet >= $error_limit
!ll&&!rl&&r
27237
MC/DC0%0/4
$error_on_sheet > 0 and $error_on_sheet >= $error_limit and $error_on_sheet % 2 == 0 and not _is_swapping($swap)
$error_on_sheet > 0 $error_on_sheet >= $error_limit $error_on_sheet % 2 == 0 not _is_swapping($swap)
Truth table80%4/5
A and B and C and not(D)
A= $error_on_sheet > 0B= $error_on_sheet >= $error_limitC= $error_on_sheet % 2 == 0D= not _is_swapping($swap)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
375 && ($error_on_sheet >= $error_limit)
376 && ($error_on_sheet % 2 == 0)
377 && !_is_swapping($swap)) {
378 0 ▶ return $status if ($status == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $status == 0 0 0 0
379 }
380 } # row
381  
382 45 ▶ if ( exists $rule->{swap_check}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"swap_check"} and defined $rule->{"swap_check"} and $rule->{"swap_check"}) 9 36 45
Condition66%4/6
exists $rule->{"swap_check"} and defined $rule->{"swap_check"} and $rule->{"swap_check"}
!ll&&!rl&&r
3609
exists $rule->{"swap_check"} and defined $rule->{"swap_check"}
!ll&&!rl&&r
3609
MC/DC33%1/3
exists $rule->{"swap_check"} and defined $rule->{"swap_check"} and $rule->{"swap_check"}
exists $rule->{"swap_check"} defined $rule->{"swap_check"} $rule->{"swap_check"}
Truth table50%2/4
A and B and C
A= exists $rule->{"swap_check"}B= defined $rule->{"swap_check"}C= $rule->{"swap_check"}
ABCresult
0XX0
10X0
1100
1111
383 && defined $rule->{swap_check}
384 && $rule->{swap_check}
385 ) {
386 9 ▶ if (($error_on_sheet > 0) && _is_swapping($swap)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_on_sheet > 0 and _is_swapping($swap)) 3 6 9
Condition66%2/3
$error_on_sheet > 0 and _is_swapping($swap)
!ll&&!rl&&r
603
MC/DC50%1/2
$error_on_sheet > 0 and _is_swapping($swap)
$error_on_sheet > 0 _is_swapping($swap)
Truth table66%2/3
A and B
A= $error_on_sheet > 0B= _is_swapping($swap)
ABresult
0X0
100
111
387 3 _log_message("WARN: SWAP OCCURRED.\n");
388 3 $status = 1;
389 }
390 }
391  
392 45 _log_message("INFO: [$gotSheetName]: ..... [OK].\n");
393 } # sheet
394  
395 23 return $status;
396 }
397  
398 =head1 RULE
399  
400 The parameter C<rule> can be used optionally to apply exception when comparing the
401 contents. This should be passed in as has ref and may contain keys from the table
402 below.
403  
404 +-----------------+---------------------------------------------------------+
405 | Key | Description |
406 +-----------------+---------------------------------------------------------+
407 | sheet | "|" seperated sheet names. |
408 | tolerance | Number. Apply to all NUMBERS except on 'sheet'/'spec'. |
409 | | e.g. 10**-12 |
410 | sheet_tolerance | Number. Apply to sheets/ranges in the spec. e.g. 0.20 |
411 | spec | Path to the specification file. |
412 | swap_check | Number (optional) (1 or 0). Row swapping check. |
413 | | Default is 0. |
414 | error_limit | Number (optional). Limit error per sheet. Default is 0. |
415 +-----------------+---------------------------------------------------------+
416  
417 =head1 SPECIFICATION FILE
418  
419 A spec file containing rules used should be in the format mentioned below. Keys
420 and values are space-separated.
421  
422 sheet Sheet1
423 range A3:B14
424 range B5:C5
425 sheet Sheet2
426 range A1:B2
427 ignorerange B3:B8
428  
429 As in C<v1.51> or above, we now support the use of C<regex> in the
430 specification file.
431  
432 The following specification forces regex comparison in all sheets in
433 range C<B2:B4>.
434  
435 sheet ALL
436 range B2:B4
437 regex 2022\-\d\d\-\d\d
438  
439 The following specification forces regex comparison in all sheets.
440  
441 sheet ALL
442 regex 2022\-\d\d\-\d\d
443  
444 The following specification forces regex comparison in the sheet
445 named C<Demo> in range C<B2:B4>.
446  
447 sheet Demo
448 range B2:B4
449 regex 2022\-\d\d\-\d\d
450  
451 =head1 What Is "Visually" Similar?
452  
453 This module uses the L<Spreadsheet::Read> module to parse the Excel
454 files and then compares the parsed data structure for differences. It
455 ignores certain components of the Excel file, such as embedded fonts,
456 images, forms and annotations, and focuses entirely on the layout of
457 each Excel page instead. Future versions may support font and image
458 comparisons as well.
459  
460 =head1 How to find out what failed the comparison?
461  
462 Setting the environment variable DEBUG to a non-zero, non-empty value
463 will output the PASS/FAIL comparison. For example:
464  
465 $> $DEBUG=1 perl your-test-script.pl
466  
467 =cut
468  
469 #
470 #
471 # PRIVATE METHODS
472  
473 61 ▶sub _column_row {
Subroutine _column_row: called CC=3 SCAR=11.4
474 61 my ($cell) = @_;
475  
476 61 ▶ return unless defined $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cell 0 61 61
477  
478 61 ▶ die "ERROR: Invalid cell address [$cell].\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $cell =~ /([A-Za-z]+)(\d+)/ 0 61 61
479 unless ($cell =~ /([A-Za-z]+)(\d+)/);
480  
481 61 return ($1, $2);
482 }
483  
484 61 ▶sub _letter_to_number {
Subroutine _letter_to_number: called CC=1 SCAR=0.0
485 61 my ($letter) = @_;
486  
487 61 return col2int($letter);
488 }
489  
490 # -------------------------------------------------------------------
491 # col2int (for Spreadsheet::ParseExcel::Utility)
492 # -------------------------------------------------------------------
493 61 ▶sub col2int {
Subroutine col2int: called CC=3 SCAR=11.0
Pod undocumented
494 61 my $result = 0;
495 61 my $str = shift;
496 61 my $incr = 0;
497  
498 61 ▶ for ( my $i = length($str) ; $i > 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 62 61 123
499 62 my $char = substr( $str, $i - 1 );
500 62 my $curr += ord( lc($char) ) - ord('a') + 1;
501 62 ▶ $curr *= $incr if ($incr);
Branch: 1 branch
BranchTrueFalseTotal
if $incr 1 61 62
502 62 $result += $curr;
503 62 $incr += 26;
504 }
505  
506 # this is one out as we range 0..x-1 not 1..x
507 61 $result--;
508  
509 61 return $result;
510 }
511  
512 33 ▶sub _number_to_letter {
Subroutine _number_to_letter: called CC=1 SCAR=0.0
513 33 my ($number) = @_;
514  
515 33 return int2col($number);
516 }
517  
518 # -------------------------------------------------------------------
519 # int2col (for Spreadsheet::ParseExcel::Utility)
520 # -------------------------------------------------------------------
521 33 ▶sub int2col {
Subroutine int2col: called CC=2 SCAR=6.9
Pod undocumented
522 33 my $out = "";
523 33 my $val = shift;
524  
525 33 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$val >= 0 and do { do { $out .= chr($val % 26 + 65); $val = int($val / 26) - 1 } } 1 33 34
526 34 $out .= chr( ( $val % 26 ) + ord('A') );
527 34 $val = int( $val / 26 ) - 1;
528 } while ( $val >= 0 );
529  
530 33 return scalar reverse $out;
531 }
532  
533 28 ▶sub _cells_within_range {
Subroutine _cells_within_range: called CC=6 SCAR=18.0
534 28 my ($range) = @_;
535  
536 28 ▶ return unless defined $range;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $range 0 28 28
537  
538 28 my $cells = [];
539 28 foreach my $_range (split /\,/,$range) {
540 30 ▶ die "ERROR: Invalid range [$_range].\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $_range =~ /(\w+\d+):(\w+\d+)/ 0 30 30
541 unless ($_range =~ /(\w+\d+):(\w+\d+)/);
542  
543 30 my $from = $1;
544 30 my $to = $2;
545 30 my ($min_col, $min_row) = Test::Excel::_column_row($from);
546 30 my ($max_col, $max_row) = Test::Excel::_column_row($to);
547  
548 30 $min_col = Test::Excel::_letter_to_number($min_col);
549 30 $max_col = Test::Excel::_letter_to_number($max_col);
550  
551 30 ▶ for (my $row = $min_row; $row <= $max_row; $row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row <= $max_row) 78 30 108
552 78 ▶ for (my $col = $min_col; $col <= $max_col; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col <= $max_col) 83 78 161
553 83 push @{$cells}, { col => $col, row => $row };
554 }
555 }
556 }
557  
558 28 return $cells;
559 }
560  
561 17 ▶sub _parse {
Subroutine _parse: called CC=18 SCAR=29.2
562 17 my ($spec) = @_;
563  
564 17 ▶ return unless defined $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec 0 17 17
565  
566 17 ▶ die "ERROR: Unable to locate spec file [$spec][$!].\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $spec 1 16 17
567 unless (-f $spec);
568  
569 16 my $data = undef;
570 16 my $sheet = undef;
571 16 my $regex = undef;
572 16 ▶ my $handle = IO::File->new($spec)
Condition50%1/2
'IO::File'->new($spec) || die("ERROR: Couldn't open file [$spec][$!].\n")
l!l
160
MC/DC0%0/1
'IO::File'->new($spec) || die("ERROR: Couldn't open file [$spec][$!].\n")
'IO::File'->new($spec)
Truth table50%1/2
A
A= 'IO::File'->new($spec)
Aresult
11
00
573 || die "ERROR: Couldn't open file [$spec][$!].\n";
574  
575 16 ▶ while (my $row = <$handle>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $row = readline $handle)) 62 15 77
576 62 chomp($row);
577 62 ▶ next unless ($row =~ /\w/);
Branch: 1 branch
BranchTrueFalseTotal
unless $row =~ /\w/ 0 62 62
578 62 ▶ next if ($row =~ /^#/);
Branch: 1 branch
BranchTrueFalseTotal
if $row =~ /^#/ 0 62 62
579  
580 62 ▶ if ($row =~ /^sheet\s+(.*)/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($row =~ /^sheet\s+(.*)/i) { } 29 33 62
elsif (defined $sheet and $row =~ /^range\s+(.*)/i) { } 21 12 33
elsif (defined $sheet and $row =~ /^regex\s+(.*)/i) { } 5 7 12
elsif (defined $sheet and $row =~ /^ignorerange\s+(.*)/i) { } 6 1 7
581 29 $sheet = $1;
582 }
583 ▶ elsif (defined $sheet && ($row =~ /^range\s+(.*)/i)) {
Condition66%2/3
defined $sheet and $row =~ /^range\s+(.*)/i
!ll&&!rl&&r
01221
MC/DC50%1/2
defined $sheet and $row =~ /^range\s+(.*)/i
defined $sheet $row =~ /^range\s+(.*)/i
Truth table66%2/3
A and B
A= defined $sheetB= $row =~ /^range\s+(.*)/i
ABresult
0X0
100
111
584 21 my $cells = Test::Excel::_cells_within_range($1);
585 21 foreach my $cell (@{$cells}) {
586 55 $data->{uc($sheet)}->{$cell->{col}+1}->{$cell->{row}}->{$SPECIAL_CASE} = 1
587 }
588 }
589 ▶ elsif (defined($sheet) && ($row =~ /^regex\s+(.*)/i)) {
Condition66%2/3
defined $sheet and $row =~ /^regex\s+(.*)/i
!ll&&!rl&&r
075
MC/DC50%1/2
defined $sheet and $row =~ /^regex\s+(.*)/i
defined $sheet $row =~ /^regex\s+(.*)/i
Truth table66%2/3
A and B
A= defined $sheetB= $row =~ /^regex\s+(.*)/i
ABresult
0X0
100
111
590 5 foreach my $c (keys %{$data->{uc($sheet)}}) {
591 4 foreach my $r (keys %{$data->{uc($sheet)}->{$c}}) {
592 # Needs overriding to be regex friendly
593 12 $data->{uc($sheet)}->{$c}->{$r}->{$REGEX_CASE} = $1;
594 }
595 }
596 }
597 ▶ elsif (defined($sheet) && ($row =~ /^ignorerange\s+(.*)/i)) {
Condition66%2/3
defined $sheet and $row =~ /^ignorerange\s+(.*)/i
!ll&&!rl&&r
016
MC/DC50%1/2
defined $sheet and $row =~ /^ignorerange\s+(.*)/i
defined $sheet $row =~ /^ignorerange\s+(.*)/i
Truth table66%2/3
A and B
A= defined $sheetB= $row =~ /^ignorerange\s+(.*)/i
ABresult
0X0
100
111
598 6 my $cells = Test::Excel::_cells_within_range($1);
599 6 foreach my $cell (@{$cells}) {
600 22 $data->{uc($sheet)}->{$cell->{col}+1}->{$cell->{row}}->{$IGNORE} = 1;
601 }
602 }
603 else {
604 1 die "ERROR: Invalid format data [$row] found in spec file.\n";
605 }
606 }
607  
608 15 $handle->close();
609  
610 15 return $data;
611 }
612  
613 90 ▶sub _get_hashval {
Subroutine _get_hashval: called CC=5 SCAR=16.2
614 90 my ($hash, $key) = @_;
615  
616 90 ▶ return unless (defined $hash && defined $key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash and defined $key 9 81 90
Condition66%2/3
defined $hash and defined $key
!ll&&!rl&&r
9081
MC/DC50%1/2
defined $hash and defined $key
defined $hash defined $key
Truth table66%2/3
A and B
A= defined $hashB= defined $key
ABresult
0X0
100
111
617 81 ▶ die "_get_hashval(): Not a hash." unless (ref($hash) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 81 81
618  
619 81 ▶ return unless (exists $hash->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{$key} 55 26 81
620 26 return $hash->{$key};
621 }
622  
623 13 ▶sub _is_swapping {
Subroutine _is_swapping: called CC=4 SCAR=14.0
624 13 my ($data) = @_;
625  
626 13 ▶ return 0 unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 13 13
627  
628 13 foreach (keys %{$data->{exp}}) {
629 24 my $exp = $data->{exp}->{$_};
630 24 my $out = $data->{out}->{$_};
631  
632 24 ▶ return 0 if grep(/$exp->[0]/,@{$out});
Branch: 1 branch
BranchTrueFalseTotal
if grep /$exp->[0]/, @{$out;} 0 24 24
633 }
634  
635 13 return 1;
636 }
637  
638 3532 ▶sub _log_message {
Subroutine _log_message: called CC=3 SCAR=12.5
639 3532 my ($message) = @_;
640  
641 3532 ▶ return unless defined($message);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 3532 3532
642  
643 0 ▶ print {*STDOUT} $message if ($ENV{DEBUG});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 3532 3532
644 }
645  
646 41 ▶sub _validate_rule {
Subroutine _validate_rule: called CC=26 SCAR=38.6
647 41 my ($rule) = @_;
648  
649 41 ▶ return unless defined $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule 3 38 41
650  
651 38 ▶ die "ERROR: Invalid RULE definitions. It has to be reference to a HASH.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rule eq "HASH" 2 36 38
652 unless (ref($rule) eq 'HASH');
653  
654 36 my ($keys, $valid);
655 36 $keys = scalar(keys(%{$rule}));
656 36 ▶ return if (($keys == 1) && exists $rule->{message});
Branch: 1 branch
BranchTrueFalseTotal
if $keys == 1 and exists $rule->{"message"} 0 36 36
Condition66%2/3
$keys == 1 and exists $rule->{"message"}
!ll&&!rl&&r
2790
MC/DC0%0/2
$keys == 1 and exists $rule->{"message"}
$keys == 1 exists $rule->{"message"}
Truth table66%2/3
A and B
A= $keys == 1B= exists $rule->{"message"}
ABresult
0X0
100
111
657  
658 36 ▶ die "ERROR: Rule has more than 8 keys defined.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $keys > 8 1 35 36
659 if $keys > 8;
660  
661 35 $valid = {
662 'message' => 1,
663 'sheet' => 2,
664 'spec' => 3,
665 'tolerance' => 4,
666 'sheet_tolerance' => 5,
667 'error_limit' => 6,
668 'swap_check' => 7,
669 'test' => 8,
670 };
671  
672 35 foreach my $key (keys %{$rule}) {
673 die "ERROR: Invalid key '$key' found in the rule definitions.\n"
674 79 ▶ unless exists($valid->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid->{$key} 2 77 79
675 }
676  
677 33 ▶ return if (exists $rule->{spec} && (keys %$rule == 1));
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"spec"} and keys %$rule == 1 7 26 33
Condition100%3/3
exists $rule->{"spec"} and keys %$rule == 1
!ll&&!rl&&r
13137
MC/DC100%2/2
exists $rule->{"spec"} and keys %$rule == 1
exists $rule->{"spec"} keys %$rule == 1
Truth table100%3/3
A and B
A= exists $rule->{"spec"}B= keys %$rule == 1
ABresult
0X0
100
111
678  
679 26 ▶ if ((exists $rule->{spec} && defined $rule->{spec})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{'spec'} and defined $rule->{'spec'} or exists $rule->{'sheet'} and defined $rule->{'sheet'}) { } 22 4 26
Condition77%7/9
exists $rule->{"spec"} and defined $rule->{"spec"} or exists $rule->{"sheet"} and defined $rule->{"sheet"}
l!l&&r!l&&!r
1394
exists $rule->{"spec"} and defined $rule->{"spec"}
!ll&&!rl&&r
13013
exists $rule->{"sheet"} and defined $rule->{"sheet"}
!ll&&!rl&&r
409
MC/DC50%2/4
exists $rule->{"spec"} and defined $rule->{"spec"} or exists $rule->{"sheet"} and defined $rule->{"sheet"}
exists $rule->{"spec"} defined $rule->{"spec"} exists $rule->{"sheet"} defined $rule->{"sheet"}
Truth table42%3/7
A and B or C and D
A= exists $rule->{"spec"}B= defined $rule->{"spec"}C= exists $rule->{"sheet"}D= defined $rule->{"sheet"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
680 ||
681 (exists $rule->{sheet} && defined $rule->{sheet})
682 ) {
683 die "ERROR: Missing key sheet_tolerance in the rule definitions.\n"
684 unless ( exists $rule->{sheet_tolerance}
685 22 ▶ && defined $rule->{sheet_tolerance});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rule->{"sheet_tolerance"} and defined $rule->{"sheet_tolerance"} 2 20 22
Condition66%2/3
exists $rule->{"sheet_tolerance"} and defined $rule->{"sheet_tolerance"}
!ll&&!rl&&r
2020
MC/DC50%1/2
exists $rule->{"sheet_tolerance"} and defined $rule->{"sheet_tolerance"}
exists $rule->{"sheet_tolerance"} defined $rule->{"sheet_tolerance"}
Truth table66%2/3
A and B
A= exists $rule->{"sheet_tolerance"}B= defined $rule->{"sheet_tolerance"}
ABresult
0X0
100
111
686 die "ERROR: Missing key tolerance in the rule definitions.\n"
687 unless ( exists $rule->{tolerance}
688 20 ▶ && defined $rule->{tolerance});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rule->{"tolerance"} and defined $rule->{"tolerance"} 4 16 20
Condition66%2/3
exists $rule->{"tolerance"} and defined $rule->{"tolerance"}
!ll&&!rl&&r
4016
MC/DC50%1/2
exists $rule->{"tolerance"} and defined $rule->{"tolerance"}
exists $rule->{"tolerance"} defined $rule->{"tolerance"}
Truth table66%2/3
A and B
A= exists $rule->{"tolerance"}B= defined $rule->{"tolerance"}
ABresult
0X0
100
111
689 }
690 else {
691 4 ▶ if ((exists $rule->{sheet_tolerance} && defined $rule->{sheet_tolerance})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"sheet_tolerance"} and defined $rule->{"sheet_tolerance"} or exists $rule->{"tolerance"} and defined $rule->{"tolerance"}) 0 4 4
Condition33%3/9
exists $rule->{"sheet_tolerance"} and defined $rule->{"sheet_tolerance"} or exists $rule->{"tolerance"} and defined $rule->{"tolerance"}
l!l&&r!l&&!r
004
exists $rule->{"sheet_tolerance"} and defined $rule->{"sheet_tolerance"}
!ll&&!rl&&r
400
exists $rule->{"tolerance"} and defined $rule->{"tolerance"}
!ll&&!rl&&r
400
MC/DC0%0/4
exists $rule->{"sheet_tolerance"} and defined $rule->{"sheet_tolerance"} or exists $rule->{"tolerance"} and defined $rule->{"tolerance"}
exists $rule->{"sheet_tolerance"} defined $rule->{"sheet_tolerance"} exists $rule->{"tolerance"} defined $rule->{"tolerance"}
Truth table14%1/7
A and B or C and D
A= exists $rule->{"sheet_tolerance"}B= defined $rule->{"sheet_tolerance"}C= exists $rule->{"tolerance"}D= defined $rule->{"tolerance"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
692 ||
693 (exists $rule->{tolerance} && defined $rule->{tolerance})
694 ) {
695 die "ERROR: Missing key sheet/spec in the rule definitions.\n"
696 unless (
697 (exists $rule->{sheet} && defined $rule->{sheet})
698 ||
699 (exists $rule->{spec} && defined $rule->{spec})
700 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rule->{"sheet"} and defined $rule->{"sheet"} or exists $rule->{"spec"} and defined $rule->{"spec"} 0 0 0
Condition0%0/9
exists $rule->{"sheet"} and defined $rule->{"sheet"} or exists $rule->{"spec"} and defined $rule->{"spec"}
l!l&&r!l&&!r
000
exists $rule->{"sheet"} and defined $rule->{"sheet"}
!ll&&!rl&&r
000
exists $rule->{"spec"} and defined $rule->{"spec"}
!ll&&!rl&&r
000
MC/DC0%0/4
exists $rule->{"sheet"} and defined $rule->{"sheet"} or exists $rule->{"spec"} and defined $rule->{"spec"}
exists $rule->{"sheet"} defined $rule->{"sheet"} exists $rule->{"spec"} defined $rule->{"spec"}
Truth table0%0/7
A and B or C and D
A= exists $rule->{"sheet"}B= defined $rule->{"sheet"}C= exists $rule->{"spec"}D= defined $rule->{"spec"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
701 }
702 }
703 }
704  
705 =head1 NOTES
706  
707 It should be clearly noted that this module does not claim to provide fool-proof
708 comparison of generated Excel files. In fact there are still a number of ways in
709 which I want to expand the existing comparison functionality. This module is no
710 longer actively being developed as I moved to another company. This work was part
711 of one of my projects. Having said that, I would be more than happy to add new
712 features if requested. Any suggestions / ideas most welcome.
713  
714 =head1 CAVEATS
715  
716 Testing large Excel files can take a long time. This is because, well, it is doing
717 a lot of computation. In fact, the test suite for this module includes tests against
718 several large Excel files; however, I am not including those in this distibution for
719 obvious reasons.
720  
721 =head1 BUGS
722  
723 None that I am aware of. Of course, if you find a bug, let me know, and I would do
724 my best to fix it. This is still a very early version, so it is always possible
725 that I have just "gotten it wrong" in some places.
726  
727 =head1 SEE ALSO
728  
729 =over 4
730  
731 =item L<Spreadsheet::Read> - I could not have written without this module.
732  
733 =back
734  
735 =head1 ACKNOWLEDGEMENTS
736  
737 =over 4
738  
739 =item H.Merijn Brand (author of L<Spreadsheet::Read>).
740  
741 =item Kawai Takanori (author of L<Spreadsheet::ParseExcel::Utility>).
742  
743 =item Stevan Little (author of L<Test::PDF>).
744  
745 =back
746  
747 =head1 AUTHOR
748  
749 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
750  
751 =head1 CONTRIBUTORS
752  
753 =over 4
754  
755 =item * Julien Fiegehenn
756  
757 =item * Ed Sabol
758  
759 =back
760  
761 =head1 REPOSITORY
762  
763 L<https://github.com/manwar/Test-Excel>
764  
765 =head1 BUGS
766  
767 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/Test-Excel/issues>.
768 I will be notified, and then you'll automatically be notified of progress on your
769 bug as I make changes.
770  
771 =head1 SUPPORT
772  
773 You can find documentation for this module with the perldoc command.
774  
775 perldoc Test::Excel
776  
777 You can also look for information at:
778  
779 =over 4
780  
781 =item * BUG Report
782  
783 L<https://github.com/manwar/Test-Excel/issues>
784  
785 =item * AnnoCPAN: Annotated CPAN documentation
786  
787 L<http://annocpan.org/dist/Test-Excel>
788  
789 =item * CPAN Ratings
790  
791 L<http://cpanratings.perl.org/d/Test-Excel>
792  
793 =item * Search MetaCPAN
794  
795 L<https://metacpan.org/dist/Test-Excel/>
796  
797 =back
798  
799 =head1 LICENSE AND COPYRIGHT
800  
801 Copyright (C) 2010 - 2024 Mohammad Sajid Anwar.
802  
803 This program is free software; you can redistribute it and/or modify it under
804 the terms of the the Artistic License (2.0). You may obtain a copy of the full
805 license at:
806  
807 L<http://www.perlfoundation.org/artistic_license_2_0>
808  
809 Any use, modification, and distribution of the Standard or Modified Versions is
810 governed by this Artistic License.By using, modifying or distributing the Package,
811 you accept this license. Do not use, modify, or distribute the Package, if you do
812 not accept this license.
813  
814 If your Modified Version has been derived from a Modified Version made by someone
815 other than you,you are nevertheless required to ensure that your Modified Version
816 complies with the requirements of this license.
817  
818 This license does not grant you the right to use any trademark, service mark,
819 tradename, or logo of the Copyright Holder.
820  
821 This license includes the non-exclusive, worldwide, free-of-charge patent license
822 to make, have made, use, offer to sell, sell, import and otherwise transfer the
823 Package with respect to any patent claims licensable by the Copyright Holder that
824 are necessarily infringed by the Package. If you institute patent litigation
825 (including a cross-claim or counterclaim) against any party alleging that the
826 Package constitutes direct or contributory patent infringement,then this Artistic
827 License to you shall terminate on the date that such litigation is filed.
828  
829 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
830 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
831 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
832 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
833 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
834 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
835 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
836  
837 =cut
838  
839 7 1; # End of Test::Excel
↑ Summary