pass args to ::TT along to all the manually instantiated Template components.
[scpubgit/HTML-String.git] / xt / tt.t
1 use strictures 1;
2 use Test::More;
3 use HTML::String::TT;
4 plan tests => 10;
5 my $tt = HTML::String::TT->new;
6
7 sub do_tt {
8     my $output;
9     $tt->process(\$_[0], $_[1], \$output) or die $tt->error;
10     return "$output";
11 }
12
13 is(
14     do_tt('<tag>[% foo %]</tag>', { foo => 'Hi <bob>' }),
15     '<tag>Hi &lt;bob&gt;</tag>',
16 );
17
18 is(
19     do_tt(q{[%
20         VIEW myview; BLOCK render; '<tag>'; foo; '</tag>'; END; END;
21         myview.include('render');
22     %]}, { foo => 'Hi <bob>' }),
23     '<tag>Hi &lt;bob&gt;</tag>',
24 );
25
26 is(
27     do_tt('<tag>[% foo | no_escape %]</tag>', { foo => 'Hi <bob>' }),
28     '<tag>Hi <bob></tag>',
29 );
30
31 # Check we aren't nailed by https://rt.perl.org/rt3/Ticket/Display.html?id=49594
32
33 is(
34     do_tt('<foo>"$b\\ar"</foo>'."\n"),
35     '<foo>"$b\\ar"</foo>'."\n"
36 );
37
38 { # non-ASCII characters can also trigger the bug
39
40     use utf8;
41
42     is(
43         do_tt('<li>foo – bar.</li>', {}),
44         '<li>foo – bar.</li>',
45     );
46 }
47
48 is(
49     do_tt(
50         '[% FOREACH item IN items %][% item %][% END %]',
51         { items => [ '<script>alert("lalala")</script>', '-> & so "on" <-' ] }
52     ),
53     '&lt;script&gt;alert(&quot;lalala&quot;)&lt;/script&gt;'
54         .'-&gt; &amp; so &quot;on&quot; &lt;-'
55 );
56
57 is( do_tt('"0"', {}), '"0"' );
58
59 {
60     my $tmpl = q[
61         [%- MACRO test(name, value) BLOCK;
62             IF !value.length;
63                "ok";
64             END;
65         END; -%]
66 [%- test("foo", "") -%]
67 ];
68
69     my $with_html_string_tt = do_tt($tmpl, {});
70     $tt = Template->new(STASH => Template::Stash->new);
71     my $with_template = do_tt($tmpl, {});
72
73     is $with_html_string_tt, $with_template;
74 }
75
76 sub _is_escaped {
77     my $test_name = shift;
78     $tt = HTML::String::TT->new( @_ );
79     is(
80         do_tt(
81             '[%- if 1; unsafe_js; %][% end %]',
82             { unsafe_js => '<script>alert("anycase alert");</script>' }
83         ),
84         '&lt;script&gt;alert(&quot;anycase alert&quot;);&lt;/script&gt;',
85         $test_name
86     );
87 }
88 _is_escaped('ANYCASE => 1 in a hash',ANYCASE => 1 );
89 _is_escaped('ANYCASE => 1 in a hashref',{ ANYCASE => 1 });
90 done_testing;