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 132 133 134 135 136 137 138
|
#!/usr/bin/perl -w
use strict;
use Test::More tests => 21;
use_ok 'Text::MicroMason';
ok my $m = Text::MicroMason->new( -Filters );
my $res_nofilter = 'Hello <"world">!';
######################################################################
# Test an expression inside a template using logical or.
is $m->execute( text => q(Var is <% $ARGS{foo} || 0 %>) ), "Var is 0";
######################################################################
# Test h encoding flag if we have HTML::Entities
SKIP: {
skip "HTML::Entities is not installed", 4
unless HTML::Entities->can('encode');
my $src_h = q(Hello <% '<"world">' |h %>!);
my $res_h = 'Hello <"world">!';
is $m->execute(text => $src_h), $res_h, "Execute text with HTML::Entity filter";
# Test h as a default filter
{
local $m->{default_filters} = 'h';
my $src_h2 = q(Hello <% '<"world">' %>!);
is $m->execute( text => $src_h2), $res_h, "Execute text with HTML::Entity default filter";
# Explicitly disable the default filters
my $src_h3 = q(Hello <% '<"world">' | n %>!);
is $m->execute( text => $src_h3), $res_nofilter, "Execute text with HTML::Entity default turned off";
}
my $src_unh = qq(Hello <% '<"world">' |unh %>!);
my $res_unh = 'Hello <"world">!';
is $m->execute( text => $src_unh), $res_unh, "Execute text with stacking h filter";
} # SKIP
######################################################################
# Test default u encoding flag if we have URI::Escape
SKIP: {
skip "URI::Escape is not installed", 8
unless URI::Escape->can('uri_escape');
my $res_u = 'Hello %3C%3Fworld%3F%3E!';
is $m->execute(text => qq(Hello <% "<?world?>" |u %>!)), $res_u,
"Execute text with URI::Escape filter";
ok my $res = eval {$m->execute(text => qq(Hello <% "<?world?>"|u %>!))},
"Execute text with URI::Escape filter and no space";
is $res, $res_u;
# Test |u encoding flag in a file
ok $res = eval {$m->execute(file => 'samples/test-filter.msn', msg => "foo")},
"Execute text from file with URI::Escape filter and no space";
is $res, "foo", "Filter execution error: $@";
# Test u as a default filter
{
local $m->{default_filters} = 'u';
my $src_u2 = qq(Hello <% "<?world?>" %>!);
is $m->execute( text => $src_u2), $res_u, "Execute text with URI::Escape default filter";
# Explicitly disable the default filters
my $src_u3 = qq(Hello <% "<?world?>" | n %>!);
my $res_u3 = q(Hello <?world?>!);
is $m->execute( text => $src_u3), "Hello <?world?>!", "Execute text with URI::Escape default turned off";
}
# Test stacking and canceling with n
my $res_hnu = 'Hello %3C%3Fworld%3F%3E!';
my $src_hnu = qq(Hello <% "<?world?>" |hnu %>!);
is $m->execute( text => $src_hnu), $res_hnu, "Execute text with stacking u filter";
}
######################################################################
# Test custom filters
sub f1 {
$_ = shift;
tr/elo/apy/;
$_;
}
sub f2 {
$_ = shift;
s/wyrpd/birthday/;
$_;
}
$m->filter_functions( f1 => \&f1 );
$m->filter_functions( f2 => \&f2 );
# Try one custom filter
my $src_custom1 = qq(<% 'hello <"world">' | f1 %>);
my $res_custom1 = qq(happy <"wyrpd">);
is $m->execute( text => $src_custom1), $res_custom1;
# Try two filters in order: they're order dependant, so this will fail
# if they execute in the wrong order.
my $src_custom2 = qq(<% 'hello <"world">' | f1 , f2 %>);
my $res_custom2 = qq(happy <"birthday">);
is $m->execute( text => $src_custom2), $res_custom2;
# Try both filters as defaults
{
local $m->{default_filters} = 'f1, f2';
my $src_custom3 = qq(<% 'hello <"world">' %>);
is $m->execute( text => $src_custom3), $res_custom2;
# Override default filters
my $src_custom4 = qq(<% 'hello <"world">' |n, f1 %>);
is $m->execute( text => $src_custom4), $res_custom1;
}
# Try one default filter and one additional filter
{
local $m->{default_filters} = 'f1';
my $src_custom3 = qq(<% 'hello <"world">' %>);
is $m->execute( text => $src_custom3), $res_custom1;
my $src_custom4 = qq(<% 'hello <"world">' | f2 %>);
is $m->execute( text => $src_custom4), $res_custom2;
}
|