NAME
Test::Excel - A module for testing and comparing Excel files
VERSION
Version 1.03
SYNOPSIS
use Test::More no_plan => 1;
use Test::Excel;
cmp('foo.xls', 'bar.xls', { message => 'EXCELSs are identical.' });
# or
my $foo = Spreadsheet::ParseExcel::Workbook->Parse('foo.xls');
my $bar = Spreadsheet::ParseExcel::Workbook->Parse('bar.xls');
cmp($foo, $bar, { message => 'EXCELs are identical.' });
# or even in standalone mode:
use Test::Excel;
print "EXCELs are identical.\n"
if compare("foo.xls", "bar.xls");
DESCRIPTION
This module is meant to be used for testing custom generated Excel files, it provides two functions at the moment, which is cmp
and compare
. These can be used to compare two Excel files to see if they are visually similar. The function cmp
is for testing purpose where function compare
can be used as standalone. Future versions may include other testing functions.
Definition of Rule
The new paramter has been added to both method cmp() and method compare() called rule. This is optional, however, this would allow to apply your own rule for comparison. This should be passed in as reference to a HASH with the keys 'sheet', 'tolerance', 'sheet_tolerance' and optionally 'message'(only relevant to method cmp()).
- sheet: "|" seperated sheet name.
-
Apply sheet_tolerance to all the NUMBERS found on these sheets. Example: 'Sheet1|Sheet2'
- tolerance: Number.
-
This would apply to all the NUMBERS found on all sheets in the excel except the one specified by the key sheet and by the title sheet in the spec file. Example: 10**-12
- sheet_tolerance: Number.
-
These rule would be applied to all the sheets defined in the spec file by the title 'sheet' within the range specified by 'range' in the spec file and also by the key 'sheet'. Example: 0.20
- spec: Path to the spec file.
-
This would have the path to the spec file to be used in comparing excel file.
- swap_check: Number (Optional).
-
Set it to 1 if you want to do swap check. The default is 0. Swap check ignores if the row has been swaped around in the same sheet.
- error_limit: Number (Optional).
-
Limit the error per sheet. Default is 0.
- message: String (Optional)
-
Test message to be displayed. Only required when calling method cmp().
What is "Visually" Similar?
This module uses the Spreadsheet::ParseExcel
module to parse Excel files, then compares the parsed data structure for differences. We ignore cetain components of the Excel file, such as embedded fonts, images, forms and annotations, and focus entirely on the layout of each Excel page instead. Future versions will likely support font and image comparisons, but not in this initial release.
DEBUGGING
Debug mode can be turned on or off by setting package variable $DEBUG, for example,
$Test::Excel::DEBUG = 1;
You can set it anything greater than 1 for fine grained debug information. i.e.
$Test::Excel::DEBUG = 2;
METHODS
_validate_rule()
This is a local method to validate the rule definitions.
cmp($got, $exp, { ...rule... })
This function will tell you whether the two Excel files are "visually" different, ignoring differences in embedded fonts/images and metadata.
Both $got and $expected can be either instances of Spreadsheet::ParseExcel or a file path (which is in turn passed to the Spreadsheet::ParseExcel constructor).
compare($got, $exp, { ...rule... })
This function will tell you whether the two Excel files are "visually" different, ignoring differences in embedded fonts/images and metadata in standalone mode.
Both $got and $exp can be either instances of Spreadsheet::ParseExcel or a file path (which is in turn passed to the Spreadsheet::ParseExcel constructor).
is_swapping()
parse()
This method parse spec file provided by the user. It expects spec file to be in a format mentioned below:
sheet Sheet1
range A3:B14
range B5:C5
sheet Sheet2
range A1:B2
ignorerange B3:B8
column_row()
This method accepts a cell address and returns column and row address as a list.
use strict; use warnings;
use Test::Excel;
my $cell = 'A23';
my ($col, $row) = Test::Excel::column_row($cell);
# You should expect these values:
# $col => 'A'
# $row => 23
letter_to_number()
This method accepts a letter and returns back its equivalent number. This simply wraps around Spreadsheet::ParseExcel::Utility::col2int().
use strict; use warnings;
use Test::Excel;
my $number = Test::Excel::letter_to_number('AB');
# You should expect $number to be 27.
number_to_letter()
This number accepts a number and returns its equivalent letter. This simply wraps around Spreadsheet::ParseExcel::Utility::int2col().
use strict; use warnings;
use Test::Excel;
my $letter = Test::Excel::number_to_letter(27);
# You should expect $letter to be 'AB'.
cells_within_range()
This method accepts address range and returns all cell address within the range.
use strict; use warnings;
use Test::Excel;
my $range = 'A1:B3';
my $cells = Test::Excel::cells_within_range($range);
# $cells would have something like below:
# [ {row => 1, col => 0},
# {row => 1, col => 1},
# {row => 2, col => 0},
# {row => 2, col => 1},
# {row => 3, col => 0},
# {row => 3, col => 1} ]
_log_message()
This is an internal method that dumps the message to STDOUT.
Important Disclaimer
It should be clearly noted that this module does not claim to provide a fool-proof comparison of generated Excels. In fact there are still a number of ways in which I want to expand the existing comparison functionality. This module is actively being developed for a number of projects I am currently working on, so expect many changes to happen. If you have any suggestions/comments/questions please feel free to contact me.
CAVEATS
Testing Large Excels
Testing of large Excels can take a long time, this is because, well, we are doing a lot of computation. In fact, this module test suite includes tests against several large Excels, however I am not including those in this distibution for obvious reasons.
TO DO
BUGS
None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it. This is still a very early version, so it is always possible that I have just "gotten it wrong" in some places.
SEE ALSO
ACKNOWLEDGEMENTS
AUTHOR
Mohammad S Anwar, <mohammad.anwar@yahoo.com>
COPYRIGHT AND LICENSE
Copyright 2010 by Mohammad S Anwar.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.