From: Rob Kinyon Date: Thu, 10 Mar 2005 15:47:33 +0000 (+0000) Subject: Added more tests and fixed formats to work correctly w/multiple templates. X-Git-Tag: v0.24~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9ee3aea0708bca221861aabdd8eb9a9fdeaa9ea5;p=p5sagit%2FExcel-Template.git Added more tests and fixed formats to work correctly w/multiple templates. --- diff --git a/Changes b/Changes index 61ee02e..fe6aff1 100644 --- a/Changes +++ b/Changes @@ -1,9 +1,13 @@ Revision history for Perl distribution Excel::Template -0.24 ??? Mar 0? ??:00:00 2005 +0.24 Thu Mar 10 11:00:00 2005 - Implemented the KEEP_LEADING_ZEROS node - This wraps the keep_leading_zeros() worksheet method - Improved code coverage with more and better tests + - Am now above 90% coverage. + - Fixed bug involving relative values. + - Fixed bug involving formats when multiple Excel-Template objects ran in + the same process. - Improved POD linking - Every module/node reference in POD should link to the appropriate POD diff --git a/META.yml b/META.yml index ce185d2..2353c41 100644 --- a/META.yml +++ b/META.yml @@ -1,13 +1,14 @@ # http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: Excel-Template -version: 0.23 +version: 0.24 version_from: lib/Excel/Template.pm installdirs: site requires: File::Basename: 0.01 IO::Scalar: 0.01 Spreadsheet::WriteExcel: 0.42 + Spreadsheet::WriteExcel::Utility: 0.01 Test::More: 0.01 XML::Parser: 0.01 diff --git a/lib/Excel/Template.pm b/lib/Excel/Template.pm index 554807d..1b686d1 100644 --- a/lib/Excel/Template.pm +++ b/lib/Excel/Template.pm @@ -109,6 +109,7 @@ sub write_file eval { $self->_prepare_output($xls); }; +print $@ if $@; $xls->close; @@ -404,7 +405,7 @@ This node represents a loop. It behaves just like L's TMPL_LOOP. =item * L -This node represents a row of data. This is the A in A1. +This node represents a row of data. This is the 1 in A1. There is no COLUMN node, as of yet. =item * L @@ -465,39 +466,40 @@ I used L to test the coverage of my tests. Every release, I intend Excel::Template is also part of the CPAN Kwalitee initiative, being one of the top 100 non-core modules downloaded from CPAN. If you wish to help out, please feel free to contribute tests, patches, and/or suggestions. - ----------------------------------------------------------------------- - File stmt brnch cond sub pod time total - ----------------------------------------------------------------------- - blib/lib/Excel/Template.pm 96.0 62.5 58.8 100.0 100.0 25.2 86.2 - ...ib/Excel/Template/Base.pm 87.0 50.0 66.7 81.8 87.5 8.7 83.0 - ...cel/Template/Container.pm 71.4 50.0 33.3 70.0 80.0 4.9 68.4 - ...emplate/Container/Bold.pm 100.0 n/a n/a 100.0 0.0 0.7 95.0 - .../Container/Conditional.pm 64.9 57.5 66.7 100.0 0.0 0.7 63.9 - ...plate/Container/Format.pm 100.0 n/a n/a 100.0 0.0 0.7 96.6 - ...plate/Container/Hidden.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 - ...plate/Container/Italic.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 - ...plate/Container/Locked.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 - ...emplate/Container/Loop.pm 90.9 50.0 50.0 100.0 50.0 0.5 80.4 - ...late/Container/Outline.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 - ...Template/Container/Row.pm 100.0 75.0 n/a 100.0 50.0 0.3 93.8 - ...mplate/Container/Scope.pm 100.0 n/a n/a 100.0 n/a 0.1 100.0 - ...plate/Container/Shadow.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 - ...te/Container/Strikeout.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 - ...ate/Container/Workbook.pm 100.0 n/a n/a 100.0 n/a 0.9 100.0 - ...te/Container/Worksheet.pm 94.1 50.0 n/a 100.0 0.0 0.9 88.0 - ...Excel/Template/Context.pm 84.3 53.4 54.2 100.0 92.3 19.5 76.0 - ...Excel/Template/Element.pm 100.0 n/a n/a 100.0 n/a 0.5 100.0 - ...mplate/Element/Backref.pm 100.0 50.0 33.3 100.0 0.0 0.1 87.1 - .../Template/Element/Cell.pm 95.8 65.0 80.0 100.0 66.7 3.8 86.9 - ...mplate/Element/Formula.pm 100.0 n/a n/a 100.0 0.0 0.3 94.1 - ...Template/Element/Range.pm 100.0 66.7 n/a 100.0 66.7 0.2 93.3 - ...l/Template/Element/Var.pm 100.0 n/a n/a 100.0 0.0 0.1 94.1 - ...Excel/Template/Factory.pm 100.0 73.1 n/a 100.0 100.0 16.3 92.6 - .../Excel/Template/Format.pm 98.3 81.2 33.3 100.0 100.0 10.0 93.2 - ...xcel/Template/Iterator.pm 98.6 80.0 70.6 100.0 83.3 1.9 90.3 - ...el/Template/TextObject.pm 92.9 62.5 33.3 100.0 50.0 3.1 83.0 - Total 92.0 63.5 58.3 97.5 98.5 100.0 86.0 - ----------------------------------------------------------------------- + ---------------------------- ------ ------ ------ ------ ------ ------ ------ + File stmt branch cond sub pod time total + ---------------------------- ------ ------ ------ ------ ------ ------ ------ + blib/lib/Excel/Template.pm 96.0 64.3 58.8 100.0 100.0 26.3 85.8 + ...ib/Excel/Template/Base.pm 94.4 50.0 n/a 100.0 75.0 6.6 90.0 + ...cel/Template/Container.pm 100.0 50.0 33.3 100.0 66.7 4.5 88.1 + ...emplate/Container/Bold.pm 100.0 n/a n/a 100.0 0.0 0.2 95.0 + .../Container/Conditional.pm 95.9 90.0 66.7 100.0 0.0 1.7 91.0 + ...plate/Container/Format.pm 100.0 n/a n/a 100.0 0.0 0.6 96.6 + ...plate/Container/Hidden.pm 100.0 n/a n/a 100.0 0.0 0.2 95.0 + ...plate/Container/Italic.pm 100.0 n/a n/a 100.0 0.0 0.2 95.0 + ...ainer/KeepLeadingZeros.pm 100.0 100.0 n/a 100.0 0.0 0.1 96.3 + ...plate/Container/Locked.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 + ...emplate/Container/Loop.pm 96.8 50.0 50.0 100.0 50.0 0.4 84.6 + ...late/Container/Outline.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 + ...Template/Container/Row.pm 100.0 75.0 n/a 100.0 50.0 0.3 93.8 + ...mplate/Container/Scope.pm 100.0 n/a n/a 100.0 n/a 0.1 100.0 + ...plate/Container/Shadow.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 + ...te/Container/Strikeout.pm 100.0 n/a n/a 100.0 0.0 0.1 95.0 + ...ate/Container/Workbook.pm 100.0 n/a n/a 100.0 n/a 1.0 100.0 + ...te/Container/Worksheet.pm 94.7 75.0 n/a 100.0 50.0 1.0 90.3 + ...Excel/Template/Context.pm 98.0 80.0 75.0 100.0 93.3 23.4 91.8 + ...Excel/Template/Element.pm 100.0 n/a n/a 100.0 n/a 0.3 100.0 + ...mplate/Element/Backref.pm 100.0 50.0 33.3 100.0 0.0 0.2 87.1 + .../Template/Element/Cell.pm 97.9 75.0 80.0 100.0 66.7 3.9 91.1 + ...mplate/Element/Formula.pm 100.0 n/a n/a 100.0 0.0 0.2 94.1 + ...Template/Element/Range.pm 100.0 66.7 n/a 100.0 66.7 0.2 93.3 + ...l/Template/Element/Var.pm 100.0 n/a n/a 100.0 0.0 0.2 94.1 + ...Excel/Template/Factory.pm 100.0 75.0 n/a 100.0 100.0 15.5 92.4 + .../Excel/Template/Format.pm 98.4 75.0 33.3 100.0 66.7 8.1 91.2 + ...xcel/Template/Iterator.pm 98.6 80.0 70.6 100.0 83.3 1.4 90.3 + ...el/Template/TextObject.pm 92.9 62.5 33.3 100.0 50.0 3.2 83.0 + Total 98.0 75.2 63.4 100.0 98.5 100.0 92.2 + ---------------------------- ------ ------ ------ ------ ------ ------ ------ =head1 COPYRIGHT diff --git a/lib/Excel/Template/Container/Format.pm b/lib/Excel/Template/Container/Format.pm index 6548b28..9e7da80 100644 --- a/lib/Excel/Template/Container/Format.pm +++ b/lib/Excel/Template/Container/Format.pm @@ -17,7 +17,7 @@ sub render my ($context) = @_; my $old_format = $context->active_format; - my $format = Excel::Template::Format->copy( + my $format = $context->format_object->copy( $context, $old_format, %{$self}, diff --git a/lib/Excel/Template/Container/Loop.pm b/lib/Excel/Template/Container/Loop.pm index ad22f67..6848c70 100644 --- a/lib/Excel/Template/Container/Loop.pm +++ b/lib/Excel/Template/Container/Loop.pm @@ -55,11 +55,18 @@ sub render { $iterator->next; - unless ($self->iterate_over_children($context)) - { - $iterator->back_up; - last; - } + $self->iterate_over_children($context); + + # It doesn't seem that iterate_over_children() can ever fail, because + # I'm not sure that render() can return false. In PDF::Template, where + # this module got most of its code, render() can certainly return false, + # in the case of page-breaks. I left the code in because it didn't seem + # like it would hurt. + #unless ($self->iterate_over_children($context)) + #{ + # $iterator->back_up; + # last; + #} } $iterator->exit_scope; @@ -69,6 +76,8 @@ sub render return 1; } +# These methods are used in PDF::Template to calculate pagebreaks. I'm not sure +# if they will ever be needed in Excel::Template. #sub total_of #{ # my $self = shift; diff --git a/lib/Excel/Template/Context.pm b/lib/Excel/Template/Context.pm index 4584e3b..c9686b1 100644 --- a/lib/Excel/Template/Context.pm +++ b/lib/Excel/Template/Context.pm @@ -12,7 +12,7 @@ BEGIN { use Excel::Template::Format; # This is a helper object. It is not instantiated by the user, nor does it -# represent an XML object. Rather, every container will use this object to +# represent an XML node. Rather, every container will use this object to # maintain the context for its children. my %isAbsolute = map { $_ => ~~1 } qw( @@ -26,7 +26,8 @@ sub new my $self = $class->SUPER::new(@_); $self->{ACTIVE_WORKSHEET} = undef; - $self->{ACTIVE_FORMAT} = Excel::Template::Format->blank_format($self); + $self->{FORMAT_OBJECT} = Excel::Template::Format->new; + $self->{ACTIVE_FORMAT} = $self->{FORMAT_OBJECT}->blank_format($self); $self->{WORKSHEET_NAMES} = undef; $self->{__MARKS} = {}; @@ -122,7 +123,7 @@ sub resolve return $prev_val unless defined $obj_val; # Prevent divide-by-zero issues. - return $val if $op eq '/' and $val == 0; + return $prev_val if $op eq '/' and $val == 0; my $new_val; for ($op) @@ -300,6 +301,8 @@ sub get_last_reference return @{ $self->{REFERENCES}{$ref}[-1] }; } +sub format_object { $_[0]{FORMAT_OBJECT} } + 1; __END__ @@ -345,6 +348,8 @@ None =head2 add_reference +=head2 format_object + =head2 get =head2 get_all_references diff --git a/lib/Excel/Template/Format.pm b/lib/Excel/Template/Format.pm index c164d0a..fe18f21 100644 --- a/lib/Excel/Template/Format.pm +++ b/lib/Excel/Template/Format.pm @@ -9,27 +9,27 @@ use strict; # places in the file. This provides a way of keeping track of already-allocated # formats and making new formats based on old ones. -{ - my %_Formats; - - sub _assign { $_Formats{$_[0]} = $_[1]; $_Formats{$_[1]} = $_[0] } -# my $key = shift; -# my $format = shift; -# $_Formats{$key} = $format; -# $_Formats{$format} = $key; -# } - - sub _retrieve_key { $_Formats{ $_[0] } } -# my $format = shift; -# return $_Formats{$format}; -# } - - *_retrieve_format = \&_retrieve_key; -# sub _retrieve_format { -# my $key = shift; -# return $_Formats{$key}; -# } -} +sub new { bless {}, shift } + +sub _assign { $_[0]{$_[1]} = $_[2]; $_[0]{$_[2]} = $_[1] } +# my $self = shift; +# my ($key, $format) = @_; +# $self->{$key} = $format; +# $self->{$format} = $key; +#} + +sub _retrieve_key { $_[0]{ $_[1] } } +# my $self = shift; +# my ($format) = @_; +# return $self->{$format}; +#} + +*_retrieve_format = \&_retrieve_key; +#sub _retrieve_format { +# my $self = shift; +# my ($key) = @_; +# return $self->{$key}; +#} { my @_boolean_formats = qw( @@ -53,7 +53,7 @@ use strict; $params{lc $_} = delete $params{$_} for keys %params; my @parts = ( - (map { !! $params{$_} } @_boolean_formats), + (map { !!$params{$_} } @_boolean_formats), (map { $params{$_} ? $params{$_} + 0 : '' } @_integer_formats), (map { $params{$_} || '' } @_string_formats), ); @@ -86,13 +86,13 @@ use strict; sub copy { - shift; + my $self = shift; my ($context, $old_fmt, %properties) = @_; # This is a key used for non-format book-keeping. delete $properties{ ELEMENTS }; - defined(my $key = _retrieve_key($old_fmt)) + defined(my $key = _retrieve_key($self, $old_fmt)) || die "Internal Error: Cannot find key for format '$old_fmt'!\n"; my %params = _key_to_params($key); @@ -120,27 +120,27 @@ use strict; my $new_key = _params_to_key(%params); - my $format = _retrieve_format($new_key); + my $format = _retrieve_format($self, $new_key); return $format if $format; $format = $context->{XLS}->add_format(%params); - _assign($new_key, $format); + _assign($self, $new_key, $format); return $format; } } sub blank_format { - shift; + my $self = shift; my ($context) = @_; my $blank_key = _params_to_key(); - my $format = _retrieve_format($blank_key); + my $format = _retrieve_format($self, $blank_key); return $format if $format; $format = $context->{XLS}->add_format; - _assign($blank_key, $format); + _assign($self, $blank_key, $format); return $format; } diff --git a/t/016_renderers.t b/t/016_renderers.t index bfca8c8..a8754f1 100644 --- a/t/016_renderers.t +++ b/t/016_renderers.t @@ -39,6 +39,7 @@ __END_EXPECTED__ my @calls = mock::get_calls; is( join( $/, @calls, '' ), <<__END_EXPECTED__, 'Calls match up' ); Spreadsheet::WriteExcelXML::new( 'filename' ) +Spreadsheet::WriteExcelXML::add_format( '' ) Spreadsheet::WriteExcelXML::close( '' ) __END_EXPECTED__ } @@ -56,6 +57,7 @@ __END_EXPECTED__ my @calls = mock::get_calls; is( join( $/, @calls, '' ), <<__END_EXPECTED__, 'Calls match up' ); Spreadsheet::WriteExcel::new( 'filename' ) +Spreadsheet::WriteExcel::add_format( '' ) Spreadsheet::WriteExcel::close( '' ) __END_EXPECTED__ } diff --git a/t/023_relative_values.t b/t/023_relative_values.t new file mode 100644 index 0000000..f5a9030 --- /dev/null +++ b/t/023_relative_values.t @@ -0,0 +1,64 @@ +BEGIN{ $^W = 0 } +use strict; + +use Test::More tests => 4; + +use lib 't'; +use mock; +mock::reset; + +my $CLASS = 'Excel::Template'; +use_ok( $CLASS ); + +my $object = $CLASS->new( + file => \*DATA, +); +isa_ok( $object, $CLASS ); + +ok( $object->write_file( 'filename' ), 'Successfuly wrote file' ); + +my @calls = mock::get_calls; +is( join( $/, @calls, '' ), <<__END_EXPECTED__, 'Calls match up' ); +Spreadsheet::WriteExcel::new( 'filename' ) +Spreadsheet::WriteExcel::add_format( '' ) +Spreadsheet::WriteExcel::add_worksheet( '' ) +Spreadsheet::WriteExcel::Worksheet::new( '' ) +Spreadsheet::WriteExcel::Worksheet::set_row( '0', '8' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '0', '', '1' ) +Spreadsheet::WriteExcel::Worksheet::set_row( '1', '10' ) +Spreadsheet::WriteExcel::Worksheet::write( '1', '0', '', '1' ) +Spreadsheet::WriteExcel::Worksheet::set_row( '2', '6' ) +Spreadsheet::WriteExcel::Worksheet::write( '2', '0', '', '1' ) +Spreadsheet::WriteExcel::Worksheet::set_row( '3', '16' ) +Spreadsheet::WriteExcel::Worksheet::write( '3', '0', '', '1' ) +Spreadsheet::WriteExcel::Worksheet::set_row( '4', '4' ) +Spreadsheet::WriteExcel::Worksheet::write( '4', '0', '', '1' ) +Spreadsheet::WriteExcel::Worksheet::set_row( '5', '8' ) +Spreadsheet::WriteExcel::Worksheet::write( '5', '0', '', '1' ) +Spreadsheet::WriteExcel::close( '' ) +__END_EXPECTED__ + +__DATA__ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/t/Register_018.pm b/t/Register_018.pm index 241b95b..b96db0e 100644 --- a/t/Register_018.pm +++ b/t/Register_018.pm @@ -7,7 +7,7 @@ sub render my ($self, $context) = @_; my $old_format = $context->active_format; - my $format = Excel::Template::Format->copy( + my $format = $context->format_object->copy( $context, $old_format, align => 'center', bold => 1,