File Coverage

blib/lib/EJS/Template/Test.pm
Criterion Covered Total %
statement 45 47 95.7
branch 8 12 66.6
condition 2 2 100.0
subroutine 10 10 100.0
pod 2 2 100.0
total 67 73 91.7


line stmt bran cond sub pod time code
1 6     6   79970 use 5.006;
  6         16  
2 6     6   18 use strict;
  6         7  
  6         127  
3 6     6   18 use warnings;
  6         5  
  6         204  
4              
5             =head1 NAME
6              
7             EJS::Template::Test - Testing utility for EJS::Template
8              
9             =cut
10              
11             package EJS::Template::Test;
12 6     6   18 use base 'Exporter';
  6         6  
  6         456  
13              
14 6     6   21 use Carp qw(croak);
  6         6  
  6         294  
15 6     6   2030 use EJS::Template;
  6         10  
  6         135  
16 6     6   25 use Test::Builder;
  6         5  
  6         87  
17 6     6   16 use Test::More;
  6         6  
  6         335  
18              
19             our @EXPORT = qw(ejs_test ejs_test_parse);
20              
21             =head1 Methods
22              
23             =head2 ejs_test
24              
25             =cut
26              
27             sub ejs_test {
28 49     49 1 307253 my ($source, $expected, $variables, $config) = @_;
29 49         98 local $Test::Builder::Level = $Test::Builder::Level + 1;
30            
31 49 100       75 my $ejs = EJS::Template->new(%{$config || {}});
  49         457  
32 49         85 my $prefix = '';
33            
34 49         101 for my $name (qw(engine escape)) {
35 98 100       240 if ($ejs->{$name}) {
36 31         73 $prefix .= $ejs->{$name}.': ';
37             }
38             }
39            
40 49   100     135 $prefix ||= 'source: ';
41            
42 49         50 my $output;
43            
44 49         64 eval {
45 49 50       156 $ejs->process(\$source, $variables, \$output) or croak $@;
46             };
47            
48 49 50       118 if ($@) {
49 0         0 fail $prefix."[$source]: $@";
50             } else {
51 49         307 is($output, $expected, $prefix."[$source]");
52             }
53             }
54              
55             =head2 ejs_test_parse
56              
57             =cut
58              
59             sub ejs_test_parse {
60 20     20 1 76450 my ($source, $expected) = @_;
61 20         24 local $Test::Builder::Level = $Test::Builder::Level + 1;
62            
63 20         21 my $prefix = 'source: ';
64 20         14 my $output;
65            
66 20         55 eval {
67 20 50       86 EJS::Template->parse(\$source, \$output) or croak $@;
68             };
69            
70 20 50       24 if ($@) {
71 0         0 fail $prefix."[$source]: $@";
72             } else {
73 20         70 is($output, $expected, $prefix."[$source]");
74             }
75             }
76              
77             =head1 SEE ALSO
78              
79             =over 4
80              
81             =item * L
82              
83             =back
84              
85             =cut
86              
87             1;