token_re, include cleanup, git cleanup
[urisagit/Template-Simple.git] / t / common.pm
1 # common.pm - common test driver code
2
3 use Test::More ;
4 use Template::Simple ;
5
6 use Data::Dumper ;
7
8 sub template_tester {
9
10         my( $tests ) = @_ ;
11
12 # plan for one expected ok() call per test
13
14         plan( tests => scalar @{$tests} * 2 ) ;
15
16         my( $obj, $tmpl ) ;
17
18 # loop over all the tests
19
20         foreach my $compiled ( 0, 1 ) {
21
22                 foreach my $test ( @{$tests} ) {
23
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} ) {
37
38 # if there is no kept object, we will constuct one
39
40                                 $obj = eval {
41                                         Template::Simple->new(
42                                                 %{ $test->{opts} || {} }
43                                         ) ;
44                                 } ;
45
46 print $@ if $@ ;
47
48 # check for expected errors
49 # no errors in new() to catch (yet)
50
51                         }
52
53                         $test->{obj} = $obj ;
54
55 # see if we use the test's template or keep the previous one
56
57                         $tmpl = $test->{template} if defined $test->{template} ;
58
59 #print "TMPL [$tmpl]\n" ;
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
63                         if( my $pretest = $test->{pretest} ) {
64                                 $pretest->($test) ;
65                         }
66
67 #print Dumper $obj if $test->{dump} ;
68
69                         if ( $compiled ) {
70
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                         }
78
79 # get any existing template object
80 # render the template and catch any fatal errors
81
82                         my $rendered = eval {
83                                 $obj->render( $tmpl, $test->{data} ) ;
84                         } ;
85
86 # if we had an error and expected it, we pass this test
87
88                         next if check_error( $test ) ;
89
90                         if( my $posttest = $test->{posttest} ) {
91                                 $posttest->($test) ;
92                         }
93
94
95 # see if the expansion was what we expected
96
97                         my $ok = ${$rendered} eq $test->{expected} ;
98
99 # dump any bad renderings
100
101                         print <<ERR unless $ok ;
102 RENDERED
103 [${$rendered}]
104 EXPECTED
105 [$test->{expected}]
106 ------
107 ERR
108
109 # report success/failure for this test
110
111                         ok( $ok, $test->{name} ) ;
112                 }
113         }
114 }
115
116 sub 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
138 1 ;