File: autofilter.t

package info (click to toggle)
libtemplate-autofilter-perl 0.143050-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 144 kB
  • sloc: perl: 226; makefile: 2
file content (131 lines) | stat: -rw-r--r-- 3,385 bytes parent folder | download | duplicates (3)
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 => '&lt;a&gt;',
    },
    {
        name => 'specifically filtered tokens get filtered',
        tmpl => '[% test | html %]',
        expect => '&lt;a&gt;',
    },
    {
        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 &lt;Element&gt; &lt;a&gt; &lt;Element&gt; &lt;b&gt; &lt;Element&gt; &amp;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 => '&lt;a&gt;',
    },
)}

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;
}