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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
|
#============================================================= -*-perl-*-
#
# t/filter.t
#
# Template script testing FILTER directive.
#
# Written by Andy Wardley <abw@kfs.org>
#
# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# $Id$
#
#========================================================================
use strict;
use warnings;
use lib qw( ./lib ../lib );
use Template::Filters;
use Template qw( :status );
use Template::Parser;
use Template::Test;
use Template::Constants qw( :debug );
my $DEBUG = grep(/^--?d(debug)?$/, @ARGV);
$Template::Test::DEBUG = 0;
$Template::Test::EXTRA = 1; # ensure redirected file is created
#$Template::Context::DEBUG = 1;
#$Template::DEBUG = 1;
#$Template::Parser::DEBUG = 1;
#$Template::Directive::PRETTY = 1;
#------------------------------------------------------------------------
# hack to allow STDERR to be tied to a variable.
# (I'm really surprised there isn't a standard module which does this)
#------------------------------------------------------------------------
package Tie::File2Str;
sub TIEHANDLE {
my ($class, $textref) = @_;
bless $textref, $class;
}
sub PRINT {
my $self = shift;
$$self .= join('', @_);
}
#------------------------------------------------------------------------
# now for the main event...
#------------------------------------------------------------------------
package main;
# tie STDERR to a variable
my $stderr = '';
#tie(*STDERR, "Tie::File2Str", \$stderr);
my $dir = -d 't' ? 't/test/tmp' : 'test/tmp';
my $file = 'xyz';
my ($a, $b, $c, $d) = qw( alpha bravo charlie delta );
my $params = {
'a' => $a,
'b' => $b,
'c' => $c,
'd' => $d,
'list' => [ $a, $b, $c, $d ],
'text' => 'The cat sat on the mat',
outfile => $file,
stderr => sub { $stderr },
despace => bless(\&despace, 'anything'),
widetext => "wide:\x{65e5}\x{672c}\x{8a9e}",
};
my $filters = {
'nonfilt' => 'nonsense',
'microjive' => \µjive,
'microsloth' => [ \µsloth, 0 ],
'censor' => [ \&censor_factory, 1 ],
'badfact' => [ sub { return 'nonsense' }, 1 ],
'badfilt' => [ 'rubbish', 1 ],
'barfilt' => [ \&barf_up, 1 ],
};
my $config1 = {
INTERPOLATE => 1,
POST_CHOMP => 1,
FILTERS => $filters,
};
my $config2 = {
EVAL_PERL => 1,
FILTERS => $filters,
OUTPUT_PATH => $dir,
BARVAL => 'some random value',
};
unlink "$dir/$file" if -f "$dir/$file";
my $tt1 = Template->new($config1)
|| die Template->error();
my $tt2 = Template->new($config2)
|| die Template->error();
$tt2->context->define_filter('another', \&another, 1);
tie(*STDERR, "Tie::File2Str", \$stderr);
test_expect(\*DATA, [ default => $tt1, evalperl => $tt2 ], $params);
ok( -f "$dir/$file", "$dir/$file exists" );
unlink "$dir/$file" if -f "$dir/$file";
#------------------------------------------------------------------------
# custom filter subs
#------------------------------------------------------------------------
sub microjive {
my $text = shift;
$text =~ s/microsoft/The 'Soft/sig;
$text;
}
sub microsloth {
my $text = shift;
$text =~ s/microsoft/Microsloth/sig;
$text;
}
sub censor_factory {
my @forbidden = @_;
return sub {
my $text = shift;
foreach my $word (@forbidden) {
$text =~ s/$word/[** CENSORED **]/sig;
}
return $text;
}
}
sub barf_up {
my $context = shift;
my $foad = shift || 0;
if ($foad == 0) {
return (undef, "barfed");
}
elsif ($foad == 1) {
return (undef, Template::Exception->new('dead', 'deceased'));
}
elsif ($foad == 2) {
die "keeled over\n";
}
else {
die (Template::Exception->new('unwell', 'sick as a parrot'));
}
}
sub despace {
my $text = shift;
$text =~ s/\s+/_/g;
return $text;
}
sub another {
my ($context, $n) = @_;
return sub {
my $text = shift;
return $text x $n;
}
}
__DATA__
#------------------------------------------------------------------------
# test failures
#------------------------------------------------------------------------
-- test --
[% TRY %]
[% FILTER nonfilt %]
blah blah blah
[% END %]
[% CATCH %]
BZZZT: [% error.type %]: [% error.info %]
[% END %]
-- expect --
BZZZT: filter: invalid FILTER entry for 'nonfilt' (not a CODE ref)
-- test --
[% TRY %]
[% FILTER badfact %]
blah blah blah
[% END %]
[% CATCH %]
BZZZT: [% error.type %]: [% error.info %]
[% END %]
-- expect --
BZZZT: filter: invalid FILTER for 'badfact' (not a CODE ref)
-- test --
[% TRY %]
[% FILTER badfilt %]
blah blah blah
[% END %]
[% CATCH %]
BZZZT: [% error.type %]: [% error.info %]
[% END %]
-- expect --
BZZZT: filter: invalid FILTER entry for 'badfilt' (not a CODE ref)
-- test --
[% TRY;
"foo" | barfilt;
CATCH;
"$error.type: $error.info";
END
%]
-- expect --
filter: barfed
-- test --
[% TRY;
"foo" | barfilt(1);
CATCH;
"$error.type: $error.info";
END
%]
-- expect --
dead: deceased
-- test --
[% TRY;
"foo" | barfilt(2);
CATCH;
"$error.type: $error.info";
END
%]
-- expect --
filter: keeled over
-- test --
[% TRY;
"foo" | barfilt(3);
CATCH;
"$error.type: $error.info";
END
%]
-- expect --
unwell: sick as a parrot
#------------------------------------------------------------------------
# test filters
#------------------------------------------------------------------------
-- test --
[% FILTER html %]
This is some html text
All the <tags> should be escaped & protected
[% END %]
-- expect --
This is some html text
All the <tags> should be escaped & protected
-- test --
[% text = "The <cat> sat on the <mat>" %]
[% FILTER html %]
text: $text
[% END %]
-- expect --
text: The <cat> sat on the <mat>
-- test --
[% text = "The <cat> sat on the <mat>" %]
[% text FILTER html %]
-- expect --
The <cat> sat on the <mat>
-- test --
[% FILTER html %]
"It isn't what I expected", he replied.
[% END %]
-- expect --
"It isn't what I expected", he replied.
-- test --
[% FILTER xml %]
"It isn't what I expected", he replied.
[% END %]
-- expect --
"It isn't what I expected", he replied.
-- test --
[% FILTER format %]
Hello World!
[% END %]
-- expect --
Hello World!
-- test --
# test aliasing of a filter
[% FILTER comment = format('<!-- %s -->') %]
Hello World!
[% END +%]
[% "Goodbye, cruel World" FILTER comment %]
-- expect --
<!-- Hello World! -->
<!-- Goodbye, cruel World -->
-- test --
[% FILTER format %]
Hello World!
[% END %]
-- expect --
Hello World!
-- test --
[% "Foo" FILTER test1 = format('+++ %-4s +++') +%]
[% FOREACH item = [ 'Bar' 'Baz' 'Duz' 'Doze' ] %]
[% item FILTER test1 +%]
[% END %]
[% "Wiz" FILTER test1 = format("*** %-4s ***") +%]
[% "Waz" FILTER test1 +%]
-- expect --
+++ Foo +++
+++ Bar +++
+++ Baz +++
+++ Duz +++
+++ Doze +++
*** Wiz ***
*** Waz ***
-- test --
[% FILTER microjive %]
The "Halloween Document", leaked to Eric Raymond from an insider
at Microsoft, illustrated Microsoft's strategy of "Embrace,
Extend, Extinguish"
[% END %]
-- expect --
The "Halloween Document", leaked to Eric Raymond from an insider
at The 'Soft, illustrated The 'Soft's strategy of "Embrace,
Extend, Extinguish"
-- test --
[% FILTER microsloth %]
The "Halloween Document", leaked to Eric Raymond from an insider
at Microsoft, illustrated Microsoft's strategy of "Embrace,
Extend, Extinguish"
[% END %]
-- expect --
The "Halloween Document", leaked to Eric Raymond from an insider
at Microsloth, illustrated Microsloth's strategy of "Embrace,
Extend, Extinguish"
-- test --
[% FILTER censor('bottom' 'nipple') %]
At the bottom of the hill, he had to pinch the
nipple to reduce the oil flow.
[% END %]
-- expect --
At the [** CENSORED **] of the hill, he had to pinch the
[** CENSORED **] to reduce the oil flow.
-- test --
[% FILTER bold = format('<b>%s</b>') %]
This is bold
[% END +%]
[% FILTER italic = format('<i>%s</i>') %]
This is italic
[% END +%]
[% 'This is both' FILTER bold FILTER italic %]
-- expect --
<b>This is bold</b>
<i>This is italic</i>
<i><b>This is both</b></i>
-- test --
[% "foo" FILTER format("<< %s >>") FILTER format("=%s=") %]
-- expect --
=<< foo >>=
-- test --
[% blocktext = BLOCK %]
The cat sat on the mat
Mary had a little Lamb
You shall have a fishy on a little dishy, when the boat comes in. What
if I can't wait until then? I'm hungry!
[% END -%]
[% global.blocktext = blocktext; blocktext %]
-- expect --
The cat sat on the mat
Mary had a little Lamb
You shall have a fishy on a little dishy, when the boat comes in. What
if I can't wait until then? I'm hungry!
-- test --
[% global.blocktext FILTER html_para %]
-- expect --
<p>
The cat sat on the mat
</p>
<p>
Mary had a little Lamb
</p>
<p>
You shall have a fishy on a little dishy, when the boat comes in. What
if I can't wait until then? I'm hungry!
</p>
-- test --
[% global.blocktext FILTER html_break %]
-- expect --
The cat sat on the mat
<br />
<br />
Mary had a little Lamb
<br />
<br />
You shall have a fishy on a little dishy, when the boat comes in. What
if I can't wait until then? I'm hungry!
-- test --
[% global.blocktext FILTER html_para_break %]
-- expect --
The cat sat on the mat
<br />
<br />
Mary had a little Lamb
<br />
<br />
You shall have a fishy on a little dishy, when the boat comes in. What
if I can't wait until then? I'm hungry!
-- test --
[% global.blocktext FILTER html_line_break %]
-- expect --
The cat sat on the mat<br />
<br />
Mary had a little Lamb<br />
<br />
<br />
<br />
You shall have a fishy on a little dishy, when the boat comes in. What <br />
if I can't wait until then? I'm hungry!<br />
-- test --
[% global.blocktext FILTER truncate(10) %]
-- expect --
The cat...
-- test --
[% global.blocktext FILTER truncate %]
-- expect --
The cat sat on the mat
Mary ...
-- test --
[% 'Hello World' | truncate(2) +%]
[% 'Hello World' | truncate(8) +%]
[% 'Hello World' | truncate(10) +%]
[% 'Hello World' | truncate(11) +%]
[% 'Hello World' | truncate(20) +%]
-- expect --
..
Hello...
Hello W...
Hello World
Hello World
-- test --
[% "foo..." FILTER repeat(5) %]
-- expect --
foo...foo...foo...foo...foo...
-- test --
[% FILTER truncate(21) %]
I have much to say on this matter that has previously been said
on more than one occassion.
[% END %]
-- expect --
I have much to say...
-- test --
[% FILTER truncate(25) %]
Nothing much to say
[% END %]
-- expect --
Nothing much to say
-- test --
[% FILTER repeat(3) %]
Am I repeating myself?
[% END %]
-- expect --
Am I repeating myself?
Am I repeating myself?
Am I repeating myself?
-- test --
[% text FILTER remove(' ') +%]
[% text FILTER remove('\s+') +%]
[% text FILTER remove('cat') +%]
[% text FILTER remove('at') +%]
[% text FILTER remove('at', 'splat') +%]
-- expect --
Thecatsatonthemat
Thecatsatonthemat
The sat on the mat
The c s on the m
The c s on the m
-- test --
[% text FILTER replace(' ', '_') +%]
[% text FILTER replace('sat', 'shat') +%]
[% text FILTER replace('at', 'plat') +%]
-- expect --
The_cat_sat_on_the_mat
The cat shat on the mat
The cplat splat on the mplat
-- test --
[% text = 'The <=> operator' %]
[% text|html %]
-- expect --
The <=> operator
-- test --
[% text = 'The <=> operator, blah, blah' %]
[% text | html | replace('blah', 'rhubarb') %]
-- expect --
The <=> operator, rhubarb, rhubarb
-- test --
[% | truncate(25) %]
The cat sat on the mat, and wondered to itself,
"How might I be able to climb up onto the shelf?",
For up there I am sure I'll see,
A tasty fishy snack for me.
[% END %]
-- expect --
The cat sat on the mat...
-- test --
[% FILTER upper %]
The cat sat on the mat
[% END %]
-- expect --
THE CAT SAT ON THE MAT
-- test --
[% FILTER lower %]
The cat sat on the mat
[% END %]
-- expect --
the cat sat on the mat
-- test --
[% 'arse' | stderr %]
stderr: [% stderr %]
-- expect --
stderr: arse
-- test --
[% percent = '%'
left = "[$percent"
right = "$percent]"
dir = "$left a $right blah blah $left b $right"
%]
[% dir +%]
FILTER [[% dir | eval %]]
FILTER [[% dir | evaltt %]]
-- expect --
[% a %] blah blah [% b %]
FILTER [alpha blah blah bravo]
FILTER [alpha blah blah bravo]
-- test --
[% TRY %]
[% dir = "[\% FOREACH a = { 1 2 3 } %\]a: [\% a %\]\n[\% END %\]" %]
[% dir | eval %]
[% CATCH %]
error: [[% error.type %]] [[% error.info %]]
[% END %]
-- expect --
error: [file] [parse error - input text line 1: unexpected token (1)
[% FOREACH a = { 1 2 3 } %]]
-- test --
nothing
[% TRY;
'$x = 10; $b = 20; $x + $b' | evalperl;
CATCH;
"$error.type: $error.info";
END
+%]
happening
-- expect --
nothing
perl: EVAL_PERL is not set
happening
-- test --
[% TRY -%]
before
[% FILTER redirect('xyz') %]
blah blah blah
here is the news
[% a %]
[% END %]
after
[% CATCH %]
ERROR [% error.type %]: [% error.info %]
[% END %]
-- expect --
before
ERROR redirect: OUTPUT_PATH is not set
-- test --
-- use evalperl --
[% FILTER evalperl %]
$a = 10;
$b = 20;
$stash->{ foo } = $a + $b;
$stash->{ bar } = $context->config->{ BARVAL };
"all done"
[% END +%]
foo: [% foo +%]
bar: [% bar %]
-- expect --
all done
foo: 30
bar: some random value
-- test --
[% TRY -%]
before
[% FILTER file(outfile) -%]
blah blah blah
here is the news
[% a %]
[% END -%]
after
[% CATCH %]
ERROR [% error.type %]: [% error.info %]
[% END %]
-- expect --
before
after
-- test --
[% PERL %]
# static filter subroutine
$Template::Filters::FILTERS->{ bar } = sub {
my $text = shift;
$text =~ s/^/bar: /gm;
return $text;
};
[% END -%]
[% FILTER bar -%]
The cat sat on the mat
The dog sat on the log
[% END %]
-- expect --
bar: The cat sat on the mat
bar: The dog sat on the log
-- test --
[% PERL %]
# dynamic filter factory
$Template::Filters::FILTERS->{ baz } = [
sub {
my $context = shift;
my $word = shift || 'baz';
return sub {
my $text = shift;
$text =~ s/^/$word: /gm;
return $text;
};
}, 1 ];
[% END -%]
[% FILTER baz -%]
The cat sat on the mat
The dog sat on the log
[% END %]
[% FILTER baz('wiz') -%]
The cat sat on the mat
The dog sat on the log
[% END %]
-- expect --
baz: The cat sat on the mat
baz: The dog sat on the log
wiz: The cat sat on the mat
wiz: The dog sat on the log
-- test --
-- use evalperl --
[% PERL %]
$stash->set('merlyn', bless \&merlyn1, 'ttfilter');
sub merlyn1 {
my $text = shift || '<no text>';
$text =~ s/stone/henge/g;
return $text;
}
[% END -%]
[% FILTER $merlyn -%]
Let him who is without sin cast the first stone.
[% END %]
-- expect --
Let him who is without sin cast the first henge.
-- test --
-- use evalperl --
[% PERL %]
$stash->set('merlyn', sub { \&merlyn2 });
sub merlyn2 {
my $text = shift || '<no text>';
$text =~ s/stone/henge/g;
return $text;
}
[% END -%]
[% FILTER $merlyn -%]
Let him who is without sin cast the first stone.
[% END %]
-- expect --
Let him who is without sin cast the first henge.
-- test --
[% myfilter = 'html' -%]
[% FILTER $myfilter -%]
<html>
[% END %]
-- expect --
<html>
-- test --
[% FILTER $despace -%]
blah blah blah
[%- END %]
-- expect --
blah_blah_blah
-- test --
-- use evalperl --
[% PERL %]
$context->filter(\&newfilt, undef, 'myfilter');
sub newfilt {
my $text = shift;
$text =~ s/\s+/=/g;
return $text;
}
[% END -%]
[% FILTER myfilter -%]
This is a test
[%- END %]
-- expect --
This=is=a=test
-- test --
[% PERL %]
$context->define_filter('xfilter', \&xfilter);
sub xfilter {
my $text = shift;
$text =~ s/\s+/X/g;
return $text;
}
[% END -%]
[% FILTER xfilter -%]
blah blah blah
[%- END %]
-- expect --
blahXblahXblah
-- test --
[% FILTER another(3) -%]
foo bar baz
[% END %]
-- expect --
foo bar baz
foo bar baz
foo bar baz
-- test --
[% '$stash->{ a } = 25' FILTER evalperl %]
[% a %]
-- expect --
25
25
-- test --
[% '$stash->{ a } = 25' FILTER perl %]
[% a %]
-- expect --
25
25
-- test --
[% FILTER indent -%]
The cat sat
on the mat
[% END %]
-- expect --
The cat sat
on the mat
-- test --
[% FILTER indent(2) -%]
The cat sat
on the mat
[% END %]
-- expect --
The cat sat
on the mat
-- test --
[% FILTER indent('>> ') -%]
The cat sat
on the mat
[% END %]
-- expect --
>> The cat sat
>> on the mat
-- test --
[% text = 'The cat sat on the mat';
text | indent('> ') | indent('+') %]
-- expect --
+> The cat sat on the mat
-- test --
<<[% FILTER trim %]
The cat sat
on the
mat
[% END %]>>
-- expect --
<<The cat sat
on the
mat>>
-- test --
<<[% FILTER collapse %]
The cat sat
on the
mat
[% END %]>>
-- expect --
<<The cat sat on the mat>>
-- test --
[% FILTER format('++%s++') %]Hello World[% END %]
[% FILTER format %]Hello World[% END %]
-- expect --
++Hello World++
Hello World
-- test --
[% "my file.html" FILTER uri %]
-- expect --
my%20file.html
-- test --
[% "my<file & your>file.html" FILTER uri %]
-- expect --
my%3Cfile%20%26%20your%3Efile.html
-- test --
[% "foo@bar" FILTER uri %]
-- expect --
foo%40bar
-- test --
[% "foo@bar" FILTER url %]
-- expect --
foo@bar
-- test --
[% "my<file & your>file.html" | uri | html %]
-- expect --
my%3Cfile%20%26%20your%3Efile.html
-- test --
[% widetext | uri %]
-- expect --
wide%3A%E6%97%A5%E6%9C%AC%E8%AA%9E
-- test --
[% 'foobar' | ucfirst %]
-- expect --
Foobar
-- test --
[% 'FOOBAR' | lcfirst %]
-- expect --
fOOBAR
|