1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#!/usr/bin/perl
use strict;
use warnings;
package autofilter;
use Test::InDistDir;
use Test::More 0.96 ();
use Test::Most;
run_tests();
done_testing;
exit;
sub tests {(
{
name => 'plain text remains unfiltered',
tmpl => '<a>',
expect => '<a>',
},
{
name => 'excluded tokens remain unfiltered',
tmpl => '[% test | none %]',
expect => '<a>',
},
{
name => 'unfiltered tokens get filtered',
tmpl => '[% test %]',
expect => '<a>',
},
{
name => 'specifically filtered tokens get filtered',
tmpl => '[% test | html %]',
expect => '<a>',
},
{
name => 'other filters are applied without the autofilter',
tmpl => '[% test | upper %]',
expect => '<A>',
},
{
name => 'parameters make it possible to set the autofilter',
tmpl => '[% test %]',
expect => '<A>',
params => { AUTO_FILTER => 'upper' }
},
{
name => 'includes are not filtered',
tmpl => '[% test %] [% INCLUDE included.tt %]',
expect => "<A> test <html> <A>\n",
params => {
AUTO_FILTER => 'upper',
INCLUDE_PATH => 't',
},
},
{
name => 'SKIP_DIRECTIVES modifications are observed',
tmpl => '[% test %] [% INCLUDE included.tt %]',
expect => "<A> TEST <HTML> <A>\n",
params => {
AUTO_FILTER => 'upper',
INCLUDE_PATH => 't',
SKIP_DIRECTIVES => [],
},
},
{
name => '[%# comments are parsed correctly',
tmpl => 'pre[%# This is a comment -%]post',
expect => 'prepost',
},
{
name => '[% # comments are parsed correctly',
tmpl => 'pre[% # This is a comment -%]post',
expect => 'prepost',
},
{
name => 'empty tokens are parsed correctly',
tmpl => 'pre[% -%]post',
expect => 'prepost',
},
{
name => "INTERPOLATE => 1 doesn't cause runtime errors (but isn't supported)",
tmpl => 'pre $test post',
expect => 'pre <a> post',
params => {
INTERPOLATE => 1,
},
},
{
name => 'split apart compound statements',
tmpl => 'pre [% FOR foo IN ["<a>", "<b>", "&c"]; "<Element> $foo "; END %] post',
expect => 'pre <Element> <a> <Element> <b> <Element> &c post',
},
{
name => 'split apart compound statements',
tmpl => 'pre [% FOR foo IN ["<a>", "<b>", "&c"]; "<Element> $foo " | none; END %] post',
expect => 'pre <Element> <a> <Element> <b> <Element> &c post',
},
{
name => 'tailing semi-colon parsed ok',
tmpl => '[% foo=test; %][% foo %]',
expect => '<a>',
},
)}
sub run_tests {
use_ok "Template::AutoFilter";
run_test($_) for tests();
return;
}
sub run_test {
my ( $test ) = @_;
$test->{params} ||= {};
my $tt = Template::AutoFilter->new( $test->{params} );
my $out;
my $res = $tt->process( \$test->{tmpl}, { test => '<a>' }, \$out );
subtest $test->{name} => sub {
cmp_deeply( [ $tt->error."", $res ], [ '', 1 ], 'no template errors' );
is( $out, $test->{expect}, 'output is correct' );
};
return;
}
|