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 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
|
package MyTemplateExporter;
use Mojo::Base -strict;
sub import {
my $caller = caller;
no strict 'refs';
*{$caller . '::foo'} = sub {'works!'};
}
package MyTemplateException;
use Mojo::Base -strict;
sub exception { die 'ohoh' }
package main;
use Mojo::Base -strict;
use utf8;
use Test::More tests => 214;
# "When I held that gun in my hand, I felt a surge of power...
# like God must feel when he's holding a gun."
use File::Spec::Functions qw(catfile splitdir);
use File::Temp;
use FindBin;
use Mojo::Template;
# Capture helper
my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }';
# Consistent scalar context
my $mt = Mojo::Template->new;
$mt->prepend('my @foo = (3, 4);');
my $output = $mt->render('<%= @foo %>:<%== @foo %>');
is $output, "2:2\n", 'same context';
# Trim tag
$mt = Mojo::Template->new;
$output = $mt->render(" ♥ <%= 'test♥' =%> \n");
is $output, ' ♥test♥', 'tag trimmed';
# Trim expression
$mt = Mojo::Template->new;
$output = $mt->render("<%= '123' %><%= 'test' =%>\n");
is $output, '123test', 'expression trimmed';
# Trim expression (multiple lines)
$mt = Mojo::Template->new;
$output = $mt->render(" foo \n <%= 'test' =%>\n foo\n");
is $output, " foo \ntest foo\n", 'expression trimmed';
# Trim expression (at start of line)
$mt = Mojo::Template->new;
$output = $mt->render(" \n<%= 'test' =%>\n ");
is $output, " \ntest \n", 'expression trimmed';
# Trim expression (multiple lines)
$mt = Mojo::Template->new;
$output = $mt->render(" bar\n foo\n <%= 'test' =%>\n foo\n bar\n");
is $output, " bar\n foo\ntest foo\n bar\n", 'expression trimmed';
# Trim expression (multiple empty lines)
$mt = Mojo::Template->new;
$output = $mt->render(" \n<%= 'test' =%>\n ");
is $output, " \ntest \n", 'expression trimmed';
# Trim expression tags
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(' <%= capture begin =%><html><% end =%> ');
is $output, '<html>', 'expression tags trimmed';
# Trim expression tags (relaxed expression end)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(' <%= capture begin =%><html><%= end =%> ');
is $output, '<html>', 'expression tags trimmed';
# Trim expression tags (relaxed escaped expression end)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(' <%= capture begin =%><html><%== end =%> ');
is $output, '<html>', 'expression tags trimmed';
# Trim expression tags (trim reset)
$mt = Mojo::Template->new;
$output = $mt->render(' <%= "one" =%><%= "two" %> three');
is $output, "onetwo three\n", 'expression tags trimmed';
# Replace tag
$mt = Mojo::Template->new;
$output = $mt->render('<%% 1 + 1 %>');
is $output, "<% 1 + 1 %>\n", 'tag has been replaced';
# Replace expression tag
$mt = Mojo::Template->new;
$output = $mt->render('<%%= 1 + 1 %>');
is $output, "<%= 1 + 1 %>\n", 'expression tag has been replaced';
# Replace expression tag (alternative)
$mt = Mojo::Template->new;
$output = $mt->render(' lalala <%%= 1 + 1 %> 1234 ');
is $output, " lalala <%= 1 + 1 %> 1234 \n", 'expression tag has been replaced';
# Replace expression tag (another alternative)
$mt = Mojo::Template->new;
$output = $mt->render(<<EOF);
lalala <%%= 1 +
1 %> 12
34
EOF
is $output, "lalala <%= 1 +\n 1 %> 12\n34\n",
'expression tag has been replaced';
# Replace comment tag
$mt = Mojo::Template->new;
$output = $mt->render('<%%# 1 + 1 %>');
is $output, "<%# 1 + 1 %>\n", 'comment tag has been replaced';
# Replace line
$mt = Mojo::Template->new;
$output = $mt->render('%% my $foo = 23;');
is $output, "% my \$foo = 23;\n", 'line has been replaced';
# Replace expression line
$mt = Mojo::Template->new;
$output = $mt->render(' %%= 1 + 1');
is $output, " %= 1 + 1\n", 'expression line has been replaced';
# Replace expression line (alternative)
$mt = Mojo::Template->new;
$output = $mt->render('%%= 1 + 1');
is $output, "%= 1 + 1\n", 'expression line has been replaced';
# Replace comment line
$mt = Mojo::Template->new;
$output = $mt->render(' %%# 1 + 1');
is $output, " %# 1 + 1\n", 'comment line has been replaced';
# Replace mixed
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
%% my $number = <%= 20 + 3%>;
The number is <%%= <%= '$' %>number %>.
EOF
is $output, "% my \$number = 23;\nThe number is <%= \$number %>.\n",
'mixed lines have been replaced';
# Helper starting with "end"
$mt = Mojo::Template->new(prepend => 'sub endpoint { "works!" }');
$output = $mt->render(<<'EOF');
% endpoint;
%= endpoint
%== endpoint
<% endpoint; %><%= endpoint %><%== endpoint =%>
EOF
is $output, "works!\nworks!\nworks!works!", 'helper worked';
# Helper ending with "begin"
$mt = Mojo::Template->new(prepend => 'sub funbegin { "works too!" }');
$output = $mt->render(<<'EOF');
% funbegin;
%= funbegin
%== funbegin
<% funbegin; %><%= funbegin %><%== funbegin =%>\
EOF
is $output, "works too!\nworks too!\nworks too!works too!", 'helper worked';
# Catched exception
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% eval { die {foo => 'bar'} };
%= $@->{foo}
EOF
is $output, "bar\n", 'exception passed through';
# Dummy exception object
package MyException;
use Mojo::Base -base;
use overload '""' => sub { shift->error }, fallback => 1;
has 'error';
package main;
# Catched exception object
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% eval { die MyException->new(error => 'works!') };
%= $@->error
EOF
is $output, "works!\n", 'exception object passed through';
# Recursive block
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block;
<% $block = begin =%>
% my $i = shift;
<html>
<%= $block->(--$i) if $i %>
<% end =%>
<%= $block->(2) %>
EOF
is $output, "<html>\n<html>\n<html>\n\n\n\n\n", 'recursive block';
# Recursive block (perl lines)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block;
% $block = begin
% my $i = shift;
<html>
%= $block->(--$i) if $i
% end
%= $block->(2)
EOF
is $output, "<html>\n<html>\n<html>\n\n\n\n\n", 'recursive block';
# Recursive block (indented perl lines)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block;
% $block = begin
% my $i = shift;
<html>
<%= $block->(--$i) if $i =%>
% end
%= $block->(2)
EOF
is $output, " <html>\n<html>\n<html>\n\n", 'recursive block';
# Expression block (less whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<% my $block =begin=%>
<html>
<%end=%>
<%= $block->() %>
EOF
is $output, "<html>\n\n", 'expression block';
# Expression block (perl lines and less whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block =begin
<html>
%end
<%= $block->() %>
EOF
is $output, "<html>\n\n", 'expression block';
# Expression block (indented perl lines and less whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block =begin
<html>
%end
<%= $block->() %>
EOF
is $output, "<html>\n\n", 'expression block';
# Escaped expression block (passed through with extra whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<% my $block = begin %>
<html>
<% end %>
<%== $block->() %>
EOF
is $output, "\n\n<html>\n\n", 'escaped expression block';
# Escaped expression block
# (passed through with perl lines and extra whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block = begin
<html>
<% end %>
<%== $block->() %>
EOF
is $output, "\n<html>\n\n", 'escaped expression block';
# Escaped expression block
# (passed through with indented perl lines and extra whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block = begin
<html>
% end
<%== $block->() %>
EOF
is $output, "<html>\n\n", 'escaped expression block';
# Capture lines (passed through with extra whitespace)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
<% my $result = capture begin %>
<html>
<% end %>
%== $result
EOF
is $output, "\n\n<html>\n\n", 'captured lines';
# Capture tags (passed through)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
<% my $result = capture begin %><html><% end %><%== $result %>
EOF
is $output, "<html>\n", 'capture tags';
# Capture tags (passed through alternative)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
<% my $result = capture begin %><html><% end %><%== $result %>
EOF
is $output, "<html>\n", 'capture tags';
# Capture tags with appended code (passed through)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
<% my $result = +(capture begin %><html><% end); %><%== $result %>
EOF
is $output, "<html>\n", 'capture tags with appended code';
# Capture tags with appended code (passed through alternative)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
<% my $result = +( capture begin %><html><% end ); %><%= $result %>
EOF
is $output, "<html>\n", 'capture tags with appended code';
# Nested capture tags (passed through)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
<% my $result = capture
begin %><%= capture begin %><html><% end
%><% end %><%== $result %>
EOF
is $output, "<html>\n", 'nested capture tags';
# Nested capture tags (passed through alternative)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
<% my $result = capture begin =%>
<%== capture begin =%>
<html>
<% end =%>
<% end =%>
<%= $result =%>
EOF
is $output, " <html>\n", 'nested capture tags';
# Advanced capturing (extra whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<% my $block = begin =%>
<% my $name = shift; =%>
Hello <%= $name %>.
<% end =%>
<%= $block->('Baerbel') =%>
<%= $block->('Wolfgang') =%>
EOF
is $output, <<EOF, 'advanced capturing';
Hello Baerbel.
Hello Wolfgang.
EOF
# Advanced capturing (perl lines extra whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block = begin
<% my $name = shift; =%>
Hello <%= $name %>.
% end
<%= $block->('Baerbel') %>
<%= $block->('Wolfgang') %>
EOF
is $output, <<EOF, 'advanced capturing';
Hello Baerbel.
Hello Wolfgang.
EOF
# Advanced capturing (indented perl lines extra whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block = begin
<% my $name = shift; =%>
Hello <%= $name %>.
% end
<%= $block->('Baerbel') %>
<%= $block->('Wolfgang') %>
EOF
is $output, <<EOF, 'advanced capturing';
Hello Baerbel.
Hello Wolfgang.
EOF
# Advanced capturing with tags
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<% my $block = begin =%>
<% my $name = shift; =%>
Hello <%= $name %>.
<% end =%>
<%= $block->('Sebastian') %>
<%= $block->('Sara') %>
EOF
is $output, <<EOF, 'advanced capturing with tags';
Hello Sebastian.
Hello Sara.
EOF
# Advanced capturing with tags (perl lines)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block = begin
<% my $name = shift; =%>
Hello <%= $name %>.
% end
%= $block->('Sebastian')
%= $block->('Sara')
EOF
is $output, <<EOF, 'advanced capturing with tags';
Hello Sebastian.
Hello Sara.
EOF
# Advanced capturing with tags (indented perl lines)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block = begin
% my $name = shift;
Hello <%= $name %>.
% end
%= $block->('Sebastian')
%= $block->('Sara')
EOF
is $output, <<EOF, 'advanced capturing with tags';
Hello Sebastian.
Hello Sara.
EOF
# Advanced capturing with tags (alternative)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<% my $block = begin =%>
<% my $name = shift; =%>
Hello <%= $name %>.
<% end =%>
<%= $block->('Sebastian') %>
<%= $block->('Sara') %>
EOF
is $output, <<EOF, 'advanced capturing with tags';
Hello Sebastian.
Hello Sara.
EOF
# Advanced capturing with tags (perl lines and alternative)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block = begin
<% my $name = shift; =%>
Hello <%= $name %>.
% end
%= $block->('Sebastian')
%= $block->('Sara')
EOF
is $output, <<EOF, 'advanced capturing with tags';
Hello Sebastian.
Hello Sara.
EOF
# Advanced capturing with tags (indented perl lines and alternative)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $block = begin
% my $name = shift;
Hello <%= $name %>.
% end
%= $block->('Sebastian')
%= $block->('Sara')
EOF
is $output, <<EOF, 'advanced capturing with tags';
Hello Sebastian.
Hello Sara.
EOF
# More advanced capturing with tags (alternative)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<% my
$block1 = begin =%>
<% my $name = shift; =%>
Hello <%= $name %>.
<% end =%>
<% my
$block2 =
begin =%>
<% my $name = shift; =%>
Bye <%= $name %>.
<% end =%>
<%= $block1->('Sebastian') %>
<%= $block2->('Sara') %>
EOF
is $output, <<EOF, 'advanced capturing with tags';
Hello Sebastian.
Bye Sara.
EOF
# Block loop
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
% my $i = 2;
<%= capture begin %>
<%= $i++ %>
<% end for 1 .. 3; %>
EOF
is $output, <<EOF, 'block loop';
2
3
4
EOF
# Block loop (perl lines)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
% my $i = 2;
%= capture begin
<%= $i++ =%>
% end for 1 .. 3;
EOF
is $output, "\n2\n3\n4", 'block loop';
# Block loop (indented perl lines)
$mt = Mojo::Template->new(prepend => $capture);
$output = $mt->render(<<'EOF');
% my $i = 2;
%= capture begin
%= $i++
% end for 1 .. 3;
EOF
is $output, " \n 2\n\n 3\n\n 4\n", 'block loop';
# Strict
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% $foo = 1;
EOF
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/^Global symbol "\$foo" requires/, 'right message';
# Importing into a template
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% BEGIN { MyTemplateExporter->import }
%= __PACKAGE__
%= foo
EOF
is $output, "Mojo::Template::SandBox\nworks!\n", 'right result';
$output = $mt->render(<<'EOF');
% BEGIN { MyTemplateExporter->import }
%= __PACKAGE__
%= foo
EOF
is $output, "Mojo::Template::SandBox\nworks!\n", 'right result';
# Unusable error message (stacktrace required)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
test
123
% die "x\n";
test
EOF
isa_ok $output, 'Mojo::Exception', 'right exception';
is $output->message, "x\n", 'right message';
is $output->lines_before->[0]->[0], 1, 'right number';
is $output->lines_before->[0]->[1], 'test', 'right line';
is $output->lines_before->[1]->[0], 2, 'right number';
is $output->lines_before->[1]->[1], '123', 'right line';
is $output->line->[0], 3, 'right number';
is $output->line->[1], '% die "x\n";', 'right line';
like "$output", qr/^x/, 'right result';
# Compile time exception
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
test
123
% {
%= 1 + 1
test
EOF
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/^Missing right curly or square bracket/,
'right message';
like $output->message, qr/syntax error at template line 5.$/, 'right message';
is $output->lines_before->[0]->[0], 1, 'right number';
is $output->lines_before->[0]->[1], 'test', 'right line';
is $output->lines_before->[1]->[0], 2, 'right number';
is $output->lines_before->[1]->[1], '123', 'right line';
is $output->lines_before->[2]->[0], 3, 'right number';
is $output->lines_before->[2]->[1], '% {', 'right line';
is $output->lines_before->[3]->[0], 4, 'right number';
is $output->lines_before->[3]->[1], '%= 1 + 1', 'right line';
is $output->line->[0], 5, 'right number';
is $output->line->[1], 'test', 'right line';
like "$output", qr/^Missing right curly or square bracket/, 'right result';
like $output->frames->[0]->[1], qr/Template\.pm$/, 'right file';
# Exception in module
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
test
123
%= MyTemplateException->exception
%= 1 + 1
test
EOF
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/ohoh/, 'right message';
is $output->lines_before->[0]->[0], 8, 'right number';
is $output->lines_before->[0]->[1], '}', 'right line';
is $output->lines_before->[1]->[0], 9, 'right number';
is $output->lines_before->[1]->[1], '', 'right line';
is $output->lines_before->[2]->[0], 10, 'right number';
is $output->lines_before->[2]->[1], 'package MyTemplateException;',
'right line';
is $output->lines_before->[3]->[0], 11, 'right number';
is $output->lines_before->[3]->[1], 'use Mojo::Base -strict;', 'right line';
is $output->lines_before->[4]->[0], 12, 'right number';
is $output->lines_before->[4]->[1], '', 'right line';
is $output->line->[0], 13, 'right number';
is $output->line->[1], "sub exception { die 'ohoh' }", 'right line';
is $output->lines_after->[0]->[0], 14, 'right number';
is $output->lines_after->[0]->[1], '', 'right line';
is $output->lines_after->[1]->[0], 15, 'right number';
is $output->lines_after->[1]->[1], 'package main;', 'right line';
like "$output", qr/ohoh/, 'right result';
# Exception in template
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
test
123
% die 'oops!';
%= 1 + 1
test
EOF
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/oops\!/, 'right message';
is $output->lines_before->[0]->[0], 1, 'right number';
is $output->lines_before->[0]->[1], 'test', 'right line';
is $output->lines_before->[1]->[0], 2, 'right number';
is $output->lines_before->[1]->[1], '123', 'right line';
is $output->line->[0], 3, 'right number';
is $output->line->[1], "% die 'oops!';", 'right line';
is $output->lines_after->[0]->[0], 4, 'right number';
is $output->lines_after->[0]->[1], '%= 1 + 1', 'right line';
is $output->lines_after->[1]->[0], 5, 'right number';
is $output->lines_after->[1]->[1], 'test', 'right line';
like "$output", qr/oops\! at template line 3, near "%= 1 \+ 1"./,
'right result';
# Exception in template (empty perl lines)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
test
123
%
% die 'oops!';
%
%
%
%= 1 + 1
test
EOF
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/oops\!/, 'right message';
is $output->lines_before->[0]->[0], 1, 'right number';
is $output->lines_before->[0]->[1], 'test', 'right line';
ok $output->lines_before->[0]->[2], 'contains code';
is $output->lines_before->[1]->[0], 2, 'right number';
is $output->lines_before->[1]->[1], '123', 'right line';
ok $output->lines_before->[1]->[2], 'contains code';
is $output->lines_before->[2]->[0], 3, 'right number';
is $output->lines_before->[2]->[1], '%', 'right line';
is $output->lines_before->[2]->[2], ' ', 'right code';
is $output->line->[0], 4, 'right number';
is $output->line->[1], "% die 'oops!';", 'right line';
is $output->lines_after->[0]->[0], 5, 'right number';
is $output->lines_after->[0]->[1], '%', 'right line';
is $output->lines_after->[0]->[2], ' ', 'right code';
is $output->lines_after->[1]->[0], 6, 'right number';
is $output->lines_after->[1]->[1], ' %', 'right line';
is $output->lines_after->[1]->[2], ' ', 'right code';
is $output->lines_after->[2]->[0], 7, 'right number';
is $output->lines_after->[2]->[1], '%', 'right line';
is $output->lines_after->[2]->[2], ' ', 'right code';
like "$output", qr/oops\! at template line 4, near "%"./, 'right result';
# Exception in nested template
$mt = Mojo::Template->new;
$mt->tag_start('[$-');
$mt->tag_end('-$]');
$mt->line_start('$-');
$output = $mt->render(<<'EOF');
test
$- my $mt = Mojo::Template->new;
[$- my $output = $mt->render(<<'EOT');
%= bar
EOT
-$]
$-= $output
EOF
is $output, <<'EOF', 'exception in nested template';
test
Bareword "bar" not allowed while "strict subs" in use at template line 1.
1: %= bar
EOF
# Control structures
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% if (23 > 22) {
foo
% }
% else {
bar
% }
% if (23 > 22) {
bar
% }
% else {
foo
% }
EOF
is $output, "foo\nbar\n", 'control structure';
# All tags
$mt = Mojo::Template->new;
$mt->parse(<<'EOF');
<html foo="bar">
<%= $_[0] + 1 %> test <%= 2 + 2 %> lala <%# comment lalala %>
%# This is a comment!
% my $i = 2;
%= $i * 2
</html>
EOF
$mt->build;
like $mt->code, qr/^package /, 'right code';
like $mt->code, qr/lala/, 'right code';
unlike $mt->code, qr/ comment lalala /, 'right code';
ok !defined($mt->compiled), 'nothing compiled';
$mt->compile;
isa_ok $mt->compiled, 'CODE', 'code compiled';
$output = $mt->interpret(2);
is $output, "<html foo=\"bar\">\n3 test 4 lala \n4\n\</html>\n", 'all tags';
# Arguments
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF', 'test', {foo => 'bar'});
% my $message = shift;
<html><% my $hash = $_[0]; %>
%= $message . ' ' . $hash->{foo}
</html>
EOF
is $output, "<html>\ntest bar\n</html>\n", 'arguments';
is $mt->interpret('tset', {foo => 'baz'}), "<html>\ntset baz\n</html>\n",
'arguments again';
is $mt->interpret('tset', {foo => 'yada'}), "<html>\ntset yada\n</html>\n",
'arguments again';
# Ugly multiline loop
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $nums = '';
<html><% for my $i (1..4) {
$nums .= "$i";
} %><%= $nums%></html>
EOF
is $output, "<html>1234</html>\n", 'ugly multiline loop';
# Clean multiline loop
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<html>
% for my $i (1..4) {
%= $i
% }
</html>
EOF
is $output, "<html>\n1\n2\n3\n4\n</html>\n", 'clean multiline loop';
# Escaped line ending
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<html>\
%= '2' x 4
</html>\\\\
EOF
is $output, "<html>2222\n</html>\\\\\\\n", 'escaped line ending';
# XML escape
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<html><%== '<html>' %>
%== '<'
</html>
EOF
is $output, "<html><html>\n&lt;\n</html>\n", 'XML escape';
# XML auto escape
$mt = Mojo::Template->new;
$mt->auto_escape(1);
$output = $mt->render(<<'EOF');
<html><%= '<html>' %>
%= '<'
%== '<'
</html>
EOF
is $output, <<EOF, 'XML auto escape';
<html><html>
&lt;
<
</html>
EOF
# Complicated XML auto escape
$mt = Mojo::Template->new;
$mt->auto_escape(1);
$output = $mt->render(<<'EOF', {foo => 23});
% use Data::Dumper;
%= Data::Dumper->new([shift])->Maxdepth(2)->Indent(1)->Terse(1)->Dump
EOF
is $output, <<'EOF', 'complicated XML auto escape';
{
'foo' => 23
}
EOF
# Complicated XML auto escape
$mt = Mojo::Template->new;
$mt->auto_escape(1);
$output = $mt->render(<<'EOF');
<html><%= '<html>' for 1 .. 3 %></html>
EOF
is $output, <<EOF, 'complicated XML auto escape';
<html><html><html><html></html>
EOF
# Prepending code
$mt = Mojo::Template->new;
$mt->prepend('my $foo = shift; my $bar = "something\nelse"');
$output = $mt->render(<<'EOF', 23);
<%= $foo %>
%= $bar
% my $bar = 23;
%= $bar
EOF
is $output, "23\nsomething\nelse\n23\n", 'prepending code';
$mt = Mojo::Template->new;
$mt->prepend(q[{no warnings 'redefine'; no strict 'refs'; *foo = sub { 23 }}]);
$output = $mt->render('<%= foo() %>');
is $output, "23\n", 'right result';
$output = $mt->render('%= foo()');
is $output, "23\n", 'right result';
# Appending code
$mt = Mojo::Template->new;
$mt->append('$_M = "FOO!"');
$output = $mt->render('23');
is $output, "FOO!", 'appending code';
# Multiline comment
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<html><%# this is
a
comment %>this not
% for my $i (1..4) {
%= $i
% }
</html>
EOF
is $output, "<html>this not\n1\n2\n3\n4\n</html>\n", 'multiline comment';
# Oneliner
$mt = Mojo::Template->new;
$output = $mt->render('<html><%= 3 * 3 %></html>\\');
is $output, '<html>9</html>', 'oneliner';
# Different line start
$mt = Mojo::Template->new;
$mt->line_start('$');
$output = $mt->render(<<'EOF');
<html>\
$= '2' x 4
</html>\\\\
EOF
is $output, "<html>2222\n</html>\\\\\\\n", 'different line start';
# Inline comments
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% if (1) { # test
works!
% } # tset
great!
EOF
is $output, "works!\ngreat!\n", 'comments did not affect the result';
# Multiline expression
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<html><%= do { my $i = '2';
$i x 4; }; %>\
</html>\
EOF
is $output, '<html>2222</html>', 'multiline expression';
# Different multiline expression
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<%= do { my $i = '2';
$i x 4; };
%>\
EOF
is $output, '2222', 'multiline expression';
# Yet another multiline expression
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<%= 'hello' .
' world' %>\
EOF
is $output, 'hello world', 'multiline expression';
# And another multiline expression
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<%= 'hello' .
' world' %>\
EOF
is $output, 'hello world', 'multiline expression';
# And another multiline expression
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<%= 'hello' .
' wo' .
'rld'
%>\
EOF
is $output, 'hello world', 'multiline expression';
# Escaped multiline expression
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
<%==
'hello '
.'world'
%>
EOF
is $output, "hello world\n", 'escaped multiline expression';
# Scoped scalar
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% my $foo = 'bar';
<%= $foo %>
EOF
is $output, "bar\n", 'scoped scalar';
# Different tags and line start
$mt = Mojo::Template->new;
$mt->tag_start('[$-');
$mt->tag_end('-$]');
$mt->line_start('$-');
$output = $mt->render(<<'EOF', 'test', {foo => 'bar'});
$- my $message = shift;
<html>[$- my $hash = $_[0]; -$]
$-= $message . ' ' . $hash->{foo}
</html>
EOF
is $output, "<html>\ntest bar\n</html>\n", 'different tags and line start';
# Different expression and comment marks
$mt = Mojo::Template->new;
$mt->comment_mark('@@@');
$mt->expression_mark('---');
$output = $mt->render(<<'EOF', 'test', {foo => 'bar'});
% my $message = shift;
<html><% my $hash = $_[0]; %><%@@@ comment lalala %>
%--- $message . ' ' . $hash->{foo}
</html>
EOF
is $output, <<EOF, 'different expression and comment mark';
<html>
test bar
</html>
EOF
# File
$mt = Mojo::Template->new;
my $file = catfile(splitdir($FindBin::Bin), qw(lib test.mt));
$output = $mt->render_file($file, 3);
like $output, qr/23\nHello World!/, 'file';
# Exception in file
$mt = Mojo::Template->new;
$file = catfile(splitdir($FindBin::Bin), qw(lib exception.mt));
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/exception\.mt line 2/, 'message contains filename';
is $output->lines_before->[0]->[0], 1, 'right number';
is $output->lines_before->[0]->[1], 'test', 'right line';
is $output->line->[0], 2, 'right number';
is $output->line->[1], '% die;', 'right line';
is $output->lines_after->[0]->[0], 3, 'right number';
is $output->lines_after->[0]->[1], '123', 'right line';
like "$output", qr/exception\.mt line 2/, 'right result';
# Exception in file (different name)
$mt = Mojo::Template->new;
$output = $mt->name('foo.mt')->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/foo\.mt line 2/, 'message contains filename';
is $output->lines_before->[0]->[0], 1, 'right number';
is $output->lines_before->[0]->[1], 'test', 'right line';
is $output->line->[0], 2, 'right number';
is $output->line->[1], '% die;', 'right line';
is $output->lines_after->[0]->[0], 3, 'right number';
is $output->lines_after->[0]->[1], '123', 'right line';
like "$output", qr/foo\.mt line 2/, 'right result';
# Exception in file (rendering to file)
$mt = Mojo::Template->new;
my $dir = File::Temp::tempdir(CLEANUP => 1);
my $file2 = catfile $dir, 'test1.mt';
$output = $mt->render_file_to_file($file, $file2);
ok !-e $file2, 'file has not been rendered';
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/exception\.mt line 2/, 'message contains filename';
is $output->lines_before->[0]->[0], 1, 'right number';
is $output->lines_before->[0]->[1], 'test', 'right line';
is $output->line->[0], 2, 'right number';
is $output->line->[1], '% die;', 'right line';
is $output->lines_after->[0]->[0], 3, 'right number';
is $output->lines_after->[0]->[1], '123', 'right line';
like "$output", qr/exception\.mt line 2/, 'right result';
# File to file with utf8 data
$mt = Mojo::Template->new;
$mt->tag_start('[$-');
$mt->tag_end('-$]');
$file = catfile $dir, 'test.mt';
is $mt->render_to_file(<<"EOF", $file), undef, 'file rendered';
<% my \$i = 23; %> foo bar
\x{df}\x{0100}bar\x{263a} <%= \$i %>
test
EOF
$mt = Mojo::Template->new;
$file2 = catfile $dir, 'test2.mt';
is $mt->render_file_to_file($file, $file2), undef, 'file rendered to file';
$mt = Mojo::Template->new;
$output = $mt->render_file($file2);
is $output, " foo bar\n\x{df}\x{0100}bar\x{263a} 23\ntest\n", 'right result';
# Exception with utf8 context
$mt = Mojo::Template->new;
$file = catfile(splitdir($FindBin::Bin), qw(lib utf8_exception.mt));
$output = $mt->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
is $output->lines_before->[0]->[1], '☃', 'right line';
is $output->line->[1], '% die;♥', 'right line';
is $output->lines_after->[0]->[1], '☃', 'right line';
is utf8::is_utf8($output->lines_before->[0]->[1]), 1, 'context has utf8 flag';
is utf8::is_utf8($output->line->[1]), 1, 'context has utf8 flag';
is utf8::is_utf8($output->lines_after->[0]->[1]), 1, 'context has utf8 flag';
# Different encodings
$mt = Mojo::Template->new(encoding => 'ISO-8859-1');
$file = catfile $dir, 'test3.mt';
is $mt->render_to_file('ü', $file), undef, 'file rendered';
$mt = Mojo::Template->new(encoding => 'UTF-8');
ok !eval { $mt->render_file($file) }, 'file not rendered';
like $@, qr/invalid encoding/, 'right error';
|