File: 03_die_on_non_escaped.t

package info (click to toggle)
libtemplate-stash-autoescaping-perl 0.0303-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 196 kB
  • sloc: perl: 491; makefile: 7
file content (97 lines) | stat: -r--r--r-- 1,813 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
use Test::Base;

plan 'no_plan';

use URI;
use Template::Stash::AutoEscaping;
use Template::Stash::AutoEscaping::Escaped::HTML;

my $html_class = Template::Stash::AutoEscaping->class_for('HTML');

sub my_html_filter {
    my $text = shift;
    if ( ref $text eq $html_class ) {
        warn "already escaped: " . $text;
        return $text;
    }
    for ($text) {
        s/&/&/g;
        s/</&lt;/g;
        s/>/&gt;/g;
        s/"/&quot;/g;
    }
    return $text;
}

sub as_html {
    Template::Stash::AutoEscaping->class_for('HTML')->new_as_escaped( $_[0] );
}

my $stash = Template::Stash::AutoEscaping->new(
    escape_type    => "HTML",      # default => HTML
    method_for_raw => "raw",       # default => raw
    method_for_escape => "escape", # default => escape
    die_on_unescaped => 1,         # default => 0
);

my $tt = Template->new(
    {
        STASH   => $stash,
        FILTERS => { html => \&my_html_filter, },
    }
);

filters {
    template => ['chomp'],
        data => ['chomp'],
    expected => ['chomp'],
       error => ['eval'],
};

sub templatize {
    my $input = $_[0];
}

run {
    my $block = shift;

    my $error_code = $tt->process( \($block->template), { data => $block->data }, \my $output );

    ok (($error_code xor $block->error()),
        $block->name . " - Error code and error agree",
    );

    if (! $block->error())
    {
        is( "$output", $block->expected, $block->name . ' output ok' );
    }
};

__DATA__

=== simple error
--- template
[% data %]
--- data eval
'<One&Two>'
--- expected
--- error
1
=== escape
--- template
[% data.escape %]
--- data eval
'<One&Two>'
--- expected
&lt;One&amp;Two&gt;
--- error
0
=== raw
--- template
[% data.raw %]
--- data eval
'<p>Hello &amp; Welcome!</p>'
--- expected
<p>Hello &amp; Welcome!</p>
--- error
0