token_re, include cleanup, git cleanup
[urisagit/Template-Simple.git] / t / common.pm
CommitLineData
e374d8da 1# common.pm - common test driver code
2
3use Test::More ;
4use Template::Simple ;
5
f5c8badf 6use Data::Dumper ;
7
e374d8da 8sub template_tester {
9
10 my( $tests ) = @_ ;
11
12# plan for one expected ok() call per test
13
f5c8badf 14 plan( tests => scalar @{$tests} * 2 ) ;
e374d8da 15
16 my( $obj, $tmpl ) ;
17
18# loop over all the tests
19
f5c8badf 20 foreach my $compiled ( 0, 1 ) {
e374d8da 21
f5c8badf 22 foreach my $test ( @{$tests} ) {
e374d8da 23
f5c8badf 24 $test->{name} .= ' - compiled' if $compiled ;
25
26 if ( $test->{skip} ) {
27 ok( 1, "SKIPPING $test->{name}" ) ;
28 next ;
29 }
30
31 if ( $test->{compile_skip} && $compiled ) {
32 ok( 1, "SKIPPING $test->{name}" ) ;
33 next ;
34 }
35
36 unless( $obj && $test->{keep_obj} ) {
e374d8da 37
38# if there is no kept object, we will constuct one
39
f5c8badf 40 $obj = eval {
41 Template::Simple->new(
42 %{ $test->{opts} || {} }
43 ) ;
44 } ;
e374d8da 45
46print $@ if $@ ;
47
48# check for expected errors
49# no errors in new() to catch (yet)
50
f5c8badf 51 }
e374d8da 52
f5c8badf 53 $test->{obj} = $obj ;
e374d8da 54
55# see if we use the test's template or keep the previous one
56
f5c8badf 57 $tmpl = $test->{template} if defined $test->{template} ;
e374d8da 58
f5c8badf 59#print "TMPL [$tmpl]\n" ;
e374d8da 60# run any setup sub before this test. this can is used to modify the
61# object for this test (e.g. delete templates from the cache).
62
f5c8badf 63 if( my $pretest = $test->{pretest} ) {
64 $pretest->($test) ;
65 }
e374d8da 66
f5c8badf 67#print Dumper $obj if $test->{dump} ;
e374d8da 68
f5c8badf 69 if ( $compiled ) {
e374d8da 70
f5c8badf 71 my $tmpl_name = 'test' ;
72 $obj->add_templates( {$tmpl_name => $tmpl} ) ;
73 eval { $obj->compile( $tmpl_name ) } ;
74#print "ERR $@\n" ;
75
76 next if check_error( $test ) ;
77 }
e374d8da 78
f5c8badf 79# get any existing template object
80# render the template and catch any fatal errors
e374d8da 81
f5c8badf 82 my $rendered = eval {
83 $obj->render( $tmpl, $test->{data} ) ;
84 } ;
e374d8da 85
86# if we had an error and expected it, we pass this test
87
f5c8badf 88 next if check_error( $test ) ;
e374d8da 89
f5c8badf 90 if( my $posttest = $test->{posttest} ) {
91 $posttest->($test) ;
e374d8da 92 }
e374d8da 93
e374d8da 94
95# see if the expansion was what we expected
96
f5c8badf 97 my $ok = ${$rendered} eq $test->{expected} ;
e374d8da 98
f5c8badf 99# dump any bad renderings
e374d8da 100
f5c8badf 101 print <<ERR unless $ok ;
e374d8da 102RENDERED
103[${$rendered}]
104EXPECTED
105[$test->{expected}]
106------
107ERR
108
109# report success/failure for this test
110
f5c8badf 111 ok( $ok, $test->{name} ) ;
112 }
e374d8da 113 }
114}
115
f5c8badf 116sub check_error {
117
118 my( $test ) = @_ ;
119
120 if ( $@ ) {
121#print "ERR $@\n" ;
122 if ( $test->{error} && $@ =~ /$test->{error}/ ) {
123
124 ok( 1, $test->{name} ) ;
125 }
126 else {
127
128 print "unexpected error: $@\n" ;
129 ok( 0, $test->{name} ) ;
130 }
131
132 return 1 ;
133 }
134
135 return ;
136}
137
e374d8da 1381 ;