From: Rob Kinyon Date: Fri, 4 Mar 2005 20:56:53 +0000 (+0000) Subject: Added more tests for conditionals. Need to fix linking in POD. - Intermediate commit! X-Git-Tag: v0.24~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8fd01531ddd80d30451d269f14c3a7964e100e31;p=p5sagit%2FExcel-Template.git Added more tests for conditionals. Need to fix linking in POD. - Intermediate commit! --- diff --git a/Changes b/Changes index 35f1f4a..61ee02e 100644 --- a/Changes +++ b/Changes @@ -1,7 +1,11 @@ Revision history for Perl distribution Excel::Template 0.24 ??? Mar 0? ??: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 + - Improved POD linking + - Every module/node reference in POD should link to the appropriate POD 0.23 Fri Feb 25 15:00:00 2005 - Improved code coverage with more and better tests diff --git a/MANIFEST b/MANIFEST index 79cfacb..ec69f69 100644 --- a/MANIFEST +++ b/MANIFEST @@ -18,6 +18,7 @@ lib/Excel/Template/Container/Conditional.pm lib/Excel/Template/Container/Format.pm lib/Excel/Template/Container/Hidden.pm lib/Excel/Template/Container/Italic.pm +lib/Excel/Template/Container/KeepLeadingZeros.pm lib/Excel/Template/Container/Loop.pm lib/Excel/Template/Container/Locked.pm lib/Excel/Template/Container/Outline.pm @@ -67,6 +68,8 @@ t/017_filehandle.t t/018_register.t t/019_output.t t/020_worksheet_attributes.t +t/021_loop_error.t +t/022_keep_leading_zeros.t t/998_pod.t t/999_pod_coverage.t t/mock.pm diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP index b0baad5..af11f38 100644 --- a/MANIFEST.SKIP +++ b/MANIFEST.SKIP @@ -21,7 +21,6 @@ # Devel::Cover ^cover_db/? ^tags? -.err? # developer-only tests -^dev_.*\.t$ +^t/dev_.*\.t$ diff --git a/lib/Excel/Template/Container/Conditional.pm b/lib/Excel/Template/Container/Conditional.pm index ccdb88f..0717e6c 100644 --- a/lib/Excel/Template/Container/Conditional.pm +++ b/lib/Excel/Template/Container/Conditional.pm @@ -53,7 +53,7 @@ sub _conditional_passes /^ge$/ && do { $res = ($val ge $value); last }; /^le$/ && do { $res = ($val le $value); last }; - die "Unknown operator in conditional resolve", $/; + die "Unknown operator '$op' in conditional resolve", $/; } return $res && 1; @@ -124,7 +124,7 @@ IF =head1 INHERITANCE -Excel::Template::Container +L =head1 ATTRIBUTES @@ -132,24 +132,21 @@ Excel::Template::Container =item * NAME -This is the name of the parameter to be testing. It is resolved like any other -parameter. +This is the name of the parameter to test. It is resolved like any other parameter name. (q.v. L for more info.) =item * VALUE -If VALUE is set, then a comparison operation is done. The value of NAME is -compared to VALUE using the value of OP. +If VALUE is set, then a comparison operation is done. The value of NAME is compared to VALUE using the value of OP. =item * OP -If VALUE is set, then this is checked. If it isn't present, then '==' (numeric -equality) is assumed. OP must be one of the numeric comparison operators or the -string comparison operators. All 6 of each kind is supported. +If VALUE is set, then this is checked. If it isn't present, then '==' (numeric equality) is assumed. OP must be one of Perl the numeric comparison operators or the string comparison operators. All 6 of each kind is supported. + +B: If you want to use < or <=, you must instead use < or <=. This is to make sure it will parse with L. You should not need to use > or >= instead of > and >=, respectively. =item * IS -If VALUE is not set, then IS is checked. IS is allowed to be either "TRUE" or -"FALSE". The boolean value of NAME is checked against IS. +If VALUE is not set, then IS is checked. IS is allowed to be either "TRUE" or "FALSE". The boolean value of NAME is checked against IS. =back @@ -171,8 +168,7 @@ None ... Children here -In the above example, the children will be executed if the value of __ODD__ -(which is set by the LOOP node) is false. So, for all even iterations. +In the above example, the children will be executed if the value of __ODD__ (which is set by the L node) is false. So, for all even iterations. =head1 AUTHOR @@ -180,6 +176,6 @@ Rob Kinyon (rob.kinyon@gmail.com) =head1 SEE ALSO -LOOP +L, L =cut diff --git a/lib/Excel/Template/Container/KeepLeadingZeros.pm b/lib/Excel/Template/Container/KeepLeadingZeros.pm new file mode 100644 index 0000000..7c5edad --- /dev/null +++ b/lib/Excel/Template/Container/KeepLeadingZeros.pm @@ -0,0 +1,94 @@ +package Excel::Template::Container::KeepLeadingZeros; + +use strict; + +BEGIN { + use vars qw(@ISA); + @ISA = qw(Excel::Template::Container); + + use Excel::Template::Container; +} + +sub render +{ + my $self = shift; + my ($context) = @_; + + my $worksheet = $context->active_worksheet; + + $worksheet + ? $worksheet->keep_leading_zeros( 1 ) + : $context->mark( keep_leading_zeros => 1 ); + + my $rv = $self->SUPER::render($context); + + $worksheet + ? $worksheet->keep_leading_zeros( 0 ) + : $context->mark( keep_leading_zeros => 0 ); + + return $rv; +} + +1; +__END__ + +=head1 NAME + +Excel::Template::Container::KeepLeadingZeros - Excel::Template::Container::KeepLeadingZeros + +=head1 PURPOSE + +To set the keep_leading_zeros flag for the surrounding worksheet or any worksheets that might be contained within this node. + +=head1 NODE NAME + +KEEP_LEADING_ZEROS + +=head1 INHERITANCE + +L + +=head1 ATTRIBUTES + +None + +=head1 CHILDREN + +None + +=head1 EFFECTS + +Alters how leading zeros are interpreted by L. + +=head1 DEPENDENCIES + +None + +=head1 USAGE + + + ... Cells here will NOT have leading-zeros preserved + + ... Cells here will have leading-zeros preserved + + ... Cells here will NOT have leading-zeros preserved + + + + + ... Cells here will have leading-zeros preserved + + + ... Cells here will have leading-zeros preserved + + + +=head1 AUTHOR + +Rob Kinyon (rob.kinyon@gmail.com) + +=head1 SEE ALSO + +L, L + +=cut diff --git a/lib/Excel/Template/Container/Worksheet.pm b/lib/Excel/Template/Container/Worksheet.pm index cade828..378915b 100644 --- a/lib/Excel/Template/Container/Worksheet.pm +++ b/lib/Excel/Template/Container/Worksheet.pm @@ -9,24 +9,23 @@ BEGIN { use Excel::Template::Container; } +sub exit_scope { $_[1]->active_worksheet( undef ) } + sub render { my $self = shift; my ($context) = @_; - $context->new_worksheet( $self ); + my $worksheet = $context->new_worksheet( $self ); my $password = $context->get( $self, 'PROTECT' ); if (defined $password) { - $context->active_worksheet->protect( $password ); + $worksheet->protect( $password ); } - my $keep_zeros = $context->get( $self, 'KEEP_LEADING_ZEROS' ); - if ( defined $keep_zeros ) - { - $context->active_worksheet->keep_leading_zeros( $keep_zeros ? 1 : 0 ); - } + $worksheet->keep_leading_zeros( 1 ) + if $context->mark( 'keep_leading_zeros' ); return $self->SUPER::render($context); } diff --git a/lib/Excel/Template/Context.pm b/lib/Excel/Template/Context.pm index 2c72a91..4584e3b 100644 --- a/lib/Excel/Template/Context.pm +++ b/lib/Excel/Template/Context.pm @@ -29,6 +29,8 @@ sub new $self->{ACTIVE_FORMAT} = Excel::Template::Format->blank_format($self); $self->{WORKSHEET_NAMES} = undef; + $self->{__MARKS} = {}; + # Removed NAME_MAP until I figure out what the heck it's for for (qw( STACK PARAM_MAP )) { @@ -107,7 +109,7 @@ sub resolve # 2) A decimal number #GGG Convert this to use //x - my ($op, $val) = $obj_val =~ m!^\s*([\+\*\/\-])?\s*([\d.]*\d)\s*$!oi; + my ($op, $val) = $obj_val =~ m/^\s*([\+\*\/\-])?\s*([\d.]*\d)\s*$/oi; # Unless it's a relative value, we have what we came for. return $obj_val unless $op; @@ -237,11 +239,25 @@ sub new_worksheet $name = ''; } - $self->active_worksheet( + return $self->active_worksheet( $self->{XLS}->add_worksheet( $name ), ); } +sub mark +{ + my $self = shift; + + if ( @_ > 1 ) + { + my %args = @_; + + @{$self->{__MARKS}}{keys %args} = values %args; + } + + return $self->{__MARKS}{$_[0]} +} + sub active_worksheet { my $self = shift; @@ -335,7 +351,7 @@ None =head2 get_last_reference -=head2 named_param +=head2 mark =head2 new_worksheet diff --git a/lib/Excel/Template/Element/Cell.pm b/lib/Excel/Template/Element/Cell.pm index 08aece9..3e5bb3c 100755 --- a/lib/Excel/Template/Element/Cell.pm +++ b/lib/Excel/Template/Element/Cell.pm @@ -13,9 +13,9 @@ sub new { my $class = shift; my $self = $class->SUPER::new(@_); - + $self->{TXTOBJ} = Excel::Template::Factory->_create('TEXTOBJECT'); - + return $self; } @@ -23,7 +23,7 @@ sub _get_text { my $self = shift; my ($context) = @_; - + my $txt = $context->get($self, 'TEXT'); if (defined $txt) { @@ -31,17 +31,11 @@ sub _get_text push @{$txt_obj->{STACK}}, $txt; $txt = $txt_obj->resolve($context); } - elsif ($self->{TXTOBJ}) - { - $txt = $self->{TXTOBJ}->resolve($context) - } else { - $txt = $context->use_unicode - ? Unicode::String::utf8('') - : ''; + $txt = $self->{TXTOBJ}->resolve($context) } - + return $txt; } @@ -81,10 +75,10 @@ sub render my ($row, $col) = map { $context->get($self, $_) } qw(ROW COL); - my $ref = uc $context->get( $self, 'REF' ); + my $ref = $context->get( $self, 'REF' ); if (defined $ref && length $ref) { - $context->add_reference( $ref, $row, $col ); + $context->add_reference( uc( $ref ), $row, $col ); } # Apply the cell width to the current column @@ -96,7 +90,7 @@ sub render { $context->active_worksheet->set_column($col, $col, $width); } - } + } $context->active_worksheet->$method( $row, $col, @@ -131,7 +125,7 @@ CELL =head1 INHERITANCE -Excel::Template::Element +L =head1 ATTRIBUTES @@ -139,20 +133,15 @@ Excel::Template::Element =item * TEXT -This is the text to write to the cell. This can either be text or a parameter -with a dollar-sign in front of the parameter name. +This is the text to write to the cell. This can either be text or a parameter with a dollar-sign in front of the parameter name. =item * COL -Optionally, you can specify which column you want this cell to be in. It can be -either a number (zero-based) or an offset. See Excel::Template for more info on -offset-based numbering. +Optionally, you can specify which column you want this cell to be in. It can be either a number (zero-based) or an offset. See L for more info on offset-based numbering. =item * REF -Adds the current cell to the a list of cells that can be backreferenced. -This is useful when the current cell needs to be referenced by a -formula. See BACKREF and RANGE. +Adds the current cell to the a list of cells that can be backreferenced. This is useful when the current cell needs to be referenced by a formula. See L and L. =item * WIDTH @@ -161,12 +150,9 @@ will win out. =item * TYPE -This allows you to specify what write_*() method will be used. The default is to -call write() and let S::WE make the right call. However, you may wish to -override it. Excel::Template will not do any form of validation on what you -provide. You are assumed to know what you're doing. +This allows you to specify what write_*() method will be used. The default is to call write() and let L make the right call. However, you may wish to override it. L will not do any form of validation on what you provide. You are assumed to know what you're doing. -The legal types are: +The legal types (taken from L) are: =over 4 @@ -188,11 +174,11 @@ q.v. L for more info. =head1 CHILDREN -Excel::Template::Element::Formula +L =head1 EFFECTS -This will consume one column on the current row. +This will consume one column in the current row. =head1 DEPENDENCIES @@ -206,13 +192,9 @@ None Some text here -In the above example, four cells are written out. The first two have text hard- -coded. The second two have variables. The third and fourth items have another -thing that should be noted. If you have text where you want a variable in the -middle, you have to use the latter form. Variables within parameters are the -entire parameter's value. +In the above example, four cells are written out. The first two have text hard-coded. The second two have variables. The third and fourth items have another thing that should be noted. If you have text where you want a variable in the middle, you have to use the latter form. Variables within parameters are the entire parameter's value. -Please see Spreadsheet::WriteExcel for what constitutes a legal formula. +Please see L for what constitutes a legal formula. =head1 AUTHOR @@ -220,6 +202,6 @@ Rob Kinyon (rob.kinyon@gmail.com) =head1 SEE ALSO -ROW, VAR, FORMULA +L, L, L =cut diff --git a/lib/Excel/Template/Factory.pm b/lib/Excel/Template/Factory.pm index 52fe723..98187b2 100644 --- a/lib/Excel/Template/Factory.pm +++ b/lib/Excel/Template/Factory.pm @@ -30,6 +30,8 @@ my %Manifest = ( 'SHADOW' => 'Excel::Template::Container::Shadow', 'STRIKEOUT' => 'Excel::Template::Container::Strikeout', + 'KEEP_LEADING_ZEROS' => 'Excel::Template::Container::KeepLeadingZeros', + # These are the helper objects # They are also in here to make E::T::Factory::isa() work. 'CONTEXT' => 'Excel::Template::Context', @@ -45,7 +47,7 @@ my %Manifest = ( my %isBuildable = map { $_ => ~~1 } qw( WORKBOOK WORKSHEET FORMAT BOLD HIDDEN ITALIC LOCKED OUTLINE SHADOW STRIKEOUT - IF ROW LOOP SCOPE + IF ROW LOOP SCOPE KEEP_LEADING_ZEROS CELL FORMULA VAR BACKREF RANGE ); diff --git a/t/011.xml b/t/011.xml index fea37f7..d2c6cea 100644 --- a/t/011.xml +++ b/t/011.xml @@ -3,16 +3,46 @@ - + - + - + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + diff --git a/t/011_conditional.t b/t/011_conditional.t index 19b3d0c..093e332 100644 --- a/t/011_conditional.t +++ b/t/011_conditional.t @@ -19,8 +19,8 @@ ok( loopy => [ { int => 0, char => 'n' }, { int => 0, char => 'y' }, - { int => 1, char => 'n' }, - { int => 1, char => 'y' }, + { int => 1, char => 'z' }, + { int => -1, char => 'y' }, ], ), 'Parameters set', @@ -34,13 +34,33 @@ Spreadsheet::WriteExcel::new( 'filename' ) Spreadsheet::WriteExcel::add_format( '' ) Spreadsheet::WriteExcel::add_worksheet( 'conditional' ) Spreadsheet::WriteExcel::Worksheet::new( '' ) -Spreadsheet::WriteExcel::Worksheet::write( '0', '0', 'not bool', '1' ) -Spreadsheet::WriteExcel::Worksheet::write( '0', '1', 'int', '1' ) -Spreadsheet::WriteExcel::Worksheet::write( '1', '0', 'not bool', '1' ) -Spreadsheet::WriteExcel::Worksheet::write( '1', '1', 'int', '1' ) -Spreadsheet::WriteExcel::Worksheet::write( '1', '2', 'char', '1' ) -Spreadsheet::WriteExcel::Worksheet::write( '2', '0', 'bool', '1' ) -Spreadsheet::WriteExcel::Worksheet::write( '3', '0', 'bool', '1' ) -Spreadsheet::WriteExcel::Worksheet::write( '3', '1', 'char', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '0', 'bool false', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '1', 'num == passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '2', 'num >= passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '3', 'num <= passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '4', 'char ne passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '5', 'char lt passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '6', 'char le passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '1', '0', 'bool false', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '1', '1', 'num == passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '1', '2', 'num >= passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '1', '3', 'num <= passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '1', '4', 'char eq passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '1', '5', 'char ge passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '1', '6', 'char le passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '2', '0', 'bool true', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '2', '1', 'num != passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '2', '2', 'num > passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '2', '3', 'num >= passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '2', '4', 'char ne passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '2', '5', 'char gt passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '2', '6', 'char ge passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '3', '0', 'bool true', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '3', '1', 'num != passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '3', '2', 'num < passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '3', '3', 'num <= passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '3', '4', 'char eq passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '3', '5', 'char ge passes', '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '3', '6', 'char le passes', '1' ) Spreadsheet::WriteExcel::close( '' ) __END_EXPECTED__ diff --git a/t/020_worksheet_attributes.t b/t/020_worksheet_attributes.t index 073454f..3e28278 100644 --- a/t/020_worksheet_attributes.t +++ b/t/020_worksheet_attributes.t @@ -22,14 +22,13 @@ Spreadsheet::WriteExcel::new( 'filename' ) Spreadsheet::WriteExcel::add_format( '' ) Spreadsheet::WriteExcel::add_worksheet( 'worksheet attributes' ) Spreadsheet::WriteExcel::Worksheet::new( '' ) -Spreadsheet::WriteExcel::Worksheet::keep_leading_zeros( '1' ) Spreadsheet::WriteExcel::Worksheet::write( '0', '0', '03', '1' ) Spreadsheet::WriteExcel::close( '' ) __END_EXPECTED__ __DATA__ - + diff --git a/t/022_keep_leading_zeros.t b/t/022_keep_leading_zeros.t new file mode 100644 index 0000000..97fcce1 --- /dev/null +++ b/t/022_keep_leading_zeros.t @@ -0,0 +1,59 @@ +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::write( '0', '0', 'before', '1' ) +Spreadsheet::WriteExcel::Worksheet::keep_leading_zeros( '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '1', 'inside', '1' ) +Spreadsheet::WriteExcel::Worksheet::keep_leading_zeros( '0' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '2', 'after', '1' ) +Spreadsheet::WriteExcel::add_worksheet( '' ) +Spreadsheet::WriteExcel::Worksheet::new( '' ) +Spreadsheet::WriteExcel::Worksheet::keep_leading_zeros( '1' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '0', 'within', '1' ) +Spreadsheet::WriteExcel::add_worksheet( '' ) +Spreadsheet::WriteExcel::Worksheet::new( '' ) +Spreadsheet::WriteExcel::Worksheet::write( '0', '0', 'after', '1' ) +Spreadsheet::WriteExcel::close( '' ) +__END_EXPECTED__ + +__DATA__ + + + + + + + + + + + + + + + + + +