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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#!perl -w
use strict;
use Test::More;
use Text::Xslate;
use Text::Xslate::Compiler;
my $tx = Text::Xslate->new();
my @set = (
[ <<'T', { data => [[qw(Perl)]] }, <<'X' ],
: macro foo ->($x) {
: for $x -> ($item) {
Hello, <: $item :> world!
: }
: }
: for $data -> ($item) {
: foo($item)
: }
T
Hello, Perl world!
X
[ <<'T', { data => [[qw(Perl Xslate)]] }, <<'X' ],
: macro foo ->($x) {
: for $x -> ($item) {
Hello, <: $item :> world!
: }
: }
: for $data -> ($item) {
: foo($item)
: }
T
Hello, Perl world!
Hello, Xslate world!
X
[ <<'T', { data => [[qw(Perl Xslate)]] }, <<'X' ],
: macro foo ->($x) {
: for $x -> ($item) {
Hello, <: $item :> world!
: }
: }
: for $data -> ($item) {
: foo($item)
: foo($item)
: }
T
Hello, Perl world!
Hello, Xslate world!
Hello, Perl world!
Hello, Xslate world!
X
[ <<'T', { }, <<'X' ],
: macro foo ->($x) {
<strong><:$x:></strong>
: }
: macro bar ->($x) {
: foo($x)
: }
: foo("FOO")
: bar("BAR")
T
<strong>FOO</strong>
<strong>BAR</strong>
X
[ <<'T', { }, <<'X', "nested call" ],
: macro foo ->($x) {
: "[" ~ $x ~ "]"
: }
<: foo(foo("FOO")) :>
T
[[FOO]]
X
[ <<'T', { }, <<'X', "nested (arithmatic)" ],
: macro add ->($x, $y) { $x + $y }
<: add(1, 2) + add(10, 20) :>
T
33
X
[ <<'T', { }, <<'X', "multi call" ],
: macro foo ->($x) {
: "[" ~ $x ~ "]"
: }
<: foo("FOO") ~ foo("BAR") ~ foo("BAZ") :>
T
[FOO][BAR][BAZ]
X
[ <<'T', { }, <<'X', "nested multi call" ],
: macro foo ->($x) {
: "[" ~ $x ~ "]"
: }
<: foo(foo("FOO") ~ foo("BAR")) :>
T
[[FOO][BAR]]
X
[ <<'T', { }, <<'X', "nested multi call (arithmatic)" ],
: macro add ->($x, $y) { $x + $y }
<: add(add(1, 2) + add(10, 20) + add(100, 200), 1000) :>
T
1333
X
[ <<'T', { }, <<'X', "recursion" ],
: macro factorial ->($x) {
: $x == 0 ? 1 : $x * factorial($x-1)
: }
<: factorial(0) :>
<: factorial(1) :>
<: factorial(2) :>
<: factorial(3) :>
T
1
1
2
6
X
[ <<'T', { }, <<'X', "filter operator" ],
: macro factorial ->($x) {
: $x == 0 ? 1 : $x * factorial($x-1)
: }
<: 0 | factorial :>
<: 1 | factorial :>
<: 2 | factorial :>
<: 3 | factorial :>
T
1
1
2
6
X
[ <<'T', { }, <<'X', "a macro returns escaped string" ],
<: macro em ->($x) { :><em><: $x :></em><: } -:>
<: "foo" | em :>
<: "bar" | em | raw :>
<: "baz" | em | html :>
T
<em>foo</em>
<em>bar</em>
<em>baz</em>
X
[ <<'T', { }, <<'X', "save macro" ],
<: macro em ->($x) { :><em><: $x :></em><: } -:>
: for [em] -> $m {
<: $m("foo") :>
: }
T
<em>foo</em>
X
[<<'T', { value10 => 10 }, '100'],
: macro foo ->($x) { $x.foo }
: foo( { foo => $value10 == 10 ? 100 : $value10 == 20 ? 200 : 300 } )
T
[<<'T', { value20 => 20 }, '200'],
: macro foo ->($x) { $x.foo }
: foo( { foo => $value20 == 10 ? 100 : $value20 == 20 ? 200 : 300 } )
T
);
foreach my $d(@set) {
my($in, $vars, $out, $msg) = @$d;
is eval { $tx->render_string($in, $vars) }, $out, $msg or diag $in;
diag $@ if $@;
}
eval {
$tx->render_string('<: foo("x") :>', {});
};
like $@, qr/\b foo \b/xms, "don't affect the parser";
eval {
$tx->render_string(<<'T', {});
: macro foo{
: foo()
: }
: foo()
T
};
like $@, qr/too deep/, 'deep recursion';
like $@, qr/\b foo \b/xms;
done_testing;
|