d61d67c48ca442bb8ea40d2b84e6cf5a8af0a3d4
[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 sub template_tester {
7
8         my( $tests ) = @_ ;
9
10 # plan for one expected ok() call per test
11
12         plan( tests => scalar @{$tests} ) ;
13
14         my( $obj, $tmpl ) ;
15
16 # loop over all the tests
17
18         foreach my $test ( @{$tests} ) {
19
20                 if ( $test->{skip} ) {
21                         ok( 1, "SKIPPING $test->{name}" ) ;
22                         next ;
23                 }
24
25                 unless( $obj && $test->{keep_obj} ) {
26
27 # if there is no kept object, we will constuct one
28
29                         $obj = eval {
30                                 Template::Simple->new(
31                                         %{ $test->{opts} || {} }
32                                 ) ;
33                         } ;
34
35 print $@ if $@ ;
36
37 # check for expected errors
38 # no errors in new() to catch (yet)
39
40                 }
41
42                 $test->{obj} = $obj ;
43
44 # see if we use the test's template or keep the previous one
45
46                 $tmpl = $test->{template} if defined $test->{template} ;
47
48 # run any setup sub before this test. this can is used to modify the
49 # object for this test (e.g. delete templates from the cache).
50
51                 if( my $pretest = $test->{pretest} ) {
52
53                         $pretest->($test) ;
54                 }
55
56 # get any existing template object
57
58 # render the template and catch any fatal errors
59
60                 my $rendered = eval {
61                         $obj->render( $tmpl, $test->{data} ) ;
62                 } ;
63
64 #print "ERR $@\n" if $@;
65
66 # if we had an error and expected it, we pass this test
67
68                 if ( $@ ) {
69
70                         if ( $test->{error} && $@ =~ /$test->{error}/ ) {
71
72                                 ok( 1, $test->{name} ) ;
73                         }
74                         else {
75
76                                 print "unexpected error: $@\n" ;
77                                 ok( 0, $test->{name} ) ;
78                         }
79                         next ;
80                 }
81
82 # see if the expansion was what we expected
83
84                 my $ok = ${$rendered} eq $test->{expected} ;
85
86 # dump any bad expansions
87
88                 print <<ERR unless $ok ;
89 RENDERED
90 [${$rendered}]
91 EXPECTED
92 [$test->{expected}]
93 ------
94 ERR
95
96 # report success/failure for this test
97
98                 ok( $ok, $test->{name} ) ;
99         }
100 }
101
102 1 ;