X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=urisagit%2FTemplate-Simple.git;a=blobdiff_plain;f=t%2Fcommon.pm;h=b730f694cb1732b32e6ca80619dbc350353c349f;hp=d61d67c48ca442bb8ea40d2b84e6cf5a8af0a3d4;hb=f5c8badf9cff80771975450c60d32c987c7f6af9;hpb=f12977deb12d8e9e3f2555f77905ba78ed609046 diff --git a/t/common.pm b/t/common.pm index d61d67c..b730f69 100644 --- a/t/common.pm +++ b/t/common.pm @@ -3,89 +3,102 @@ use Test::More ; use Template::Simple ; +use Data::Dumper ; + sub template_tester { my( $tests ) = @_ ; # plan for one expected ok() call per test - plan( tests => scalar @{$tests} ) ; + plan( tests => scalar @{$tests} * 2 ) ; my( $obj, $tmpl ) ; # loop over all the tests - foreach my $test ( @{$tests} ) { + foreach my $compiled ( 0, 1 ) { - if ( $test->{skip} ) { - ok( 1, "SKIPPING $test->{name}" ) ; - next ; - } + foreach my $test ( @{$tests} ) { - unless( $obj && $test->{keep_obj} ) { + $test->{name} .= ' - compiled' if $compiled ; + + if ( $test->{skip} ) { + ok( 1, "SKIPPING $test->{name}" ) ; + next ; + } + + if ( $test->{compile_skip} && $compiled ) { + ok( 1, "SKIPPING $test->{name}" ) ; + next ; + } + + unless( $obj && $test->{keep_obj} ) { # if there is no kept object, we will constuct one - $obj = eval { - Template::Simple->new( - %{ $test->{opts} || {} } - ) ; - } ; + $obj = eval { + Template::Simple->new( + %{ $test->{opts} || {} } + ) ; + } ; print $@ if $@ ; # check for expected errors # no errors in new() to catch (yet) - } + } - $test->{obj} = $obj ; + $test->{obj} = $obj ; # see if we use the test's template or keep the previous one - $tmpl = $test->{template} if defined $test->{template} ; + $tmpl = $test->{template} if defined $test->{template} ; +#print "TMPL [$tmpl]\n" ; # run any setup sub before this test. this can is used to modify the # object for this test (e.g. delete templates from the cache). - if( my $pretest = $test->{pretest} ) { + if( my $pretest = $test->{pretest} ) { + $pretest->($test) ; + } - $pretest->($test) ; - } +#print Dumper $obj if $test->{dump} ; -# get any existing template object + if ( $compiled ) { -# render the template and catch any fatal errors + my $tmpl_name = 'test' ; + $obj->add_templates( {$tmpl_name => $tmpl} ) ; + eval { $obj->compile( $tmpl_name ) } ; +#print "ERR $@\n" ; + + next if check_error( $test ) ; + } - my $rendered = eval { - $obj->render( $tmpl, $test->{data} ) ; - } ; +# get any existing template object +# render the template and catch any fatal errors -#print "ERR $@\n" if $@; + my $rendered = eval { + $obj->render( $tmpl, $test->{data} ) ; + } ; # if we had an error and expected it, we pass this test - if ( $@ ) { - - if ( $test->{error} && $@ =~ /$test->{error}/ ) { + next if check_error( $test ) ; - ok( 1, $test->{name} ) ; + if( my $posttest = $test->{posttest} ) { + $posttest->($test) ; } - else { - print "unexpected error: $@\n" ; - ok( 0, $test->{name} ) ; - } - next ; - } # see if the expansion was what we expected - my $ok = ${$rendered} eq $test->{expected} ; + my $ok = ${$rendered} eq $test->{expected} ; -# dump any bad expansions +# dump any bad renderings - print <{name} ) ; + ok( $ok, $test->{name} ) ; + } } } +sub check_error { + + my( $test ) = @_ ; + + if ( $@ ) { +#print "ERR $@\n" ; + if ( $test->{error} && $@ =~ /$test->{error}/ ) { + + ok( 1, $test->{name} ) ; + } + else { + + print "unexpected error: $@\n" ; + ok( 0, $test->{name} ) ; + } + + return 1 ; + } + + return ; +} + 1 ;